py_compat: Update for Python 3.13 and 3.14

This commit is contained in:
rdb 2025-08-28 11:33:12 +02:00
parent aa554a2130
commit 86ba156a7b
1 changed files with 71 additions and 0 deletions

View File

@ -287,6 +287,77 @@ INLINE bool PyLong_IsNonNegative(PyObject *value) {
# define PyLong_AsInt(x) (_PyLong_AsInt(x))
#endif
#if PY_VERSION_HEX < 0x030D00A1
ALWAYS_INLINE int
PyModule_Add(PyObject *mod, const char *name, PyObject *value) {
int res = PyModule_AddObjectRef(mod, name, value);
Py_XDECREF(value);
return res;
}
#endif
#if PY_VERSION_HEX < 0x030D00A1
INLINE int
PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result) {
#if PY_MAJOR_VERSION >= 3
PyObject *item = PyDict_GetItemWithError(mp, key);
#else
PyObject *item = _PyDict_GetItemWithError(mp, key);
#endif
if (item != nullptr) {
*result = Py_NewRef(item);
return 1;
}
*result = nullptr;
return PyErr_Occurred() ? -1 : 0;
}
INLINE int
PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result) {
PyObject *item = nullptr;
#if PY_MAJOR_VERSION >= 3
PyObject *key_obj = PyUnicode_FromString(key);
item = key_obj ? PyDict_GetItemWithError(mp, key_obj) : nullptr;
#else
PyObject *key_obj = PyString_FromString(key);
item = key_obj ? _PyDict_GetItemWithError(mp, key_obj) : nullptr;
#endif
Py_DECREF(key_obj);
if (item != nullptr) {
*result = Py_NewRef(item);
return 1;
}
*result = nullptr;
return PyErr_Occurred() ? -1 : 0;
}
#endif
#if PY_VERSION_HEX >= 0x03050200 && PY_VERSION_HEX < 0x030D00A1
# define PyThreadState_GetUnchecked() (_PyThreadState_UncheckedGet())
#endif
#if PY_VERSION_HEX < 0x030D00A2
# define PyList_Extend(list, iterable) (PyList_SetSlice((list), PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, (iterable)))
# define PyList_Clear(list) (PyList_SetSlice((list), 0, PY_SSIZE_T_MAX, nullptr))
#endif
#if PY_VERSION_HEX < 0x030D00A4
# define PyList_GetItemRef(op, index) (Py_XNewRef(PyList_GetItem((op), (index))))
#endif
#if PY_VERSION_HEX < 0x030D00B3
# define Py_BEGIN_CRITICAL_SECTION(op) {
# define Py_END_CRITICAL_SECTION() }
# define Py_BEGIN_CRITICAL_SECTION2(a, b) {
# define Py_END_CRITICAL_SECTION2() }
#endif
/* Python 3.14 */
#if PY_VERSION_HEX < 0x030E00A8
# define PyUnstable_Object_IsUniquelyReferenced(op) (Py_REFCNT((op)) == 1)
#endif
/* Other Python implementations */
// _PyErr_OCCURRED is an undocumented macro version of PyErr_Occurred.