[pypy-commit] pypy default: Fix for issue #806.

arigo noreply at buildbot.pypy.org
Sun Jul 24 12:12:39 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r45928:c525a219909f
Date: 2011-07-24 12:12 +0200
http://bitbucket.org/pypy/pypy/changeset/c525a219909f/

Log:	Fix for issue #806.

diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -1251,7 +1251,10 @@
     def _compile(self, func):
         assert isinstance(func, ast.FunctionDef)
         # If there's a docstring, store it as the first constant.
+        if func.body:
         doc_expr = self.possible_docstring(func.body[0])
+        else:
+            doc_expr = None
         if doc_expr is not None:
             self.add_const(doc_expr.s)
             start = 1
@@ -1263,6 +1266,7 @@
         if args.args:
             self._handle_nested_args(args.args)
             self.argcount = len(args.args)
+        if func.body:
         for i in range(start, len(func.body)):
             func.body[i].walkabout(self)
 
diff --git a/pypy/interpreter/astcompiler/symtable.py b/pypy/interpreter/astcompiler/symtable.py
--- a/pypy/interpreter/astcompiler/symtable.py
+++ b/pypy/interpreter/astcompiler/symtable.py
@@ -363,6 +363,9 @@
         new_scope = FunctionScope(func.name, func.lineno, func.col_offset)
         self.push_scope(new_scope, func)
         func.args.walkabout(self)
+        # func.body should never be empty (or else it contains one Pass)
+        # but it can be if we make an ast.FunctionDef() from app-level.
+        if func.body:
         self.visit_sequence(func.body)
         self.pop_scope()
 
diff --git a/pypy/module/_ast/test/test_ast.py b/pypy/module/_ast/test/test_ast.py
--- a/pypy/module/_ast/test/test_ast.py
+++ b/pypy/module/_ast/test/test_ast.py
@@ -250,3 +250,19 @@
         assert x.left == n1
         assert x.op == addop
         assert x.right == n3
+
+    def test_functiondef(self):
+        import _ast as ast
+        fAst = ast.FunctionDef(
+            name="foo",
+            args=ast.arguments(
+                args=[], vararg=None, kwarg=None, defaults=[],
+                kwonlyargs=[], kw_defaults=[]),
+            body=[], decorator_list=[], lineno=5, col_offset=0)
+        exprAst = ast.Interactive(body=[fAst])
+        compiled = compile(exprAst, "<foo>", "single")
+        #
+        d = {}
+        eval(compiled, d, d)
+        assert type(d['foo']) is type(lambda: 42)
+        assert d['foo']() is None


More information about the pypy-commit mailing list