diff options
author | Christian Tismer <tismer@stackless.com> | 2019-06-05 13:40:35 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-06-06 11:58:52 +0200 |
commit | be92136910f88c3fb6f2d1810e702fcf5e7cd25a (patch) | |
tree | 342d7ccdbb098013a3fa4ca88c5e21646b505726 /sources | |
parent | fd126b28e1d9b02ea16c813bc392461bdb05bd1d (diff) |
correct bool(qApp) on Python 2
The idiom to create an application if none exists
qApp or QApplication()
did not work on Python 2. This was recognized when another
unrelated test broke that tried to use the idiom.
This patch adds the missing method to Py_TYPE(Py_None)
which Python 3 already has.
A test case was added.
Side-effect.. The warning about missing braces is gone,
after the "PYTHON_IS_PYTHON3" macro is defined. This
was forgotten in the review when when the macro was
moved out of signature.cpp .
Task-number: PYSIDE-571
Change-Id: I9b9304f407cd4cafb3ba0945ffb045802503a343
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py | 2 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/qapp_macro.cpp | 32 |
2 files changed, 32 insertions, 2 deletions
diff --git a/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py b/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py index b38cff68b..c58aba82e 100644 --- a/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py +++ b/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py @@ -40,7 +40,7 @@ class qAppMacroTest(unittest.TestCase): except ImportError: QtWidgets = QtGui = QtCore # qApp is in the builtins - qApp + self.assertEqual(bool(qApp), False) # and also in certain PySide modules QtCore.qApp, QtGui.qApp, QtWidgets.qApp # and they are all the same diff --git a/sources/shiboken2/libshiboken/qapp_macro.cpp b/sources/shiboken2/libshiboken/qapp_macro.cpp index f69d0f937..19e985b20 100644 --- a/sources/shiboken2/libshiboken/qapp_macro.cpp +++ b/sources/shiboken2/libshiboken/qapp_macro.cpp @@ -67,7 +67,9 @@ qApp_module_index(PyObject *module) return ret; } -#define Py_NONE_TYPE Py_TYPE(Py_None) +#define PYTHON_IS_PYTHON3 (PY_VERSION_HEX >= 0x03000000) +#define PYTHON_IS_PYTHON2 (!PYTHON_IS_PYTHON3) +#define Py_NONE_TYPE Py_TYPE(Py_None) #if PYTHON_IS_PYTHON3 # define BRACE_OPEN { @@ -156,6 +158,31 @@ MakeSingletonQAppWrapper(PyTypeObject *type) return qApp_content; } +#if PYTHON_IS_PYTHON2 + +// Install support in Py_NONE_TYPE for Python 2: 'bool(qApp) == False'. +static int +none_bool(PyObject *v) +{ + return 0; +} + +static PyNumberMethods none_as_number = { + nullptr, /* nb_add */ + nullptr, /* nb_subtract */ + nullptr, /* nb_multiply */ + nullptr, /* nb_divide */ + nullptr, /* nb_remainder */ + nullptr, /* nb_divmod */ + nullptr, /* nb_power */ + nullptr, /* nb_negative */ + nullptr, /* nb_positive */ + nullptr, /* nb_absolute */ + reinterpret_cast<inquiry>(none_bool), /* nb_nonzero */ +}; + +#endif + static int setup_qApp_var(PyObject *module) { @@ -163,6 +190,9 @@ setup_qApp_var(PyObject *module) static int init_done = 0; if (!init_done) { +#if PYTHON_IS_PYTHON2 + Py_NONE_TYPE->tp_as_number = &none_as_number; +#endif qApp_var = Py_BuildValue("s", "qApp"); if (qApp_var == NULL) return -1; |