aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/PySide2/templates
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2019-08-10 18:09:17 +0200
committerChristian Tismer <tismer@stackless.com>2019-10-30 16:34:41 +0100
commit46d44749d01b7bd195e8838e681b227aef4b5f06 (patch)
treef7735a3548770ba1c93e0f4ed8dcdea6383da6d6 /sources/pyside2/PySide2/templates
parentd04df47c5aafe519123b311ce3705b6eae511dc4 (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/PySide2/templates')
-rw-r--r--sources/pyside2/PySide2/templates/core_common.xml31
-rw-r--r--sources/pyside2/PySide2/templates/gui_common.xml6
2 files changed, 28 insertions, 9 deletions
diff --git a/sources/pyside2/PySide2/templates/core_common.xml b/sources/pyside2/PySide2/templates/core_common.xml
index 4b74c6b55..8147b39e8 100644
--- a/sources/pyside2/PySide2/templates/core_common.xml
+++ b/sources/pyside2/PySide2/templates/core_common.xml
@@ -339,9 +339,16 @@
</template>
<template name="pyseq_to_cpplist_conversion">
- for (int i = 0, size = PySequence_Size(%in); i &lt; size; i++) {
-
- Shiboken::AutoDecRef pyItem(PySequence_GetItem(%in, i));
+ // PYSIDE-795: Turn all sequences into iterables.
+ Shiboken::AutoDecRef it(PyObject_GetIter(%in));
+ PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
+ for (;;) {
+ Shiboken::AutoDecRef pyItem(iternext(it));
+ if (pyItem.isNull()) {
+ if (PyErr_Occurred() &amp;&amp; PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ break;
+ }
%OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem);
%out &lt;&lt; cppItem;
}
@@ -358,10 +365,20 @@
</template>
<template name="pyseq_to_cppvector_conversion">
- int vectorSize = PySequence_Size(%in);
- %out.reserve(vectorSize);
- for (int idx = 0; idx &lt; vectorSize; ++idx) {
- Shiboken::AutoDecRef pyItem(PySequence_GetItem(%in, idx));
+ // PYSIDE-795: Turn all sequences into iterables.
+ if (PySequence_Check(%in)) {
+ int vectorSize = PySequence_Size(%in);
+ %out.reserve(vectorSize);
+ }
+ Shiboken::AutoDecRef it(PyObject_GetIter(%in));
+ PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
+ for (;;) {
+ Shiboken::AutoDecRef pyItem(iternext(it));
+ if (pyItem.isNull()) {
+ if (PyErr_Occurred() &amp;&amp; PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ break;
+ }
%OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem);
%out.push_back(cppItem);
}
diff --git a/sources/pyside2/PySide2/templates/gui_common.xml b/sources/pyside2/PySide2/templates/gui_common.xml
index f3e772a8c..96b4906ef 100644
--- a/sources/pyside2/PySide2/templates/gui_common.xml
+++ b/sources/pyside2/PySide2/templates/gui_common.xml
@@ -239,8 +239,10 @@
</template>
<template name="matrix_constructor">
- if (PySequence_Size(%PYARG_1) == %SIZE) {
- Shiboken::AutoDecRef fast(PySequence_Fast(%PYARG_1,
+ // PYSIDE-795: All PySequences can be made iterable with PySequence_Fast.
+ Shiboken::AutoDecRef seq(PySequence_Fast(%PYARG_1, "Can't turn into sequence"));
+ if (PySequence_Size(seq) == %SIZE) {
+ Shiboken::AutoDecRef fast(PySequence_Fast(seq,
"Failed to parse sequence on %TYPE constructor."));
float values[%SIZE];
for(int i=0; i &lt; %SIZE; i++) {