/pypy/interpreter/test/test_syntax.py

https://bitbucket.org/dac_io/pypy · Python · 766 lines · 681 code · 71 blank · 14 comment · 42 complexity · 5a18163cc09a16acabb44eb70018eba0 MD5 · raw file

  1. import py
  2. from pypy.conftest import gettestobjspace
  3. def splitcases(s):
  4. lines = [line.rstrip() for line in s.split('\n')]
  5. s = '\n'.join(lines)
  6. result = []
  7. for case in s.split('\n\n'):
  8. if case.strip():
  9. result.append(str(py.code.Source(case))+'\n')
  10. return result
  11. VALID = splitcases("""
  12. def f():
  13. def g():
  14. global x
  15. exec "hi"
  16. x
  17. def f():
  18. def g():
  19. global x
  20. from a import *
  21. x
  22. def f(x):
  23. def g():
  24. global x
  25. exec "hi"
  26. x
  27. def f(x):
  28. def g():
  29. global x
  30. from a import *
  31. x
  32. def f():
  33. def g():
  34. from a import *
  35. def f():
  36. def g():
  37. exec "hi"
  38. def f():
  39. from a import *
  40. def f():
  41. exec "hi"
  42. def f():
  43. from a import *
  44. def g():
  45. global x
  46. x
  47. def f():
  48. exec "hi"
  49. def g():
  50. global x
  51. x
  52. def f():
  53. from a import *
  54. def g(x):
  55. x
  56. def f():
  57. exec "hi"
  58. def g(x):
  59. x
  60. def f():
  61. from a import *
  62. lambda x: x
  63. def f():
  64. exec "hi"
  65. lambda x: x
  66. def f():
  67. from a import *
  68. x
  69. def f():
  70. exec "hi"
  71. x
  72. def f():
  73. from a import *
  74. (i for i in x)
  75. def f():
  76. exec "hi"
  77. (i for i in x)
  78. def f():
  79. class g:
  80. exec "hi"
  81. x
  82. def f():
  83. class g:
  84. from a import *
  85. x
  86. """)
  87. ## --- the following ones are valid in CPython, but not sensibly so:
  88. ## --- if x is rebound, then it is even rebound in the parent scope!
  89. ## def f(x):
  90. ## class g:
  91. ## from a import *
  92. ## x
  93. ## def f(x):
  94. ## class g:
  95. ## exec "x=41"
  96. ## x
  97. INVALID = splitcases("""
  98. def f():
  99. def g():
  100. exec "hi"
  101. x
  102. # NB. the above one is invalid in CPython, but there is no real reason
  103. def f(x):
  104. def g():
  105. exec "hi"
  106. x
  107. def f():
  108. def g():
  109. from a import *
  110. x
  111. # NB. the above one is invalid in CPython, but there is no real reason
  112. def f(x):
  113. def g():
  114. from a import *
  115. x
  116. def f():
  117. exec "hi"
  118. def g():
  119. x
  120. def f():
  121. exec "hi"
  122. lambda x: y
  123. def f():
  124. from a import *
  125. x
  126. def g():
  127. x
  128. def f():
  129. from a import *
  130. lambda x: y
  131. def f():
  132. exec "hi"
  133. class g:
  134. x
  135. def f():
  136. from a import *
  137. x
  138. class g:
  139. x
  140. def f():
  141. exec "hi"
  142. x = 5
  143. class g:
  144. def h():
  145. x
  146. def f():
  147. from a import *
  148. x = 4
  149. class g:
  150. def h():
  151. x
  152. def f(x):
  153. exec "hi"
  154. x = 4
  155. class g:
  156. x
  157. def f(x):
  158. from a import *
  159. class g:
  160. x
  161. def f(x):
  162. exec "hi"
  163. x = 5
  164. class g:
  165. def h():
  166. x
  167. def f(x):
  168. from a import *
  169. x = 5
  170. class g:
  171. def h():
  172. x
  173. def f():
  174. (i for i in x) = 10
  175. def f(x):
  176. def g():
  177. from a import *
  178. def k():
  179. return x
  180. """)
  181. for i in range(len(VALID)):
  182. exec """def test_valid_%d(space):
  183. checkvalid(space, %r)
  184. """ % (i, VALID[i])
  185. for i in range(len(INVALID)):
  186. exec """def test_invalid_%d(space):
  187. checkinvalid(space, %r)
  188. """ % (i, INVALID[i])
  189. def checkvalid(space, s):
  190. try:
  191. space.call_function(space.builtin.get('compile'),
  192. space.wrap(s),
  193. space.wrap('?'),
  194. space.wrap('exec'))
  195. except:
  196. print '\n' + s
  197. raise
  198. def checkinvalid(space, s):
  199. from pypy.interpreter.error import OperationError
  200. try:
  201. try:
  202. space.call_function(space.builtin.get('compile'),
  203. space.wrap(s),
  204. space.wrap('?'),
  205. space.wrap('exec'))
  206. except OperationError, e:
  207. if not e.match(space, space.w_SyntaxError):
  208. raise
  209. else:
  210. raise Exception("Should have raised SyntaxError")
  211. except:
  212. print '\n' + s
  213. raise
  214. class AppTestCondExpr:
  215. def test_condexpr(self):
  216. for s, expected in [("x = 1 if True else 2", 1),
  217. ("x = 1 if False else 2", 2)]:
  218. exec s
  219. assert x == expected
  220. def test_condexpr_no_warning(self):
  221. import warnings
  222. warnings.simplefilter('error', SyntaxWarning)
  223. exec "1 if True else 2"
  224. warnings.simplefilter('default', SyntaxWarning)
  225. class AppTestYield:
  226. def test_bare_yield(self):
  227. s = "def f():\n yield"
  228. exec s
  229. class AppTestDecorators:
  230. def test_function_decorators(self):
  231. def other():
  232. return 4
  233. def dec(f):
  234. return other
  235. ns = {}
  236. ns["dec"] = dec
  237. exec """@dec
  238. def g():
  239. pass""" in ns
  240. assert ns["g"] is other
  241. assert ns["g"]() == 4
  242. def test_application_order(self):
  243. def dec1(f):
  244. record.append(1)
  245. return f
  246. def dec2(f):
  247. record.append(2)
  248. return f
  249. record = []
  250. ns = {"dec1" : dec1, "dec2" : dec2}
  251. exec """@dec1
  252. @dec2
  253. def g(): pass""" in ns
  254. assert record == [2, 1]
  255. del record[:]
  256. exec """@dec1
  257. @dec2
  258. class x: pass""" in ns
  259. assert record == [2, 1]
  260. def test_class_decorators(self):
  261. s = """@func
  262. class x: pass"""
  263. ns = {"func" : lambda cls: 4}
  264. exec s in ns
  265. assert ns["x"] == 4
  266. class AppTestPrintFunction:
  267. def test_simple_print(self):
  268. import __builtin__
  269. s = """from __future__ import print_function
  270. x = print
  271. """
  272. ns = {}
  273. exec s in ns
  274. assert ns["x"] is getattr(__builtin__, "print")
  275. def test_print(self):
  276. s = """from __future__ import print_function
  277. import StringIO
  278. s = StringIO.StringIO()
  279. print("Hello,", "person", file=s)
  280. """
  281. ns = {}
  282. exec s in ns
  283. assert ns["s"].getvalue() == "Hello, person\n"
  284. class AppTestUnicodeLiterals:
  285. def test_simple(self):
  286. s = """from __future__ import unicode_literals
  287. x = 'u'
  288. y = r'u'
  289. z = u'u'
  290. b = b'u'
  291. c = br'u'"""
  292. ns = {}
  293. exec s in ns
  294. assert isinstance(ns["x"], unicode)
  295. assert isinstance(ns["y"], unicode)
  296. assert isinstance(ns["z"], unicode)
  297. assert isinstance(ns["b"], str)
  298. assert isinstance(ns["c"], str)
  299. class AppTestComprehensions:
  300. def test_dictcomps(self):
  301. d = eval("{x : x for x in range(10)}")
  302. assert isinstance(d, dict)
  303. assert d == dict(zip(range(10), range(10)))
  304. d = eval("{x : x for x in range(10) if x % 2}")
  305. l = [x for x in range(10) if x % 2]
  306. assert d == dict(zip(l, l))
  307. def test_setcomps(self):
  308. s = eval("{x for x in range(10)}")
  309. assert isinstance(s, set)
  310. assert s == set(range(10))
  311. s = eval("{x for x in range(10) if x % 2}")
  312. assert s == set(x for x in range(10) if x % 2)
  313. def test_set_literal(self):
  314. s = eval("{1}")
  315. assert isinstance(s, set)
  316. assert s == set((1,))
  317. s = eval("{0, 1, 2, 3}")
  318. assert isinstance(s, set)
  319. assert s == set(range(4))
  320. class AppTestWith:
  321. def test_with_simple(self):
  322. s = """
  323. if 1:
  324. class Context:
  325. def __init__(self):
  326. self.calls = list()
  327. def __enter__(self):
  328. self.calls.append('__enter__')
  329. def __exit__(self, exc_type, exc_value, exc_tb):
  330. self.calls.append('__exit__')
  331. acontext = Context()
  332. with acontext:
  333. pass
  334. """
  335. exec s
  336. assert acontext.calls == '__enter__ __exit__'.split()
  337. def test_compound_with(self):
  338. s = """class Context:
  339. def __init__(self, var):
  340. self.record = []
  341. self.var = var
  342. def __enter__(self):
  343. self.record.append(("__enter__", self.var))
  344. return self.var
  345. def __exit__(self, tp, value, tb):
  346. self.record.append(("__exit__", self.var))
  347. c1 = Context("blah")
  348. c2 = Context("bling")
  349. with c1 as v1, c2 as v2:
  350. pass
  351. """
  352. ns = {}
  353. exec s in ns
  354. assert ns["v1"] == "blah"
  355. assert ns["v2"] == "bling"
  356. assert ns["c1"].record == [("__enter__", "blah"), ("__exit__", "blah")]
  357. assert ns["c2"].record == [("__enter__", "bling"),
  358. ("__exit__", "bling")]
  359. def test_start_with_blank_line(self):
  360. s = """
  361. if 1:
  362. class Context:
  363. def __init__(self):
  364. self.calls = list()
  365. def __enter__(self):
  366. self.calls.append('__enter__')
  367. def __exit__(self, exc_type, exc_value, exc_tb):
  368. self.calls.append('__exit__')
  369. acontext = Context()
  370. with acontext:
  371. pass
  372. """
  373. exec s
  374. assert acontext.calls == '__enter__ __exit__'.split()
  375. def test_raw_doc_string(self):
  376. s = """r'doc'
  377. class Context(object):
  378. def __enter__(self):
  379. global enter
  380. enter = True
  381. def __exit__(self, *exc):
  382. global exit
  383. exit = True
  384. with Context() as w:
  385. pass"""
  386. exec s
  387. assert enter
  388. assert exit
  389. def test_with_as_var(self):
  390. s = """
  391. if 1:
  392. class Context:
  393. def __init__(self):
  394. self.calls = list()
  395. def __enter__(self):
  396. self.calls.append('__enter__')
  397. return self.calls
  398. def __exit__(self, exc_type, exc_value, exc_tb):
  399. self.calls.append('__exit__')
  400. self.exit_params = (exc_type, exc_value, exc_tb)
  401. acontextfact = Context()
  402. with acontextfact as avar:
  403. avar.append('__body__')
  404. pass
  405. """
  406. exec s
  407. assert acontextfact.exit_params == (None, None, None)
  408. assert acontextfact.calls == '__enter__ __body__ __exit__'.split()
  409. def test_with_raise_exception(self):
  410. s = """
  411. if 1:
  412. class Context:
  413. def __init__(self):
  414. self.calls = list()
  415. def __enter__(self):
  416. self.calls.append('__enter__')
  417. return self.calls
  418. def __exit__(self, exc_type, exc_value, exc_tb):
  419. self.calls.append('__exit__')
  420. self.exit_params = (exc_type, exc_value, exc_tb)
  421. acontextfact = Context()
  422. error = RuntimeError('With Test')
  423. try:
  424. with acontextfact as avar:
  425. avar.append('__body__')
  426. raise error
  427. avar.append('__after_raise__')
  428. except RuntimeError:
  429. pass
  430. else:
  431. raise AssertionError('With did not raise RuntimeError')
  432. """
  433. exec s
  434. assert acontextfact.calls == '__enter__ __body__ __exit__'.split()
  435. assert acontextfact.exit_params[0:2] == (RuntimeError, error)
  436. import types
  437. assert isinstance(acontextfact.exit_params[2], types.TracebackType)
  438. def test_with_swallow_exception(self):
  439. s = """
  440. if 1:
  441. class Context:
  442. def __init__(self):
  443. self.calls = list()
  444. def __enter__(self):
  445. self.calls.append('__enter__')
  446. return self.calls
  447. def __exit__(self, exc_type, exc_value, exc_tb):
  448. self.calls.append('__exit__')
  449. self.exit_params = (exc_type, exc_value, exc_tb)
  450. return True
  451. acontextfact = Context()
  452. error = RuntimeError('With Test')
  453. with acontextfact as avar:
  454. avar.append('__body__')
  455. raise error
  456. avar.append('__after_raise__')
  457. """
  458. exec s
  459. assert acontextfact.calls == '__enter__ __body__ __exit__'.split()
  460. assert acontextfact.exit_params[0:2] == (RuntimeError, error)
  461. import types
  462. assert isinstance(acontextfact.exit_params[2], types.TracebackType)
  463. def test_with_break(self):
  464. s = """
  465. if 1:
  466. class Context:
  467. def __init__(self):
  468. self.calls = list()
  469. def __enter__(self):
  470. self.calls.append('__enter__')
  471. return self.calls
  472. def __exit__(self, exc_type, exc_value, exc_tb):
  473. self.calls.append('__exit__')
  474. self.exit_params = (exc_type, exc_value, exc_tb)
  475. acontextfact = Context()
  476. error = RuntimeError('With Test')
  477. for x in 1,:
  478. with acontextfact as avar:
  479. avar.append('__body__')
  480. break
  481. avar.append('__after_break__')
  482. else:
  483. raise AssertionError('Break failed with With, reached else clause')
  484. """
  485. exec s
  486. assert acontextfact.calls == '__enter__ __body__ __exit__'.split()
  487. assert acontextfact.exit_params == (None, None, None)
  488. def test_with_continue(self):
  489. s = """
  490. if 1:
  491. class Context:
  492. def __init__(self):
  493. self.calls = list()
  494. def __enter__(self):
  495. self.calls.append('__enter__')
  496. return self.calls
  497. def __exit__(self, exc_type, exc_value, exc_tb):
  498. self.calls.append('__exit__')
  499. self.exit_params = (exc_type, exc_value, exc_tb)
  500. acontextfact = Context()
  501. error = RuntimeError('With Test')
  502. for x in 1,:
  503. with acontextfact as avar:
  504. avar.append('__body__')
  505. continue
  506. avar.append('__after_continue__')
  507. else:
  508. avar.append('__continue__')
  509. """
  510. exec s
  511. assert acontextfact.calls == '__enter__ __body__ __exit__ __continue__'.split()
  512. assert acontextfact.exit_params == (None, None, None)
  513. def test_with_return(self):
  514. s = """
  515. if 1:
  516. class Context:
  517. def __init__(self):
  518. self.calls = list()
  519. def __enter__(self):
  520. self.calls.append('__enter__')
  521. return self.calls
  522. def __exit__(self, exc_type, exc_value, exc_tb):
  523. self.calls.append('__exit__')
  524. self.exit_params = (exc_type, exc_value, exc_tb)
  525. acontextfact = Context()
  526. error = RuntimeError('With Test')
  527. def g(acontextfact):
  528. with acontextfact as avar:
  529. avar.append('__body__')
  530. return '__return__'
  531. avar.append('__after_return__')
  532. acontextfact.calls.append(g(acontextfact))
  533. """
  534. exec s
  535. assert acontextfact.calls == '__enter__ __body__ __exit__ __return__'.split()
  536. assert acontextfact.exit_params == (None, None, None)
  537. def test_with_as_keyword(self):
  538. try:
  539. exec "with = 9"
  540. except SyntaxError:
  541. pass
  542. else:
  543. assert False, 'Assignment to with did not raise SyntaxError'
  544. def test_with_as_keyword_compound(self):
  545. try:
  546. exec "from __future__ import generators, with_statement\nwith = 9"
  547. except SyntaxError:
  548. pass
  549. else:
  550. assert False, 'Assignment to with did not raise SyntaxError'
  551. def test_missing_as_SyntaxError(self):
  552. snippets = [
  553. "import os.path a bar ",
  554. "from os import path a bar",
  555. """
  556. with foo a bar:
  557. pass
  558. """]
  559. for snippet in snippets:
  560. try:
  561. exec snippet
  562. except SyntaxError:
  563. pass
  564. else:
  565. assert False, "%s: did not raise SyntaxError" % snippet
  566. def test_with_propagate_compileflag(self):
  567. s = """
  568. if 1:
  569. compile('''with x:
  570. pass''', '', 'exec')
  571. """
  572. exec s
  573. class AppTestSyntaxError:
  574. def test_tokenizer_error_location(self):
  575. line4 = "if ?: pass\n"
  576. try:
  577. exec "print\nprint\nprint\n" + line4
  578. except SyntaxError, e:
  579. assert e.lineno == 4
  580. assert e.text == line4
  581. assert e.offset == e.text.index('?') + 1
  582. else:
  583. raise Exception("no SyntaxError??")
  584. def test_grammar_error_location(self):
  585. try:
  586. exec """if 1:
  587. class Foo:
  588. bla
  589. a b c d e
  590. bar
  591. """
  592. except SyntaxError, e:
  593. assert e.lineno == 4
  594. assert e.text.endswith('a b c d e\n')
  595. assert e.offset == e.text.index('b')
  596. else:
  597. raise Exception("no SyntaxError??")
  598. def test_astbuilder_error_location(self):
  599. program = "(1, 2) += (3, 4)\n"
  600. try:
  601. exec program
  602. except SyntaxError, e:
  603. assert e.lineno == 1
  604. assert e.text is None
  605. else:
  606. raise Exception("no SyntaxError??")
  607. def test_bad_encoding(self):
  608. program = """
  609. # -*- coding: uft-8 -*-
  610. pass
  611. """
  612. raises(SyntaxError, "exec program")
  613. if __name__ == '__main__':
  614. # only to check on top of CPython (you need 2.4)
  615. from py.test import raises
  616. for s in VALID:
  617. try:
  618. compile(s, '?', 'exec')
  619. except Exception, e:
  620. print '-'*20, 'FAILED TO COMPILE:', '-'*20
  621. print s
  622. print '%s: %s' % (e.__class__, e)
  623. print '-'*60
  624. for s in INVALID:
  625. try:
  626. raises(SyntaxError, compile, s, '?', 'exec')
  627. except Exception ,e:
  628. print '-'*20, 'UNEXPECTEDLY COMPILED:', '-'*20
  629. print s
  630. print '%s: %s' % (e.__class__, e)
  631. print '-'*60