Skip to content

Commit

Permalink
refactor: move _PyErr_SetKeyError shim to pyshim.hh
Browse files Browse the repository at this point in the history
`_PyErr_SetKeyError` is more complex than originally thought, use the provided API when possible

See also python/cpython#101578
  • Loading branch information
Xmader committed Sep 23, 2024
1 parent 623d83e commit a1cdfb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 14 additions & 0 deletions include/pyshim.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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_
6 changes: 1 addition & 5 deletions src/JSObjectProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a1cdfb3

Please sign in to comment.