Skip to content

Commit 8b9c854

Browse files
committed
Work around a change in CPython 3.13a1 that prevents calling PyList_SET_ITEM() in a valid use case.
See python/cpython#106168
1 parent 0de8aaf commit 8b9c854

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎Cython/Utility/Optimize.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
3434
Py_ssize_t len = Py_SIZE(list);
3535
if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
3636
Py_INCREF(x);
37+
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
38+
// In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
39+
// However, extending the size *before* setting the value would not be correct,
40+
// so we cannot call PyList_SET_ITEM().
41+
L->ob_item[len] = x;
42+
#else
3743
PyList_SET_ITEM(list, len, x);
44+
#endif
3845
__Pyx_SET_SIZE(list, len + 1);
3946
return 0;
4047
}
@@ -52,7 +59,14 @@ static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) {
5259
Py_ssize_t len = Py_SIZE(list);
5360
if (likely(L->allocated > len)) {
5461
Py_INCREF(x);
62+
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
63+
// In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
64+
// However, extending the size *before* setting the value would not be correct,
65+
// so we cannot call PyList_SET_ITEM().
66+
L->ob_item[len] = x;
67+
#else
5568
PyList_SET_ITEM(list, len, x);
69+
#endif
5670
__Pyx_SET_SIZE(list, len + 1);
5771
return 0;
5872
}

0 commit comments

Comments
 (0)