blob: b2dc61952362cecfc4c6e4e5f6d332467830e515 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
inline bool Converter< QString >::isConvertible(PyObject* pyobj)
{
return PyString_Check(pyobj)
|| PyUnicode_Check(pyobj)
|| SbkQByteArray_Check(pyobj)
|| SbkQLatin1String_Check(pyobj)
#if PY_VERSION_HEX < 0x03000000
|| (pyobj->ob_type->tp_as_buffer
&& PyType_HasFeature(pyobj->ob_type, Py_TPFLAGS_HAVE_GETCHARBUFFER)
&& pyobj->ob_type->tp_as_buffer->bf_getcharbuffer)
#endif
|| SbkQChar_Check(pyobj);
}
inline QString Converter< QString >::toCpp(PyObject* pyobj)
{
if (SbkQChar_Check(pyobj)) {
return QString(Converter< QChar >::toCpp(pyobj));
} else if (SbkQByteArray_Check(pyobj)) {
return QString(Converter< QByteArray >::toCpp(pyobj));
} else if (SbkQLatin1String_Check(pyobj)) {
return QString(Converter< QLatin1String >::toCpp(pyobj));
} else if (PyUnicode_Check(pyobj)) {
Py_UNICODE* unicode = PyUnicode_AS_UNICODE(pyobj);
#if defined(Py_UNICODE_WIDE)
// cast as Py_UNICODE can be a different type
return QString::fromUcs4(reinterpret_cast<const uint*>(unicode));
#else
return QString::fromUtf16(unicode, PyUnicode_GET_SIZE(pyobj));
#endif
} else if (PyString_Check(pyobj)) {
return QString(Converter< char * >::toCpp(pyobj));
}
#if PY_VERSION_HEX < 0x03000000
// Support for buffer objects on QString constructor
else if (pyobj->ob_type->tp_as_buffer
&& PyType_HasFeature(pyobj->ob_type, Py_TPFLAGS_HAVE_GETCHARBUFFER)
&& pyobj->ob_type->tp_as_buffer->bf_getcharbuffer) {
QByteArray data;
PyBufferProcs* bufferProcs = pyobj->ob_type->tp_as_buffer;
int segments = bufferProcs->bf_getsegcount(pyobj, 0);
for (int i = 0; i < segments; ++i) {
char* segmentData;
int length = bufferProcs->bf_getcharbuffer(pyobj, i, &segmentData);
if (length == -1)
break;
data.append(segmentData, length);
}
return QString(data);
}
#endif
return *SbkQString_cptr(pyobj);
}
|