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:
Campbell Barton
2025-07-04 02:08:57 +00:00
parent dad8bddd78
commit 2a414eea0c
2 changed files with 102 additions and 63 deletions

View File

@@ -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