From 20108dcdd4780e436d04e5a2d02c2721c0fef462 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 21 Nov 2008 12:07:26 +0100 Subject: [PATCH] fix sequence assignments for value coercion to non-Python types - coercion code cannot currently be taken out of the conditional as the temp release of the coercion code happens straight away --- Cython/Compiler/ExprNodes.py | 10 +++-- tests/run/tupleassign.pyx | 86 +++++++++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 2f4560e9..9612dd86 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2793,7 +2793,10 @@ class SequenceNode(ExprNode): item.result(), i)) code.put_incref(item.result(), item.ctype()) + value_node = self.coerced_unpacked_items[i] + value_node.generate_evaluation_code(code) rhs.generate_disposal_code(code) + code.putln("} else {") code.putln( @@ -2811,6 +2814,8 @@ class SequenceNode(ExprNode): item.result(), typecast(item.ctype(), py_object_type, unpack_code), code.error_goto_if_null(item.result(), self.pos))) + value_node = self.coerced_unpacked_items[i] + value_node.generate_evaluation_code(code) code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s)" % ( self.iterator.py_result())) @@ -2821,9 +2826,8 @@ class SequenceNode(ExprNode): code.putln("}") for i in range(len(self.args)): - value_node = self.coerced_unpacked_items[i] - value_node.generate_evaluation_code(code) - self.args[i].generate_assignment_code(value_node, code) + self.args[i].generate_assignment_code( + self.coerced_unpacked_items[i], code) def annotate(self, code): for arg in self.args: diff --git a/tests/run/tupleassign.pyx b/tests/run/tupleassign.pyx index 3149f2b9..4339da12 100644 --- a/tests/run/tupleassign.pyx +++ b/tests/run/tupleassign.pyx @@ -3,19 +3,41 @@ __doc__ = u""" (1, 2, 3) >>> assign3(t) (1, 2, 3) ->>> +>>> assign3_int(l) +(1, 2, 3) +>>> assign3_mixed1(l) +(1, 2, 3) +>>> assign3_mixed2(l) +(1, 2, 3) +>>> assign3_mixed3(l) +(1, 2, 3) ->>> a,b = 99,99 +>>> a,b = 99,98 >>> a,b = t Traceback (most recent call last): ValueError: too many values to unpack >>> a,b -(99, 99) +(99, 98) >>> test_overwrite(l) -(99, 99) +(99, 98) >>> test_overwrite(t) -(99, 99) +(99, 98) + +>>> test_overwrite_int(l) +(99, 98) +>>> test_overwrite_int(t) +(99, 98) + +>>> test_overwrite_mixed(l) +(99, 98) +>>> test_overwrite_mixed(t) +(99, 98) + +>>> test_overwrite_mixed2(l) +(99, 98) +>>> test_overwrite_mixed2(t) +(99, 98) """ t = (1,2,3) @@ -25,8 +47,60 @@ def assign3(t): a,b,c = t return (a,b,c) +def assign3_int(t): + cdef int a,b,c + a,b,c = t + return (a,b,c) + +def assign3_mixed1(t): + cdef int a + a,b,c = t + return (a,b,c) + +def assign3_mixed2(t): + cdef int b + a,b,c = t + return (a,b,c) + +def assign3_mixed3(t): + cdef int c + a,b,c = t + return (a,b,c) + +def assign3_mixed4(t): + cdef int b,c + a,b,c = t + return (a,b,c) + def test_overwrite(t): - a,b = 99,99 + a,b = 99,98 + try: + a,b = t + except ValueError: + pass + return (a,b) + +def test_overwrite_int(t): + cdef int a,b + a,b = 99,98 + try: + a,b = t + except ValueError: + pass + return (a,b) + +def test_overwrite_mixed(t): + cdef int b + a,b = 99,98 + try: + a,b = t + except ValueError: + pass + return (a,b) + +def test_overwrite_mixed2(t): + cdef int a + a,b = 99,98 try: a,b = t except ValueError: -- 2.26.2