Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r89463:e0ba73be669b
Date: 2017-01-10 09:19 +0100
http://bitbucket.org/pypy/pypy/changeset/e0ba73be669b/

Log:    Detail: CO_GENERATOR is set in addition to CO_COROUTINE in some
        (nonsensical imho) cases

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
@@ -375,7 +375,7 @@
             l += 1
         return l
 
-    def _visit_function(self, func, function_code_generator, extra_flag):
+    def _visit_function(self, func, function_code_generator):
         self.update_position(func.lineno, True)
         # Load decorators first, but apply them after the function is created.
         self.visit_sequence(func.decorator_list)
@@ -392,7 +392,6 @@
         oparg |= num_annotations << 16
         code, qualname = self.sub_scope(function_code_generator, func.name,
                                         func, func.lineno)
-        code.co_flags |= extra_flag
         self._make_function(code, oparg, qualname=qualname)
         # Apply decorators.
         if func.decorator_list:
@@ -401,11 +400,10 @@
         self.name_op(func.name, ast.Store)
 
     def visit_FunctionDef(self, func):
-        self._visit_function(func, FunctionCodeGenerator, 0)
+        self._visit_function(func, FunctionCodeGenerator)
 
     def visit_AsyncFunctionDef(self, func):
-        self._visit_function(func, AsyncFunctionCodeGenerator,
-                             consts.CO_COROUTINE)
+        self._visit_function(func, AsyncFunctionCodeGenerator)
 
     def visit_Lambda(self, lam):
         self.update_position(lam.lineno)
@@ -1569,6 +1567,10 @@
             for i in range(start, len(func.body)):
                 func.body[i].walkabout(self)
 
+    def _get_code_flags(self):
+        flags = AbstractFunctionCodeGenerator._get_code_flags(self)
+        return flags | consts.CO_COROUTINE
+
 class LambdaCodeGenerator(AbstractFunctionCodeGenerator):
 
     def _compile(self, lam):
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
@@ -317,7 +317,11 @@
                           yield_node.col_offset)
         
     def note_await(self, await_node):
-        pass
+        # Compatibility with CPython 3.5: set the CO_GENERATOR flag in
+        # addition to the CO_COROUTINE flag if the function uses the
+        # "await" keyword.  Don't do it if the function does not.  In
+        # that case, CO_GENERATOR is ignored anyway.
+        self.is_generator = True
 
 
 class ClassScope(Scope):
diff --git a/pypy/interpreter/astcompiler/test/test_symtable.py 
b/pypy/interpreter/astcompiler/test/test_symtable.py
--- a/pypy/interpreter/astcompiler/test/test_symtable.py
+++ b/pypy/interpreter/astcompiler/test/test_symtable.py
@@ -364,6 +364,13 @@
             scp = self.func_scope(input)
         scp = self.func_scope("def f():\n    return\n    yield x")
 
+    def test_async_def(self):
+        # CPython compatibility only; "is_generator" is otherwise ignored
+        scp = self.func_scope("async def f(): pass")
+        assert not scp.is_generator
+        scp = self.func_scope("async def f(): await 5")
+        assert scp.is_generator
+
     def test_yield_inside_try(self):
         scp = self.func_scope("def f(): yield x")
         assert not scp.has_yield_inside_try
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to