1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport test.test_support 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcompiler = test.test_support.import_module('compiler', deprecated=True) 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom compiler.ast import flatten 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os, sys, time, unittest 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom random import random 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom StringIO import StringIO 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# How much time in seconds can pass before we print a 'Still working' message. 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_PRINT_WORKING_MSG_INTERVAL = 5 * 60 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TrivialContext(object): 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __enter__(self): 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __exit__(self, *exc_info): 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CompilerTest(unittest.TestCase): 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testCompileLibrary(self): 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # A simple but large test. Compile all the code in the 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # standard library and its test suite. This doesn't verify 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # that any of the code is correct, merely the compiler is able 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # to generate some kind of code for it. 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # warning: if 'os' or 'test_support' are moved in some other dir, 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # they should be changed here. 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep libdir = os.path.dirname(os.__file__) 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep testdir = os.path.dirname(test.test_support.__file__) 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for dir in [testdir]: 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for basename in "test_os.py",: 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Print still working message since this test can be really slow 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if next_time <= time.time(): 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print >>sys.__stdout__, \ 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' testCompileLibrary still working, be patient...' 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep sys.__stdout__.flush() 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not basename.endswith(".py"): 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not TEST_ALL and random() < 0.98: 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = os.path.join(dir, basename) 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if test.test_support.verbose: 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "compiling", path 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f = open(path, "U") 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = f.read() 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f.close() 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if "badsyntax" in basename or "bad_coding" in basename: 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(SyntaxError, compiler.compile, 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf, basename, "exec") 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep compiler.compile(buf, basename, "exec") 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except Exception, e: 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = list(e.args) 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args.append("in file %s]" % basename) 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep #args[0] += "[in file %s]" % basename 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e.args = tuple(args) 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testNewClassSyntax(self): 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep compiler.compile("class foo():pass\n\n","<string>","exec") 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testYieldExpr(self): 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep compiler.compile("def g(): yield\n\n", "<string>", "exec") 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testKeywordAfterStarargs(self): 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def f(*args, **kwargs): 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4})) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec') 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in {'f': f} 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)") 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)") 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testTryExceptFinally(self): 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test that except and finally clauses in one try stmt are recognized 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1", 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "<string>", "exec") 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {} 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('e'), 1) 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('f'), 1) 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testDefaultArgs(self): 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass") 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testDocstrings(self): 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('"doc"', '<string>', 'exec') 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIn('__doc__', c.co_names) 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('def f():\n "doc"', '<string>', 'exec') 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep g = {} 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in g 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(g['f'].__doc__, "doc") 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testLineNo(self): 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test that all nodes except Module have a correct lineno attribute. 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep filename = __file__ 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if filename.endswith((".pyc", ".pyo")): 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep filename = filename[:-1] 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tree = compiler.parseFile(filename) 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.check_lineno(tree) 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def check_lineno(self, node): 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_lineno(node) 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except AssertionError: 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print node.__class__, node.lineno 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _check_lineno(self, node): 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not node.__class__ in NOLINENO: 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(node.lineno, int, 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "lineno=%s on %s" % (node.lineno, node.__class__)) 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(node.lineno > 0, 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "lineno=%s on %s" % (node.lineno, node.__class__)) 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for child in node.getChildNodes(): 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.check_lineno(child) 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testFlatten(self): 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(flatten([1, [2]]), [1, 2]) 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(flatten((1, (2,))), [1, 2]) 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testNestedScope(self): 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('def g():\n' 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' a = 1\n' 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' def f(): return a + 2\n' 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' return f()\n' 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'result = g()', 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'exec') 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {} 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('result'), 3) 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testGenExp(self): 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('list((i,j) for i in range(3) if i < 3' 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' for j in range(4) if j > 2)', 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'eval') 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), [(0, 3), (1, 3), (2, 3)]) 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testSetLiteral(self): 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{1, 2, 3}', '<string>', 'eval') 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1,2,3}) 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{1, 2, 3,}', '<string>', 'eval') 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1,2,3}) 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testDictLiteral(self): 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval') 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval') 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testSetComp(self): 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval') 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1, 2, 3}) 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{x * y for x in range(3) if x != 0' 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' for y in range(4) if y != 0}', 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'eval') 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1, 2, 3, 4, 6}) 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testDictComp(self): 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval') 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('{(x, y) : y for x in range(2) if x != 0' 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' for y in range(3) if y != 0}', 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'eval') 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(eval(c), {(1, 2): 2, (1, 1): 1}) 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testWith(self): 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # SF bug 1638243 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('from __future__ import with_statement\n' 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'def f():\n' 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' with TrivialContext():\n' 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' return 1\n' 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'result = f()', 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'exec' ) 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {'TrivialContext': TrivialContext} 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('result'), 1) 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testWithAss(self): 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('from __future__ import with_statement\n' 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'def f():\n' 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' with TrivialContext() as tc:\n' 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' return 1\n' 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'result = f()', 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'exec' ) 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {'TrivialContext': TrivialContext} 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('result'), 1) 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testWithMult(self): 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep events = [] 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class Ctx: 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, n): 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.n = n 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __enter__(self): 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep events.append(self.n) 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __exit__(self, *args): 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('from __future__ import with_statement\n' 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'def f():\n' 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' with Ctx(1) as tc, Ctx(2) as tc2:\n' 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ' return 1\n' 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'result = f()', 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'exec' ) 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {'Ctx': Ctx} 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct.get('result'), 1) 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(events, [1, 2]) 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testGlobal(self): 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep code = compiler.compile('global x\nx=1', '<string>', 'exec') 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d1 = {'__builtins__': {}} 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d2 = {} 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec code in d1, d2 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # x should be in the globals dict 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(d1.get('x'), 1) 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testPrintFunction(self): 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c = compiler.compile('from __future__ import print_function\n' 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'print("a", "b", sep="**", end="++", ' 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'file=output)', 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '<string>', 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'exec' ) 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dct = {'output': StringIO()} 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exec c in dct 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(dct['output'].getvalue(), 'a**b++') 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _testErrEnc(self, src, text, offset): 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep compile(src, "", "exec") 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except SyntaxError, e: 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(e.offset, offset) 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(e.text, text) 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testSourceCodeEncodingsError(self): 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test SyntaxError with encoding definition 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n" 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ascii = "print '12345678', '\n" 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep encdef = "#! -*- coding: ShiftJIS -*-\n" 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ascii source without encdef 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._testErrEnc(ascii, ascii, 19) 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ascii source with encdef 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._testErrEnc(encdef+ascii, ascii, 19) 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # non-ascii source with encdef 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._testErrEnc(encdef+sjis, sjis, 19) 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ShiftJIS source without encdef 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._testErrEnc(sjis, sjis, 19) 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepNOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep############################################################################### 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# code below is just used to trigger some possible errors, for the benefit of 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# testLineNo 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep############################################################################### 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Toto: 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """docstring""" 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa, b = 2, 3 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep[c, d] = 5, 6 278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepl = [(x, y) for x, y in zip(range(5), range(5,10))] 279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepl[0] 280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepl[3:4] 281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd = {'a': 2} 282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd = {} 283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd = {x: y for x, y in zip(range(5), range(5,10))} 284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeps = {x for x in range(10)} 285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeps = {1} 286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoept = () 287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoept = (1, 2) 288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepl = [] 289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepl = [1, 2] 290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif l: 291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse: 293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep a, b = b, a 294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry: 296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print yo 297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept: 298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep yo = 3 299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse: 300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep yo += 3 301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry: 303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep a += b 304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfinally: 305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep b = 0 306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom math import * 308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep############################################################################### 310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep global TEST_ALL 313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep TEST_ALL = test.test_support.is_resource_enabled("cpu") 314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test.test_support.run_unittest(CompilerTest) 315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 318