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
|
// Copyright (C) 2024 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 "dynamicslot_p.h"
#include "pysidestaticstrings.h"
#include "pysideutils.h"
#include "pysideweakref.h"
#include "signalmanager.h"
#include <autodecref.h>
#include <gilstate.h>
#include <pep384ext.h>
#include <QtCore/QDebug>
#include <QtCore/QtCompare>
#include <QtCore/QCoreApplication>
#include <QtCore/QHash>
#include <QtCore/QPointer>
namespace PySide
{
static void disconnectReceiver(PyObject *pythonSelf);
DynamicSlot::SlotType DynamicSlot::slotType(PyObject *callback)
{
if (PyMethod_Check(callback) != 0)
return SlotType::Method;
if (PySide::isCompiledMethod(callback) != 0)
return SlotType::CompiledMethod;
if (PyCFunction_Check(callback) != 0)
return SlotType::C_Function;
return SlotType::Callable;
}
// Simple callable slot.
class CallbackDynamicSlot : public DynamicSlot
{
Q_DISABLE_COPY_MOVE(CallbackDynamicSlot)
public:
explicit CallbackDynamicSlot(PyObject *callback) noexcept;
~CallbackDynamicSlot() override;
void call(const QByteArrayList ¶meterTypes, const char *returnType,
void **cppArgs) override;
void formatDebug(QDebug &debug) const override;
private:
PyObject *m_callback;
};
CallbackDynamicSlot::CallbackDynamicSlot(PyObject *callback) noexcept :
m_callback(callback)
{
Py_INCREF(m_callback);
}
CallbackDynamicSlot::~CallbackDynamicSlot()
{
Shiboken::GilState gil;
Py_DECREF(m_callback);
}
void CallbackDynamicSlot::call(const QByteArrayList ¶meterTypes, const char *returnType,
void **cppArgs)
{
SignalManager::callPythonMetaMethod(parameterTypes, returnType, cppArgs, m_callback);
}
void CallbackDynamicSlot::formatDebug(QDebug &debug) const
{
debug << "CallbackDynamicSlot(" << PySide::debugPyObject(m_callback) << ')';
}
// A method given by "signal.connect(foo.method)" is a temporarily created
// callable/partial function where self is bound as a first parameter.
// It can be split into self and the function. Keeping a reference on
// the callable itself would prevent object deletion. Instead, keep a
// reference on the function.
class MethodDynamicSlot : public DynamicSlot
{
Q_DISABLE_COPY_MOVE(MethodDynamicSlot)
public:
explicit MethodDynamicSlot(PyObject *function, PyObject *pythonSelf);
~MethodDynamicSlot() override;
PyObject *pythonSelf() const { return m_pythonSelf; }
void call(const QByteArrayList ¶meterTypes, const char *returnType,
void **cppArgs) override;
void formatDebug(QDebug &debug) const override;
private:
PyObject *m_function;
PyObject *m_pythonSelf;
};
MethodDynamicSlot::MethodDynamicSlot(PyObject *function, PyObject *pythonSelf) :
m_function(function),
m_pythonSelf(pythonSelf)
{
}
MethodDynamicSlot::~MethodDynamicSlot()
{
Shiboken::GilState gil;
Py_DECREF(m_function);
}
void MethodDynamicSlot::call(const QByteArrayList ¶meterTypes, const char *returnType,
void **cppArgs)
{
// create a callback based on method data
Shiboken::AutoDecRef callable(PepExt_Type_CallDescrGet(m_function,
m_pythonSelf, nullptr));
SignalManager::callPythonMetaMethod(parameterTypes, returnType,
cppArgs, callable.object());
}
void MethodDynamicSlot::formatDebug(QDebug &debug) const
{
debug << "MethodDynamicSlot(self=" << PySide::debugPyObject(m_pythonSelf)
<< ", function=" << PySide::debugPyObject(m_function) << ')';
}
// Store a weak reference on pythonSelf.
class TrackingMethodDynamicSlot : public MethodDynamicSlot
{
Q_DISABLE_COPY_MOVE(TrackingMethodDynamicSlot)
public:
explicit TrackingMethodDynamicSlot(PyObject *function, PyObject *pythonSelf,
PyObject *weakRef);
~TrackingMethodDynamicSlot() override;
void releaseWeakRef() { m_weakRef = nullptr; }
private:
PyObject *m_weakRef;
};
TrackingMethodDynamicSlot::TrackingMethodDynamicSlot(PyObject *function, PyObject *pythonSelf,
PyObject *weakRef) :
MethodDynamicSlot(function, pythonSelf),
m_weakRef(weakRef)
{
}
TrackingMethodDynamicSlot::~TrackingMethodDynamicSlot()
{
if (m_weakRef != nullptr) {
Shiboken::GilState gil;
// weakrefs must not be de-refed after the object has been deleted,
// else they get negative refcounts.
if (PepExt_Weakref_IsAlive(m_weakRef))
Py_DECREF(m_weakRef);
}
}
// Delete the connection on receiver deletion by weakref
class PysideReceiverMethodSlot : public TrackingMethodDynamicSlot
{
Q_DISABLE_COPY_MOVE(PysideReceiverMethodSlot)
public:
explicit PysideReceiverMethodSlot(PyObject *function, PyObject *pythonSelf);
~PysideReceiverMethodSlot() override = default;
};
static void onPysideReceiverSlotDestroyed(void *data)
{
auto *self = reinterpret_cast<PysideReceiverMethodSlot *>(data);
// Ensure the weakref is gone in case the connection stored in
// Qt's internals outlives Python.
self->releaseWeakRef();
Py_BEGIN_ALLOW_THREADS
disconnectReceiver(self->pythonSelf());
Py_END_ALLOW_THREADS
}
PysideReceiverMethodSlot::PysideReceiverMethodSlot(PyObject *function, PyObject *pythonSelf) :
TrackingMethodDynamicSlot(function, pythonSelf,
WeakRef::create(pythonSelf, onPysideReceiverSlotDestroyed, this))
{
}
DynamicSlot* DynamicSlot::create(PyObject *callback)
{
Shiboken::GilState gil;
switch (slotType(callback)) {
case SlotType::Method: {
PyObject *function = PyMethod_GET_FUNCTION(callback);
Py_INCREF(function);
PyObject *pythonSelf = PyMethod_GET_SELF(callback);
return new PysideReceiverMethodSlot(function, pythonSelf);
}
case SlotType::CompiledMethod: {
// PYSIDE-1523: PyMethod_Check is not accepting compiled form, we just go by attributes.
PyObject *function = PyObject_GetAttr(callback, PySide::PySideName::im_func());
Py_DECREF(function);
PyObject *pythonSelf = PyObject_GetAttr(callback, PySide::PySideName::im_self());
Py_DECREF(pythonSelf);
return new PysideReceiverMethodSlot(function, pythonSelf);
}
case SlotType::C_Function: // Treat C-function as normal callables
case SlotType::Callable:
break;
}
return new CallbackDynamicSlot(callback);
}
QDebug operator<<(QDebug debug, const DynamicSlot *ds)
{
QDebugStateSaver saver(debug);
debug.noquote();
debug.nospace();
if (ds != nullptr)
ds->formatDebug(debug);
else
debug << "DynamicSlot(0";
return debug;
}
// Connection code for signal connections that use a Python callable not
// targeting the QMetaMethod of a QObject (no index). They require
// invocation in a class inheriting QtPrivate::QSlotObjectBase (PySideQSlotObject
// aggregating DynamicSlot), which is passed to:
// QObjectPrivate::connect(const QObject *, int signal, QtPrivate::QSlotObjectBase *, ...).
// For each of those connections (identified by ConnectionKey), we maintain a
// hash of ConnectionKey->QMetaObject::Connection for:
//
// - Disconnecting: Retrieve QMetaObject::Connection for the connection parameters
//
// - Tracking sender (QObject) life time and clean hash on destroyed()
//
// - Tracking receiver (PyObject*) life time via weakref in case of a
// connection to a method and proactively disconnect on weakref
// notification as not to cause leaks of the instance.
struct ConnectionKey
{
const QObject *sender;
int senderIndex;
const PyObject *object;
const void *method;
friend constexpr size_t qHash(const ConnectionKey &k, size_t seed = 0) noexcept
{
return qHashMulti(seed, k.sender, k.senderIndex, k.object, k.method);
}
friend constexpr bool comparesEqual(const ConnectionKey &lhs,
const ConnectionKey &rhs) noexcept
{
return lhs.sender == rhs.sender && lhs.senderIndex == rhs.senderIndex
&& lhs.object == rhs.object && lhs.method == rhs.method;
}
Q_DECLARE_EQUALITY_COMPARABLE(ConnectionKey);
};
QDebug operator<<(QDebug debug, const ConnectionKey &k)
{
QDebugStateSaver saver(debug);
debug.noquote();
debug.nospace();
debug << "ConnectionKey(" << static_cast<const void *>(k.sender)
<< '/' << k.sender->metaObject()->className();
auto on = k.sender->objectName();
if (!on.isEmpty())
debug << "/\"" << on << '"';
debug << ", index=" << k.senderIndex << ", target="
<< PySide::debugPyObject(const_cast<PyObject *>(k.object));
if (k.method != nullptr)
debug << ", method=" << k.method;
debug << ')';
return debug;
}
QDebug operator<<(QDebug debug, const QMetaObject::Connection &c)
{
QDebugStateSaver saver(debug);
debug.noquote();
debug.nospace();
debug << "Connection(";
if (c)
debug << static_cast<const void *>(&c); // d-ptr;
else
debug << '0';
debug << ')';
return debug;
}
using ConnectionHash = QHash<ConnectionKey, QMetaObject::Connection>;
static ConnectionHash connectionHash;
static ConnectionKey connectionKey(const QObject *sender, int senderIndex,
PyObject *callback)
{
PyObject *object{};
void *method{};
switch (DynamicSlot::slotType(callback)) {
case DynamicSlot::SlotType::Method:
// PYSIDE-1422: Avoid hash on self which might be unhashable.
object = PyMethod_GET_SELF(callback);
method = PyMethod_GET_FUNCTION(callback);
break;
case DynamicSlot::SlotType::CompiledMethod: {
// PYSIDE-1589: Fix for slots in compiled functions
Shiboken::AutoDecRef self(PyObject_GetAttr(callback, PySide::PySideName::im_self()));
Shiboken::AutoDecRef func(PyObject_GetAttr(callback, PySide::PySideName::im_func()));
object = self.object();
method = func.object();
break;
}
case DynamicSlot::SlotType::Callable:
method = callback;
break;
case DynamicSlot::SlotType::C_Function:
object = PyCFunction_GetSelf(callback);
method = reinterpret_cast<void *>(PyCFunction_GetFunction(callback));
break;
}
return {sender, senderIndex, object, method};
}
// Listens to QObject::destroyed of senders and removes them from the hash.
class SenderSignalDeletionTracker : public QObject
{
Q_OBJECT
public:
using QObject::QObject;
public Q_SLOTS:
void senderDestroyed(QObject *o);
};
void SenderSignalDeletionTracker::senderDestroyed(QObject *o)
{
for (auto it = connectionHash.begin(); it != connectionHash.end(); ) {
if (it.key().sender == o)
it = connectionHash.erase(it);
else
++it;
}
}
static QPointer<SenderSignalDeletionTracker> senderSignalDeletionTracker;
static void disconnectReceiver(PyObject *pythonSelf)
{
// A check for reentrancy was added for PYSIDE-88, but has not been
// observed yet.
for (bool keepGoing = true; keepGoing; ) {
keepGoing = false;
for (auto it = connectionHash.begin(); it != connectionHash.end(); ) {
if (it.key().object == pythonSelf) {
const auto oldSize = connectionHash.size();
QObject::disconnect(it.value());
it = connectionHash.erase(it);
// Check for a disconnection causing deletion of further objects
// by a re-entrant call.
if (connectionHash.size() < oldSize - 1) {
keepGoing = true;
break; // Iterators were invalidated, retry
}
} else {
++it;
}
}
}
}
static void clearConnectionHash()
{
connectionHash.clear();
}
void registerSlotConnection(QObject *source, int signalIndex, PyObject *callback,
const QMetaObject::Connection &connection)
{
connectionHash.insert(connectionKey(source, signalIndex, callback), connection);
if (senderSignalDeletionTracker.isNull()) {
auto *app = QCoreApplication::instance();
senderSignalDeletionTracker = new SenderSignalDeletionTracker(app);
Py_AtExit(clearConnectionHash);
}
QObject::connect(source, &QObject::destroyed,
senderSignalDeletionTracker, &SenderSignalDeletionTracker::senderDestroyed,
Qt::UniqueConnection);
}
bool disconnectSlot(QObject *source, int signalIndex, PyObject *callback)
{
auto it = connectionHash.find(connectionKey(source, signalIndex, callback));
const bool ok = it != connectionHash.end();
if (ok) {
QObject::disconnect(it.value());
connectionHash.erase(it);
}
return ok;
}
} // namespace PySide
#include "dynamicslot.moc"
|