Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
aboutsummaryrefslogtreecommitdiffstats
blob: 941e38c6e162d0379966a08eed545e2be70fd70f (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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// 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

// Workaround to access protected functions. PySide builds with this, but
// since this is now a separate library, we need to add it too.

#include "pysidedynamiccommon_p.h"
#include "pysidedynamicclass_p.h"
#include "pysidecapsulemethod_p.h"
#include "pysiderephandler_p.h"

#include <basewrapper.h>
#include <sbkconverter.h>
#include <sbkstring.h>

#include <pyside_p.h>
#include <pysideproperty.h>
#include <pysideqobject.h>
#include <pysidesignal.h>
#include <pysideutils.h>

#include <QtCore/qmetaobject.h>
#include <QtCore/qvariantlist.h>

#include <QtRemoteObjects/qremoteobjectpendingcall.h>
#include <QtRemoteObjects/qremoteobjectreplica.h>

using namespace Shiboken;

class FriendlyReplica : public QRemoteObjectReplica
{
public:
    using QRemoteObjectReplica::send;
    using QRemoteObjectReplica::setProperties;
    using QRemoteObjectReplica::propAsVariant;
    using QRemoteObjectReplica::sendWithReply;
};

extern "C"
{

PyObject *propertiesAttr()
{
    static PyObject *const s = Shiboken::String::createStaticString("__PROPERTIES__");
    return s;
}

struct SourceDefs
{
    static PyTypeObject *getSbkType()
    {
        static PyTypeObject *sbkType =
            Shiboken::Conversions::getPythonTypeObject("QObject");
        return sbkType;
    }

    static PyObject *getBases()
    {
        static PyObject *bases = PyTuple_Pack(1, getSbkType());
        return bases;
    }

    static const char *getTypePrefix()
    {
        return "2:PySide6.QtRemoteObjects.DynamicSource.";
    }

    static int tp_init(PyObject *self, PyObject *args, PyObject *kwds)
    {
        static initproc initFunc = reinterpret_cast<initproc>(PepType_GetSlot(getSbkType(), Py_tp_init));
        int res = initFunc(self, args, kwds);
        if (res < 0) {
            PyErr_Print();
            return res;
        }

        // Get the properties from the type
        PyTypeObject *type = Py_TYPE(self);
        auto *pyProperties = PyObject_GetAttr(reinterpret_cast<PyObject *>(type), propertiesAttr());
        if (!pyProperties) {
            PyErr_SetString(PyExc_RuntimeError, "Failed to get properties from type");
            return -1;
        }
        // Add a copy of the properties to the object
        auto *propPtr = reinterpret_cast<QVariantList *>(PyCapsule_GetPointer(pyProperties, nullptr));
        auto *propertiesCopy = new QVariantList(*propPtr);
        PyObject *capsule = PyCapsule_New(propertiesCopy, nullptr, [](PyObject *capsule) {
                delete reinterpret_cast<QVariantList *>(PyCapsule_GetPointer(capsule, nullptr));
            });
        PyObject_SetAttr(self, propertiesAttr(), capsule);
        Py_DECREF(capsule);
        return res;
    }

    static PyObject *capsule_method_handler(PyObject *payload, PyObject *args)
    {
        auto *methodData = reinterpret_cast<CapsuleDescriptorData *>(PyCapsule_GetPointer(payload,
                                                                                          "Payload"));
        if (!methodData) {
            PyErr_SetString(PyExc_RuntimeError, "Invalid call to dynamic method.  Missing payload.");
            return nullptr;
        }
        PyObject *self = methodData->self;
        if (PyCapsule_IsValid(methodData->payload, "PropertyCapsule")) {
            // Handle property getter/setter against our hidden properties attribute
            auto *capsule = PyCapsule_GetPointer(methodData->payload, "PropertyCapsule");
            if (capsule) {
                auto *ob_dict = SbkObject_GetDict_NoRef(self);
                auto *propPtr = PyCapsule_GetPointer(PyDict_GetItem(ob_dict, propertiesAttr()),
                                                     nullptr);
                auto *currentProperties = reinterpret_cast<QVariantList *>(propPtr);
                auto *callData = reinterpret_cast<PropertyCapsule *>(capsule);
                if (callData->indexInObject < 0
                    || callData->indexInObject >= currentProperties->size()) {
                    PyErr_Format(PyExc_RuntimeError, "Unknown property method: %s",
                                 callData->name.constData());
                    return nullptr;
                }
                const QVariant &currentVariant = currentProperties->at(callData->indexInObject);

                // Handle getter
                if (PyTuple_Size(args) == 0)
                    return toPython(currentVariant);

                // Handle setter
                if (PyTuple_Size(args) != 1) {
                    PyErr_SetString(PyExc_TypeError, "Property setter takes exactly one argument");
                    return nullptr;
                }
                Conversions::SpecificConverter converter(currentVariant.metaType().name());
                QVariant variant{currentVariant.metaType()};
                auto metaType = currentVariant.metaType();
                if (metaType.flags().testFlag(QMetaType::IsEnumeration)) {
                    converter.toCpp(PyTuple_GetItem(args, 0), variant.data());
                    variant.convert(metaType);
                } else {
                    converter.toCpp(PyTuple_GetItem(args, 0), variant.data());
                }
                if (PyErr_Occurred()) // POD conversion can produce an error
                    return nullptr;
                if (variant == currentVariant)
                    Py_RETURN_NONE;

                currentProperties->replace(callData->indexInObject, variant);
                // Get the QMetaObject and emit the property changed signal if there is one
                const auto *metaObject = PySide::retrieveMetaObject(self);
                auto metaProperty = metaObject->property(callData->propertyIndex);
                if (metaProperty.hasNotifySignal()) {
                    // We know our custom types don't have multiple cpp objects
                    void *cptr = reinterpret_cast<SbkObject *>(self)->d->cptr[0];
                    auto *qObject = reinterpret_cast<QObject *>(cptr);
                    void *_args[] = {nullptr, variant.data()};
                    QMetaObject::activate(qObject, metaProperty.notifySignalIndex(), _args);
                }
                Py_RETURN_NONE;
            }
        }
        if (PyCapsule_IsValid(methodData->payload, "MethodCapsule")) {
            auto *capsule = PyCapsule_GetPointer(methodData->payload, "MethodCapsule");
            auto *callData = reinterpret_cast<MethodCapsule *>(capsule);
            if (callData->name.startsWith("push") && callData->name.size() > 4) {
                const auto *metaObject = PySide::retrieveMetaObject(self);
                // The convention for QtRO is if a property is named "something" and uses
                // push, the name of the push method will be "pushSomething". But it is
                // possible the name would be "Something", so we need to check upper
                // and lower case.
                auto name = callData->name.sliced(4);
                auto index = metaObject->indexOfProperty(name.constData());
                if (index < 0) {
                    name[0] = tolower(name[0]);  // Try lower case
                    index = metaObject->indexOfProperty(name.constData());
                }
                // It is possible a .rep names a Slot "push" or "pushSomething" that
                // isn't generated for a property. Let that fall through to regular
                // method handling.
                if (index >= 0) {
                    // Call the custom descriptor's set method
                    auto result = PyObject_SetAttrString(self, name.constData(),
                                                         PyTuple_GetItem(args, 0));
                    if (result < 0) {
                        PyErr_Print();
                        return nullptr;
                    }
                    Py_RETURN_NONE;
                }
            }
            // TODO: This doesn't do much, as it is "eaten" by a PyError_Print in
            // SignalManager::handleMetaCallError()
            // Is there a better way to address slots that need to be implemented?
            PyErr_Format(PyExc_NotImplementedError, "** The method %s is not implemented",
                         callData->name.constData());
            return nullptr;
        }

        PyErr_SetString(PyExc_RuntimeError, "Unknown capsule type");
        return nullptr;
    }
};

struct ReplicaDefs
{
    static PyTypeObject *getSbkType()
    {
        static PyTypeObject *sbkType =
            Shiboken::Conversions::getPythonTypeObject("QRemoteObjectReplica");
        return sbkType;
    }

    static PyObject *getBases()
    {
        static PyObject *bases = PyTuple_Pack(1, getSbkType());
        return bases;
    }

    static const char *getTypePrefix()
    {
        return "2:PySide6.QtRemoteObjects.DynamicReplica.";
    }

    static int tp_init(PyObject *self, PyObject *args, PyObject *kwds)
    {
        static initproc initFunc = reinterpret_cast<initproc>(PepType_GetSlot(getSbkType(),
                                                                              Py_tp_init));
        QRemoteObjectReplica *replica = nullptr;
        if (PyTuple_Size(args) == 0) {
            if (initFunc(self, args, kwds) < 0)
                return -1;
            Shiboken::Conversions::pythonToCppPointer(getSbkType(), self, &replica);
        } else {  // Process replica with arguments passed from the added node.acquire method
            PyObject *node = nullptr;
            PyObject *constructorType = nullptr;
            PyObject *name = nullptr;
            static PyTypeObject *nodeType = Shiboken::Conversions::getPythonTypeObject("QRemoteObjectNode");
            if (!PyArg_UnpackTuple(args, "Replica.__init__", 2, 3, &node, &constructorType, &name) ||
                !PySide::inherits(Py_TYPE(node), nodeType->tp_name)) {
                PyErr_SetString(PyExc_TypeError,
                                "Replicas can be initialized with no arguments or by node.acquire only");
                return -1;
            }
            static auto *constructorArgs = PyTuple_Pack(1, constructorType);
            if (initFunc(self, constructorArgs, kwds) < 0)
                return -1;
            if (name)
                PyObject_CallMethod(self, "initializeNode", "OO", node, name);
            else
                PyObject_CallMethod(self, "initializeNode", "O", node);
            Shiboken::Conversions::pythonToCppPointer(getSbkType(), self, &replica);
        }
        if (!replica) {
            PyErr_SetString(PyExc_RuntimeError, "Failed to initialize replica");
            return -1;
        }
        // Get the properties from the type
        PyTypeObject *type = Py_TYPE(self);
        auto *pyProperties = PyObject_GetAttr(reinterpret_cast<PyObject *>(type), propertiesAttr());
        if (!pyProperties) {
            PyErr_SetString(PyExc_RuntimeError, "Failed to get properties from type");
            return -1;
        }
        // Make a copy of the properties and set them on the replica
        auto *propPtr = reinterpret_cast<QVariantList *>(PyCapsule_GetPointer(pyProperties, nullptr));
        auto propertiesCopy = QVariantList(*propPtr);
        static_cast<FriendlyReplica *>(replica)->setProperties(std::move(propertiesCopy));
        return 0;
    }

    static PyObject *capsule_method_handler(PyObject *payload, PyObject *args)
    {
        auto *methodData = reinterpret_cast<CapsuleDescriptorData *>(PyCapsule_GetPointer(payload,
                                                                                          "Payload"));
        if (!methodData) {
            PyErr_SetString(PyExc_RuntimeError, "Invalid call to dynamic method.  Missing payload.");
            return nullptr;
        }
        PyObject *self = methodData->self;
        QRemoteObjectReplica *replica = nullptr;
        Shiboken::Conversions::pythonToCppPointer(getSbkType(), self, &replica);
        if (PyCapsule_IsValid(methodData->payload, "PropertyCapsule")) {
            auto *capsule = PyCapsule_GetPointer(methodData->payload, "PropertyCapsule");
            if (capsule) {
                auto *callData = reinterpret_cast<PropertyCapsule *>(capsule);
                QVariant currentVariant = static_cast<FriendlyReplica *>(replica)->propAsVariant(callData->indexInObject);

                // Handle getter
                if (PyTuple_Size(args) == 0) // Getter
                    return toPython(currentVariant);

                // Handle setter - currentVariant is a copy, so we can modify it
                if (PyTuple_Size(args) != 1) {
                    PyErr_SetString(PyExc_TypeError,
                                    "Property setter takes exactly one argument");
                    return nullptr;
                }
                Conversions::SpecificConverter converter(currentVariant.metaType().name());
                auto metaType = currentVariant.metaType();
                if (metaType.flags().testFlag(QMetaType::IsEnumeration)) {
                    converter.toCpp(PyTuple_GetItem(args, 0), currentVariant.data());
                    currentVariant.convert(metaType);
                } else {
                    converter.toCpp(PyTuple_GetItem(args, 0), currentVariant.data());
                }
                if (PyErr_Occurred()) // POD conversion can produce an error
                    return nullptr;
                QVariantList _args{currentVariant};
                static_cast<FriendlyReplica *>(replica)->send(QMetaObject::WriteProperty, callData->propertyIndex, _args);
                Py_RETURN_NONE;
            }
        }
        if (PyCapsule_IsValid(methodData->payload, "MethodCapsule")) {
            auto *capsule = PyCapsule_GetPointer(methodData->payload, "MethodCapsule");
            if (capsule) {
                auto *callData = reinterpret_cast<MethodCapsule *>(capsule);
                if (PyTuple_Size(args) != callData->argumentTypes.size()) {
                    PyErr_SetString(PyExc_TypeError,
                                    "Method called with incorrect number of arguments");
                    return nullptr;
                }
                QVariantList _args;
                static Conversions::SpecificConverter argsConverter("QVariantList");
                argsConverter.toCpp(args, &_args);
                if (PyErr_Occurred()) // POD conversion can produce an error
                    return nullptr;
                if (!callData->returnType.isValid() ||
                    (callData->returnType.isValid() && callData->returnType.id() == QMetaType::Void)) {
                    static_cast<FriendlyReplica *>(replica)->send(QMetaObject::InvokeMetaMethod, callData->methodIndex, _args);
                    Py_RETURN_NONE;
                }
                QRemoteObjectPendingCall *cppResult = new QRemoteObjectPendingCall;
                *cppResult = static_cast<FriendlyReplica *>(replica)->sendWithReply(QMetaObject::InvokeMetaMethod,
                                                    callData->methodIndex, _args);
                static PyTypeObject *baseType =
                    Shiboken::Conversions::getPythonTypeObject("QRemoteObjectPendingCall");
                Q_ASSERT(baseType);
                auto *pyResult = Shiboken::Object::newObject(baseType, cppResult, true, true);
                return pyResult;
            }
        }

        PyErr_SetString(PyExc_RuntimeError, "Unknown capsule type");
        return nullptr;
    }
};

static int DynamicType_traverse(PyObject *self, visitproc visit, void *arg)
{
    auto traverseProc = reinterpret_cast<traverseproc>(PepType_GetSlot(SbkObject_TypeF(),
                                                                       Py_tp_traverse));
    return traverseProc(self, visit, arg);
}

static int DynamicType_clear(PyObject *self)
{
    auto clearProc = reinterpret_cast<inquiry>(PepType_GetSlot(SbkObject_TypeF(), Py_tp_clear));
    return clearProc(self);
}

static PyMethodDef DynamicClass_methods[] = {
    {"get_enum", reinterpret_cast<PyCFunction>(DynamicType_get_enum), METH_O | METH_CLASS,
     "Get enum type by name"},
    {nullptr, nullptr, 0, nullptr}
};

static PyType_Slot DynamicClass_slots[] = {
    {Py_tp_base,        nullptr}, // inserted by introduceWrapperType
    {Py_tp_init,        nullptr}, // inserted by createDynamicType
    {Py_tp_traverse,    reinterpret_cast<void *>(DynamicType_traverse)},
    {Py_tp_clear,       reinterpret_cast<void *>(DynamicType_clear)},
    {Py_tp_methods,     reinterpret_cast<void *>(DynamicClass_methods)},
    {0, nullptr}
};

} // extern "C"

template <typename T, typename BaseType>
PyTypeObject *createDynamicClassImpl(QMetaObject *meta)
{
    DynamicClass_slots[1].pfunc = reinterpret_cast<void*>(T::tp_init);

    auto fullTypeName = QByteArray{T::getTypePrefix()} + meta->className();
    PyType_Spec spec = {
        fullTypeName.constData(),
        0,
        0,
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
        DynamicClass_slots
    };

    auto type = Shiboken::ObjectType::introduceWrapperType(
        reinterpret_cast<PyObject *>(PySideRepFile_TypeF()),
        meta->className(),
        meta->className(),
        &spec,
        &Shiboken::callCppDestructor<BaseType>,
        T::getBases(),
        Shiboken::ObjectType::WrapperFlags::InternalWrapper);

    auto *self = reinterpret_cast<PyObject *>(type);
    if (create_managed_py_enums(self, meta) < 0)
        return nullptr;

    PySide::Signal::registerSignals(type, meta);
    Shiboken::ObjectType::setSubTypeInitHook(type, &PySide::initQObjectSubType);
    PySide::initDynamicMetaObject(type, meta, 0);  // Size 0?

    PyMethodDef method = {
        nullptr,
        reinterpret_cast<PyCFunction>(T::capsule_method_handler),
        METH_VARARGS,
        nullptr
    };
    for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
        // Create a PropertyCapsule for each property to store the info needed for
        // the handler. Assign the __get__ and (if needed) __set__ attributes to a
        // PySideProperty which becomes the attribute set on the new type.
        auto metaProperty = meta->property(i);
        PyObject *kwds = PyDict_New();
        auto metaType = metaProperty.metaType();
        auto *pyPropertyType = PyUnicode_FromString(metaType.name());
        PyDict_SetItemString(kwds, "type", pyPropertyType);
        Py_DECREF(pyPropertyType);

        method.ml_name = metaProperty.name();
        auto *pc = new PropertyCapsule{metaProperty.name(), i, i - meta->propertyOffset()};
        auto capsule = PyCapsule_New(pc, "PropertyCapsule", [](PyObject *capsule) {
            delete static_cast<PropertyCapsule *>(PyCapsule_GetPointer(capsule, "PropertyCapsule"));
        });
        auto capsulePropObject = make_capsule_property(&method, capsule,
                                                       metaProperty.isWritable());
        PyObject *fget = PyObject_GetAttrString(capsulePropObject, "__get__");
        PyDict_SetItemString(kwds, "fget", fget);
        if (metaProperty.isWritable()) {
            PyObject *fset = PyObject_GetAttrString(capsulePropObject, "__set__");
            PyDict_SetItemString(kwds, "fset", fset);
            if (metaProperty.hasNotifySignal()) {
                auto nameString = metaProperty.notifySignal().name();
                auto *notify = PyObject_GetAttrString(reinterpret_cast<PyObject *>(type),
                                                      nameString.constData());
                PyDict_SetItemString(kwds, "notify", notify);
            }
        }
        PyObject *pyProperty = PyObject_Call(reinterpret_cast<PyObject *>(PySideProperty_TypeF()),
                                             PyTuple_New(0), kwds);
        if (PyObject_SetAttrString(reinterpret_cast<PyObject *>(type),
                                   metaProperty.name(), pyProperty) < 0) {
            return nullptr;
        }
        Py_DECREF(pyProperty);
    }
    for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) {
        // Create a CapsuleMethod for each Slot method to store the info needed
        // for the handler.
        auto metaMethod = meta->method(i);
        // Note: We are creating our custom metatype ourselves, which makes our added
        // (non-signal), methods return QMetaMethod::MethodType::Method, not
        // MethodType::Slot. This is fine, we just need to create a CapsuleMethod
        // for those methods.
        if (metaMethod.methodType() == QMetaMethod::MethodType::Signal)
            continue;
        auto name = metaMethod.name();
        method.ml_name = name.constData();
        QList<QMetaType> argumentTypes;
        for (int j = 0; j < metaMethod.parameterCount(); ++j)
            argumentTypes << metaMethod.parameterMetaType(j);
        MethodCapsule *capsuleData = new MethodCapsule{metaMethod.name(),
                                                       metaMethod.methodIndex(),
                                                       std::move(argumentTypes),
                                                       metaMethod.returnMetaType()};
        add_capsule_method_to_type(type, &method,
                                   PyCapsule_New(capsuleData, "MethodCapsule",
                                                 [](PyObject *capsule) {
            delete reinterpret_cast<MethodCapsule *>(PyCapsule_GetPointer(capsule, "MethodCapsule"));
        }));
    }

    return type;
}

PyTypeObject *createDynamicClass(QMetaObject *meta, PyObject *properties_capsule)
{
    bool isSource;
    if (strncmp(meta->superClass()->className(), "QObject", 7) == 0) {
        isSource = true;
    } else if (strncmp(meta->superClass()->className(), "QRemoteObjectReplica", 20) == 0) {
        isSource = false;
    } else {
        PyErr_SetString(PyExc_RuntimeError,
                        "Dynamic type must be a subclass of QObject or QRemoteObjectReplica");
        return nullptr;
    }

    PyTypeObject *newType = nullptr;

    if (isSource)
        newType = createDynamicClassImpl<SourceDefs, QObject>(meta);
    else
        newType = createDynamicClassImpl<ReplicaDefs, QRemoteObjectReplica>(meta);

    // Add the properties to the new type as an attribute
    if (PyObject_SetAttr(reinterpret_cast<PyObject *>(newType), propertiesAttr(),
                         properties_capsule) < 0) {
        Py_DECREF(newType);
        return nullptr;
    }

    return newType;
}