From a4c432bc007d515b843b30765dfa0b82851255b9 Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Wed, 22 Sep 2021 14:50:50 +0200 Subject: shiboken6: Fix an impossible reference to type_new [ChangeLog][shiboken6] It is now possible to derive classes from enum types like `QtCore.Key` in earlier Python versions. In SbkEnumTypeTpNew the reference to `PyType_Type.tp_new` was made through `PyType_GetSlot` in a Limited API compatible way. Unfortunately, `PyType_GetSlot` works for non-heaptypes in Python 3.10, only, and `PyType_Type is not a heaptype. This was found while creating a minimum example to prove that the PyPy implementation of type_new is not correct. Task-number: PYSIDE-535 Change-Id: Ibe300f4670d9db6d29fbdeb1d147e1a074ec23c6 Reviewed-by: Friedemann Kleint --- sources/pyside6/tests/QtCore/qenum_test.py | 6 ++++++ sources/shiboken6/libshiboken/sbkenum.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sources/pyside6/tests/QtCore/qenum_test.py b/sources/pyside6/tests/QtCore/qenum_test.py index 0d524505a..76caa9a37 100644 --- a/sources/pyside6/tests/QtCore/qenum_test.py +++ b/sources/pyside6/tests/QtCore/qenum_test.py @@ -77,6 +77,12 @@ class TestEnum(unittest.TestCase): with self.assertRaises(TypeError): a = k * 2.0 + def testInherit(self): + class A(Qt.Key): + pass + + self.assertEqual(A.Key_1, Qt.Key.Key_1) + @unittest.skipUnless(getattr(sys, "getobjects", None), "requires --with-trace-refs") @unittest.skipUnless(getattr(sys, "gettotalrefcount", None), "requires --with-pydebug") def testEnumNew_NoLeak(self): diff --git a/sources/shiboken6/libshiboken/sbkenum.cpp b/sources/shiboken6/libshiboken/sbkenum.cpp index a6f65c7ab..b97cff209 100644 --- a/sources/shiboken6/libshiboken/sbkenum.cpp +++ b/sources/shiboken6/libshiboken/sbkenum.cpp @@ -310,7 +310,7 @@ void SbkEnumTypeDealloc(PyObject *pyObj) PyObject *SbkEnumTypeTpNew(PyTypeObject *metatype, PyObject *args, PyObject *kwds) { - auto type_new = reinterpret_cast(PyType_GetSlot(&PyType_Type, Py_tp_new)); + auto type_new = reinterpret_cast(PyType_Type.tp_new); auto newType = reinterpret_cast(type_new(metatype, args, kwds)); if (!newType) return nullptr; -- cgit v1.2.3