diff options
author | Christian Tismer <tismer@stackless.com> | 2022-07-03 18:36:38 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-07-07 18:30:06 +0000 |
commit | c19f7dae41a9e27c24553eb870620662863c308a (patch) | |
tree | c0cbb8333c98e55af1000296d6286e36de541fa6 | |
parent | 5d053862fbfe41752fb61d9de03bbfaa2bca145b (diff) |
Shiboken: Rewrite the signature initialization
[ChangeLog][shiboken6] The initialization of the signature module
was moved into Shiboken and rearranged. This was necessary for
the new backward-compatible PyEnum module.
This change makes even sense if the PyEnum forgiveness should
not work in 3.11 because it is a real cleanup.
Change-Id: I5de54584154fb43648617adcac823f42049be57b
Task-number: PYSIDE-1735
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit dc058bddb1f1089c007721c9301e174f3f7a7887)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
11 files changed, 68 insertions, 104 deletions
diff --git a/sources/shiboken6/libshiboken/basewrapper.cpp b/sources/shiboken6/libshiboken/basewrapper.cpp index 74ed68faa..f049975b7 100644 --- a/sources/shiboken6/libshiboken/basewrapper.cpp +++ b/sources/shiboken6/libshiboken/basewrapper.cpp @@ -766,8 +766,7 @@ void initShibokenSupport(PyObject *module) // PYSIDE-1735: When the initialization was moved into Shiboken import, this // Py_INCREF became necessary. No idea why. Py_INCREF(module); - init_module_1(); - init_module_2(); + init_shibokensupport_module(); auto *type = SbkObject_TypeF(); if (InitSignatureStrings(type, SbkObject_SignatureStrings) < 0) diff --git a/sources/shiboken6/libshiboken/signature/signature.cpp b/sources/shiboken6/libshiboken/signature/signature.cpp index ab33becac..7b6eb8462 100644 --- a/sources/shiboken6/libshiboken/signature/signature.cpp +++ b/sources/shiboken6/libshiboken/signature/signature.cpp @@ -290,8 +290,6 @@ static PyObject *get_signature(PyObject * /* self */, PyObject *args) PyObject *ob; PyObject *modifier = nullptr; - init_module_1(); - if (!PyArg_ParseTuple(args, "O|O", &ob, &modifier)) return nullptr; if (Py_TYPE(ob) == PepFunction_TypePtr) @@ -353,7 +351,6 @@ PyMethodDef signature_methods[] = { static int PySide_BuildSignatureArgs(PyObject *obtype_mod, const char *signatures[]) { - init_module_1(); AutoDecRef type_key(GetTypeKey(obtype_mod)); /* * PYSIDE-996: Avoid string overflow in MSVC, which has a limit of @@ -381,7 +378,6 @@ PyObject *PySide_BuildSignatureProps(PyObject *type_key) * We simply pick up the arguments that we stored here and replace * them by the function result. */ - init_module_2(); if (type_key == nullptr) return nullptr; PyObject *numkey = PyDict_GetItem(pyside_globals->arg_dict, type_key); @@ -480,6 +476,7 @@ static int PySide_FinishSignatures(PyObject *module, const char *signatures[]) int InitSignatureStrings(PyTypeObject *type, const char *signatures[]) { + init_shibokensupport_module(); auto *ob_type = reinterpret_cast<PyObject *>(type); int ret = PySide_BuildSignatureArgs(ob_type, signatures); if (ret < 0) { @@ -599,13 +596,12 @@ static PyObject *adjustFuncName(const char *func_name) void SetError_Argument(PyObject *args, const char *func_name, PyObject *info) { + init_shibokensupport_module(); /* * This function replaces the type error construction with extra * overloads parameter in favor of using the signature module. * Error messages are rare, so we do it completely in Python. */ - init_module_1(); - init_module_2(); // PYSIDE-1305: Handle errors set by fillQtProperties. if (PyErr_Occurred()) { @@ -647,17 +643,19 @@ void SetError_Argument(PyObject *args, const char *func_name, PyObject *info) PyObject *Sbk_TypeGet___signature__(PyObject *ob, PyObject *modifier) { + init_shibokensupport_module(); return pyside_tp_get___signature__(ob, modifier); } PyObject *Sbk_TypeGet___doc__(PyObject *ob) { + init_shibokensupport_module(); return pyside_tp_get___doc__(ob); } PyObject *GetFeatureDict() { - init_module_1(); + init_shibokensupport_module(); return pyside_globals->feature_dict; } diff --git a/sources/shiboken6/libshiboken/signature/signature_extend.cpp b/sources/shiboken6/libshiboken/signature/signature_extend.cpp index d67f40ff9..db3cd8e78 100644 --- a/sources/shiboken6/libshiboken/signature/signature_extend.cpp +++ b/sources/shiboken6/libshiboken/signature/signature_extend.cpp @@ -94,20 +94,17 @@ static PyObject *_get_written_signature(signaturefunc sf, PyObject *ob, PyObject #ifdef PYPY_VERSION PyObject *pyside_bm_get___signature__(PyObject *func, PyObject *modifier) { - init_module_2(); return _get_written_signature(GetSignature_Method, func, modifier); } #endif PyObject *pyside_cf_get___signature__(PyObject *func, PyObject *modifier) { - init_module_2(); return _get_written_signature(GetSignature_Function, func, modifier); } PyObject *pyside_sm_get___signature__(PyObject *sm, PyObject *modifier) { - init_module_2(); AutoDecRef func(PyObject_GetAttr(sm, PyMagicName::func())); if (Py_TYPE(func) == PepFunction_TypePtr) return PyObject_GetAttr(func, PyMagicName::signature()); @@ -116,7 +113,6 @@ PyObject *pyside_sm_get___signature__(PyObject *sm, PyObject *modifier) PyObject *pyside_md_get___signature__(PyObject *ob_md, PyObject *modifier) { - init_module_2(); AutoDecRef func(name_key_to_func(ob_md)); if (func.object() == Py_None) return Py_None; @@ -127,13 +123,11 @@ PyObject *pyside_md_get___signature__(PyObject *ob_md, PyObject *modifier) PyObject *pyside_wd_get___signature__(PyObject *ob, PyObject *modifier) { - init_module_2(); return _get_written_signature(GetSignature_Wrapper, ob, modifier); } PyObject *pyside_tp_get___signature__(PyObject *obtype_mod, PyObject *modifier) { - init_module_2(); return _get_written_signature(GetSignature_TypeMod, obtype_mod, modifier); } @@ -163,8 +157,6 @@ static int handle_doc_in_progress = 0; static PyObject *handle_doc(PyObject *ob, PyObject *old_descr) { - init_module_1(); - init_module_2(); AutoDecRef ob_type_mod(GetClassOrModOf(ob)); const char *name; if (PyModule_Check(ob_type_mod.object())) diff --git a/sources/shiboken6/libshiboken/signature/signature_globals.cpp b/sources/shiboken6/libshiboken/signature/signature_globals.cpp index eb5e1c3bc..36ed7c689 100644 --- a/sources/shiboken6/libshiboken/signature/signature_globals.cpp +++ b/sources/shiboken6/libshiboken/signature/signature_globals.cpp @@ -64,26 +64,13 @@ static const unsigned char PySide_SignatureLoader[] = { #include "embed/signature_bootstrap_inc.h" }; -static PyObject *_init_pyside_extension(PyObject * /* self */, PyObject * /* args */) +static safe_globals_struc *init_phase_1() { - init_module_1(); - init_module_2(); - Py_RETURN_NONE; -} - -// This function will be inserted into __builtins__. -static PyMethodDef init_methods[] = { - {"_init_pyside_extension", (PyCFunction)_init_pyside_extension, METH_NOARGS}, - {nullptr, nullptr} -}; - -static safe_globals_struc *init_phase_1(PyMethodDef *init_meth) -{ - { + do { auto *p = reinterpret_cast<safe_globals_struc *> (malloc(sizeof(safe_globals_struc))); if (p == nullptr) - goto error; + break; /* * Initializing module signature_bootstrap. * Since we now have an embedding script, we can do this without any @@ -93,23 +80,21 @@ static safe_globals_struc *init_phase_1(PyMethodDef *init_meth) // We must work for multiple versions or we are cross-building for a different // Python version interpreter, so use source code. #else - AutoDecRef marshal_module(PyImport_Import(PyName::marshal())); - if (marshal_module.isNull()) - goto error; + AutoDecRef marshal_module(PyImport_Import(PyName::marshal())); // builtin AutoDecRef loads(PyObject_GetAttr(marshal_module, PyName::loads())); if (loads.isNull()) - goto error; + break; #endif char *bytes_cast = reinterpret_cast<char *>( const_cast<unsigned char *>(PySide_SignatureLoader)); AutoDecRef bytes(PyBytes_FromStringAndSize(bytes_cast, sizeof(PySide_SignatureLoader))); if (bytes.isNull()) - goto error; + break; #if defined(Py_LIMITED_API) || defined(SHIBOKEN_NO_EMBEDDING_PYC) PyObject *builtins = PyEval_GetBuiltins(); PyObject *compile = PyDict_GetItem(builtins, PyName::compile()); if (compile == nullptr) - goto error; + break; AutoDecRef code_obj(PyObject_CallFunction(compile, "Oss", bytes.object(), "signature_bootstrap.py", "exec")); #else @@ -117,69 +102,64 @@ static safe_globals_struc *init_phase_1(PyMethodDef *init_meth) loads, bytes.object(), nullptr)); #endif if (code_obj.isNull()) - goto error; + break; p->helper_module = PyImport_ExecCodeModule("signature_bootstrap", code_obj); if (p->helper_module == nullptr) - goto error; + break; // Initialize the module PyObject *mdict = PyModule_GetDict(p->helper_module); if (PyDict_SetItem(mdict, PyMagicName::builtins(), PyEval_GetBuiltins()) < 0) - goto error; - /* - * Unpack an embedded ZIP file with more signature modules. + break; + + /********************************************************************* + * + * Attention! + * ---------- + * + * We are unpacking an embedded ZIP file with more signature modules. * They will be loaded later with the zipimporter. - * Due to MSVC's limitation to 64k strings, we need to assemble pieces. + * The file `signature_bootstrap.py` does the unpacking and starts the + * loader. See `init_phase_2`. + * + * Due to MSVC's limitation to 64k strings, we needed to assemble pieces. */ auto **block_ptr = reinterpret_cast<const char **>(PySide_CompressedSignaturePackage); int npieces = 0; - PyObject *piece, *zipped_string_sequence = PyList_New(0); - if (zipped_string_sequence == nullptr) - return nullptr; + PyObject *piece{}; + AutoDecRef zipped_string_sequence(PyList_New(0)); for (; **block_ptr != 0; ++block_ptr) { npieces++; // we avoid the string/unicode dilemma by not using PyString_XXX: piece = Py_BuildValue("s", *block_ptr); if (piece == nullptr || PyList_Append(zipped_string_sequence, piece) < 0) - goto error; + break; } if (PyDict_SetItemString(mdict, "zipstring_sequence", zipped_string_sequence) < 0) - goto error; - Py_DECREF(zipped_string_sequence); + break; // build a dict for diverse mappings p->map_dict = PyDict_New(); - if (p->map_dict == nullptr) - goto error; // build a dict for the prepared arguments p->arg_dict = PyDict_New(); - if (p->arg_dict == nullptr - || PyObject_SetAttrString(p->helper_module, "pyside_arg_dict", p->arg_dict) < 0) - goto error; + if (PyObject_SetAttrString(p->helper_module, "pyside_arg_dict", p->arg_dict) < 0) + break; // build a dict for assigned signature values p->value_dict = PyDict_New(); - if (p->value_dict == nullptr) - goto error; // PYSIDE-1019: build a __feature__ dict p->feature_dict = PyDict_New(); - if (p->feature_dict == nullptr - || PyObject_SetAttrString(p->helper_module, "pyside_feature_dict", p->feature_dict) < 0) - goto error; + if (PyObject_SetAttrString(p->helper_module, "pyside_feature_dict", p->feature_dict) < 0) + break; // This function will be disabled until phase 2 is done. p->finish_import_func = nullptr; - // Initialize the explicit init function. - AutoDecRef init(PyCFunction_NewEx(init_meth, nullptr, nullptr)); - if (init.isNull() - || PyDict_SetItemString(PyEval_GetBuiltins(), init_meth->ml_name, init) != 0) - goto error; - return p; - } -error: + + } while (0); + PyErr_Print(); Py_FatalError("could not initialize part 1"); return nullptr; @@ -187,7 +167,7 @@ error: static int init_phase_2(safe_globals_struc *p, PyMethodDef *methods) { - { + do { PyMethodDef *ml; // The single function to be called, but maybe more to come. @@ -195,7 +175,7 @@ static int init_phase_2(safe_globals_struc *p, PyMethodDef *methods) PyObject *v = PyCFunction_NewEx(ml, nullptr, nullptr); if (v == nullptr || PyObject_SetAttrString(p->helper_module, ml->ml_name, v) != 0) - goto error; + break; Py_DECREF(v); } // The first entry is __feature_import__, add documentation. @@ -206,33 +186,46 @@ static int init_phase_2(safe_globals_struc *p, PyMethodDef *methods) PyObject *bootstrap_func = PyObject_GetAttrString(p->helper_module, "bootstrap"); if (bootstrap_func == nullptr) - goto error; - // The return value of the bootstrap function is the loader module. - PyObject *loader = PyObject_CallFunction(bootstrap_func, "()"); + break; + + /********************************************************************* + * + * Attention! + * ---------- + * + * This is the entry point where everything in folder + * `shibokensupport` becomes initialized. It starts with + * `signature_bootstrap.py` and continues from there to `loader.py`. + * + * The return value of the bootstrap function is the loader module. + */ + PyObject *loader = PyObject_CallFunctionObjArgs(bootstrap_func, nullptr); if (loader == nullptr) - goto error; + break; + // now the loader should be initialized p->pyside_type_init_func = PyObject_GetAttrString(loader, "pyside_type_init"); if (p->pyside_type_init_func == nullptr) - goto error; + break; p->create_signature_func = PyObject_GetAttrString(loader, "create_signature"); if (p->create_signature_func == nullptr) - goto error; + break; p->seterror_argument_func = PyObject_GetAttrString(loader, "seterror_argument"); if (p->seterror_argument_func == nullptr) - goto error; + break; p->make_helptext_func = PyObject_GetAttrString(loader, "make_helptext"); if (p->make_helptext_func == nullptr) - goto error; + break; p->finish_import_func = PyObject_GetAttrString(loader, "finish_import"); if (p->finish_import_func == nullptr) - goto error; + break; p->feature_import_func = PyObject_GetAttrString(loader, "feature_import"); if (p->feature_import_func == nullptr) - goto error; + break; return 0; - } -error: + + } while (0); + PyErr_Print(); Py_FatalError("could not initialize part 2"); return -1; @@ -271,12 +264,12 @@ static void handler(int sig) { safe_globals pyside_globals = nullptr; -void init_module_1(void) +void init_shibokensupport_module(void) { static int init_done = 0; if (!init_done) { - pyside_globals = init_phase_1(init_methods); + pyside_globals = init_phase_1(); if (pyside_globals != nullptr) init_done = 1; @@ -287,17 +280,6 @@ void init_module_1(void) signal(SIGSEGV, handler); // install our handler #endif // _WIN32 - } -} - -void init_module_2(void) -{ - static int init_done = 0; - - if (!init_done) { - // Phase 2 will call __init__.py which touches a signature, itself. - // Therefore we set init_done prior to init_phase_2(). - init_done = 1; init_phase_2(pyside_globals, signature_methods); // Enum must be initialized when signatures exist, not earlier. init_enum(); diff --git a/sources/shiboken6/libshiboken/signature_p.h b/sources/shiboken6/libshiboken/signature_p.h index 898ad9a3b..751f4f59d 100644 --- a/sources/shiboken6/libshiboken/signature_p.h +++ b/sources/shiboken6/libshiboken/signature_p.h @@ -65,8 +65,7 @@ typedef struct safe_globals_struc { extern safe_globals pyside_globals; extern PyMethodDef signature_methods[]; -void init_module_1(void); -void init_module_2(void); +void init_shibokensupport_module(void); // signature.cpp diff --git a/sources/shiboken6/tests/minimalbinding/brace_pattern_test.py b/sources/shiboken6/tests/minimalbinding/brace_pattern_test.py index e036eafb9..bcc5bf47d 100644 --- a/sources/shiboken6/tests/minimalbinding/brace_pattern_test.py +++ b/sources/shiboken6/tests/minimalbinding/brace_pattern_test.py @@ -48,7 +48,6 @@ from shiboken_paths import init_paths init_paths() from shiboken6 import Shiboken -_init_pyside_extension() # trigger bootstrap from shibokensupport.signature.lib.tool import build_brace_pattern diff --git a/sources/shiboken6/tests/otherbinding/signature_test.py b/sources/shiboken6/tests/otherbinding/signature_test.py index 81fc8cdc0..313d5f369 100644 --- a/sources/shiboken6/tests/otherbinding/signature_test.py +++ b/sources/shiboken6/tests/otherbinding/signature_test.py @@ -44,7 +44,6 @@ from other import OtherObjectType from shiboken_test_helper import objectFullname from shiboken6 import Shiboken -_init_pyside_extension() # trigger bootstrap from shibokensupport.signature import get_signature diff --git a/sources/shiboken6/tests/samplebinding/enumfromremovednamespace_test.py b/sources/shiboken6/tests/samplebinding/enumfromremovednamespace_test.py index 17fd4a84b..bb626ff84 100644 --- a/sources/shiboken6/tests/samplebinding/enumfromremovednamespace_test.py +++ b/sources/shiboken6/tests/samplebinding/enumfromremovednamespace_test.py @@ -42,7 +42,6 @@ import sample from shiboken_test_helper import objectFullname from shiboken6 import Shiboken -_init_pyside_extension() # trigger bootstrap from shibokensupport.signature import get_signature diff --git a/sources/shiboken6/tests/samplebinding/namespace_test.py b/sources/shiboken6/tests/samplebinding/namespace_test.py index 2bd5f8f4e..19e291c4c 100644 --- a/sources/shiboken6/tests/samplebinding/namespace_test.py +++ b/sources/shiboken6/tests/samplebinding/namespace_test.py @@ -44,7 +44,6 @@ from sample import * from shiboken_test_helper import objectFullname from shiboken6 import Shiboken -_init_pyside_extension() # trigger bootstrap from shibokensupport.signature import get_signature diff --git a/sources/shiboken6/tests/samplebinding/pointerprimitivetype_test.py b/sources/shiboken6/tests/samplebinding/pointerprimitivetype_test.py index a1ee230ce..99bffad97 100644 --- a/sources/shiboken6/tests/samplebinding/pointerprimitivetype_test.py +++ b/sources/shiboken6/tests/samplebinding/pointerprimitivetype_test.py @@ -55,7 +55,6 @@ init_paths() from sample import IntArray2, VirtualMethods import shiboken6 -_init_pyside_extension() # trigger init, which does not happen in tests from shibokensupport.signature import get_signature import typing diff --git a/sources/shiboken6/tests/samplebinding/renaming_test.py b/sources/shiboken6/tests/samplebinding/renaming_test.py index 32467c025..4b9e01f04 100644 --- a/sources/shiboken6/tests/samplebinding/renaming_test.py +++ b/sources/shiboken6/tests/samplebinding/renaming_test.py @@ -44,7 +44,6 @@ init_paths() from sample import RenamedValue, RenamedUser from shiboken6 import Shiboken -_init_pyside_extension() # trigger bootstrap from shibokensupport.signature import get_signature |