diff options
author | Georg Brandl <georg@python.org> | 2008-03-28 12:11:56 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-03-28 12:11:56 (GMT) |
commit | fc8eef3c78200593c9c70974e48ab859779c607a (patch) | |
tree | 6c1cddba98dad1770c0f22a08d88455d7dc7eeaa /Lib | |
parent | b9803421d231fc66489eafb45f6ae440010cacfc (diff) | |
download | cpython-fc8eef3c78200593c9c70974e48ab859779c607a.zip cpython-fc8eef3c78200593c9c70974e48ab859779c607a.tar.gz cpython-fc8eef3c78200593c9c70974e48ab859779c607a.tar.bz2 |
Patch #1810 by Thomas Lee, reviewed by myself:
allow compiling Python AST objects into code objects
in compile().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_compile.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 465a90c..ba55dfa 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): @@ -416,6 +417,32 @@ if 1: self.assert_("_A__mangled_mod" in A.f.func_code.co_varnames) self.assert_("__package__" in A.f.func_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'], + ['<print1>', 'print 1'], + ['<printv>', 'print v'], + ['<printTrue>', 'print True'], + ['<printList>', 'print []'], + ['<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) |