summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2023-04-21 09:05:50 +0200
committerscoder <stefan_ml@behnel.de>2023-04-24 16:05:24 +0200
commitbe73c3cb8f30b79867bcc3e78cfc30e932486ec5 (patch)
tree6fd03c121848187af80547d31216bd8141182552
parenteb79e600256440895561fcb8404e486c77b2842c (diff)
downloadcython-be73c3cb8f30b79867bcc3e78cfc30e932486ec5.tar.gz
Improve error message when assigning tuples of incorrect size to a ctuple. Previously, it said "Expected tuple of size 4, got tuple", which is unhelpful and confusing.
-rw-r--r--Cython/Utility/TypeConversion.c8
-rw-r--r--tests/run/ctuple.pyx6
2 files changed, 9 insertions, 5 deletions
diff --git a/Cython/Utility/TypeConversion.c b/Cython/Utility/TypeConversion.c
index 6c454d145..9ca29a0f0 100644
--- a/Cython/Utility/TypeConversion.c
+++ b/Cython/Utility/TypeConversion.c
@@ -576,12 +576,16 @@ static {{struct_type_decl}} {{funcname}}(PyObject *);
static {{struct_type_decl}} {{funcname}}(PyObject * o) {
{{struct_type_decl}} result;
- if (!PyTuple_Check(o) || PyTuple_GET_SIZE(o) != {{size}}) {
+ if (unlikely(!PyTuple_Check(o))) {
__Pyx_TypeName o_type_name = __Pyx_PyType_GetName(Py_TYPE(o));
PyErr_Format(PyExc_TypeError,
- "Expected a tuple of size %d, got " __Pyx_FMT_TYPENAME, {{size}}, o_type_name);
+ "Expected a tuple of size %zd, got " __Pyx_FMT_TYPENAME, (Py_ssize_t) {{size}}, o_type_name);
__Pyx_DECREF_TypeName(o_type_name);
goto bad;
+ } else if (unlikely(PyTuple_GET_SIZE(o) != {{size}})) {
+ PyErr_Format(PyExc_TypeError,
+ "Expected a tuple of size %zd, got size %zd", (Py_ssize_t) {{size}}, PyTuple_GET_SIZE(o));
+ goto bad;
}
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
diff --git a/tests/run/ctuple.pyx b/tests/run/ctuple.pyx
index 2d92f8bad..9412a2cc3 100644
--- a/tests/run/ctuple.pyx
+++ b/tests/run/ctuple.pyx
@@ -8,11 +8,11 @@ def simple_convert(*o):
>>> simple_convert(1)
Traceback (most recent call last):
...
- TypeError: Expected a tuple of size 2, got tuple
+ TypeError: Expected a tuple of size 2, got size 1
>>> simple_convert(1, 2, 3)
Traceback (most recent call last):
...
- TypeError: Expected a tuple of size 2, got tuple
+ TypeError: Expected a tuple of size 2, got size 3
"""
cdef (int, double) xy = o
return xy
@@ -231,7 +231,7 @@ def test_mul_to_ctuple((int, int) ab, int c):
(1, 2, 1, 2)
>>> test_mul_to_ctuple((1, 2), 3)
Traceback (most recent call last):
- TypeError: Expected a tuple of size 4, got tuple
+ TypeError: Expected a tuple of size 4, got size 6
"""
result: tuple[cython.int, cython.int, cython.int, cython.int] = ab * c
return result