diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-13 13:03:18 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-13 13:07:52 +0200 |
commit | 843b476cf4bf7f20c7a3c433e41a76995b0cebca (patch) | |
tree | 3f3dc80c8c8c370e0d62462f9d352412b9cb4595 /sources/pyside2 | |
parent | 3899ed571eb2b453736b3b55a8991df83d33d609 (diff) | |
parent | 7211180820d76814c7060d39d7ce0e9902865e14 (diff) |
Merge remote-tracking branch 'origin/5.15' into dev
Change-Id: I509b55363c29639cd29bfbc012c4bda3a4d008e5
Diffstat (limited to 'sources/pyside2')
-rw-r--r-- | sources/pyside2/PySide2/global.h.in | 7 | ||||
-rw-r--r-- | sources/pyside2/libpyside/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sources/pyside2/libpyside/feature_select.cpp | 395 | ||||
-rw-r--r-- | sources/pyside2/libpyside/feature_select.h | 53 | ||||
-rw-r--r-- | sources/pyside2/libpyside/pyside.cpp | 2 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/feature_test.py | 105 |
7 files changed, 565 insertions, 0 deletions
diff --git a/sources/pyside2/PySide2/global.h.in b/sources/pyside2/PySide2/global.h.in index ae1b103f5..a23b0f332 100644 --- a/sources/pyside2/PySide2/global.h.in +++ b/sources/pyside2/PySide2/global.h.in @@ -40,6 +40,13 @@ // Make "signals:", "slots:" visible as access specifiers #define QT_ANNOTATE_ACCESS_SPECIFIER(a) __attribute__((annotate(#a))) +// Q_PROPERTY is defined as class annotation which does not work since a +// sequence of properties will to expand to a sequence of annotations +// annotating nothing, causing clang to complain. Instead, define it away in a +// static assert with the stringified argument in a ','-operator (cf qdoc). + +#define QT_ANNOTATE_CLASS(type,...) static_assert(sizeof(#__VA_ARGS__),#type); + #include <QtCore/qnamespace.h> #if @ENABLE_X11@ diff --git a/sources/pyside2/libpyside/CMakeLists.txt b/sources/pyside2/libpyside/CMakeLists.txt index 31f68749a..d6b20c45a 100644 --- a/sources/pyside2/libpyside/CMakeLists.txt +++ b/sources/pyside2/libpyside/CMakeLists.txt @@ -40,6 +40,7 @@ endif() set(libpyside_SRC dynamicqmetaobject.cpp + feature_select.cpp signalmanager.cpp globalreceiverv2.cpp pysideclassinfo.cpp @@ -125,6 +126,7 @@ endif() set(libpyside_HEADERS dynamicqmetaobject.h + feature_select.h pysideclassinfo.h pysideqenum.h pysidemacros.h diff --git a/sources/pyside2/libpyside/feature_select.cpp b/sources/pyside2/libpyside/feature_select.cpp new file mode 100644 index 000000000..b7234ad75 --- /dev/null +++ b/sources/pyside2/libpyside/feature_select.cpp @@ -0,0 +1,395 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "feature_select.h" + +#include <shiboken.h> +#include <sbkstaticstrings.h> + +#include <QtCore/QtGlobal> + +////////////////////////////////////////////////////////////////////////////// +// +// PYSIDE-1019: Support switchable extensions +// +// This functionality is no longer implemented in the signature module, since +// the PyCFunction getsets do not have to be modified any longer. +// Instead, we simply exchange the complete class dicts. This is done in the +// basewrapper.cpp file. +// +// This is the general framework of the switchable extensions. +// A maximum of eight features is planned so far. This seems to be enough. +// More features are possible, but then we must somehow register the +// extra `select_id`s above 255. +// + +/***************************************************************************** + + How Does This Feature Selection Work? + ------------------------------------- + +The basic idea is to replace the `tp_dict` of a QObject derived type. +This way, we can replace the methods of the dict in no time. + +The crucial point to understand is how the `tp_dict` is actually accessed: +When you type "QObject.__dict__", the descriptor of SbkObjectType_Type +is called. This descriptor is per default unassigned, so the base class +PyType_Type provides the tp_getset method `type_dict`: + + static PyObject * + type_dict(PyTypeObject *type, void *context) + { + if (type->tp_dict == NULL) { + Py_RETURN_NONE; + } + return PyDictProxy_New(type->tp_dict); + } + +In order to change that, we need to insert our own version into SbkObjectType: + + static PyObject *Sbk_TypeGet___dict__(PyTypeObject *type, void *context) + { + auto dict = type->tp_dict; + if (dict == NULL) + Py_RETURN_NONE; + if (SelectFeatureSet != nullptr) + dict = SelectFeatureSet(type); + return PyDictProxy_New(dict); + } + +This way, the Python function `type_ready()` does not fill in the default, +but uses our modified version. It a similar way, we overwrite type_getattro +with our own version, again in SbkObjectType, replacing the default of +PyType_Type. + +Now we can exchange the dict with a customized version. +We have our own derived type `ChameleonDict` with additional attributes. +These allow us to create a ring of dicts which can be rotated to the actual +needed dict version: + +Every dict has a field `select_id` which is selected by the `from __feature__` +import. The dicts are cyclic connected by the `dict_ring` field. + +When a class dict is required, now always `SelectFeatureSet` is called, which +looks into the `__name__` attribute of the active module and decides which +version of `tp_dict` is needed. Then the right dict is searched in the ring +and created if not already there. + +This is everything that the following code does. + +*****************************************************************************/ + + +namespace PySide { namespace FeatureSelector { + +using namespace Shiboken; + +static PyObject *getFeatureSelectID() +{ + static PyObject *zero = PyInt_FromLong(0); + static PyObject *feature_dict = GetFeatureDict(); + // these things are all borrowed + PyObject *globals = PyEval_GetGlobals(); + if (globals == nullptr) + return zero; + PyObject *modname = PyDict_GetItem(globals, PyMagicName::name()); + if (modname == nullptr) + return zero; + PyObject *flag = PyDict_GetItem(feature_dict, modname); + if (flag == nullptr || !PyInt_Check(flag)) // int/long cheating + return zero; + return flag; +} + +// Create a derived dict class +static PyTypeObject * +createDerivedDictType() +{ + // It is not easy to create a compatible dict object with the + // limited API. Easier is to use Python to create a derived + // type and to modify that a bit from the C code. + PyObject *ChameleonDict = PepRun_GetResult(R"CPP(if True: + + class ChameleonDict(dict): + __slots__ = ("dict_ring", "select_id") + + result = ChameleonDict + + )CPP"); + return reinterpret_cast<PyTypeObject *>(ChameleonDict); +} + +static PyTypeObject *old_dict_type = Py_TYPE(PyType_Type.tp_dict); +static PyTypeObject *new_dict_type = nullptr; + +static void ensureNewDictType() +{ + if (new_dict_type == nullptr) { + new_dict_type = createDerivedDictType(); + if (new_dict_type == nullptr) + Py_FatalError("PySide2: Problem creating ChameleonDict"); + } +} + +static inline PyObject *nextInCircle(PyObject *dict) +{ + // returns a borrowed ref + assert(Py_TYPE(dict) != old_dict_type); + AutoDecRef next_dict(PyObject_GetAttr(dict, PyName::dict_ring())); + return next_dict; +} + +static inline void setNextDict(PyObject *dict, PyObject *next_dict) +{ + assert(Py_TYPE(dict) != old_dict_type); + PyObject_SetAttr(dict, PyName::dict_ring(), next_dict); +} + +static inline void setSelectId(PyObject *dict, PyObject *select_id) +{ + assert(Py_TYPE(dict) != old_dict_type); + PyObject_SetAttr(dict, PyName::select_id(), select_id); +} + +static inline PyObject *getSelectId(PyObject *dict) +{ + assert(Py_TYPE(dict) != old_dict_type); + auto select_id = PyObject_GetAttr(dict, PyName::select_id()); + return select_id; +} + +static bool replaceClassDict(PyTypeObject *type) +{ + /* + * Replace the type dict by the derived ChameleonDict. + * This is mandatory for all type dicts when they are touched. + */ + ensureNewDictType(); + PyObject *dict = type->tp_dict; + auto ob_ndt = reinterpret_cast<PyObject *>(new_dict_type); + PyObject *new_dict = PyObject_CallObject(ob_ndt, nullptr); + if (new_dict == nullptr || PyDict_Update(new_dict, dict) < 0) + return false; + // Insert the default id. Cannot fail for small numbers. + AutoDecRef select_id(PyInt_FromLong(0)); + setSelectId(new_dict, select_id); + // insert the dict into itself as ring + setNextDict(new_dict, new_dict); + // We have now an exact copy of the dict with a new type. + // Replace `__dict__` which usually has refcount 1 (but see cyclic_test.py) + Py_DECREF(type->tp_dict); + type->tp_dict = new_dict; + return true; +} + +static bool addNewDict(PyTypeObject *type, PyObject *select_id) +{ + /* + * Add a new dict to the ring and set it as `type->tp_dict`. + * A 'false' return is fatal. + */ + auto dict = type->tp_dict; + auto ob_ndt = reinterpret_cast<PyObject *>(new_dict_type); + auto new_dict = PyObject_CallObject(ob_ndt, nullptr); + if (new_dict == nullptr) + return false; + setSelectId(new_dict, select_id); + // insert the dict into the ring + auto next_dict = nextInCircle(dict); + setNextDict(dict, new_dict); + setNextDict(new_dict, next_dict); + type->tp_dict = new_dict; + return true; +} + +static bool moveToFeatureSet(PyTypeObject *type, PyObject *select_id) +{ + /* + * Rotate the ring to the given `select_id` and return `true`. + * If not found, stay at the current position and return `false`. + */ + auto initial_dict = type->tp_dict; + auto dict = initial_dict; + do { + dict = nextInCircle(dict); + AutoDecRef current_id(getSelectId(dict)); + // This works because small numbers are singleton objects. + if (current_id == select_id) { + type->tp_dict = dict; + return true; + } + } while (dict != initial_dict); + type->tp_dict = initial_dict; + return false; +} + +typedef bool(*FeatureProc)(PyTypeObject *type, PyObject *prev_dict); + +static FeatureProc *featurePointer = nullptr; + +static bool createNewFeatureSet(PyTypeObject *type, PyObject *select_id) +{ + /* + * Create a new feature set. + * A `false` return value is a fatal error. + * + * A FeatureProc sees an empty `type->tp_dict` and the previous dict + * content in `prev_dict`. It is responsible of filling `type->tp_dict` + * with modified content. + */ + static auto small_1 = PyInt_FromLong(255); + Q_UNUSED(small_1); + static auto small_2 = PyInt_FromLong(255); + Q_UNUSED(small_2); + // make sure that small integers are cached + assert(small_1 != nullptr && small_1 == small_2); + + static auto zero = PyInt_FromLong(0); + bool ok = moveToFeatureSet(type, zero); + Q_UNUSED(ok); + assert(ok); + + AutoDecRef prev_dict(type->tp_dict); + Py_INCREF(prev_dict); + if (!addNewDict(type, select_id)) + return false; + int id = PyInt_AsSsize_t(select_id); + if (id == -1) + return false; + FeatureProc *proc = featurePointer; + for (int idx = id; *proc != nullptr; ++proc, idx >>= 1) { + if (idx & 1) { + // clear the tp_dict that will get new content + PyDict_Clear(type->tp_dict); + // let the proc re-fill the tp_dict + if (!(*proc)(type, prev_dict)) + return false; + // if there is still a step, prepare `prev_dict` + if (idx >> 1) { + prev_dict.reset(PyDict_Copy(type->tp_dict)); + if (prev_dict.isNull()) + return false; + } + } + } + return true; +} + +static PyObject *SelectFeatureSet(PyTypeObject *type) +{ + /* + * This is the main function of the module. + * It just makes no sense to make the function public, because + * Shiboken will assign it via a public hook of `basewrapper.cpp`. + */ + if (Py_TYPE(type->tp_dict) == old_dict_type) { + // PYSIDE-1019: On first touch, we initialize the dynamic naming. + // The dict type will be replaced after the first call. + if (!replaceClassDict(type)) + return nullptr; + } + PyObject *select_id = getFeatureSelectID(); // borrowed + AutoDecRef current_id(getSelectId(type->tp_dict)); + if (select_id != current_id) { + if (!moveToFeatureSet(type, select_id)) + if (!createNewFeatureSet(type, select_id)) { + Py_FatalError("failed to create a new feature set!"); + return nullptr; + } + } + return type->tp_dict; +} + +static bool feature_01_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_02_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_04_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_08_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_10_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_20_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_40_addDummyNames(PyTypeObject *type, PyObject *prev_dict); +static bool feature_80_addDummyNames(PyTypeObject *type, PyObject *prev_dict); + +static FeatureProc featureProcArray[] = { + feature_01_addDummyNames, + feature_02_addDummyNames, + feature_04_addDummyNames, + feature_08_addDummyNames, + feature_10_addDummyNames, + feature_20_addDummyNames, + feature_40_addDummyNames, + feature_80_addDummyNames, + nullptr +}; + +void init() +{ + featurePointer = featureProcArray; + initSelectableFeature(SelectFeatureSet); +} + +////////////////////////////////////////////////////////////////////////////// +// +// PYSIDE-1019: Support switchable extensions +// +// Feature 0x01..0x80: A fake switchable option for testing +// + +#define SIMILAR_FEATURE(xx) \ +static bool feature_##xx##_addDummyNames(PyTypeObject *type, PyObject *prev_dict) \ +{ \ + PyObject *dict = type->tp_dict; \ + if (PyDict_Update(dict, prev_dict) < 0) \ + return false; \ + Py_INCREF(Py_None); \ + if (PyDict_SetItemString(dict, "fake_feature_" #xx, Py_None) < 0) \ + return false; \ + return true; \ +} + +SIMILAR_FEATURE(01) +SIMILAR_FEATURE(02) +SIMILAR_FEATURE(04) +SIMILAR_FEATURE(08) +SIMILAR_FEATURE(10) +SIMILAR_FEATURE(20) +SIMILAR_FEATURE(40) +SIMILAR_FEATURE(80) + +} // namespace PySide +} // namespace FeatureSelector diff --git a/sources/pyside2/libpyside/feature_select.h b/sources/pyside2/libpyside/feature_select.h new file mode 100644 index 000000000..68e29292d --- /dev/null +++ b/sources/pyside2/libpyside/feature_select.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FEATURE_SELECT_H +#define FEATURE_SELECT_H + +#include "pysidemacros.h" + +namespace PySide { +namespace FeatureSelector { + +PYSIDE_API void init(); + +} // namespace PySide +} // namespace FeatureSelector + +#endif // FEATURE_SELECT_H diff --git a/sources/pyside2/libpyside/pyside.cpp b/sources/pyside2/libpyside/pyside.cpp index 66e931164..074fa764b 100644 --- a/sources/pyside2/libpyside/pyside.cpp +++ b/sources/pyside2/libpyside/pyside.cpp @@ -50,6 +50,7 @@ #include "pysidemetafunction_p.h" #include "pysidemetafunction.h" #include "dynamicqmetaobject.h" +#include "feature_select.h" #include <autodecref.h> #include <basewrapper.h> @@ -93,6 +94,7 @@ void init(PyObject *module) MetaFunction::init(module); // Init signal manager, so it will register some meta types used by QVariant. SignalManager::instance(); + FeatureSelector::init(); initQApp(); } diff --git a/sources/pyside2/tests/QtCore/CMakeLists.txt b/sources/pyside2/tests/QtCore/CMakeLists.txt index f3c3ddfdd..c0ec2ef79 100644 --- a/sources/pyside2/tests/QtCore/CMakeLists.txt +++ b/sources/pyside2/tests/QtCore/CMakeLists.txt @@ -37,6 +37,7 @@ PYSIDE_TEST(deletelater_test.py) PYSIDE_TEST(destroysignal_test.py) PYSIDE_TEST(duck_punching_test.py) PYSIDE_TEST(emoji_string_test.py) +PYSIDE_TEST(feature_test.py) PYSIDE_TEST(hash_test.py) PYSIDE_TEST(inherits_test.py) PYSIDE_TEST(max_signals.py) diff --git a/sources/pyside2/tests/QtCore/feature_test.py b/sources/pyside2/tests/QtCore/feature_test.py new file mode 100644 index 000000000..cf1e8c3f2 --- /dev/null +++ b/sources/pyside2/tests/QtCore/feature_test.py @@ -0,0 +1,105 @@ +############################################################################# +## +## Copyright (C) 2020 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of Qt for Python. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU Lesser General Public License Usage +## Alternatively, this file may be used under the terms of the GNU Lesser +## General Public License version 3 as published by the Free Software +## Foundation and appearing in the file LICENSE.LGPL3 included in the +## packaging of this file. Please review the following information to +## ensure the GNU Lesser General Public License version 3 requirements +## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 2.0 or (at your option) the GNU General +## Public license version 3 or any later version approved by the KDE Free +## Qt Foundation. The licenses are as published by the Free Software +## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-2.0.html and +## https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os +import sys +import unittest + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from init_paths import init_test_paths +init_test_paths(False) + +from PySide2 import QtCore +from PySide2.support.__feature__ import _really_all_feature_names +from textwrap import dedent + +""" +feature_test.py +-------------- + +This tests the selectable features in PySide. + +There are no real features implemented. They will be added, later. +""" + +class FeaturesTest(unittest.TestCase): + + def testAllFeatureCombinations(self): + """ + Test for all 256 possible combinations of `__feature__` imports. + """ + global __name__ + + for bit in range(8): + # We are cheating here, since the functions are in the globals. + exec(dedent(""" + + def tst_bit{0}(flag, self): + if flag == 0: + with self.assertRaises(AttributeError): + QtCore.QCborArray.fake_feature_{1:02x} + with self.assertRaises(KeyError): + QtCore.QCborArray.__dict__["fake_feature_{1:02x}"] + else: + QtCore.QCborArray.fake_feature_{1:02x} + QtCore.QCborArray.__dict__["fake_feature_{1:02x}"] + + """.format(bit, 1 << bit)), globals(), globals()) + feature_list = _really_all_feature_names + func_list = [tst_bit0, tst_bit1, tst_bit2, tst_bit3, + tst_bit4, tst_bit5, tst_bit6, tst_bit7] + + for idx in range(0x100): + __name__ = "feature_{:02x}".format(idx) + print() + print("--- Feature Test Module `{}` ---".format(__name__)) + print("Imports:") + for bit in range(8): + if idx & 1 << bit: + feature = feature_list[bit] + text = "from __feature__ import {}".format(feature) + print(text) + exec(text) + for bit in range(8): + value = idx & 1 << bit + func_list[bit](value, self=self) + + +if __name__ == '__main__': + unittest.main() |