Skip to content

Commit

Permalink
Work around a change in CPython 3.13a1 that prevents calling PyList_S…
Browse files Browse the repository at this point in the history
…ET_ITEM() in a valid use case.

See python/cpython#106168
  • Loading branch information
scoder committed Oct 29, 2023
1 parent 0de8aaf commit 8b9c854
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Cython/Utility/Optimize.c
Expand Up @@ -34,7 +34,14 @@ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
Py_ssize_t len = Py_SIZE(list);
if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
Py_INCREF(x);
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
// In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
// However, extending the size *before* setting the value would not be correct,
// so we cannot call PyList_SET_ITEM().
L->ob_item[len] = x;
#else
PyList_SET_ITEM(list, len, x);
#endif
__Pyx_SET_SIZE(list, len + 1);
return 0;
}
Expand All @@ -52,7 +59,14 @@ static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) {
Py_ssize_t len = Py_SIZE(list);
if (likely(L->allocated > len)) {
Py_INCREF(x);
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
// In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
// However, extending the size *before* setting the value would not be correct,
// so we cannot call PyList_SET_ITEM().
L->ob_item[len] = x;
#else
PyList_SET_ITEM(list, len, x);
#endif
__Pyx_SET_SIZE(list, len + 1);
return 0;
}
Expand Down

0 comments on commit 8b9c854

Please sign in to comment.