diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:03:44 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:03:44 (GMT) |
commit | 618dc5e06459dab5fae2dc0a8caee7d15afd6410 (patch) | |
tree | 2881b2c079821ef43895742f3c65171acf582cc1 /Lib/test/test_compile.py | |
parent | d3372793d6de665be2f866e9245a33bcefeaaa76 (diff) | |
download | cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.zip cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.tar.gz cpython-618dc5e06459dab5fae2dc0a8caee7d15afd6410.tar.bz2 |
Merged revisions 62004 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62004 | georg.brandl | 2008-03-28 13:11:56 +0100 (Fr, 28 Mär 2008) | 4 lines
Patch #1810 by Thomas Lee, reviewed by myself:
allow compiling Python AST objects into code objects
in compile().
........
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r-- | Lib/test/test_compile.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 4979f92..376369b 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1,5 +1,6 @@ import unittest import sys +import _ast from test import test_support class TestSpecifics(unittest.TestCase): @@ -406,6 +407,28 @@ if 1: self.assert_("_A__mangled_mod" in A.f.__code__.co_varnames) self.assert_("__package__" in A.f.__code__.co_varnames) + def test_compile_ast(self): + fname = __file__ + if fname.lower().endswith(('pyc', 'pyo')): + fname = fname[:-1] + with open(fname, 'r') as f: + fcontents = f.read() + sample_code = [ + ['<assign>', 'x = 5'], + ['<ifblock>', """if True:\n pass\n"""], + ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], + ['<deffunc>', """def foo():\n pass\nfoo()\n"""], + [fname, fcontents], + ] + + for fname, code in sample_code: + co1 = compile(code, '%s1' % fname, 'exec') + ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) + self.assert_(type(ast) == _ast.Module) + co2 = compile(ast, '%s3' % fname, 'exec') + self.assertEqual(co1, co2) + + def test_main(): test_support.run_unittest(TestSpecifics) |