diff options
author | Christian Tismer <tismer@stackless.com> | 2020-05-04 17:38:04 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2020-05-18 18:11:15 +0200 |
commit | c82ec2bcbdf4db8fc83fefaec6377aaf2bb3614b (patch) | |
tree | c52df7a209ac3a4d697e7a88e7fbdffdf2f7bc91 /sources/pyside2/tests | |
parent | ef10f62e66a3f409d0077c36f3849c87f96950d2 (diff) |
Support pickling of Qt Enum objects
Pickling for types exists in most cases.
Pickling of Qt Enum objects works fine.
Pickling of Qt Enum types is supported, but does not work
because the builtin type pickling intercepts and then fails..
This problem is now solved because PySide supports
now __qualname__.
So pickling of nested types works now without any extra
code in Python 3. Python 2 is not supported since it would
require too strange patches to Python itself.
Fixes: PYSIDE-15
Task-number: PYSIDE-1286
Change-Id: I346bde07a63afcf2555a3324fcca04efe25e704a
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside2/tests')
-rw-r--r-- | sources/pyside2/tests/QtCore/qenum_test.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/sources/pyside2/tests/QtCore/qenum_test.py b/sources/pyside2/tests/QtCore/qenum_test.py index dd91d1581..ed58f4f20 100644 --- a/sources/pyside2/tests/QtCore/qenum_test.py +++ b/sources/pyside2/tests/QtCore/qenum_test.py @@ -32,13 +32,15 @@ import os import sys +import pickle 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.QtCore import * +from PySide2.QtCore import Qt, QIODevice + class TestEnum(unittest.TestCase): @@ -73,6 +75,7 @@ class TestEnum(unittest.TestCase): with self.assertRaises(TypeError): a = k*2.0 + class TestQFlags(unittest.TestCase): def testToItn(self): om = QIODevice.NotOpen @@ -94,5 +97,33 @@ class TestQFlags(unittest.TestCase): except: pass + +# PYSIDE-15: Pickling of enums +class TestEnumPickling(unittest.TestCase): + def testPickleEnum(self): + + # Pickling of enums with different depth works. + ret = pickle.loads(pickle.dumps(QIODevice.Append)) + self.assertEqual(ret, QIODevice.Append) + + ret = pickle.loads(pickle.dumps(Qt.Key.Key_Asterisk)) + self.assertEqual(ret, Qt.Key.Key_Asterisk) + self.assertEqual(ret, Qt.Key(42)) + + # We can also pickle the whole enum class (built in): + ret = pickle.loads(pickle.dumps(QIODevice)) + + # This works also with nested classes for Python 3, after we + # introduced the correct __qualname__ attribute. + + # Note: For Python 2, we would need quite strange patches. + func = lambda: pickle.loads(pickle.dumps(Qt.Key)) + if sys.version_info[0] < 3: + with self.assertRaises(pickle.PicklingError): + func() + else: + func() + + if __name__ == '__main__': unittest.main() |