diff options
| author | Christian Tismer <tismer@stackless.com> | 2019-08-10 18:09:17 +0200 |
|---|---|---|
| committer | Christian Tismer <tismer@stackless.com> | 2019-10-30 16:34:41 +0100 |
| commit | 46d44749d01b7bd195e8838e681b227aef4b5f06 (patch) | |
| tree | f7735a3548770ba1c93e0f4ed8dcdea6383da6d6 /sources/pyside2/tests/pysidetest | |
| parent | d04df47c5aafe519123b311ce3705b6eae511dc4 (diff) | |
Improve the NumPy Support by iterables
Working example, by overriding cppgenerator:
>>> from PySide2 import *
>>> QtCore.QUrl.fromStringList(("asd", "def"))
[PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('def')]
>>> def func(lis):
... for thing in lis:
... yield thing
...
>>> QtCore.QUrl.fromStringList(func(["asd", "def"]))
[PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('def')]
Also working, by overriding shibokengenerator
>>> QtGui.QMatrix4x4(func(range(16)))
And all other QMatrix sizes as well:
>>> QtGui.QMatrix2x2(func(range(4)))
>>> QtGui.QMatrix2x3(func(range(6)))
The PySequence cases seem to be quite completely covered.
Supporting lists and QVector is not yet clear and needs
more research.
Note.. QtOpenGLFunctions is not tested at all and nothing works
on macOS, segfault. Ignored for now!
A simple numpy test shows how versatile this solution is.
We now need to improve signatures and error messages
to optimize the experience.
Task-number: PYSIDE-795
Change-Id: I195cd46cf47c2eb83276fe48fce8e6070cf30fda
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside2/tests/pysidetest')
| -rw-r--r-- | sources/pyside2/tests/pysidetest/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | sources/pyside2/tests/pysidetest/iterable_test.py | 86 |
2 files changed, 95 insertions, 8 deletions
diff --git a/sources/pyside2/tests/pysidetest/CMakeLists.txt b/sources/pyside2/tests/pysidetest/CMakeLists.txt index 3c993cf4e..f81de55f7 100644 --- a/sources/pyside2/tests/pysidetest/CMakeLists.txt +++ b/sources/pyside2/tests/pysidetest/CMakeLists.txt @@ -124,21 +124,22 @@ PYSIDE_TEST(decoratedslot_test.py) if (Qt5Core_VERSION VERSION_GREATER 5.7.0) PYSIDE_TEST(delegatecreateseditor_test.py) endif() +PYSIDE_TEST(all_modules_load_test.py) +PYSIDE_TEST(bug_1016.py) +PYSIDE_TEST(embedding_test.py) PYSIDE_TEST(enum_test.py) PYSIDE_TEST(homonymoussignalandmethod_test.py) +PYSIDE_TEST(iterable_test.py) PYSIDE_TEST(list_signal_test.py) +PYSIDE_TEST(mixin_signal_slots_test.py) PYSIDE_TEST(modelview_test.py) PYSIDE_TEST(new_inherited_functions_test.py) PYSIDE_TEST(notify_id.py) +PYSIDE_TEST(qapp_like_a_macro_test.py) PYSIDE_TEST(qvariant_test.py) +PYSIDE_TEST(signal_slot_warning.py) PYSIDE_TEST(signalandnamespace_test.py) -PYSIDE_TEST(signalwithdefaultvalue_test.py) PYSIDE_TEST(signalemissionfrompython_test.py) -PYSIDE_TEST(version_test.py) +PYSIDE_TEST(signalwithdefaultvalue_test.py) PYSIDE_TEST(typedef_signal_test.py) -PYSIDE_TEST(bug_1016.py) -PYSIDE_TEST(mixin_signal_slots_test.py) -PYSIDE_TEST(signal_slot_warning.py) -PYSIDE_TEST(all_modules_load_test.py) -PYSIDE_TEST(qapp_like_a_macro_test.py) -PYSIDE_TEST(embedding_test.py) +PYSIDE_TEST(version_test.py) diff --git a/sources/pyside2/tests/pysidetest/iterable_test.py b/sources/pyside2/tests/pysidetest/iterable_test.py new file mode 100644 index 000000000..2f488875e --- /dev/null +++ b/sources/pyside2/tests/pysidetest/iterable_test.py @@ -0,0 +1,86 @@ +############################################################################# +## +## Copyright (C) 2019 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of Qt for Python. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## 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 General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## 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-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +""" +iterable_test.py + +This test checks that the Iterable protocol is implemented correctly. +""" + +import os +import sys +import unittest +import PySide2 +from PySide2 import QtCore, QtGui + +try: + import numpy as np + have_numpy = True +except ImportError: + have_numpy = False + +class PySequenceTest(unittest.TestCase): + + def test_iterable(self): + def gen(lis): + for item in lis: + if item == "crash": + raise IndexError + yield item + # testing "pyseq_to_cpplist_conversion" + testfunc = QtCore.QUrl.fromStringList + # use a generator (iterable) + self.assertEqual(testfunc(gen(["asd", "ghj"])), + [PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('ghj')]) + # use an iterator + self.assertEqual(testfunc(iter(["asd", "ghj"])), + [PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('ghj')]) + self.assertRaises(IndexError, testfunc, gen(["asd", "crash", "ghj"])) + # testing QMatrix4x4 + testfunc = QtGui.QMatrix4x4 + self.assertEqual(testfunc(gen(range(16))), testfunc(range(16))) + # Note: The errormessage needs to be improved! + # We should better get a ValueError + self.assertRaises((TypeError, ValueError), testfunc, gen(range(15))) + # All other matrix sizes: + testfunc = QtGui.QMatrix2x2 + self.assertEqual(testfunc(gen(range(4))), testfunc(range(4))) + testfunc = QtGui.QMatrix2x3 + self.assertEqual(testfunc(gen(range(6))), testfunc(range(6))) + + @unittest.skipUnless(have_numpy, "requires numpy") + def test_iterable_numpy(self): + # Demo for numpy: We create a unit matrix. + num_mat = np.eye(4) + num_mat.shape = 16 + unit = QtGui.QMatrix4x4(num_mat) + self.assertEqual(unit, QtGui.QMatrix4x4()) + + +if __name__ == '__main__': + unittest.main() |
