diff --git a/include/pyshim.hh b/include/pyshim.hh index bc47ce52..48151f23 100644 --- a/include/pyshim.hh +++ b/include/pyshim.hh @@ -93,4 +93,18 @@ inline PyObject *PyDictViewObject_new(PyObject *dict, PyTypeObject *type) { #endif } +/** + * @brief Shim for `_PyErr_SetKeyError`. + * Since Python 3.13, `_PyErr_SetKeyError` function became an internal API. + */ +inline void PyErr_SetKeyError(PyObject *key) { + // Use the provided API when possible, as `PyErr_SetObject`'s behaviour is more complex than originally thought + // see also: https://github.com/python/cpython/issues/101578 +#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13 + return _PyErr_SetKeyError(key); +#else + return PyErr_SetObject(PyExc_KeyError, key); +#endif +} + #endif // #ifndef PythonMonkey_py_version_shim_ diff --git a/src/JSObjectProxy.cc b/src/JSObjectProxy.cc index b6b6ee7d..4ae14af0 100644 --- a/src/JSObjectProxy.cc +++ b/src/JSObjectProxy.cc @@ -702,11 +702,7 @@ PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method(JSObjectProxy Py_INCREF(default_value); return default_value; } - #if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13 - PyErr_SetObject(PyExc_KeyError, key); - #else - _PyErr_SetKeyError(key); - #endif + PyErr_SetKeyError(key); return NULL; } else {