[Python-checkins] r66900 - in python/branches/tlee-ast-optimize/Lib: ast.py test/test_ast.py

thomas.lee python-checkins at python.org
Wed Oct 15 06:06:44 CEST 2008


Author: thomas.lee
Date: Wed Oct 15 06:06:44 2008
New Revision: 66900

Log:
Get test_ast passing. Added the optimize keyword arg to ast.parse().

Modified:
   python/branches/tlee-ast-optimize/Lib/ast.py
   python/branches/tlee-ast-optimize/Lib/test/test_ast.py

Modified: python/branches/tlee-ast-optimize/Lib/ast.py
==============================================================================
--- python/branches/tlee-ast-optimize/Lib/ast.py	(original)
+++ python/branches/tlee-ast-optimize/Lib/ast.py	Wed Oct 15 06:06:44 2008
@@ -28,12 +28,15 @@
 from _ast import *
 
 
-def parse(expr, filename='<unknown>', mode='exec'):
+def parse(expr, filename='<unknown>', mode='exec', optimize=True):
     """
     Parse an expression into an AST node.
     Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST).
     """
-    return compile(expr, filename, mode, PyCF_ONLY_AST)
+    flags = PyCF_ONLY_AST
+    if not optimize:
+        flags |= PyCF_NO_OPTIMIZE
+    return compile(expr, filename, mode, flags)
 
 
 def literal_eval(node_or_string):
@@ -63,6 +66,8 @@
         elif isinstance(node, Name):
             if node.id in _safe_names:
                 return _safe_names[node.id]
+        elif isinstance(node, Const):
+            return node.value
         raise ValueError('malformed string')
     return _convert(node_or_string)
 

Modified: python/branches/tlee-ast-optimize/Lib/test/test_ast.py
==============================================================================
--- python/branches/tlee-ast-optimize/Lib/test/test_ast.py	(original)
+++ python/branches/tlee-ast-optimize/Lib/test/test_ast.py	Wed Oct 15 06:06:44 2008
@@ -210,7 +210,7 @@
         )
 
     def test_copy_location(self):
-        src = ast.parse('1 + 1', mode='eval')
+        src = ast.parse('1 + 1', mode='eval', optimize=False)
         src.body.right = ast.copy_location(ast.Num(2), src.body.right)
         self.assertEqual(ast.dump(src, include_attributes=True),
             'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
@@ -235,7 +235,7 @@
         )
 
     def test_increment_lineno(self):
-        src = ast.parse('1 + 1', mode='eval')
+        src = ast.parse('1 + 1', mode='eval', optimize=False)
         self.assertEqual(ast.increment_lineno(src, n=3), src)
         self.assertEqual(ast.dump(src, include_attributes=True),
             'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '


More information about the Python-checkins mailing list