PyAPI: Refactor attribute access to avoid creating & clearing exceptions
- Add PyObject_GetOptionalAttrString (only available in Python 3.13). - When registering RNA classes use optional attribute access to avoid raising and clearing exceptions. - Refactor macro that handles substituting Python's built-ins with a `for` loop for better readability.
This commit is contained in:
@@ -28,6 +28,19 @@
|
||||
#if PY_VERSION_HEX < 0x030d0000
|
||||
# define PyObject_GetOptionalAttr _PyObject_LookupAttr
|
||||
|
||||
[[nodiscard]] Py_LOCAL_INLINE(int)
|
||||
PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result)
|
||||
{
|
||||
PyObject *oname = PyUnicode_FromString(name);
|
||||
if (oname == nullptr) {
|
||||
*result = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
const int status = PyObject_GetOptionalAttr(obj, oname, result);
|
||||
Py_DECREF(oname);
|
||||
return status;
|
||||
}
|
||||
|
||||
# define Py_IsFinalizing _Py_IsFinalizing
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user