Add support for return with no value inside generator
authorVitja Makarov <vitja.makarov@gmail.com>
Sun, 9 Jan 2011 09:41:08 +0000 (12:41 +0300)
committerVitja Makarov <vitja.makarov@gmail.com>
Sun, 9 Jan 2011 09:41:08 +0000 (12:41 +0300)
Cython/Compiler/Nodes.py
Cython/Compiler/ParseTreeTransforms.py
tests/errors/e_generators.pyx
tests/run/generators.pyx

index 55ba02122617fc8071cd1c3690ee60788486ef8f..8aedbde4eacdaabbe09b852ecbcf4a33c2ab0759 100644 (file)
@@ -3019,7 +3019,7 @@ class GeneratorBodyDefNode(DefNode):
 
         # ----- Non-error return cleanup
         code.put_label(code.return_label)
-
+        code.put_xdecref(Naming.retval_cname, py_object_type)
         code.putln('%s->%s.resume_label = -1;' % (Naming.cur_scope_cname, Naming.obj_base_cname))
         code.put_finish_refcount_context()
         code.putln('return NULL;');
index 5c9e2afa7d6855e2ddfaa938ed8f71d691f774c1..2a97619ea9f8a9b98027f68bd643761e5a4fb633 100644 (file)
@@ -1370,8 +1370,8 @@ class MarkClosureVisitor(CythonTransform):
         collector.visitchildren(node)
 
         if collector.yields:
-            if collector.returns and not collector.has_return_value:
-                error(collector.returns[0].pos, "'return' inside generators not yet supported ")
+            #if collector.returns and not collector.has_return_value:
+            #    error(collector.returns[0].pos, "'return' inside generators not yet supported ")
 
             gbody = Nodes.GeneratorBodyDefNode(pos=node.pos,
                                                name=node.name,
index 5cee59dc8538477b5100290c2ce061e5b6b395a5..af5ca738a0898c95b691c54f62e86a78de4c830c 100644 (file)
@@ -6,10 +6,6 @@ def bar(a):
     return 0
     yield
 
-def xxx():
-    yield
-    return
-
 yield
 
 class Foo:
@@ -18,7 +14,6 @@ class Foo:
 _ERRORS = u"""
 3:4: 'return' with argument inside generator
 7:4: 'yield' outside function
-11:4: 'return' inside generators not yet supported
-13:0: 'yield' not supported here
-16:4: 'yield' not supported here
+9:0: 'yield' not supported here
+12:4: 'yield' not supported here
 """
index fe2efb5f0cdda3c264991bc7d3d7747efbbdbec7..42e61352743de6904e16d01c39a988c8f8524987 100644 (file)
@@ -244,3 +244,18 @@ def test_decorated(*args):
     for i in args:
         yield i
 
+def test_return(a):
+    """
+    >>> d = dict()
+    >>> obj = test_return(d)
+    >>> next(obj)
+    1
+    >>> next(obj)
+    Traceback (most recent call last):
+    StopIteration
+    >>> d['i_was_here']
+    True
+    """
+    yield 1
+    a['i_was_here'] = True
+    return