aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2022-09-07 19:46:05 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-09-08 16:08:38 +0000
commit7fe9b97640a30b2e46342033e752e4602c0512fa (patch)
treed343ff7f361b3e3e52f836b5ac6f8f404d77afed
parent96ee8e0b23e14e3655755f1ac7de7ac3b994ae66 (diff)
Fix TypeError when comparing Qt.Flags and objects
Comparing a QtCore.Qt.Flags object with any other object calls the PySideQFlags_tp_richcompare() function. This function always threw a TypeError when comparing to a non-numerical object, causing undesired effects. Account for these cases now by returning True or False if the compare operator is != or ==, respectively. Fixes: PYSIDE-2048 Change-Id: Ic81a65ace743b57e90ffe7883e42eb8330b78832 Reviewed-by: Christian Tismer <tismer@stackless.com> (cherry picked from commit bab4f34755a9c00430f9ad528e9c1209a94f0778) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/pyside6/libpyside/pysideqflags.cpp10
-rw-r--r--sources/pyside6/tests/QtCore/qflags_test.py19
2 files changed, 27 insertions, 2 deletions
diff --git a/sources/pyside6/libpyside/pysideqflags.cpp b/sources/pyside6/libpyside/pysideqflags.cpp
index 8cc6b0b30..197119ec3 100644
--- a/sources/pyside6/libpyside/pysideqflags.cpp
+++ b/sources/pyside6/libpyside/pysideqflags.cpp
@@ -100,8 +100,14 @@ extern "C" {
{
int result = 0;
if (!PyNumber_Check(other)) {
- PyErr_BadArgument();
- return nullptr;
+ switch (op) {
+ case Py_EQ:
+ Py_RETURN_FALSE;
+ case Py_NE:
+ Py_RETURN_TRUE;
+ default:
+ Py_RETURN_NOTIMPLEMENTED;
+ }
}
if (self == other) {
diff --git a/sources/pyside6/tests/QtCore/qflags_test.py b/sources/pyside6/tests/QtCore/qflags_test.py
index 8cc0b36c0..77e61560f 100644
--- a/sources/pyside6/tests/QtCore/qflags_test.py
+++ b/sources/pyside6/tests/QtCore/qflags_test.py
@@ -111,6 +111,25 @@ class QFlagOperatorTest(unittest.TestCase):
flags = Qt.NoItemFlags | Qt.ItemIsUserCheckable
self.assertEqual(flags | Qt.ItemIsEnabled, Qt.ItemIsEnabled | flags)
+ def testEqualNonNumericalObject(self):
+ '''QFlags ==,!= non-numerical object '''
+ flags = Qt.NoItemFlags | Qt.ItemIsUserCheckable
+
+ self.assertTrue(flags != None) # noqa: E711
+ self.assertFalse(flags == None) # noqa: E711
+
+ self.assertTrue(flags != "tomato")
+ self.assertFalse(flags == "tomato")
+
+ with self.assertRaises(TypeError):
+ flags > None
+ with self.assertRaises(TypeError):
+ flags >= None
+ with self.assertRaises(TypeError):
+ flags < None
+ with self.assertRaises(TypeError):
+ flags <= None
+
class QFlagsOnQVariant(unittest.TestCase):
def testQFlagsOnQVariant(self):