From: Stefan Behnel Date: Tue, 14 Dec 2010 05:57:34 +0000 (+0100) Subject: fix first assignments to closure variables X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1c4d20b829ffa3533f8cf54ff2b0e720484c618b;p=cython.git fix first assignments to closure variables --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 78af3e15..e46b9521 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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: diff --git a/tests/run/generators.pyx b/tests/run/generators.pyx index 02f519d4..de15c1ad 100644 --- a/tests/run/generators.pyx +++ b/tests/run/generators.pyx @@ -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): """