fix first assignments to closure variables
authorStefan Behnel <scoder@users.berlios.de>
Tue, 14 Dec 2010 05:57:34 +0000 (06:57 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 14 Dec 2010 05:57:34 +0000 (06:57 +0100)
Cython/Compiler/ExprNodes.py
tests/run/generators.pyx

index 78af3e15d24714ffff9216945d77807fcf346726..e46b95212ccf684a6c9c738843705ff19f131019 100755 (executable)
@@ -1619,9 +1619,9 @@ class NameNode(AtomicExprNode):
                 if self.use_managed_ref:
                     rhs.make_owned_reference(code)
                     is_external_ref = entry.is_cglobal or self.entry.in_closure or self.entry.from_closure
-                    if is_external_ref:
-                        code.put_gotref(self.py_result())
                     if not self.lhs_of_first_assignment:
+                        if is_external_ref:
+                            code.put_gotref(self.py_result())
                         if entry.is_local and not Options.init_local_none:
                             initialized = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
                             if initialized is True:
index 02f519d4080be531dec2860a27ea47cce28fa936..de15c1adfbfd5a8ca94dca90bf3a2f44251d05fa 100644 (file)
@@ -148,6 +148,22 @@ def check_throw():
         except ValueError:
             pass
 
+def test_first_assignment():
+    """
+    >>> gen = test_first_assignment()
+    >>> next(gen)
+    5
+    >>> next(gen)
+    10
+    >>> next(gen)
+    (5, 10)
+    """
+    cdef x = 5 # first
+    yield x
+    cdef y = 10 # first
+    yield y
+    yield (x,y)
+
 
 class Foo(object):
     """