Builtin types no longer have tp_dict set. We need to
use PyType_GetDict, instead. This works without Limited API
at the moment.
With some great cheating, this works with Limited API, too.
We emulate PyType_GetDict by tp_dict if that is not 0.
Otherwise we create an empty dict.
Some small changes to Exception handling and longer
warm-up in leaking tests were found, too.
Pick-to: 6.6 6.5 6.2
Task-number: PYSIDE-2230
Change-Id: I8a56de6208ec00979255b39b5784dfc9b4b92def
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit
441ffbd4fc622e67acd81e9c1c6d3a0b0fbcacf0)
Gbp-Pq: Name Support-running-PySide-on-Python-3.12.patch
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
- 'Programming Language :: Python :: 3.11'
+ 'Programming Language :: Python :: 3.11',
+ 'Programming Language :: Python :: 3.12',
]
self.setup_script_dir = None
"""
def _typevar__repr__(self):
return "typing." + self.__name__
- typing.TypeVar.__repr__ = _typevar__repr__
-
+ # This is no longer necessary for modern typing versions.
+ # Ignore therefore if the repr is read-only and cannot be changed.
+ try:
+ typing.TypeVar.__repr__ = _typevar__repr__
+ except TypeError:
+ pass
# Adding a pattern to substitute "Union[T, NoneType]" by "Optional[T]"
# I tried hard to replace typing.Optional by a simple override, but
# this became _way_ too much.
* This is the selector for one sublass. We need to call this for
* every subclass until no more subclasses or reaching the wanted id.
*/
- if (Py_TYPE(type->tp_dict) == Py_TYPE(PyType_Type.tp_dict)) {
+ static const auto *pyTypeType_tp_dict = PepType_GetDict(&PyType_Type);
+ if (Py_TYPE(type->tp_dict) == Py_TYPE(pyTypeType_tp_dict)) {
// On first touch, we initialize the dynamic naming.
// The dict type will be replaced after the first call.
if (!replaceClassDict(type)) {
* Generated functions call this directly.
* Shiboken will assign it via a public hook of `basewrapper.cpp`.
*/
- if (Py_TYPE(type->tp_dict) == Py_TYPE(PyType_Type.tp_dict)) {
+ static const auto *pyTypeType_tp_dict = PepType_GetDict(&PyType_Type);
+ if (Py_TYPE(type->tp_dict) == Py_TYPE(pyTypeType_tp_dict)) {
// We initialize the dynamic features by using our own dict type.
if (!replaceClassDict(type))
return nullptr;
// Turn `__doc__` into a computed attribute without changing writability.
auto gsp = property_getset;
auto type = &PyProperty_Type;
- auto dict = type->tp_dict;
+ AutoDecRef dict(PepType_GetDict(type));
AutoDecRef descr(PyDescr_NewGetSet(type, gsp));
if (descr.isNull())
return false;
- if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
+ if (PyDict_SetItemString(dict.object(), gsp->name, descr) < 0)
return false;
// Replace property_descr_get/set by slightly changed versions
return true;
static PyObject *getFromType(PyTypeObject *type, PyObject *name)
{
- PyObject *attr = nullptr;
- attr = PyDict_GetItem(type->tp_dict, name);
+ AutoDecRef tpDict(PepType_GetDict(type));
+ auto *attr = PyDict_GetItem(tpDict.object(), name);
if (!attr) {
PyObject *bases = type->tp_bases;
int size = PyTuple_GET_SIZE(bases);
Py_ssize_t pos = 0;
PyObject *value;
PyObject *key;
-
- while (PyDict_Next(objType->tp_dict, &pos, &key, &value)) {
+ Shiboken::AutoDecRef tpDict(PepType_GetDict(objType));
+ while (PyDict_Next(tpDict, &pos, &key, &value)) {
if (PyObject_TypeCheck(value, PySideSignalTypeF())) {
Shiboken::AutoDecRef signalInstance(reinterpret_cast<PyObject *>(PyObject_New(PySideSignalInstance, PySideSignalInstanceTypeF())));
instanceInitialize(signalInstance.cast<PySideSignalInstance *>(), key, reinterpret_cast<PySideSignal *>(value), source, 0);
import sys
class testQTextBlock(unittest.TestCase):
- def tesIterator(self):
+
+ def testIterator(self):
edit = QTextEdit()
cursor = edit.textCursor()
fmt = QTextCharFormat()
gc.collect()
# if this is no debug build, then we check at least that
# we do not crash any longer.
+ for idx in range(200):
+ # PYSIDE-2230: Warm-up is necessary before measuring, because
+ # the code changes the constant parts after some time.
+ o.selectionModel().destroyed.connect(self.callback)
+ o.selectionModel().destroyed.disconnect(self.callback)
if not skiptest:
total = gettotalrefcount()
for idx in range(1000):
PepRuntime_38_flag = 1;
}
+#ifdef Py_LIMITED_API
+static PyObject *emulatePyType_GetDict(PyTypeObject *type)
+{
+ if (_PepRuntimeVersion() < 0x030C00 || type->tp_dict) {
+ auto *res = type->tp_dict;
+ Py_XINCREF(res);
+ return res;
+ }
+ // PYSIDE-2230: Here we are really cheating. We don't know how to
+ // access an internal dict, and so we simply pretend
+ // it were an empty dict. This works great for our types.
+ // This was an unexpectedly simple solution :D
+ return PyDict_New();
+}
+#endif
+
+// PyType_GetDict: replacement for <static type>.tp_dict, which is
+// zero for builtin types since 3.12.
+PyObject *PepType_GetDict(PyTypeObject *type)
+{
+#if !defined(Py_LIMITED_API)
+# if PY_VERSION_HEX >= 0x030C0000
+ return PyType_GetDict(type);
+# else
+ // pre 3.12 fallback code, mimicking the addref-behavior.
+ Py_XINCREF(type->tp_dict);
+ return type->tp_dict;
+# endif
+#else
+ return emulatePyType_GetDict(type);
+#endif // Py_LIMITED_API
+}
+
/*****************************************************************************
*
* Module Initialization
extern LIBSHIBOKEN_API int PepRuntime_38_flag;
+/*****************************************************************************
+ *
+ * Runtime support for Python 3.12 incompatibility
+ *
+ */
+
+LIBSHIBOKEN_API PyObject *PepType_GetDict(PyTypeObject *type);
+
/*****************************************************************************
*
* Module Initialization
// Find the feature flags
auto type = reinterpret_cast<PyTypeObject *>(obtype.object());
- auto dict = type->tp_dict;
+ AutoDecRef dict(PepType_GetDict(type));
int id = SbkObjectType_GetReserved(type);
id = id < 0 ? 0 : id; // if undefined, set to zero
auto lower = id & 0x01;
*/
assert(PyType_Check(type));
PyType_Ready(type);
- PyObject *dict = type->tp_dict;
+ AutoDecRef tpDict(PepType_GetDict(type));
+ auto *dict = tpDict.object();
for (; gsp->name != nullptr; gsp++) {
PyObject *have_descr = PyDict_GetItemString(dict, gsp->name);
if (have_descr != nullptr) {
* We also check for hidden methods, see below.
*/
auto *type = reinterpret_cast<PyTypeObject *>(obtype);
- PyObject *dict = type->tp_dict;
+ AutoDecRef tpDict(PepType_GetDict(type));
+ auto *dict = tpDict.object();
PyMethodDef *meth = type->tp_methods;
if (meth == nullptr)
msg = "{func_name}(): {info}".format(**locals())
err = AttributeError
return err, msg
+ if isinstance(info, Exception):
+ # PYSIDE-2230: Python 3.12 seems to always do normalization.
+ err = type(info)
+ info = info.args[0]
+ msg = f"{func_name}(): {info}"
+ return err, msg
if info and type(info) is dict:
keyword = tuple(info)[0]
msg = "{func_name}(): unsupported keyword '{keyword}'".format(**locals())
def testEnumConstructorWithTooManyParameters(self):
'''Calling the constructor of non-extensible enum with the wrong number of parameters.'''
- self.assertRaises(TypeError, SampleNamespace.InValue, 13, 14)
+ self.assertRaises((TypeError, ValueError), SampleNamespace.InValue, 13, 14)
def testEnumConstructorWithNonNumberParameter(self):
'''Calling the constructor of non-extensible enum with a string.'''