Browse Source

Introduce Py_IS_TYPE, use for Py_TYPE comparisons

rdb 1 year ago
parent
commit
0ba2aadcd6

+ 1 - 1
dtool/src/dtoolutil/filename_ext.cxx

@@ -56,7 +56,7 @@ __init__(PyObject *path) {
     return;
   }
 
-  if (Py_TYPE(path) == &Dtool_Filename._PyType) {
+  if (Py_IS_TYPE(path, Dtool_GetPyTypeObject(&Dtool_Filename))) {
     // Copy constructor.
     *_this = *(Filename *)DtoolInstance_VOID_PTR(path);
     return;

+ 5 - 1
dtool/src/interrogatedb/py_compat.h

@@ -217,8 +217,12 @@ INLINE PyObject *_PyLong_Lshift(PyObject *a, size_t shiftby) {
 
 /* Python 3.9 */
 
+#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
+#  define Py_IS_TYPE(ob, type) (Py_TYPE((PyObject *)ob) == type)
+#endif
+
 #ifndef PyCFunction_CheckExact
-#  define PyCFunction_CheckExact(op) (Py_TYPE(op) == &PyCFunction_Type)
+#  define PyCFunction_CheckExact(op) (Py_IS_TYPE(op, &PyCFunction_Type))
 #endif
 
 #if PY_VERSION_HEX < 0x03090000

+ 1 - 1
dtool/src/interrogatedb/py_panda.cxx

@@ -300,7 +300,7 @@ static PyObject *Dtool_EnumType_New(PyTypeObject *subtype, PyObject *args, PyObj
                         subtype->tp_name);
   }
 
-  if (Py_TYPE(arg) == subtype) {
+  if (Py_IS_TYPE(arg, subtype)) {
     Py_INCREF(arg);
     return arg;
   }

+ 2 - 2
panda/src/event/pythonTask.cxx

@@ -590,7 +590,7 @@ do_python_task() {
         _retrieved_exception = false;
 
         if (task_cat.is_debug()) {
-          if (_exception != nullptr && Py_TYPE(_exception) == &PyType_Type) {
+          if (_exception != nullptr && Py_IS_TYPE(_exception, &PyType_Type)) {
             task_cat.debug()
               << *this << " received " << ((PyTypeObject *)_exception)->tp_name << " from coroutine.\n";
           } else {
@@ -741,7 +741,7 @@ do_python_task() {
   if (PyCFunction_Check(result)) {
     meth = ((PyCFunctionObject *)result)->m_ml;
 #if PY_MAJOR_VERSION >= 3
-  } else if (Py_TYPE(result) == &PyMethodDescr_Type) {
+  } else if (Py_IS_TYPE(result, &PyMethodDescr_Type)) {
 #else
   } else if (strcmp(Py_TYPE(result)->tp_name, "method_descriptor") == 0) {
 #endif