1// This is the SIP interface definition for the QStringList mapped type.
2//
3// Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4//
5// This file is part of PyQt5.
6//
7// This file may be used under the terms of the GNU General Public License
8// version 3.0 as published by the Free Software Foundation and appearing in
9// the file LICENSE included in the packaging of this file.  Please review the
10// following information to ensure the GNU General Public License version 3.0
11// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12//
13// If you do not wish to use this file under the terms of the GPL version 3.0
14// then you may purchase a commercial license.  For more information contact
15// info@riverbankcomputing.com.
16//
17// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19
20
21// Note that when we test the type of an object to see if it can be converted
22// to a collection we only check if it is iterable.  We do not check the
23// types of the contents - we assume we will be able to convert them when
24// requested.  This allows us to raise exceptions specific to an individual
25// object.  This approach doesn't work if there are overloads that can only be
26// distinguished by the types of the template arguments.  Currently there are
27// no such cases in PyQt5.  Note also that we don't consider strings to be
28// iterables.
29
30
31%MappedType QStringList
32        /TypeHintIn="Iterable[QString]", TypeHintOut="List[QString]",
33        TypeHintValue="[]"/
34{
35%TypeHeaderCode
36#include <qstringlist.h>
37%End
38
39%ConvertFromTypeCode
40    PyObject *l = PyList_New(sipCpp->size());
41
42    if (!l)
43        return 0;
44
45    for (int i = 0; i < sipCpp->size(); ++i)
46    {
47        QString *t = new QString(sipCpp->at(i));
48        PyObject *tobj = sipConvertFromNewType(t, sipType_QString,
49                sipTransferObj);
50
51        if (!tobj)
52        {
53            delete t;
54            Py_DECREF(l);
55
56            return 0;
57        }
58
59        PyList_SetItem(l, i, tobj);
60    }
61
62    return l;
63%End
64
65%ConvertToTypeCode
66    PyObject *iter = PyObject_GetIter(sipPy);
67
68    if (!sipIsErr)
69    {
70        PyErr_Clear();
71        Py_XDECREF(iter);
72
73        return (iter
74#if PY_MAJOR_VERSION < 3
75                && !PyString_Check(sipPy)
76#endif
77                && !PyUnicode_Check(sipPy));
78    }
79
80    if (!iter)
81    {
82        *sipIsErr = 1;
83
84        return 0;
85    }
86
87    QStringList *ql = new QStringList;
88
89    for (Py_ssize_t i = 0; ; ++i)
90    {
91        PyErr_Clear();
92        PyObject *itm = PyIter_Next(iter);
93
94        if (!itm)
95        {
96            if (PyErr_Occurred())
97            {
98                delete ql;
99                Py_DECREF(iter);
100                *sipIsErr = 1;
101
102                return 0;
103            }
104
105            break;
106        }
107
108        int state;
109        QString *t = reinterpret_cast<QString *>(
110                sipForceConvertToType(itm, sipType_QString, sipTransferObj,
111                        SIP_NOT_NONE, &state, sipIsErr));
112
113        if (*sipIsErr)
114        {
115            PyErr_Format(PyExc_TypeError,
116                    "index %zd has type '%s' but 'str' is expected", i,
117                    sipPyTypeName(Py_TYPE(itm)));
118
119            Py_DECREF(itm);
120            delete ql;
121            Py_DECREF(iter);
122
123            return 0;
124        }
125
126        ql->append(*t);
127
128        sipReleaseType(t, sipType_QString, state);
129        Py_DECREF(itm);
130    }
131
132    Py_DECREF(iter);
133
134    *sipCppPtr = ql;
135
136    return sipGetState(sipTransferObj);
137%End
138};
139