Skip to content

Commit a1cdfb3

Browse files
committed
refactor: move _PyErr_SetKeyError shim to pyshim.hh
`_PyErr_SetKeyError` is more complex than originally thought, use the provided API when possible See also python/cpython#101578
1 parent 623d83e commit a1cdfb3

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

include/pyshim.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,18 @@ inline PyObject *PyDictViewObject_new(PyObject *dict, PyTypeObject *type) {
9393
#endif
9494
}
9595

96+
/**
97+
* @brief Shim for `_PyErr_SetKeyError`.
98+
* Since Python 3.13, `_PyErr_SetKeyError` function became an internal API.
99+
*/
100+
inline void PyErr_SetKeyError(PyObject *key) {
101+
// Use the provided API when possible, as `PyErr_SetObject`'s behaviour is more complex than originally thought
102+
// see also: https://github.com/python/cpython/issues/101578
103+
#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13
104+
return _PyErr_SetKeyError(key);
105+
#else
106+
return PyErr_SetObject(PyExc_KeyError, key);
107+
#endif
108+
}
109+
96110
#endif // #ifndef PythonMonkey_py_version_shim_

src/JSObjectProxy.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,7 @@ PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method(JSObjectProxy
702702
Py_INCREF(default_value);
703703
return default_value;
704704
}
705-
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
706-
PyErr_SetObject(PyExc_KeyError, key);
707-
#else
708-
_PyErr_SetKeyError(key);
709-
#endif
705+
PyErr_SetKeyError(key);
710706
return NULL;
711707
}
712708
else {

0 commit comments

Comments
 (0)