aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-11 16:24:39 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-11 17:22:54 +0200
commit407cc58614a822c46b4ac5c18d15a28cd17b633f (patch)
treefa2fdff15235ae16e1239abc4a38190806a5ea2a
parent8c4a749c4b1802160fa27033f8f7333fc05db932 (diff)
Work around deprecation of PyWeakref_GetObject() in Python 3.13
Add a helper function checking on a weak reference. Task-number: PYSIDE-2751 Change-Id: I4f2d505636a24df083b0d2f4d3d312fcc44d125e Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/pyside6/libpyside/dynamicslot.cpp2
-rw-r--r--sources/shiboken6/libshiboken/pep384ext.h2
-rw-r--r--sources/shiboken6/libshiboken/pep384impl.cpp14
-rw-r--r--sources/shiboken6/libshiboken/pep384impl.h2
4 files changed, 19 insertions, 1 deletions
diff --git a/sources/pyside6/libpyside/dynamicslot.cpp b/sources/pyside6/libpyside/dynamicslot.cpp
index b25928ea9..b0ae59470 100644
--- a/sources/pyside6/libpyside/dynamicslot.cpp
+++ b/sources/pyside6/libpyside/dynamicslot.cpp
@@ -150,7 +150,7 @@ TrackingMethodDynamicSlot::~TrackingMethodDynamicSlot()
Shiboken::GilState gil;
// weakrefs must not be de-refed after the object has been deleted,
// else they get negative refcounts.
- if (PyWeakref_GetObject(m_weakRef) != Py_None)
+ if (PepExt_Weakref_IsAlive(m_weakRef))
Py_DECREF(m_weakRef);
}
}
diff --git a/sources/shiboken6/libshiboken/pep384ext.h b/sources/shiboken6/libshiboken/pep384ext.h
index 021c53d16..0ce53d3a7 100644
--- a/sources/shiboken6/libshiboken/pep384ext.h
+++ b/sources/shiboken6/libshiboken/pep384ext.h
@@ -86,4 +86,6 @@ inline void PepExt_TypeCallFree(PyObject *object)
PepExt_Type_GetFreeSlot(Py_TYPE(object))(object);
}
+LIBSHIBOKEN_API bool PepExt_Weakref_IsAlive(PyObject *weakRef);
+
#endif // PEP384EXT_H
diff --git a/sources/shiboken6/libshiboken/pep384impl.cpp b/sources/shiboken6/libshiboken/pep384impl.cpp
index eae968c20..283b2f3be 100644
--- a/sources/shiboken6/libshiboken/pep384impl.cpp
+++ b/sources/shiboken6/libshiboken/pep384impl.cpp
@@ -1299,3 +1299,17 @@ Pep384_Init()
}
} // extern "C"
+
+LIBSHIBOKEN_API bool PepExt_Weakref_IsAlive(PyObject *weakRef)
+{
+#if defined(Py_LIMITED_API) || PY_VERSION_HEX < 0x030D0000
+ return PyWeakref_GetObject(weakRef) != Py_None;
+#else
+ // FIXME: Make this the default code path once Limited API has been raised to 3.13
+ // Note: PyWeakref_GetObject() will be removed in 3.15.
+ PyObject *pobj = nullptr;
+ const bool result = PyWeakref_GetRef(weakRef, &pobj) == 1;
+ Py_XDECREF(pobj);
+ return result;
+#endif
+}
diff --git a/sources/shiboken6/libshiboken/pep384impl.h b/sources/shiboken6/libshiboken/pep384impl.h
index 06b24eaf7..0612d05cd 100644
--- a/sources/shiboken6/libshiboken/pep384impl.h
+++ b/sources/shiboken6/libshiboken/pep384impl.h
@@ -4,6 +4,8 @@
#ifndef PEP384IMPL_H
#define PEP384IMPL_H
+#include "shibokenmacros.h"
+
extern "C"
{