Skip to content

Commit 3e0badc

Browse files
committed
Fix sip conversion from QgsFields to QList<QgsField> (fixes #9208)
1 parent 9686735 commit 3e0badc

2 files changed

Lines changed: 106 additions & 2 deletions

File tree

‎python/core/conversions.sip‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,110 @@ template <TYPE>
18341834
%End
18351835
};
18361836

1837+
// QList<QgsField> is implemented as a Python list of QgsField.
1838+
%MappedType QList<QgsField> /DocType="list-of-qgsfield"/
1839+
{
1840+
%TypeHeaderCode
1841+
#include <qgsfield.h>
1842+
%End
1843+
1844+
%ConvertFromTypeCode
1845+
PyObject *l = PyList_New(sipCpp->size());
1846+
1847+
if (!l)
1848+
return 0;
1849+
1850+
for (int i = 0; i < sipCpp->size(); ++i)
1851+
{
1852+
QgsField *t = new QgsField(sipCpp->at(i));
1853+
PyObject *tobj = sipConvertFromNewType(t, sipType_QgsField, sipTransferObj);
1854+
1855+
if (!tobj)
1856+
{
1857+
delete t;
1858+
Py_DECREF(l);
1859+
1860+
return 0;
1861+
}
1862+
1863+
PyList_SET_ITEM(l, i, tobj);
1864+
}
1865+
1866+
return l;
1867+
%End
1868+
1869+
%ConvertToTypeCode
1870+
PyObject *iter = PyObject_GetIter(sipPy);
1871+
1872+
if (!sipIsErr)
1873+
{
1874+
Py_XDECREF(iter);
1875+
1876+
return (iter
1877+
#if PY_MAJOR_VERSION < 3
1878+
&& !PyString_Check(sipPy)
1879+
#endif
1880+
&& !PyUnicode_Check(sipPy));
1881+
}
1882+
1883+
if (!iter)
1884+
{
1885+
*sipIsErr = 1;
1886+
1887+
return 0;
1888+
}
1889+
1890+
QList<QgsField> *ql = new QList<QgsField>;
1891+
1892+
for (SIP_SSIZE_T i = 0; ; ++i)
1893+
{
1894+
PyErr_Clear();
1895+
PyObject *itm = PyIter_Next(iter);
1896+
1897+
if (!itm)
1898+
{
1899+
if (PyErr_Occurred())
1900+
{
1901+
delete ql;
1902+
Py_DECREF(iter);
1903+
*sipIsErr = 1;
1904+
1905+
return 0;
1906+
}
1907+
1908+
break;
1909+
}
1910+
1911+
int state;
1912+
QgsField *t = reinterpret_cast<QgsField *>( sipForceConvertToType(itm, sipType_QgsField, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
1913+
1914+
if (*sipIsErr)
1915+
{
1916+
PyErr_Format(PyExc_TypeError,
1917+
"index %ld has type '%s' but 'QgsField' is expected",
1918+
(long) i, Py_TYPE(itm)->tp_name);
1919+
1920+
Py_DECREF(itm);
1921+
delete ql;
1922+
Py_DECREF(iter);
1923+
1924+
return 0;
1925+
}
1926+
1927+
ql->append(*t);
1928+
1929+
sipReleaseType(t, sipType_QgsField, state);
1930+
Py_DECREF(itm);
1931+
}
1932+
1933+
Py_DECREF(iter);
1934+
1935+
*sipCppPtr = ql;
1936+
1937+
return sipGetState(sipTransferObj);
1938+
%End
1939+
};
1940+
18371941
%If (QVECTORINT_CONVERSION)
18381942
// QVector<int> is implemented as a Python list of integers.
18391943
%MappedType QVector<int> /DocType="list-of-int"/

‎python/core/qgsfield.sip‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ class QgsFields
249249

250250
//! Get field at particular index (must be in range 0..N-1)
251251
// const QgsField& operator[]( int i ) const;
252-
QgsField& operator[](int i) /Factory/;
252+
QgsField& operator[](int i);
253253
%MethodCode
254254
SIP_SSIZE_T idx = sipConvertFromSequenceIndex(a0, sipCpp->count());
255255
if (idx < 0)
256256
sipIsErr = 1;
257257
else
258-
sipRes = new QgsField(sipCpp->operator[](idx));
258+
sipRes = &(*sipCpp)[idx];
259259
%End
260260

261261
//! Get field at particular index (must be in range 0..N-1)

0 commit comments

Comments
 (0)