[pypy-svn] r15045 - in pypy/dist/pypy/interpreter/pyparser: . test test/samples

adim at codespeak.net adim at codespeak.net
Mon Jul 25 17:58:04 CEST 2005


Author: adim
Date: Mon Jul 25 17:58:02 2005
New Revision: 15045

Added:
   pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_several_statements.py
Modified:
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
Log:
implemented file_input target (missing docstring management)



Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Mon Jul 25 17:58:02 2005
@@ -66,9 +66,6 @@
     L.reverse()
     return L
     
-def build_single_input( builder, nb ):
-    pass
-
 def eval_number(value):
     """temporary implementation"""
     return eval(value)
@@ -313,6 +310,26 @@
             nodes.append(node)
     builder.push( ast.Stmt(nodes) )
 
+def build_file_input(builder, nb):
+    # FIXME: need to handle docstring !
+    doc = None
+    # doc = self.get_docstring(nodelist, symbol.file_input)
+    # if doc is not None:
+    #     i = 1
+    # else:
+    #     i = 0
+    stmts = []
+    L = get_atoms(builder, nb)
+    for node in L:
+        if isinstance(node, ast.Stmt):
+            stmts.extend(node.nodes)
+        elif isinstance(node, TokenObject) and node.name == tok.ENDMARKER:
+            # XXX Can't we just remove the last element of the list ?
+            break
+        else:
+            stmts.append(node)
+    return builder.push(ast.Module(doc, ast.Stmt(stmts)))
+
 def build_single_input( builder, nb ):
     L = get_atoms( builder, nb )
     l = len(L)
@@ -320,7 +337,8 @@
         builder.push(ast.Module(None, L[0]))
         return
     raise WalkerError("error")
-    
+
+
 def build_testlist_gexp(builder, nb):
     L = get_atoms(builder, nb)
     l = len(L)
@@ -554,6 +572,7 @@
     sym.small_stmt : return_one,
     sym.simple_stmt : build_simple_stmt,
     sym.single_input : build_single_input,
+    sym.file_input : build_file_input,
     sym.testlist_gexp : build_testlist_gexp,
     sym.lambdef : build_lambdef,
     sym.varargslist : build_varargslist,

Added: pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_several_statements.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_several_statements.py	Mon Jul 25 17:58:02 2005
@@ -0,0 +1,3 @@
+a = 1
+b = 2
+c = a + b

Modified: pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	Mon Jul 25 17:58:02 2005
@@ -105,18 +105,25 @@
     multiexpr,
     ]
 
-def ast_parse_expr(expr):
+TARGET_DICT = {
+    'single' : 'single_input',
+    'exec'   : 'file_input',
+    'eval'   : 'eval_input',
+    }
+
+def ast_parse_expr(expr, target='single'):
+    target = TARGET_DICT[target]
     builder = AstBuilder()
-    PYTHON_PARSER.parse_source(expr, 'single_input', builder)
+    PYTHON_PARSER.parse_source(expr, target, builder)
     return builder
 
-def tuple_parse_expr(expr):
+def tuple_parse_expr(expr, target='single'):
     t = Transformer()
-    return ast_from_input(expr, 'single', t)
+    return ast_from_input(expr, target, t)
 
-def check_expression(expr):
-    r1 = ast_parse_expr(expr)
-    ast = tuple_parse_expr(expr)
+def check_expression(expr, target='single'):
+    r1 = ast_parse_expr(expr, target)
+    ast = tuple_parse_expr(expr, target)
     print "-" * 30
     print "ORIG :", ast
     print 
@@ -131,8 +138,9 @@
             yield check_expression, expr
 
 
-SNIPPETS = [
-#     'snippet_1.py',
+SNIPPETS = [    
+    'snippet_1.py',
+    'snippet_several_statements.py',
 #    'snippet_2.py',
 #    'snippet_3.py',
 #    'snippet_4.py',
@@ -157,10 +165,9 @@
 #    'snippet_whitespaces.py',
     ]
 
-def skippedtest_snippets():
-    py.test.skip('Not ready to test on real snippet files')
+def test_snippets():
     for snippet_name in SNIPPETS:
         filepath = os.path.join(os.path.dirname(__file__), 'samples', snippet_name)
         source = file(filepath).read()
-        yield check_expression, source
+        yield check_expression, source, 'exec'
 



More information about the Pypy-commit mailing list