Details
-
User Story
-
Resolution: Done
-
Not Evaluated
-
None
-
None
-
3db6c256b2083a7f6f734285e3cf95ad58514230 (pyside/tqtc-pyside-setup/dev)
Description
To convert Qt-types or C++ types containers to Python, we use different snippets and templates that are often quite similar, for example in
sources/pyside6/PySide6/templates/core_common.xml for transforming a C++ list to a Python list:
<template name="cpplist_to_pylist_conversion"> PyObject *%out = PyList_New(Py_ssize_t(%in.size())); Py_ssize_t idx = 0; for (auto it = %in.cbegin(), end = %in.cend(); it != end; ++it, ++idx) { const auto &cppItem = *it; PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); } return %out; </template>
and from a python sequence to a C++ list
<template name="pyseq_to_cpplist_conversion"> // PYSIDE-795: Turn all sequences into iterables. if (PyList_Check(%in)) { const Py_ssize_t size = PySequence_Size(%in); if (size > 10) (%out).reserve(size); } 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() && PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); break; } %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); %out << cppItem; } </template>
If another project is also handling std::list or other containers, the template needs to be copy around, it would be good to have a default conversion between vector, lists, arrays, etc into python lists and the same with std::pair and tuple to python tuple.
The same can be worked with maps and python dictionaries.
Attachments
Issue Links
- relates to
-
PYSIDE-174 container conversion ignores 'type', 'check'
- Closed
-
PYSIDE-2116 shiboken2 generates duplicate const for container INTYPE_#
- Closed
- mentioned in
-
Page Loading...