Skip to content
Permalink
Browse files

Fix sip conversion from QgsFields to QList<QgsField> (fixes #9208)

  • Loading branch information
jef-n committed Oct 17, 2015
1 parent 9686735 commit 3e0badcb550731a773050dddc210e71328246db3
Showing with 106 additions and 2 deletions.
  1. +104 −0 python/core/conversions.sip
  2. +2 −2 python/core/qgsfield.sip
@@ -1834,6 +1834,110 @@ template <TYPE>
%End
};

// QList<QgsField> is implemented as a Python list of QgsField.
%MappedType QList<QgsField> /DocType="list-of-qgsfield"/
{
%TypeHeaderCode
#include <qgsfield.h>
%End

%ConvertFromTypeCode
PyObject *l = PyList_New(sipCpp->size());

if (!l)
return 0;

for (int i = 0; i < sipCpp->size(); ++i)
{
QgsField *t = new QgsField(sipCpp->at(i));
PyObject *tobj = sipConvertFromNewType(t, sipType_QgsField, sipTransferObj);

if (!tobj)
{
delete t;
Py_DECREF(l);

return 0;
}

PyList_SET_ITEM(l, i, tobj);
}

return l;
%End

%ConvertToTypeCode
PyObject *iter = PyObject_GetIter(sipPy);

if (!sipIsErr)
{
Py_XDECREF(iter);

return (iter
#if PY_MAJOR_VERSION < 3
&& !PyString_Check(sipPy)
#endif
&& !PyUnicode_Check(sipPy));
}

if (!iter)
{
*sipIsErr = 1;

return 0;
}

QList<QgsField> *ql = new QList<QgsField>;

for (SIP_SSIZE_T i = 0; ; ++i)
{
PyErr_Clear();
PyObject *itm = PyIter_Next(iter);

if (!itm)
{
if (PyErr_Occurred())
{
delete ql;
Py_DECREF(iter);
*sipIsErr = 1;

return 0;
}

break;
}

int state;
QgsField *t = reinterpret_cast<QgsField *>( sipForceConvertToType(itm, sipType_QgsField, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));

if (*sipIsErr)
{
PyErr_Format(PyExc_TypeError,
"index %ld has type '%s' but 'QgsField' is expected",
(long) i, Py_TYPE(itm)->tp_name);

Py_DECREF(itm);
delete ql;
Py_DECREF(iter);

return 0;
}

ql->append(*t);

sipReleaseType(t, sipType_QgsField, state);
Py_DECREF(itm);
}

Py_DECREF(iter);

*sipCppPtr = ql;

return sipGetState(sipTransferObj);
%End
};

%If (QVECTORINT_CONVERSION)
// QVector<int> is implemented as a Python list of integers.
%MappedType QVector<int> /DocType="list-of-int"/
@@ -249,13 +249,13 @@ class QgsFields

//! Get field at particular index (must be in range 0..N-1)
// const QgsField& operator[]( int i ) const;
QgsField& operator[](int i) /Factory/;
QgsField& operator[](int i);
%MethodCode
SIP_SSIZE_T idx = sipConvertFromSequenceIndex(a0, sipCpp->count());
if (idx < 0)
sipIsErr = 1;
else
sipRes = new QgsField(sipCpp->operator[](idx));
sipRes = &(*sipCpp)[idx];
%End

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

1 comment on commit 3e0badc

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson commented on 3e0badc Oct 21, 2015

Please sign in to comment.
You can’t perform that action at this time.