Skip to content

Commit

Permalink
Do no-type-slots dictoffset check as python lookup
Browse files Browse the repository at this point in the history
Rather than just declaring it impossible.

Fixes cython#5696
  • Loading branch information
da-woods committed Sep 12, 2023
1 parent 1078cc6 commit 3b3de95
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions Cython/Utility/ExtensionTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,38 +155,36 @@ static int __Pyx_validate_bases_tuple(const char *type_name, Py_ssize_t dictoffs
#endif
return -1;
}
#if !CYTHON_USE_TYPE_SLOTS
if (dictoffset == 0) {
PyErr_Format(PyExc_TypeError,
"extension type '%s.200s': "
"unable to validate whether bases have a __dict__ "
"when CYTHON_USE_TYPE_SLOTS is off "
"(likely because you are building in the limited API). "
"Therefore, all extension types with multiple bases "
"must add 'cdef dict __dict__' in this compilation mode",
type_name);
#if CYTHON_AVOID_BORROWED_REFS
Py_DECREF(b0);
#endif
return -1;
}
#else
if (dictoffset == 0 && b->tp_dictoffset)
if (dictoffset == 0)
{
__Pyx_TypeName b_name = __Pyx_PyType_GetName(b);
PyErr_Format(PyExc_TypeError,
"extension type '%.200s' has no __dict__ slot, "
"but base type '" __Pyx_FMT_TYPENAME "' has: "
"either add 'cdef dict __dict__' to the extension type "
"or add '__slots__ = [...]' to the base type",
type_name, b_name);
__Pyx_DECREF_TypeName(b_name);
#if CYTHON_AVOID_BORROWED_REFS
Py_DECREF(b0);
#endif
return -1;
Py_ssize_t b_dictoffset = 0;
#if CYTHON_USE_TYPE_SLOTS
b = = b->tp_dictoffset;
#else
PyObject *py_b_dictoffset = PyObject_GetAttrString((PyObject*)b, "__dictoffset__");
if (!py_b_dictoffset) goto dictoffset_return;
b_dictoffset = PyLong_AsSsize_t(py_b_dictoffset);
Py_DECREF(py_b_dictoffset);
if (b_dictoffset == -1 && PyErr_Occurred()) goto dictoffset_return;
#endif
if (b_dictoffset) {
__Pyx_TypeName b_name = __Pyx_PyType_GetName(b);
PyErr_Format(PyExc_TypeError,
"extension type '%.200s' has no __dict__ slot, "
"but base type '" __Pyx_FMT_TYPENAME "' has: "
"either add 'cdef dict __dict__' to the extension type "
"or add '__slots__ = [...]' to the base type",
type_name, b_name);
__Pyx_DECREF_TypeName(b_name);
{
dictoffset_return:
#if CYTHON_AVOID_BORROWED_REFS
Py_DECREF(b0);
#endif
return -1;
}
}
}
#endif
#if CYTHON_AVOID_BORROWED_REFS
Py_DECREF(b0);
#endif
Expand Down

0 comments on commit 3b3de95

Please sign in to comment.