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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "pysidevariantutils.h"
#include "pysideutils.h"
#include <QtCore/qvariantmap.h>
#include <autodecref.h>
#include <sbkconverter.h>
#include <basewrapper.h>
using namespace Qt::StringLiterals;
static const char qVariantTypeName[] = "QVariant";
static void warnConverter(const char *name)
{
qWarning("Type converter for: %s not registered.", name);
}
// Helper converting each item of a non-empty list using the "QVariant" converter
static std::optional<QVariantList> pyListToVariantListHelper(PyObject *list, Py_ssize_t size)
{
Q_ASSERT(size > 0);
QVariantList result;
result.reserve(size);
Shiboken::Conversions::SpecificConverter converter(qVariantTypeName);
if (!converter) {
warnConverter(qVariantTypeName);
return std::nullopt;
}
for (Py_ssize_t i = 0; i < size; ++i) {
Shiboken::AutoDecRef pyItem(PySequence_GetItem(list, i));
QVariant item;
converter.toCpp(pyItem.object(), &item);
result.append(item);
}
return result;
}
// Helper checking for a sequence of Unicode objects
static bool isStringList(PyObject *list)
{
const Py_ssize_t size = PySequence_Size(list);
if (size == 0)
return false;
for (Py_ssize_t i = 0; i < size; ++i) {
Shiboken::AutoDecRef item(PySequence_GetItem(list, i));
if (PyUnicode_Check(item) == 0)
return false;
}
return true;
}
// Helper to convert to a QStringList
static std::optional<QStringList> listToStringList(PyObject *list)
{
static const char listType[] = "QList<QString>";
Shiboken::Conversions::SpecificConverter converter(listType);
if (!converter) {
warnConverter(listType);
return std::nullopt;
}
QStringList result;
converter.toCpp(list, &result);
return result;
}
// Helper to convert a non-empty, homogenous list using the converter of the first item
static QVariant convertToValueList(PyObject *list)
{
Q_ASSERT(PySequence_Size(list) >= 0);
Shiboken::AutoDecRef element(PySequence_GetItem(list, 0));
auto *type = reinterpret_cast<PyTypeObject *>(element.object());
QMetaType metaType = PySide::Variant::resolveMetaType(type);
if (!metaType.isValid())
return {};
const QByteArray listTypeName = QByteArrayLiteral("QList<") + metaType.name() + '>';
metaType = QMetaType::fromName(listTypeName);
if (!metaType.isValid())
return {};
Shiboken::Conversions::SpecificConverter converter(listTypeName);
if (!converter) {
warnConverter(listTypeName.constData());
return {};
}
QVariant var(metaType);
converter.toCpp(list, &var);
return var;
}
namespace PySide::Variant
{
QMetaType resolveMetaType(PyTypeObject *type)
{
if (!PyObject_TypeCheck(type, SbkObjectType_TypeF()))
return {};
const char *typeName = Shiboken::ObjectType::getOriginalName(type);
if (!typeName)
return {};
const bool valueType = '*' != typeName[qstrlen(typeName) - 1];
// Do not convert user type of value
if (valueType && Shiboken::ObjectType::isUserType(type))
return {};
QMetaType metaType = QMetaType::fromName(typeName);
if (metaType.isValid())
return metaType;
// Do not resolve types to value type
if (valueType)
return {};
// Find in base types. First check tp_bases, and only after check tp_base, because
// tp_base does not always point to the first base class, but rather to the first
// that has added any python fields or slots to its object layout.
// See https://mail.python.org/pipermail/python-list/2009-January/520733.html
if (type->tp_bases) {
const auto size = PyTuple_Size(type->tp_bases);
Py_ssize_t i = 0;
// PYSIDE-1887, PYSIDE-86: Skip QObject base class of QGraphicsObject;
// it needs to use always QGraphicsItem as a QVariant type for
// QGraphicsItem::itemChange() to work.
if (qstrcmp(typeName, "QGraphicsObject*") == 0)
++i;
for ( ; i < size; ++i) {
auto baseType = reinterpret_cast<PyTypeObject *>(PyTuple_GetItem(type->tp_bases, i));
const QMetaType derived = resolveMetaType(baseType);
if (derived.isValid())
return derived;
}
return {};
}
if (type->tp_base != nullptr)
return resolveMetaType(type->tp_base);
return {};
}
std::optional<QVariantList> pyListToVariantList(PyObject *list)
{
if (list == nullptr || PySequence_Check(list) == 0)
return std::nullopt;
const auto size = PySequence_Size(list);
if (size < 0) { // Some infinite (I/O read) thing? - bail out
PyErr_Clear();
return std::nullopt;
}
if (size == 0)
return QVariantList{};
return pyListToVariantListHelper(list, size);
}
QVariant convertToVariantList(PyObject *list)
{
const auto size = PySequence_Size(list);
if (size < 0) { // Some infinite (I/O read) thing? - bail out
PyErr_Clear();
return {};
}
if (size == 0)
return QVariantList{};
if (isStringList(list)) {
auto stringListO = listToStringList(list);
if (stringListO.has_value())
return {stringListO.value()};
}
if (QVariant valueList = convertToValueList(list); valueList.isValid())
return valueList;
if (auto vlO = pyListToVariantListHelper(list, size); vlO.has_value())
return vlO.value();
return {};
}
QVariant convertToVariantMap(PyObject *map)
{
if (map == nullptr || PyDict_Check(map) == 0)
return {};
QVariantMap result;
if (PyDict_Size(map) == 0)
return result;
Py_ssize_t pos = 0;
Shiboken::AutoDecRef keys(PyDict_Keys(map));
if (!isStringList(keys))
return {};
Shiboken::Conversions::SpecificConverter converter(qVariantTypeName);
if (!converter) {
warnConverter(qVariantTypeName);
return {};
}
PyObject *key{};
PyObject *value{};
while (PyDict_Next(map, &pos, &key, &value)) {
QVariant cppValue;
converter.toCpp(value, &cppValue);
result.insert(PySide::pyUnicodeToQString(key), cppValue);
}
return result;
}
} // namespace PySide::Variant
|