From: Stefan Behnel Date: Sat, 17 Jul 2010 15:28:12 +0000 (+0200) Subject: better error message for tuple unpacking, following Python 3.2 (including a test... X-Git-Tag: 0.13.beta0~10 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8f8ef5cd969e0fc8589455e0407e8b664cf919a8;p=cython.git better error message for tuple unpacking, following Python 3.2 (including a test fix) --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 60e79b4e..1905a30d 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -3642,9 +3642,9 @@ class SequenceNode(ExprNode): code.put_gotref(item.py_result()) 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())) + code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s, %d)" % ( + self.iterator.py_result(), + len(self.args))) if debug_disposal_code: print("UnpackNode.generate_assignment_code:") print("...generating disposal code for %s" % self.iterator) @@ -6981,11 +6981,16 @@ impl = """ raise_too_many_values_to_unpack = UtilityCode( proto = """ -static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(void); +static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected); """, impl = ''' -static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(void) { - PyErr_SetString(PyExc_ValueError, "too many values to unpack"); +static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) { + PyErr_Format(PyExc_ValueError, + #if PY_VERSION_HEX < 0x02050000 + "too many values to unpack (expected %d)", (int)expected); + #else + "too many values to unpack (expected %zd)", expected); + #endif } ''') @@ -7018,7 +7023,7 @@ static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) { } else if (PyTuple_GET_SIZE(t) < index) { __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t)); } else { - __Pyx_RaiseTooManyValuesError(); + __Pyx_RaiseTooManyValuesError(index); } } """, @@ -7030,7 +7035,7 @@ requires = [raise_none_iter_error_utility_code, unpacking_utility_code = UtilityCode( proto = """ static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/ -static int __Pyx_EndUnpack(PyObject *); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, Py_ssize_t expected); /*proto*/ """, impl = """ static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) { @@ -7043,11 +7048,11 @@ static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) { return item; } -static int __Pyx_EndUnpack(PyObject *iter) { +static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) { PyObject *item; if ((item = PyIter_Next(iter))) { Py_DECREF(item); - __Pyx_RaiseTooManyValuesError(); + __Pyx_RaiseTooManyValuesError(expected); return -1; } else if (!PyErr_Occurred()) diff --git a/tests/run/tupleassign.pyx b/tests/run/tupleassign.pyx index 875e70bd..eb5387b8 100644 --- a/tests/run/tupleassign.pyx +++ b/tests/run/tupleassign.pyx @@ -15,7 +15,7 @@ def assign3(t): ValueError: need more than 2 values to unpack >>> assign3((1,2,3,4)) Traceback (most recent call last): - ValueError: too many values to unpack + ValueError: too many values to unpack (expected 3) """ a,b,c = t return (a,b,c) @@ -39,16 +39,16 @@ def assign3_typed(tuple t): >>> assign3_typed((1,2)) Traceback (most recent call last): ValueError: need more than 2 values to unpack - >>> a,b,c = (1,2,3,4) + >>> a,b,c = (1,2,3,4) # doctest: +ELLIPSIS Traceback (most recent call last): - ValueError: too many values to unpack + ValueError: too many values to unpack... >>> assign3_typed((1,2,3,4)) Traceback (most recent call last): - ValueError: too many values to unpack + ValueError: too many values to unpack (expected 3) >>> a,b = 99,98 - >>> a,b = t + >>> a,b = t # doctest: +ELLIPSIS Traceback (most recent call last): - ValueError: too many values to unpack + ValueError: too many values to unpack... >>> a,b (99, 98) """