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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
// Copyright (C) 2020 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 "pysideqenum.h"
#include <autodecref.h>
#include <sbkconverter.h>
#include <sbkenum.h>
#include <sbkstaticstrings.h>
#include <sbkstring.h>
#include <map>
#include <QtCore/qmetatype.h>
#include <QtCore/qdebug.h>
#include <QtCore/qlist.h>
///////////////////////////////////////////////////////////////
//
// PYSIDE-957: Create QEnum dynamically from Python Enum
//
//
extern "C" {
using namespace Shiboken;
static PyObject *analyzePyEnum(PyObject *pyenum)
{
/*
* This is the straight-forward implementation of QEnum/QFlag. It does no
* longer create an equivalent Qt enum but takes the Python enum as-is.
*
* It parses an Enum/Flag derived Python enum completely so that
* registering can be done without error checks. This would be impossible
* in MetaObjectBuilderPrivate::parsePythonType.
*/
AutoDecRef members(PyObject_GetAttr(pyenum, Shiboken::PyMagicName::members()));
if (members.isNull())
return nullptr;
AutoDecRef items(PyMapping_Items(members));
if (items.isNull())
return nullptr;
int iflag = PySide::QEnum::isFlag(pyenum);
if (iflag < 0)
return nullptr;
Py_ssize_t nr_items = PySequence_Length(items);
if (nr_items < 0)
return nullptr;
for (Py_ssize_t idx = 0; idx < nr_items; ++idx) {
AutoDecRef item(PySequence_GetItem(items, idx));
if (item.isNull())
return nullptr;
// The item should be a 2-element sequence of the key name and an
// object containing the value.
AutoDecRef key(PySequence_GetItem(item, 0));
AutoDecRef member(PySequence_GetItem(item, 1));
if (key.isNull() || member.isNull())
return nullptr;
if (!Shiboken::String::check(key)) {
// '%.200s' is the safety stringbuffer size of most CPython functions.
PyErr_Format(PyExc_TypeError,
"QEnum expected a string mapping as __members__, got '%.200s'",
Py_TYPE(key)->tp_name);
return nullptr;
}
// Get the value.
AutoDecRef value(PyObject_GetAttr(member, Shiboken::PyName::value()));
if (value.isNull())
return nullptr;
if (!PyLong_Check(value)) {
PyErr_Format(PyExc_TypeError,
"QEnum expected an int value as '%.200s', got '%.200s'",
Shiboken::String::toCString(key), Py_TYPE(value)->tp_name);
return nullptr;
}
}
Py_RETURN_NONE;
}
static Py_ssize_t get_lineno()
{
auto *frame = reinterpret_cast<PyObject *>(PyEval_GetFrame()); // borrowed ref
AutoDecRef ob_lineno(PyObject_GetAttr(frame, Shiboken::PyName::f_lineno()));
if (ob_lineno.isNull() || !PyLong_Check(ob_lineno))
return -1;
return PyLong_AsSsize_t(ob_lineno);
}
static bool is_module_code()
{
auto *frame = reinterpret_cast<PyObject *>(PyEval_GetFrame()); // borrowed ref
AutoDecRef ob_code(PyObject_GetAttr(frame, Shiboken::PyName::f_code()));
if (ob_code.isNull())
return false;
AutoDecRef ob_name(PyObject_GetAttr(ob_code, Shiboken::PyName::co_name()));
if (ob_name.isNull())
return false;
const char *codename = Shiboken::String::toCString(ob_name);
return strcmp(codename, "<module>") == 0;
}
} // extern "C"
// Helper code for dynamically creating QMetaType's for @QEnum
template <class UnderlyingInt>
static void defaultCtr(const QtPrivate::QMetaTypeInterface *, void *addr)
{
auto *i = reinterpret_cast<UnderlyingInt *>(addr);
*i = 0;
}
template <class UnderlyingInt>
static void debugOp(const QtPrivate::QMetaTypeInterface *mti, QDebug &debug, const void *addr)
{
const auto value = *reinterpret_cast<const UnderlyingInt *>(addr);
QDebugStateSaver saver(debug);
debug << mti->name << '(';
if constexpr (std::is_unsigned<UnderlyingInt>()) {
debug << Qt::showbase << Qt::hex;
} else {
if (value >= 0)
debug << Qt::showbase << Qt::hex;
}
debug << value << ')';
}
template <class UnderlyingInt>
QMetaType createEnumMetaTypeHelper(const QByteArray &name)
{
auto *mti = new QtPrivate::QMetaTypeInterface {
1, // revision
ushort(std::alignment_of<UnderlyingInt>()),
sizeof(UnderlyingInt),
uint(QMetaType::fromType<UnderlyingInt>().flags() | QMetaType::IsEnumeration),
{}, // typeId
nullptr, // metaObjectFn
qstrdup(name.constData()),
defaultCtr<UnderlyingInt>,
nullptr, // copyCtr
nullptr, // moveCtr
nullptr, // dtor
QtPrivate::QEqualityOperatorForType<UnderlyingInt>::equals,
QtPrivate::QLessThanOperatorForType<UnderlyingInt>::lessThan,
debugOp<UnderlyingInt>,
nullptr, // dataStreamOut
nullptr, // dataStreamIn
nullptr // legacyRegisterOp
};
QMetaType metaType(mti);
metaType.id(); // enforce registration
return metaType;
}
namespace PySide::QEnum {
static std::map<int, PyObject *> enumCollector;
int isFlag(PyObject *obType)
{
/*
* Find out if this is an Enum or a Flag derived class.
* It checks also if things come from the enum module and if it is
* an Enum or Flag class at all.
*
* The function is called in MetaObjectBuilderPrivate::parsePythonType
* again to obtain the flag value.
*/
int res = enumIsFlag(obType);
if (res < 0) {
auto *type = reinterpret_cast<PyTypeObject *>(obType);
PyErr_Format(PyExc_TypeError, "type %.200s does not inherit from 'Enum' or 'Flag'",
type->tp_name);
return -1;
}
return bool(res);
}
PyObject *QEnumMacro(PyObject *pyenum, bool flag)
{
/*
* This is the official interface of 'QEnum'. It first calls 'analyzePyEnum'.
* When called as toplevel enum, it simply returns after some checks.
* Otherwise, 'pyenum' is stored for later use by the meta class registation.
*/
int computedFlag = isFlag(pyenum);
if (computedFlag < 0)
return nullptr;
if (bool(computedFlag) != flag) {
AutoDecRef name(PyObject_GetAttr(pyenum, PyMagicName::qualname()));
const auto *cname = String::toCString(name);
const char *e = "Enum";
const char *f = "Flag";
PyErr_Format(PyExc_TypeError, "expected '%s' but got '%s' (%.200s)",
flag ? f : e, flag ? e : f, cname);
return nullptr;
}
auto *ok = analyzePyEnum(pyenum);
if (ok == nullptr)
return nullptr;
if (is_module_code()) {
// This is a toplevel enum which we resolve immediately.
Py_INCREF(pyenum);
return pyenum;
}
Py_ssize_t lineno = get_lineno();
if (lineno < 0)
return nullptr;
// Handle the rest via line number and the meta class.
Py_INCREF(pyenum);
Py_XDECREF(enumCollector[lineno]);
enumCollector[lineno] = pyenum;
Py_RETURN_NONE;
}
std::vector<PyObject *> resolveDelayedQEnums(PyTypeObject *containerType)
{
/*
* This is the internal interface of 'QEnum'.
* It is called at the end of the meta class call 'SbkObjectType_tp_new' via
* MetaObjectBuilderPrivate::parsePythonType and resolves the collected
* Python Enum arguments. The result is then registered.
*/
if (enumCollector.empty())
return {};
auto *obContainerType = reinterpret_cast<PyObject *>(containerType);
Py_ssize_t lineno = get_lineno();
std::vector<PyObject *> result;
auto it = enumCollector.begin();
while (it != enumCollector.end()) {
int nr = it->first;
PyObject *pyenum = it->second;
if (nr >= lineno) {
AutoDecRef name(PyObject_GetAttr(pyenum, PyMagicName::name()));
if (name.isNull() || PyObject_SetAttr(obContainerType, name, pyenum) < 0)
return {};
result.push_back(pyenum);
it = enumCollector.erase(it);
} else {
++it;
}
}
return result;
}
QByteArray getTypeName(PyTypeObject *type)
{
if (!Shiboken::Enum::checkType(type))
return {};
Shiboken::AutoDecRef qualName(PyObject_GetAttr(reinterpret_cast<PyObject *>(type),
Shiboken::PyMagicName::qualname()));
QByteArray result = Shiboken::String::toCString(qualName.object());
result.replace(".", "::");
const auto metaType = QMetaType::fromName(result);
return metaType.isValid() && metaType.flags().testFlag(QMetaType::IsEnumeration)
? result : QByteArray{};
}
using GenericEnumType = int;
using GenericEnum64Type = unsigned long long;
struct GenericEnumRegistry
{
QList<PyTypeObject *> enumTypes;
QList<PyTypeObject *> enum64Types;
};
Q_GLOBAL_STATIC(GenericEnumRegistry, genericEnumTypeRegistry)
} // namespace PySide::QEnum
template <class IntType>
static inline void genericEnumPythonToCppTpl(PyObject *pyIn, void *cppOut)
{
const auto value = static_cast<IntType>(Shiboken::Enum::getValue(pyIn));
*reinterpret_cast<IntType *>(cppOut) = value;
}
template <class IntType>
static inline PyObject *genericEnumCppToPythonTpl(PyTypeObject *pyType, const void *cppIn)
{
const auto value = *reinterpret_cast<const IntType *>(cppIn);
return Shiboken::Enum::newItem(pyType, value);
}
extern "C"
{
// int
static void genericEnumPythonToCpp(PyObject *pyIn, void *cppOut)
{
genericEnumPythonToCppTpl<PySide::QEnum::GenericEnumType>(pyIn, cppOut);
}
static PythonToCppFunc isGenericEnumToCppConvertible(PyObject *pyIn)
{
if (PySide::QEnum::genericEnumTypeRegistry()->enumTypes.contains(Py_TYPE(pyIn)))
return genericEnumPythonToCpp;
return {};
}
static PyObject *genericEnumCppToPython(PyTypeObject *pyType, const void *cppIn)
{
return genericEnumCppToPythonTpl<PySide::QEnum::GenericEnumType>(pyType, cppIn);
}
// unsigned long long
static void genericEnumPythonToCpp64(PyObject *pyIn, void *cppOut)
{
genericEnumPythonToCppTpl<PySide::QEnum::GenericEnum64Type>(pyIn, cppOut);
}
static PythonToCppFunc isGenericEnumToCpp64Convertible(PyObject *pyIn)
{
if (PySide::QEnum::genericEnumTypeRegistry()->enum64Types.contains(Py_TYPE(pyIn)))
return genericEnumPythonToCpp64;
return {};
}
static PyObject *genericEnumCpp64ToPython(PyTypeObject *pyType, const void *cppIn)
{
return genericEnumCppToPythonTpl<PySide::QEnum::GenericEnum64Type>(pyType, cppIn);
}
} // extern "C"
namespace PySide::QEnum
{
// int
QMetaType createGenericEnumMetaType(const QByteArray &name, PyTypeObject *pyType)
{
SbkConverter *converter = Shiboken::Conversions::createConverter(pyType,
genericEnumCppToPython);
Shiboken::Conversions::addPythonToCppValueConversion(converter,
genericEnumPythonToCpp,
isGenericEnumToCppConvertible);
Shiboken::Conversions::registerConverterName(converter, name.constData());
Shiboken::Enum::setTypeConverter(pyType, converter, nullptr);
genericEnumTypeRegistry->enumTypes.append(pyType);
return createEnumMetaTypeHelper<GenericEnumType>(name);
}
// "unsigned long long"
QMetaType createGenericEnum64MetaType(const QByteArray &name, PyTypeObject *pyType)
{
SbkConverter *converter = Shiboken::Conversions::createConverter(pyType,
genericEnumCpp64ToPython);
Shiboken::Conversions::addPythonToCppValueConversion(converter,
genericEnumPythonToCpp64,
isGenericEnumToCpp64Convertible);
Shiboken::Conversions::registerConverterName(converter, name.constData());
Shiboken::Enum::setTypeConverter(pyType, converter, nullptr);
genericEnumTypeRegistry()->enum64Types.append(pyType);
return createEnumMetaTypeHelper<GenericEnum64Type>(name);
}
} // namespace PySide::QEnum
//
///////////////////////////////////////////////////////////////
|