From 1c4d20b829ffa3533f8cf54ff2b0e720484c618b Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 14 Dec 2010 06:57:34 +0100 Subject: [PATCH] fix first assignments to closure variables --- Cython/Compiler/ExprNodes.py | 4 ++-- tests/run/generators.pyx | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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): """ -- 2.26.2