diff options
author | Christian Tismer <tismer@stackless.com> | 2023-03-25 18:45:19 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-03-27 07:42:46 +0000 |
commit | 740132583c51d2b99ae555a9a69a845c258e08d1 (patch) | |
tree | 99866776cd450f5d711ac41f44990b2d108d9a85 /sources | |
parent | f2f00a848fc9dc55abc4edeb6349f70769a5a0b0 (diff) |
PyPySide: Fix a crash with PyPy 7.3.10 and 7.3.11, amended
Whether _functools or functools is retrieved, it is possible
to modify that at runtime. We therefore need to do much more
error checking.
Task-number: PYSIDE-2264
Task-number: PYSIDE-535
Change-Id: I4ad73b1729e68eccdc22df88a8332e36e052e57c
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 5529853210de3e51b2ad3c636ebee4a4d77aa54e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/shiboken6/libshiboken/pep384impl.cpp | 25 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/pep384impl.h | 2 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/sbkenum.cpp | 9 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/sbkfeature_base.cpp | 8 |
4 files changed, 29 insertions, 15 deletions
diff --git a/sources/shiboken6/libshiboken/pep384impl.cpp b/sources/shiboken6/libshiboken/pep384impl.cpp index b1a74778c..8d996a706 100644 --- a/sources/shiboken6/libshiboken/pep384impl.cpp +++ b/sources/shiboken6/libshiboken/pep384impl.cpp @@ -719,6 +719,31 @@ PepType_GetNameStr(PyTypeObject *type) return ret; } +// PYSIDE-2264: Find the _functools or functools module and retrieve the +// partial function. This can be tampered with, check carefully. +PyObject * +Pep_GetPartialFunction(void) +{ + static bool initialized = false; + static PyObject *result{}; + if (initialized) { + Py_INCREF(result); + return result; + } + auto *functools = PyImport_ImportModule("_functools"); + if (!functools) { + PyErr_Clear(); + functools = PyImport_ImportModule("functools"); + } + if (!functools) + Py_FatalError("functools cannot be found"); + result = PyObject_GetAttrString(functools, "partial"); + if (!result || !PyCallable_Check(result)) + Py_FatalError("partial not found or not a function"); + initialized = true; + return result; +} + /***************************************************************************** * * Newly introduced convenience functions diff --git a/sources/shiboken6/libshiboken/pep384impl.h b/sources/shiboken6/libshiboken/pep384impl.h index df41712b1..05c9afab3 100644 --- a/sources/shiboken6/libshiboken/pep384impl.h +++ b/sources/shiboken6/libshiboken/pep384impl.h @@ -149,6 +149,8 @@ LIBSHIBOKEN_API void PepType_PFTP_delete(PySideQFlagsType *flagsType); // functions used everywhere LIBSHIBOKEN_API const char *PepType_GetNameStr(PyTypeObject *type); +LIBSHIBOKEN_API PyObject *Pep_GetPartialFunction(void); + /***************************************************************************** * * RESOLVED: pydebug.h diff --git a/sources/shiboken6/libshiboken/sbkenum.cpp b/sources/shiboken6/libshiboken/sbkenum.cpp index fb70a1c8f..212e6fb73 100644 --- a/sources/shiboken6/libshiboken/sbkenum.cpp +++ b/sources/shiboken6/libshiboken/sbkenum.cpp @@ -1065,18 +1065,11 @@ static PyObject *create_missing_func(PyObject *klass) { // When creating the class, memorize it in the missing function by // a partial function argument. -#ifdef PYPY_VERSION - const char *functools_str = "functools"; -#else - const char *functools_str = "_functools"; -#endif static auto *const type = SbkType_FromSpec(&dummy_spec); static auto *const obType = reinterpret_cast<PyObject *>(type); static auto *const _missing = Shiboken::String::createStaticString("_missing_"); static auto *const func = PyObject_GetAttr(obType, _missing); - static auto *const functools = PyImport_ImportModule(functools_str); // builtin - static auto *const _partial = Shiboken::String::createStaticString("partial"); - static auto *const partial = PyObject_GetAttr(functools, _partial); + static auto *const partial = Pep_GetPartialFunction(); return PyObject_CallFunctionObjArgs(partial, func, klass, nullptr); } // diff --git a/sources/shiboken6/libshiboken/sbkfeature_base.cpp b/sources/shiboken6/libshiboken/sbkfeature_base.cpp index d732faa29..1242cbda4 100644 --- a/sources/shiboken6/libshiboken/sbkfeature_base.cpp +++ b/sources/shiboken6/libshiboken/sbkfeature_base.cpp @@ -178,13 +178,7 @@ void initEnumFlagsDict(PyTypeObject *type) static PyObject *replaceNoArgWithZero(PyObject *callable) { -#ifdef PYPY_VERSION - const char *functools_str = "functools"; -#else - const char *functools_str = "_functools"; -#endif - static auto *functools = PyImport_ImportModule(functools_str); // builtin - static auto *partial = PyObject_GetAttrString(functools, "partial"); + static auto *partial = Pep_GetPartialFunction(); static auto *zero = PyLong_FromLong(0); return PyObject_CallFunctionObjArgs(partial, callable, zero, nullptr); } |