From f47b34d2a13d02c67fd9e0e6cfaad2ddb0e3125f Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 30 Oct 2024 13:23:34 +0100 Subject: [PATCH] Fix remaining uses of ._PyType member in extension code This should be replaced with Dtool_GetPyTypeObject, hiding the implementation details a bit so that we can more easily change the underlying structure if we need later Also adds Py_NewRef to py_compat.h, backporting from master --- dtool/src/interrogatedb/py_compat.h | 22 ++++++++++++++++++++++ panda/src/express/datagram_ext.I | 4 ++-- panda/src/express/pointerToArray_ext.I | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/dtool/src/interrogatedb/py_compat.h b/dtool/src/interrogatedb/py_compat.h index e1ee9dd7bb..a1ec1958ab 100644 --- a/dtool/src/interrogatedb/py_compat.h +++ b/dtool/src/interrogatedb/py_compat.h @@ -247,6 +247,28 @@ INLINE PyObject *PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObje } #endif +/* Python 3.10 */ + +#if PY_VERSION_HEX < 0x030A0000 +INLINE int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value) { + int ret = PyModule_AddObject(module, name, value); + if (ret == 0) { + Py_INCREF(value); + } + return ret; +} + +ALWAYS_INLINE PyObject *Py_NewRef(PyObject *obj) { + Py_INCREF(obj); + return obj; +} + +ALWAYS_INLINE PyObject *Py_XNewRef(PyObject *obj) { + Py_XINCREF(obj); + return obj; +} +#endif + /* Python 3.12 */ #if PY_VERSION_HEX < 0x030C0000 diff --git a/panda/src/express/datagram_ext.I b/panda/src/express/datagram_ext.I index 9c997e64f6..77e2eb5827 100644 --- a/panda/src/express/datagram_ext.I +++ b/panda/src/express/datagram_ext.I @@ -52,10 +52,10 @@ __reduce__() const { } extern struct Dtool_PyTypedObject Dtool_Datagram; - Py_INCREF((PyObject *)&Dtool_Datagram._PyType); + PyObject *tp = (PyObject *)Dtool_GetPyTypeObject(&Dtool_Datagram); PyObject *result = PyTuple_New(2); - PyTuple_SET_ITEM(result, 0, (PyObject *)&Dtool_Datagram._PyType); + PyTuple_SET_ITEM(result, 0, Py_NewRef(tp)); PyTuple_SET_ITEM(result, 1, args); return result; } diff --git a/panda/src/express/pointerToArray_ext.I b/panda/src/express/pointerToArray_ext.I index 7ebb8afd4d..c0d2c66da2 100644 --- a/panda/src/express/pointerToArray_ext.I +++ b/panda/src/express/pointerToArray_ext.I @@ -100,7 +100,7 @@ __init__(PyObject *self, PyObject *source) { // Now construct the internal list by copying the elements one-at-a-time // from Python. - PyObject *dict = DtoolInstance_TYPE(self)->_PyType.tp_dict; + PyObject *dict = Dtool_GetPyTypeObject(DtoolInstance_TYPE(self))->tp_dict; PyObject *push_back = PyDict_GetItemString(dict, "push_back"); if (push_back == nullptr) { PyErr_BadArgument();