diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-25 16:33:12 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-25 16:33:12 +0200 |
| commit | 39ac266455cc1c0ae970a47c34d3a6bc7372d750 (patch) | |
| tree | 9577fe06bcb0e3096ef85af929a91eb37b037452 /sources/pyside2/PySide2 | |
| parent | 9115712ef3a1be8b401c9cd306792dbc30c89bf0 (diff) | |
| parent | 6717d3540fac74c91d9381958a08e60f6532d402 (diff) | |
Merge remote-tracking branch 'origin/5.14' into 5.14.2
Change-Id: Id0650ec2b46f3c67eb2c9b400f5b31aab98c3c58
Diffstat (limited to 'sources/pyside2/PySide2')
| -rw-r--r-- | sources/pyside2/PySide2/QtCore/typesystem_core_common.xml | 4 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/QtQml/pysideqmlregistertype.cpp | 11 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/QtUiTools/typesystem_uitools.xml | 36 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/glue/qtcore.cpp | 16 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/glue/qtuitools.cpp | 138 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/support/deprecated.py | 2 | ||||
| -rw-r--r-- | sources/pyside2/PySide2/support/generate_pyi.py | 8 |
7 files changed, 199 insertions, 16 deletions
diff --git a/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml b/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml index 53ab29382..26193a0aa 100644 --- a/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml +++ b/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml @@ -223,9 +223,9 @@ </primitive-type> <primitive-type name="quintptr" target-lang-api-name="PyLong"> <conversion-rule> - <native-to-target file="../glue/qtcore.cpp" snippet="return-pylong-unsigned"/> + <native-to-target file="../glue/qtcore.cpp" snippet="return-pylong-quintptr"/> <target-to-native> - <add-conversion type="PyLong" file="../glue/qtcore.cpp" snippet="conversion-pylong-unsigned"/> + <add-conversion type="PyLong" file="../glue/qtcore.cpp" snippet="conversion-pylong-quintptr"/> </target-to-native> </conversion-rule> </primitive-type> diff --git a/sources/pyside2/PySide2/QtQml/pysideqmlregistertype.cpp b/sources/pyside2/PySide2/QtQml/pysideqmlregistertype.cpp index fb9a5a0cf..efc86a048 100644 --- a/sources/pyside2/PySide2/QtQml/pysideqmlregistertype.cpp +++ b/sources/pyside2/PySide2/QtQml/pysideqmlregistertype.cpp @@ -240,7 +240,7 @@ static PyType_Slot PropertyListType_slots[] = { {0, 0} }; static PyType_Spec PropertyListType_spec = { - "PySide2.QtQml.ListProperty", + "2:PySide2.QtQml.ListProperty", sizeof(PySideProperty), 0, Py_TPFLAGS_DEFAULT, @@ -253,7 +253,7 @@ PyTypeObject *PropertyListTypeF(void) static PyTypeObject *type = nullptr; if (!type) { PyObject *bases = Py_BuildValue("(O)", PySidePropertyTypeF()); - type = (PyTypeObject *)PyType_FromSpecWithBases(&PropertyListType_spec, bases); + type = (PyTypeObject *)SbkType_FromSpecWithBases(&PropertyListType_spec, bases); Py_XDECREF(bases); } return type; @@ -454,7 +454,7 @@ static PyType_Slot QtQml_VolatileBoolType_slots[] = { {0, 0} }; static PyType_Spec QtQml_VolatileBoolType_spec = { - "PySide2.QtQml.VolatileBool", + "2:PySide2.QtQml.VolatileBool", sizeof(QtQml_VolatileBoolObject), 0, Py_TPFLAGS_DEFAULT, @@ -464,9 +464,8 @@ static PyType_Spec QtQml_VolatileBoolType_spec = { PyTypeObject *QtQml_VolatileBoolTypeF(void) { - static PyTypeObject *type = nullptr; - if (!type) - type = (PyTypeObject *)PyType_FromSpec(&QtQml_VolatileBoolType_spec); + static PyTypeObject *type = reinterpret_cast<PyTypeObject *>( + SbkType_FromSpec(&QtQml_VolatileBoolType_spec)); return type; } diff --git a/sources/pyside2/PySide2/QtUiTools/typesystem_uitools.xml b/sources/pyside2/PySide2/QtUiTools/typesystem_uitools.xml index 7b27e8783..2ca12e788 100644 --- a/sources/pyside2/PySide2/QtUiTools/typesystem_uitools.xml +++ b/sources/pyside2/PySide2/QtUiTools/typesystem_uitools.xml @@ -139,4 +139,40 @@ </add-function> </object-type> + <!-- + After the removal of the 'pysideuic' Python module, many users were unable to generate and + load UI classes dynamically. + This function was created to provide an equivalent solution to the 'loadUiType' function from + Riverbank's PyQt. + --> + <add-function signature="loadUiType(const QString& @uifile@)" return-type="PyObject*"> + <inject-documentation format="target" mode="append"> + This function will allow users to generate and load a `.ui` file at runtime, and it returns + a `tuple` containing the reference to the Python class, and the base class. + + We don't recommend this approach since the workflow should be to generate a Python file + from the `.ui` file, and then import and load it to use it, but we do understand that + there are some corner cases when such functionality is required. + + The internal process relies on `uic` being in the PATH, which is the same requirement for + the new `pyside2-uic` to work (which is just a wrapper around `uic -g python`) + + A Simple use can be: + + .. code-block:: python + + from PySide2.QtUiTools import loadUiType + + generated_class, base_class = loadUiType("themewidget.ui") + # the values will be: + # (<class '__main__.Ui_ThemeWidgetForm'>, <class 'PySide2.QtWidgets.QWidget'>) + + + In that case, `generated_class` will be a reference to the Python class, + and `base_class` will be a reference to the base class. + </inject-documentation> + <inject-code file="../glue/qtuitools.cpp" snippet="loaduitype"/> + </add-function> + + </typesystem> diff --git a/sources/pyside2/PySide2/glue/qtcore.cpp b/sources/pyside2/PySide2/glue/qtcore.cpp index 834383679..111e324b9 100644 --- a/sources/pyside2/PySide2/glue/qtcore.cpp +++ b/sources/pyside2/PySide2/glue/qtcore.cpp @@ -1678,6 +1678,14 @@ Py_END_ALLOW_THREADS %out = %OUTTYPE(PyLong_AsUnsignedLong(%in)); // @snippet conversion-pylong-unsigned +// @snippet conversion-pylong-quintptr +#if defined(IS_PY3K) && QT_POINTER_SIZE == 8 +%out = %OUTTYPE(PyLong_AsUnsignedLongLong(%in)); +#else +%out = %OUTTYPE(PyLong_AsUnsignedLong(%in)); +#endif +// @snippet conversion-pylong-quintptr + // @snippet conversion-pyunicode #ifndef Py_LIMITED_API Py_UNICODE *unicode = PyUnicode_AS_UNICODE(%in); @@ -1870,6 +1878,14 @@ return PyLong_FromLong(%in); return PyLong_FromUnsignedLong(%in); // @snippet return-pylong-unsigned +// @snippet return-pylong-quintptr +#if defined(IS_PY3K) && QT_POINTER_SIZE == 8 +return PyLong_FromUnsignedLongLong(%in); +#else +return PyLong_FromUnsignedLong(%in); +#endif +// @snippet return-pylong-quintptr + // @snippet return-pyunicode QByteArray ba = %in.toUtf8(); return PyUnicode_FromStringAndSize(ba.constData(), ba.size()); diff --git a/sources/pyside2/PySide2/glue/qtuitools.cpp b/sources/pyside2/PySide2/glue/qtuitools.cpp index 00fc8e44a..668b512e4 100644 --- a/sources/pyside2/PySide2/glue/qtuitools.cpp +++ b/sources/pyside2/PySide2/glue/qtuitools.cpp @@ -109,3 +109,141 @@ registerCustomWidget(%PYARG_1); // Avoid calling the original function: %CPPSELF.%FUNCTION_NAME() %PYARG_0 = QUiLoaderLoadUiFromFileName(%CPPSELF, %1, %2); // @snippet quiloader-load-2 + +// @snippet loaduitype +/* +Arguments: + %PYARG_1 (uifile) +*/ +// 1. Generate the Python code from the UI file +#ifdef IS_PY3K +PyObject *strObj = PyUnicode_AsUTF8String(%PYARG_1); +char *arg1 = PyBytes_AsString(strObj); +QByteArray uiFileName(arg1); +Py_DECREF(strObj); +#else +QByteArray uiFileName(PyBytes_AsString(%PYARG_1)); +#endif + +QFile uiFile(uiFileName); + +if (!uiFile.exists()) { + qCritical().noquote() << "File" << uiFileName << "does not exists"; + Py_RETURN_NONE; +} + +if (uiFileName.isEmpty()) { + qCritical() << "Error converting the UI filename to QByteArray"; + Py_RETURN_NONE; +} + +QString uicBin("uic"); +QStringList uicArgs = {"-g", "python", QString::fromUtf8(uiFileName)}; + +QProcess uicProcess; +uicProcess.start(uicBin, uicArgs); +if (!uicProcess.waitForFinished()) { + qCritical() << "Cannot run 'uic': " << uicProcess.errorString() << " - " + << "Exit status " << uicProcess.exitStatus() + << " (" << uicProcess.exitCode() << ")\n" + << "Check if 'uic' is in PATH"; + Py_RETURN_NONE; +} +QByteArray uiFileContent = uicProcess.readAllStandardOutput(); +QByteArray errorOutput = uicProcess.readAllStandardError(); + +if (!errorOutput.isEmpty()) { + qCritical().noquote() << errorOutput; + Py_RETURN_NONE; +} + +// 2. Obtain the 'classname' and the Qt base class. +QByteArray className; +QByteArray baseClassName; + +// Problem +// The generated Python file doesn't have the Qt Base class information. + +// Solution +// Use the XML file +if (!uiFile.open(QIODevice::ReadOnly)) + Py_RETURN_NONE; + +// This will look for the first <widget> tag, e.g.: +// <widget class="QWidget" name="ThemeWidgetForm"> +// and then extract the information from "class", and "name", +// to get the baseClassName and className respectively +QXmlStreamReader reader(&uiFile); +while (!reader.atEnd() && baseClassName.isEmpty() && className.isEmpty()) { + auto token = reader.readNext(); + if (token == QXmlStreamReader::StartElement && reader.name() == "widget") { + baseClassName = reader.attributes().value(QLatin1String("class")).toUtf8(); + className = reader.attributes().value(QLatin1String("name")).toUtf8(); + } +} + +uiFile.close(); + +if (className.isEmpty() || baseClassName.isEmpty() || reader.hasError()) { + qCritical() << "An error occurred when parsing the UI file while looking for the class info " + << reader.errorString(); + Py_RETURN_NONE; +} + +QByteArray pyClassName("Ui_"+className); + +PyObject *module = PyImport_ImportModule("__main__"); +PyObject *loc = PyModule_GetDict(module); + +// 3. exec() the code so the class exists in the context: exec(uiFileContent) +// The context of PyRun_SimpleString is __main__. +// 'Py_file_input' is the equivalent to using exec(), since it will execute +// the code, without returning anything. +Shiboken::AutoDecRef codeUi(Py_CompileString(uiFileContent.constData(), "<stdin>", Py_file_input)); +if (codeUi.isNull()) { + qCritical() << "Error while compiling the generated Python file"; + Py_RETURN_NONE; +} +PyObject *uiObj = nullptr; +#ifdef IS_PY3K +uiObj = PyEval_EvalCode(codeUi, loc, loc); +#else +uiObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeUi.object()), loc, loc); +#endif + +if (uiObj == nullptr) { + qCritical() << "Error while running exec() on the generated code"; + Py_RETURN_NONE; +} + +// 4. eval() the name of the class on a variable to return +// 'Py_eval_input' is the equivalent to using eval(), since it will just +// evaluate an expression. +Shiboken::AutoDecRef codeClass(Py_CompileString(pyClassName.constData(),"<stdin>", Py_eval_input)); +if (codeClass.isNull()) { + qCritical() << "Error while compiling the Python class"; + Py_RETURN_NONE; +} + +Shiboken::AutoDecRef codeBaseClass(Py_CompileString(baseClassName.constData(), "<stdin>", Py_eval_input)); +if (codeBaseClass.isNull()) { + qCritical() << "Error while compiling the base class"; + Py_RETURN_NONE; +} + +#ifdef IS_PY3K +PyObject *classObj = PyEval_EvalCode(codeClass, loc, loc); +PyObject *baseClassObj = PyEval_EvalCode(codeBaseClass, loc, loc); +#else +PyObject *classObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeClass.object()), loc, loc); +PyObject *baseClassObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeBaseClass.object()), loc, loc); +#endif + +%PYARG_0 = PyTuple_New(2); +if (%PYARG_0 == nullptr) { + qCritical() << "Error while creating the return Tuple"; + Py_RETURN_NONE; +} +PyTuple_SET_ITEM(%PYARG_0, 0, classObj); +PyTuple_SET_ITEM(%PYARG_0, 1, baseClassObj); +// @snippet loaduitype diff --git a/sources/pyside2/PySide2/support/deprecated.py b/sources/pyside2/PySide2/support/deprecated.py index 8538826e4..57f33d9e2 100644 --- a/sources/pyside2/PySide2/support/deprecated.py +++ b/sources/pyside2/PySide2/support/deprecated.py @@ -64,7 +64,7 @@ class PySideDeprecationWarningRemovedInQt6(Warning): def constData(self): cls = self.__class__ - name = cls.__name__ + name = cls.__qualname__ warnings.warn(dedent(""" {name}.constData is unpythonic and will be removed in Qt For Python 6.0 . Please use {name}.data instead.""" diff --git a/sources/pyside2/PySide2/support/generate_pyi.py b/sources/pyside2/PySide2/support/generate_pyi.py index f92367c82..af9f4d4f5 100644 --- a/sources/pyside2/PySide2/support/generate_pyi.py +++ b/sources/pyside2/PySide2/support/generate_pyi.py @@ -1,7 +1,7 @@ # This Python file uses the following encoding: utf-8 ############################################################################# ## -## Copyright (C) 2019 The Qt Company Ltd. +## Copyright (C) 2020 The Qt Company Ltd. ## Contact: https://www.qt.io/licensing/ ## ## This file is part of Qt for Python. @@ -169,12 +169,6 @@ class Formatter(Writer): else: self.print("{spaces}class {class_str}: ...".format(**locals())) yield - if "<" in class_name: - # This is happening in QtQuick for some reason: - ## class QSharedPointer<QQuickItemGrabResult >: - # We simply skip over this class. - self.outfile.seek(here) - self.outfile.truncate() @contextmanager def function(self, func_name, signature, modifier=None): |
