diff options
author | Christian Tismer <tismer@stackless.com> | 2022-07-15 14:54:13 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-07-19 12:28:31 +0000 |
commit | 66cc5f156554d7ee9525ab9aba38d372b8fcc928 (patch) | |
tree | 7b34cdd643a79f730965f4d7d314b49e96823068 | |
parent | a2f00bfcd00c467e53bc2ad6a14488b9e789208a (diff) |
PyEnum: Increase compatibility by allowing defaults etc., refinement
The change has been tested with Python 3.6 to 3.11.0b4 .
The enum_310.py warning triggers now only when on 3.11.0 final.
The enumFlagInfo empty check was simplified by removing empty
structures.
The code introspection was optimized version-specific for
Python 3.9 and Python 3.11 where optimizations were introduced.
Task-number: PYSIDE-1735
Change-Id: Ic5e25fd4edae0ab92aad291a8067c9dcccd5acb8
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 615d6a820137b31f03295e884f58cd46aadf5032)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
3 files changed, 38 insertions, 20 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 6d88957f5..25d16615a 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -696,11 +696,13 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon for (const auto &entry : qAsConst(classEnums)) sorter.append(BuildEnumFlagInfo(entry.typeEntry())); sorter.sort(); - s << "static const char *" << className << "_EnumFlagInfo[] = {\n" << indent; - for (const auto &entry : qAsConst(sorter)) - s << entry << ",\n"; - s << NULL_PTR << " // Sentinel\n" - << outdent << "};\n\n"; + if (!sorter.empty()) { + s << "static const char *" << className << "_EnumFlagInfo[] = {\n" << indent; + for (const auto &entry : qAsConst(sorter)) + s << entry << ",\n"; + s << NULL_PTR << " // Sentinel\n" + << outdent << "};\n\n"; + } // Write methods definition writePyMethodDefs(s, className, methodsDefinitions, typeEntry->isValue()); @@ -5964,7 +5966,7 @@ void CppGenerator::writeClassRegister(TextStream &s, metaClass->getEnumsFromInvisibleNamespacesToBeGenerated(&classEnums); writeEnumsInitialization(s, classEnums, ErrorReturn::Void); - if (!classContext.forSmartPointer()) + if (!classContext.forSmartPointer() && !classEnums.isEmpty()) s << "SbkObjectType_SetEnumFlagInfo(pyType, " << chopType(pyTypeName) << "_EnumFlagInfo);\n"; diff --git a/sources/shiboken6/libshiboken/sbkfeature_base.cpp b/sources/shiboken6/libshiboken/sbkfeature_base.cpp index e8e19ac72..770735e5c 100644 --- a/sources/shiboken6/libshiboken/sbkfeature_base.cpp +++ b/sources/shiboken6/libshiboken/sbkfeature_base.cpp @@ -137,7 +137,7 @@ SelectableFeatureHook initSelectableFeature(SelectableFeatureHook func) PyErr_Restore(error_type, error_value, error_traceback); } -// PYTHON 3.11 +// Python 3.11 static int const PRECALL = 166; // we have "big instructins" with gaps after them static int const LOAD_ATTR_GAP = 4 * 2; @@ -149,30 +149,45 @@ static int const CALL_METHOD = 161; static int const CALL_FUNCTION = 131; static int const LOAD_ATTR = 106; +static int _getVersion() +{ + static PyObject *const sysmodule = PyImport_AddModule("sys"); + static PyObject *const version = PyObject_GetAttrString(sysmodule, "version_info"); + static PyObject *const major = PyTuple_GetItem(version, 0); + static PyObject *const minor = PyTuple_GetItem(version, 1); + static auto number = PyLong_AsLong(major) * 1000 + PyLong_AsLong(minor); + return number; +} + static bool currentOpcode_Is_CallMethNoArgs() { // We look into the currently active operation if we are going to call // a method with zero arguments. + auto *frame = PyEval_GetFrame(); +#if PY_VERSION_HEX >= 0x03090000 && !Py_LIMITED_API + auto *f_code = PyFrame_GetCode(frame); +#else static PyObject *const _f_code = Shiboken::String::createStaticString("f_code"); + AutoDecRef dec_f_code(PyObject_GetAttr(reinterpret_cast<PyObject *>(frame), _f_code)); + auto *f_code = dec_f_code.object(); +#endif +#if PY_VERSION_HEX >= 0x030B0000 && !Py_LIMITED_API + AutoDecRef dec_co_code(PyCode_GetCode(f_code)); + Py_ssize_t f_lasti = PyFrame_GetLasti(frame); +#else static PyObject *const _f_lasti = Shiboken::String::createStaticString("f_lasti"); static PyObject *const _co_code = Shiboken::String::createStaticString("co_code"); - auto *frame = reinterpret_cast<PyObject *>(PyEval_GetFrame()); - // We use the limited API for frame and code objects. - AutoDecRef f_code(PyObject_GetAttr(frame, _f_code)); - AutoDecRef dec_f_lasti(PyObject_GetAttr(frame, _f_lasti)); + AutoDecRef dec_co_code(PyObject_GetAttr(reinterpret_cast<PyObject *>(f_code), _co_code)); + AutoDecRef dec_f_lasti(PyObject_GetAttr(reinterpret_cast<PyObject *>(frame), _f_lasti)); Py_ssize_t f_lasti = PyLong_AsSsize_t(dec_f_lasti); - AutoDecRef dec_co_code(PyObject_GetAttr(f_code, _co_code)); +#endif Py_ssize_t code_len; char *co_code{}; PyBytes_AsStringAndSize(dec_co_code, &co_code, &code_len); uint8_t opcode1 = co_code[f_lasti]; uint8_t opcode2 = co_code[f_lasti + 2]; uint8_t oparg2 = co_code[f_lasti + 3]; - static PyObject *sysmodule = PyImport_AddModule("sys"); - static PyObject *version = PyObject_GetAttrString(sysmodule, "version_info"); - static PyObject *major = PyTuple_GetItem(version, 0); - static PyObject *minor = PyTuple_GetItem(version, 1); - auto number = PyLong_AsLong(major) * 1000 + PyLong_AsLong(minor); + static auto number = _getVersion(); if (number < 3007) return opcode1 == LOAD_ATTR && opcode2 == CALL_FUNCTION && oparg2 == 0; if (number < 3011) @@ -270,7 +285,7 @@ PyObject *mangled_type_getattro(PyTypeObject *type, PyObject *name) auto sotp = PepType_SOTP(type_base); // The EnumFlagInfo structure tells us if there are Enums at all. const char **enumFlagInfo = sotp->enumFlagInfo; - if (!(enumFlagInfo && enumFlagInfo[0])) + if (!(enumFlagInfo)) continue; if (!sotp->flagsDict) _initFlagsDict(sotp); diff --git a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/parser.py b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/parser.py index 8f098ec13..7c2237fc3 100644 --- a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/parser.py +++ b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/parser.py @@ -104,8 +104,9 @@ def _get_flag_enum_option(): f"The file pep384_issue33738.cpp should be removed ASAP! ***") # PYSIDE-1735: Emit a warning when we may update enum_310.py if pymaxver and pymaxver > (3, 10): - warnings.warn(f"\n *** Python is at version {'.'.join(map(str, pymaxver))} now. " - f"Please check if enum_310.py should be updated! ***") + if sys.version_info >= (3, 11, 0) and sys.version_info.releaselevel == "final": + warnings.warn(f"\n *** Python is at version {'.'.join(map(str, pymaxver))} now. " + f"Please check if enum_310.py should be updated! ***") # PYSIDE-1735: Emit a warning when we may update enum_310.py if ver[:2] >= (7, 0): warnings.warn(f"\n *** PySide is at version {'.'.join(map(str, ver[:2]))} now. " |