1. Pypy
  2. Untitled project
  3. pypy

Commits

Armin Rigo  committed c525a21

Fix for issue #806.

  • Participants
  • Parent commits 8a99e7c
  • Branches default

Comments (0)

Files changed (3)

File pypy/interpreter/astcompiler/codegen.py

View file
     def _compile(self, func):
         assert isinstance(func, ast.FunctionDef)
         # If there's a docstring, store it as the first constant.
-        doc_expr = self.possible_docstring(func.body[0])
+        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
         if args.args:
             self._handle_nested_args(args.args)
             self.argcount = len(args.args)
-        for i in range(start, len(func.body)):
-            func.body[i].walkabout(self)
+        if func.body:
+            for i in range(start, len(func.body)):
+                func.body[i].walkabout(self)
 
 
 class LambdaCodeGenerator(AbstractFunctionCodeGenerator):

File pypy/interpreter/astcompiler/symtable.py

View file
         new_scope = FunctionScope(func.name, func.lineno, func.col_offset)
         self.push_scope(new_scope, func)
         func.args.walkabout(self)
-        self.visit_sequence(func.body)
+        # 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()
 
     def visit_Return(self, ret):

File pypy/module/_ast/test/test_ast.py

View file
         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