Commit 8f8ef5cd authored by Stefan Behnel's avatar Stefan Behnel

better error message for tuple unpacking, following Python 3.2 (including a test fix)

parent 9f5cace1
...@@ -3642,9 +3642,9 @@ class SequenceNode(ExprNode): ...@@ -3642,9 +3642,9 @@ class SequenceNode(ExprNode):
code.put_gotref(item.py_result()) code.put_gotref(item.py_result())
value_node = self.coerced_unpacked_items[i] value_node = self.coerced_unpacked_items[i]
value_node.generate_evaluation_code(code) value_node.generate_evaluation_code(code)
code.put_error_if_neg(self.pos, code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s, %d)" % (
"__Pyx_EndUnpack(%s)" % ( self.iterator.py_result(),
self.iterator.py_result())) len(self.args)))
if debug_disposal_code: if debug_disposal_code:
print("UnpackNode.generate_assignment_code:") print("UnpackNode.generate_assignment_code:")
print("...generating disposal code for %s" % self.iterator) print("...generating disposal code for %s" % self.iterator)
...@@ -6981,11 +6981,16 @@ impl = """ ...@@ -6981,11 +6981,16 @@ impl = """
raise_too_many_values_to_unpack = UtilityCode( raise_too_many_values_to_unpack = UtilityCode(
proto = """ proto = """
static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(void); static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
""", """,
impl = ''' impl = '''
static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(void) { static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
PyErr_SetString(PyExc_ValueError, "too many values to unpack"); 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) { ...@@ -7018,7 +7023,7 @@ static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
} else if (PyTuple_GET_SIZE(t) < index) { } else if (PyTuple_GET_SIZE(t) < index) {
__Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t)); __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
} else { } else {
__Pyx_RaiseTooManyValuesError(); __Pyx_RaiseTooManyValuesError(index);
} }
} }
""", """,
...@@ -7030,7 +7035,7 @@ requires = [raise_none_iter_error_utility_code, ...@@ -7030,7 +7035,7 @@ requires = [raise_none_iter_error_utility_code,
unpacking_utility_code = UtilityCode( unpacking_utility_code = UtilityCode(
proto = """ proto = """
static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*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 = """ impl = """
static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) { static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
...@@ -7043,11 +7048,11 @@ 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; return item;
} }
static int __Pyx_EndUnpack(PyObject *iter) { static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) {
PyObject *item; PyObject *item;
if ((item = PyIter_Next(iter))) { if ((item = PyIter_Next(iter))) {
Py_DECREF(item); Py_DECREF(item);
__Pyx_RaiseTooManyValuesError(); __Pyx_RaiseTooManyValuesError(expected);
return -1; return -1;
} }
else if (!PyErr_Occurred()) else if (!PyErr_Occurred())
......
...@@ -15,7 +15,7 @@ def assign3(t): ...@@ -15,7 +15,7 @@ def assign3(t):
ValueError: need more than 2 values to unpack ValueError: need more than 2 values to unpack
>>> assign3((1,2,3,4)) >>> assign3((1,2,3,4))
Traceback (most recent call last): Traceback (most recent call last):
ValueError: too many values to unpack ValueError: too many values to unpack (expected 3)
""" """
a,b,c = t a,b,c = t
return (a,b,c) return (a,b,c)
...@@ -39,16 +39,16 @@ def assign3_typed(tuple t): ...@@ -39,16 +39,16 @@ def assign3_typed(tuple t):
>>> assign3_typed((1,2)) >>> assign3_typed((1,2))
Traceback (most recent call last): Traceback (most recent call last):
ValueError: need more than 2 values to unpack 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): Traceback (most recent call last):
ValueError: too many values to unpack ValueError: too many values to unpack...
>>> assign3_typed((1,2,3,4)) >>> assign3_typed((1,2,3,4))
Traceback (most recent call last): 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 = 99,98
>>> a,b = t >>> a,b = t # doctest: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
ValueError: too many values to unpack ValueError: too many values to unpack...
>>> a,b >>> a,b
(99, 98) (99, 98)
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment