diff options
-rw-r--r-- | Cython/Utility/TypeConversion.c | 8 | ||||
-rw-r--r-- | tests/run/ctuple.pyx | 6 |
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 |