/Lib/test/test_compiler.py

http://unladen-swallow.googlecode.com/ · Python · 254 lines · 192 code · 41 blank · 21 comment · 27 complexity · 745f1bd8153cf5fa9ca26d6a767746c9 MD5 · raw file

  1. import test.test_support
  2. compiler = test.test_support.import_module('compiler', deprecated=True)
  3. from compiler.ast import flatten
  4. import os, sys, time, unittest
  5. from random import random
  6. from StringIO import StringIO
  7. # How much time in seconds can pass before we print a 'Still working' message.
  8. _PRINT_WORKING_MSG_INTERVAL = 5 * 60
  9. class TrivialContext(object):
  10. def __enter__(self):
  11. return self
  12. def __exit__(self, *exc_info):
  13. pass
  14. class CompilerTest(unittest.TestCase):
  15. def testCompileLibrary(self):
  16. # A simple but large test. Compile all the code in the
  17. # standard library and its test suite. This doesn't verify
  18. # that any of the code is correct, merely the compiler is able
  19. # to generate some kind of code for it.
  20. next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
  21. libdir = os.path.dirname(unittest.__file__)
  22. testdir = os.path.dirname(test.test_support.__file__)
  23. for dir in [libdir, testdir]:
  24. for basename in os.listdir(dir):
  25. # Print still working message since this test can be really slow
  26. if next_time <= time.time():
  27. next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
  28. print >>sys.__stdout__, \
  29. ' testCompileLibrary still working, be patient...'
  30. sys.__stdout__.flush()
  31. if not basename.endswith(".py"):
  32. continue
  33. if not TEST_ALL and random() < 0.98:
  34. continue
  35. path = os.path.join(dir, basename)
  36. if test.test_support.verbose:
  37. print "compiling", path
  38. f = open(path, "U")
  39. buf = f.read()
  40. f.close()
  41. if "badsyntax" in basename or "bad_coding" in basename:
  42. self.assertRaises(SyntaxError, compiler.compile,
  43. buf, basename, "exec")
  44. else:
  45. try:
  46. compiler.compile(buf, basename, "exec")
  47. except Exception, e:
  48. args = list(e.args)
  49. args.append("in file %s]" % basename)
  50. #args[0] += "[in file %s]" % basename
  51. e.args = tuple(args)
  52. raise
  53. def testNewClassSyntax(self):
  54. compiler.compile("class foo():pass\n\n","<string>","exec")
  55. def testYieldExpr(self):
  56. compiler.compile("def g(): yield\n\n", "<string>", "exec")
  57. def testKeywordAfterStarargs(self):
  58. def f(*args, **kwargs):
  59. self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4}))
  60. c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec')
  61. exec c in {'f': f}
  62. self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)")
  63. self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)")
  64. def testTryExceptFinally(self):
  65. # Test that except and finally clauses in one try stmt are recognized
  66. c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
  67. "<string>", "exec")
  68. dct = {}
  69. exec c in dct
  70. self.assertEquals(dct.get('e'), 1)
  71. self.assertEquals(dct.get('f'), 1)
  72. def testDefaultArgs(self):
  73. self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
  74. def testDocstrings(self):
  75. c = compiler.compile('"doc"', '<string>', 'exec')
  76. self.assert_('__doc__' in c.co_names)
  77. c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
  78. g = {}
  79. exec c in g
  80. self.assertEquals(g['f'].__doc__, "doc")
  81. def testLineNo(self):
  82. # Test that all nodes except Module have a correct lineno attribute.
  83. filename = __file__
  84. if filename.endswith((".pyc", ".pyo")):
  85. filename = filename[:-1]
  86. tree = compiler.parseFile(filename)
  87. self.check_lineno(tree)
  88. def check_lineno(self, node):
  89. try:
  90. self._check_lineno(node)
  91. except AssertionError:
  92. print node.__class__, node.lineno
  93. raise
  94. def _check_lineno(self, node):
  95. if not node.__class__ in NOLINENO:
  96. self.assert_(isinstance(node.lineno, int),
  97. "lineno=%s on %s" % (node.lineno, node.__class__))
  98. self.assert_(node.lineno > 0,
  99. "lineno=%s on %s" % (node.lineno, node.__class__))
  100. for child in node.getChildNodes():
  101. self.check_lineno(child)
  102. def testFlatten(self):
  103. self.assertEquals(flatten([1, [2]]), [1, 2])
  104. self.assertEquals(flatten((1, (2,))), [1, 2])
  105. def testNestedScope(self):
  106. c = compiler.compile('def g():\n'
  107. ' a = 1\n'
  108. ' def f(): return a + 2\n'
  109. ' return f()\n'
  110. 'result = g()',
  111. '<string>',
  112. 'exec')
  113. dct = {}
  114. exec c in dct
  115. self.assertEquals(dct.get('result'), 3)
  116. def testGenExp(self):
  117. c = compiler.compile('list((i,j) for i in range(3) if i < 3'
  118. ' for j in range(4) if j > 2)',
  119. '<string>',
  120. 'eval')
  121. self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
  122. def testWith(self):
  123. # SF bug 1638243
  124. c = compiler.compile('from __future__ import with_statement\n'
  125. 'def f():\n'
  126. ' with TrivialContext():\n'
  127. ' return 1\n'
  128. 'result = f()',
  129. '<string>',
  130. 'exec' )
  131. dct = {'TrivialContext': TrivialContext}
  132. exec c in dct
  133. self.assertEquals(dct.get('result'), 1)
  134. def testWithAss(self):
  135. c = compiler.compile('from __future__ import with_statement\n'
  136. 'def f():\n'
  137. ' with TrivialContext() as tc:\n'
  138. ' return 1\n'
  139. 'result = f()',
  140. '<string>',
  141. 'exec' )
  142. dct = {'TrivialContext': TrivialContext}
  143. exec c in dct
  144. self.assertEquals(dct.get('result'), 1)
  145. def testPrintFunction(self):
  146. c = compiler.compile('from __future__ import print_function\n'
  147. 'print("a", "b", sep="**", end="++", '
  148. 'file=output)',
  149. '<string>',
  150. 'exec' )
  151. dct = {'output': StringIO()}
  152. exec c in dct
  153. self.assertEquals(dct['output'].getvalue(), 'a**b++')
  154. def _testErrEnc(self, src, text, offset):
  155. try:
  156. compile(src, "", "exec")
  157. except SyntaxError, e:
  158. self.assertEquals(e.offset, offset)
  159. self.assertEquals(e.text, text)
  160. def testSourceCodeEncodingsError(self):
  161. # Test SyntaxError with encoding definition
  162. sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
  163. ascii = "print '12345678', '\n"
  164. encdef = "#! -*- coding: ShiftJIS -*-\n"
  165. # ascii source without encdef
  166. self._testErrEnc(ascii, ascii, 19)
  167. # ascii source with encdef
  168. self._testErrEnc(encdef+ascii, ascii, 19)
  169. # non-ascii source with encdef
  170. self._testErrEnc(encdef+sjis, sjis, 19)
  171. # ShiftJIS source without encdef
  172. self._testErrEnc(sjis, sjis, 19)
  173. NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
  174. ###############################################################################
  175. # code below is just used to trigger some possible errors, for the benefit of
  176. # testLineNo
  177. ###############################################################################
  178. class Toto:
  179. """docstring"""
  180. pass
  181. a, b = 2, 3
  182. [c, d] = 5, 6
  183. l = [(x, y) for x, y in zip(range(5), range(5,10))]
  184. l[0]
  185. l[3:4]
  186. d = {'a': 2}
  187. d = {}
  188. t = ()
  189. t = (1, 2)
  190. l = []
  191. l = [1, 2]
  192. if l:
  193. pass
  194. else:
  195. a, b = b, a
  196. try:
  197. print yo
  198. except:
  199. yo = 3
  200. else:
  201. yo += 3
  202. try:
  203. a += b
  204. finally:
  205. b = 0
  206. from math import *
  207. ###############################################################################
  208. def test_main():
  209. global TEST_ALL
  210. TEST_ALL = test.test_support.is_resource_enabled("compiler")
  211. test.test_support.run_unittest(CompilerTest)
  212. if __name__ == "__main__":
  213. test_main()