Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1666

Shiboken: Generic containers transformation

    XMLWordPrintable

Details

    • User Story
    • Resolution: Done
    • Not Evaluated
    • 6.3
    • None
    • Shiboken
    • None

    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 &amp;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 &gt; 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() &amp;&amp; PyErr_ExceptionMatches(PyExc_StopIteration))
                      PyErr_Clear();
                  break;
              }
              %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem);
              %out &lt;&lt; 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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              kleint Friedemann Kleint
              crmaurei Cristian Maureira-Fredes
              Vladimir Minenko Vladimir Minenko
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: