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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
// Copyright (C) 2025 Ford Motor Company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "pysiderephandler_p.h"
#include "pysidedynamicclass_p.h"
#include "pysidedynamicpod_p.h"
#include "pysidedynamiccommon_p.h"
#include <pep384ext.h>
#include <sbkerrors.h>
#include <sbkstring.h>
#include <sbktypefactory.h>
#include <signature.h>
#include <pysideutils.h>
#include <QtCore/qbuffer.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qmetaobject.h>
#include <QtRemoteObjects/qremoteobjectreplica.h>
#include <QtRemoteObjects/qremoteobjectpendingcall.h>
#include <private/qremoteobjectrepparser_p.h>
using namespace Qt::StringLiterals;
using namespace Shiboken;
/**
* @file pysiderephandler.cpp
* @brief This file contains the implementation of the PySideRepFile type and its
* associated methods for handling Qt Remote Objects in PySide6.
*
* The PySideRepFile type provides functionality to parse and handle Qt Remote Objects
* (QtRO) files, and dynamically generate Python types for QtRO sources, replicas, and
* PODs (Plain Old Data structures).
*
* The RepFile_tp_methods array defines the methods available on the PySideRepFile object:
* - source: Generates a dynamic Python type for a QtRO source class.
* - replica: Generates a dynamic Python type for a QtRO replica class.
* - pod: Generates a dynamic Python type for a QtRO POD class.
*
* When generating a source or replica type, the generateDynamicType function is
* used, creating a new Python type based on the generated QMetaObject, and adds
* method descriptors for the required methods. A QVariantList for the types
* properties is also created, populated with default values if set in the input
* .rep file.
*/
static QVariantList generateProperties(QMetaObject *meta, const ASTClass &astClass);
extern "C"
{
static PyObject *get_capsule_count()
{
return PyLong_FromLong(capsule_count);
}
// Code for the PySideRepFile type
static PyObject *RepFile_tp_string(PyObject *self);
static PyObject *RepFile_tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds);
static int RepFile_tp_init(PyObject *self, PyObject *args, PyObject *kwds);
static void RepFile_tp_free(void *self);
static void RepFile_tp_dealloc(PySideRepFile *self);
static PyObject *RepFile_get_pods(PySideRepFile *self, void * /*unused*/);
static PyObject *RepFile_get_replicas(PySideRepFile *self, void * /*unused*/);
static PyObject *RepFile_get_sources(PySideRepFile *self, void * /*unused*/);
bool instantiateFromDefaultValue(QVariant &variant, const QString &defaultValue);
static PyObject *cppToPython_POD_Tuple(const void *cppIn);
static void pythonToCpp_Tuple_POD(PyObject *pyIn, void *cppOut);
static PythonToCppFunc is_Tuple_PythonToCpp_POD_Convertible(PyObject *pyIn);
static PyGetSetDef RepFile_tp_getters[] = {
{"pod", reinterpret_cast<getter>(RepFile_get_pods), nullptr, "POD dictionary", nullptr},
{"replica", reinterpret_cast<getter>(RepFile_get_replicas), nullptr, "Replica dictionary", nullptr},
{"source", reinterpret_cast<getter>(RepFile_get_sources), nullptr, "Source dictionary", nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr} // Sentinel
};
static PyTypeObject *createRepFileType()
{
PyType_Slot PySideRepFileType_slots[] = {
{Py_tp_str, reinterpret_cast<void *>(RepFile_tp_string)},
{Py_tp_init, reinterpret_cast<void *>(RepFile_tp_init)},
{Py_tp_new, reinterpret_cast<void *>(RepFile_tp_new)},
{Py_tp_free, reinterpret_cast<void *>(RepFile_tp_free)},
{Py_tp_dealloc, reinterpret_cast<void *>(RepFile_tp_dealloc)},
{Py_tp_getset, reinterpret_cast<void *>(RepFile_tp_getters)},
{0, nullptr}
};
PyType_Spec PySideRepFileType_spec = {
"2:PySide6.QtRemoteObjects.RepFile",
sizeof(PySideRepFile),
0,
Py_TPFLAGS_DEFAULT,
PySideRepFileType_slots};
return SbkType_FromSpec(&PySideRepFileType_spec);
}
PyTypeObject *PySideRepFile_TypeF(void)
{
static auto *type = createRepFileType();
return type;
}
static PyObject *RepFile_tp_string(PyObject *self)
{
auto *cppSelf = reinterpret_cast<PySideRepFile *>(self);
QString result = QStringLiteral("RepFile(Classes: [%1], PODs: [%2])")
.arg(cppSelf->d->classes.join(", "_L1), cppSelf->d->pods.join(", "_L1));
return PyUnicode_FromString(result.toUtf8().constData());
}
static PyObject *RepFile_tp_new(PyTypeObject *subtype, PyObject * /* args */, PyObject * /* kwds */)
{
auto *me = PepExt_TypeCallAlloc<PySideRepFile>(subtype, 0);
auto *priv = new PySideRepFilePrivate;
priv->podDict = PyDict_New();
if (!priv->podDict) {
delete priv;
return nullptr;
}
priv->replicaDict = PyDict_New();
if (!priv->replicaDict) {
Py_DECREF(priv->podDict);
delete priv;
return nullptr;
}
priv->sourceDict = PyDict_New();
if (!priv->sourceDict) {
Py_DECREF(priv->podDict);
Py_DECREF(priv->replicaDict);
delete priv;
return nullptr;
}
me->d = priv;
return reinterpret_cast<PyObject *>(me);
}
static PyObject *RepFile_get_pods(PySideRepFile *self, void * /* closure */)
{
Py_INCREF(self->d->podDict);
return self->d->podDict;
}
static PyObject *RepFile_get_replicas(PySideRepFile *self, void * /* closure */)
{
Py_INCREF(self->d->replicaDict);
return self->d->replicaDict;
}
static PyObject *RepFile_get_sources(PySideRepFile *self, void * /* closure */)
{
Py_INCREF(self->d->sourceDict);
return self->d->sourceDict;
}
static void RepFile_tp_dealloc(PySideRepFile *self)
{
Py_XDECREF(self->d->podDict);
Py_XDECREF(self->d->replicaDict);
Py_XDECREF(self->d->sourceDict);
PepExt_TypeCallFree(reinterpret_cast<PyObject *>(self));
}
static int parseArgsToAST(PyObject *args, PySideRepFile *repFile)
{
// Verify args is a single string argument
if (PyTuple_Size(args) != 1 || !PyUnicode_Check(PyTuple_GetItem(args, 0))) {
PyErr_SetString(PyExc_TypeError, "RepFile constructor requires a single string argument");
return -1;
}
// Wrap contents into a QBuffer
const auto contents = PySide::pyStringToQString(PyTuple_GetItem(args, 0));
auto byteArray = contents.toUtf8();
QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
RepParser repparser(buffer);
if (!repparser.parse()) {
PyErr_Format(PyExc_RuntimeError, "Error parsing input, line %d: error: %s",
repparser.lineNumber(), qPrintable(repparser.errorString()));
auto lines = contents.split("\n"_L1);
auto lMin = std::max(1, repparser.lineNumber() - 2);
auto lMax = std::min(repparser.lineNumber() + 2, int(lines.size()));
// Print a few lines around the error
qWarning() << "Contents:";
for (int i = lMin; i <= lMax; ++i) {
if (i == repparser.lineNumber())
qWarning().nospace() << " line " << i << ": > " << lines.at(i - 1);
else
qWarning().nospace() << " line " << i << ": " << lines.at(i - 1);
}
return -1;
}
repFile->d->ast = repparser.ast();
return 0;
}
static const char *repName(QMetaObject *meta)
{
const int ind = meta->indexOfClassInfo(QCLASSINFO_REMOTEOBJECT_TYPE);
return ind >= 0 ? meta->classInfo(ind).value() : "<Invalid RemoteObject>";
}
static int RepFile_tp_init(PyObject *self, PyObject *args, PyObject * /* kwds */)
{
auto *cppSelf = reinterpret_cast<PySideRepFile *>(self);
if (parseArgsToAST(args, cppSelf) < 0)
return -1;
for (const auto &pod : std::as_const(cppSelf->d->ast.pods)) {
cppSelf->d->pods << pod.name;
auto *qobject = new QObject;
auto *meta = createAndRegisterMetaTypeFromPOD(pod, qobject);
if (!meta) {
delete qobject;
PyErr_Format(PyExc_RuntimeError, "Failed to create meta object for POD '%s'",
pod.name.toUtf8().constData());
return -1;
}
PyTypeObject *newType = createPodType(meta);
if (!newType) {
delete qobject;
PyErr_Print();
PyErr_Format(PyExc_RuntimeError, "Failed to create POD type for POD '%s'",
pod.name.toUtf8().constData());
return -1;
}
if (set_cleanup_capsule_attr_for_pointer(newType, "_qobject_capsule", qobject) < 0) {
delete qobject;
return -1;
}
PyDict_SetItemString(cppSelf->d->podDict, meta->className(),
reinterpret_cast<PyObject *>(newType));
Py_DECREF(newType);
}
if (PyErr_Occurred())
PyErr_Print();
for (const auto &cls : std::as_const(cppSelf->d->ast.classes)) {
cppSelf->d->classes << cls.name;
// Create Source type
{
auto *qobject = new QObject;
auto *meta = createAndRegisterSourceFromASTClass(cls, qobject);
if (!meta) {
delete qobject;
PyErr_Format(PyExc_RuntimeError, "Failed to create Source meta object for class '%s'",
cls.name.toUtf8().constData());
return -1;
}
auto properties = generateProperties(meta, cls);
// Check if an error occurred during generateProperties
if (PyErr_Occurred()) {
delete qobject;
return -1;
}
auto *propertiesPtr = new QVariantList(properties);
auto *pyCapsule = PyCapsule_New(propertiesPtr, nullptr, [](PyObject *capsule) {
delete reinterpret_cast<QVariantList *>(PyCapsule_GetPointer(capsule, nullptr));
});
PyTypeObject *newType = createDynamicClass(meta, pyCapsule);
if (!newType) {
delete qobject;
PyErr_Format(PyExc_RuntimeError,
"Failed to create Source Python type for class '%s'",
meta->className());
return -1;
}
if (set_cleanup_capsule_attr_for_pointer(newType, "_qobject_capsule", qobject) < 0) {
delete qobject;
return -1;
}
PyDict_SetItemString(cppSelf->d->sourceDict, repName(meta),
reinterpret_cast<PyObject *>(newType));
Py_DECREF(newType);
}
// Create Replica type
{
auto *qobject = new QObject;
auto *meta = createAndRegisterReplicaFromASTClass(cls, qobject);
if (!meta) {
delete qobject;
PyErr_Format(PyExc_RuntimeError,
"Failed to create Replica meta object for class '%s'",
qPrintable(cls.name));
return -1;
}
auto properties = generateProperties(meta, cls);
// Check if an error occurred during generateProperties
if (PyErr_Occurred()) {
delete qobject;
return -1;
}
auto *propertiesPtr = new QVariantList(properties);
auto *pyCapsule = PyCapsule_New(propertiesPtr, nullptr, [](PyObject *capsule) {
delete reinterpret_cast<QVariantList *>(PyCapsule_GetPointer(capsule, nullptr));
});
PyTypeObject *newType = createDynamicClass(meta, pyCapsule);
if (!newType) {
delete qobject;
PyErr_Format(PyExc_RuntimeError,
"Failed to create Replica Python type for class '%s'",
meta->className());
return -1;
}
if (set_cleanup_capsule_attr_for_pointer(newType, "_qobject_capsule", qobject) < 0) {
delete qobject;
return -1;
}
PyDict_SetItemString(cppSelf->d->replicaDict, repName(meta),
reinterpret_cast<PyObject *>(newType));
Py_DECREF(newType);
}
}
return 0;
}
static void RepFile_tp_free(void *self)
{
PySideRepFile *obj = reinterpret_cast<PySideRepFile*>(self);
delete obj->d;
}
/**
* @brief Sets the QVariant value based on the provided default value text.
*
* This function attempts to set the provided QVariant's value based on the
* provided text. It evaluates the text as a Python expression, the the python
* type associated with the provided QMetaType. It first retrieves the Python
* type object corresponding to the given QMetaType, then constructs a Python
* expression to instantiate the type with the default value. The expression is
* evaluated using PyRun_String, and the result is then set on the QVariant.
* Note: The variant is passed by reference and modified in place.
*
* @return True if the instantiation is successful, false otherwise.
*/
bool instantiateFromDefaultValue(QVariant &variant, const QString &defaultValue)
{
auto metaType = variant.metaType();
auto *pyType = Shiboken::Conversions::getPythonTypeObject(metaType.name());
if (!pyType) {
PyErr_Format(PyExc_TypeError, "Failed to find Python type for meta type: %s",
metaType.name());
return false;
}
// Evaluate the code
static PyObject *pyLocals = PyDict_New();
// Create the Python expression to evaluate
std::string code = std::string(pyType->tp_name) + '('
+ defaultValue.toUtf8().constData() + ')';
PyObject *pyResult = PyRun_String(code.c_str(), Py_eval_input, pyLocals, pyLocals);
if (!pyResult) {
Shiboken::Errors::Stash errorStash;
PyErr_Format(PyExc_TypeError,
"Failed to generate default value. Error: %s. Problematic code: %s",
Shiboken::String::toCString(PyObject_Str(errorStash.getException())), code.c_str());
errorStash.release();
Py_DECREF(pyLocals);
return false;
}
Conversions::SpecificConverter converter(metaType.name());
if (!converter) {
PyErr_Format(PyExc_TypeError, "Failed to find converter from Python type: %s to Qt type: %s",
pyResult->ob_type->tp_name, metaType.name());
Py_DECREF(pyResult);
return false;
}
converter.toCpp(pyResult, variant.data());
Py_DECREF(pyResult);
return true;
}
} // extern "C"
static QVariantList generateProperties(QMetaObject *meta, const ASTClass &astClass)
{
QVariantList properties;
auto propertyCount = astClass.properties.size();
properties.reserve(propertyCount);
for (auto i = 0; i < propertyCount; ++i) {
auto j = i + meta->propertyOffset(); // Corresponding property index in the meta object
auto metaProperty = meta->property(j);
auto metaType = metaProperty.metaType();
if (!metaType.isValid()) {
PyErr_Format(PyExc_RuntimeError, "Invalid meta type for property %d: %s", i,
astClass.properties[i].type.toUtf8().constData());
return {};
}
auto variant = QVariant(metaType);
if (auto defaultValue = astClass.properties[i].defaultValue; !defaultValue.isEmpty()) {
auto success = instantiateFromDefaultValue(variant, defaultValue);
if (!success) {
// Print a warning giving the property name, then propagate the error
qWarning() << "Failed to instantiate default value for property: "
<< metaProperty.name();
return {};
}
}
properties << variant;
}
return properties;
}
namespace PySide::RemoteObjects
{
static const char *RepFile_SignatureStrings[] = {
"PySide6.RemoteObjects.RepFile(self,content:str)",
nullptr}; // Sentinel
void init(PyObject *module)
{
if (InitSignatureStrings(PySideRepFile_TypeF(), RepFile_SignatureStrings) < 0)
return;
qRegisterMetaType<QRemoteObjectPendingCall>();
qRegisterMetaType<QRemoteObjectPendingCallWatcher>();
Py_INCREF(PySideRepFile_TypeF());
PyModule_AddObject(module, "RepFile", reinterpret_cast<PyObject *>(PySideRepFile_TypeF()));
// Add a test helper to verify type reference counting
static PyMethodDef get_capsule_count_def = {
"getCapsuleCount", // name of the function in Python
reinterpret_cast<PyCFunction>(get_capsule_count), // C function pointer
METH_NOARGS, // flags indicating parameters
"Returns the current count of PyCapsule objects" // docstring
};
PyModule_AddObject(module, "getCapsuleCount", PyCFunction_New(&get_capsule_count_def, nullptr));
}
} // namespace PySide::RemoteObjects
|