File: qpycore_pyqtslotproxy.cpp

package info (click to toggle)
pyqt5 5.11.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,956 kB
  • sloc: python: 93,806; cpp: 21,390; xml: 285; makefile: 266; sh: 31
file content (407 lines) | stat: -rw-r--r-- 10,839 bytes parent folder | download
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
// This contains the implementation of the PyQtSlotProxy class.
//
// Copyright (c) 2018 Riverbank Computing Limited <info@riverbankcomputing.com>
// 
// This file is part of PyQt5.
// 
// This file may be used under the terms of the GNU General Public License
// version 3.0 as published by the Free Software Foundation and appearing in
// the file LICENSE included in the packaging of this file.  Please review the
// following information to ensure the GNU General Public License version 3.0
// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
// 
// If you do not wish to use this file under the terms of the GPL version 3.0
// then you may purchase a commercial license.  For more information contact
// info@riverbankcomputing.com.
// 
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


#include <Python.h>

#include <QByteArray>
#include <QMetaObject>
#include <QMutex>
#include <QObject>

#include "qpycore_api.h"
#include "qpycore_chimera.h"
#include "qpycore_qmetaobjectbuilder.h"
#include "qpycore_pyqtslot.h"
#include "qpycore_pyqtslotproxy.h"

#include "sipAPIQtCore.h"


// Proxy flags.
#define PROXY_SINGLE_SHOT   0x01    // The slot will only be called once.
#define PROXY_SLOT_INVOKED  0x02    // The proxied slot is executing.
#define PROXY_SLOT_DISABLED 0x04    // The proxy deleteLater() has been called
                                    // or should be called after the slot has
                                    // finished executing.
#define PROXY_NO_RCVR_CHECK 0x08    // The existence of the receiver C++ object
                                    // should not be checked.


// The last QObject sender.
QObject *PyQtSlotProxy::last_sender = 0;


// Create a universal proxy used as a slot.  Note that this will leak if there
// is no transmitter and this is not a single shot slot.  There will be no
// meta-object if there was a problem creating the proxy.  This is called
// without the GIL.
PyQtSlotProxy::PyQtSlotProxy(PyObject *slot, QObject *q_tx,
        const Chimera::Signature *slot_signature, bool single_shot)
    : QObject(), proxy_flags(single_shot ? PROXY_SINGLE_SHOT : 0),
        signature(slot_signature->signature), transmitter(q_tx)
{
    SIP_BLOCK_THREADS
    real_slot = new PyQtSlot(slot, slot_signature);
    SIP_UNBLOCK_THREADS

    // Create a new meta-object on the heap so that it looks like it has a slot
    // of the right name and signature.
    QMetaObjectBuilder builder;

    builder.setClassName("PyQtSlotProxy");
    builder.setSuperClass(&QObject::staticMetaObject);

    builder.addSlot("unislot()");
    builder.addSlot("disable()");

    meta_object = builder.toMetaObject();

    // Detect when any transmitter is destroyed.  (Note that we used to do this
    // by making the proxy a child of the transmitter.  This doesn't work as
    // expected because QWidget destroys its children before emitting the
    // destroyed signal.)  We use a queued connection in case the proxy is also
    // connected to the same signal and we want to make sure it has a chance to
    // invoke the slot before being destroyed.
    if (transmitter)
    {
        // Add this one to the global hash.
        mutex->lock();
        proxy_slots.insert(transmitter, this);
        mutex->unlock();

        connect(transmitter, SIGNAL(destroyed(QObject *)), SLOT(disable()),
                Qt::QueuedConnection);
    }
}


// Destroy a universal proxy.  This is called without the GIL.
PyQtSlotProxy::~PyQtSlotProxy()
{
    Q_ASSERT((proxy_flags & PROXY_SLOT_INVOKED) == 0);

    if (transmitter)
    {
        mutex->lock();

        ProxyHash::iterator it(proxy_slots.find(transmitter));
        ProxyHash::iterator end(proxy_slots.end());

        while (it != end && it.key() == transmitter)
        {
            if (it.value() == this)
                it = proxy_slots.erase(it);
            else
                ++it;
        }

        mutex->unlock();
    }

    // Qt can still be tidying up after Python has gone so make sure that it
    // hasn't.
    if (Py_IsInitialized())
    {
        SIP_BLOCK_THREADS
        delete real_slot;
        SIP_UNBLOCK_THREADS
    }

    if (meta_object)
    {
        free(const_cast<QMetaObject *>(meta_object));
    }
}


// The static members of PyQtSlotProxy.
const QByteArray PyQtSlotProxy::proxy_slot_signature(SLOT(unislot()));
QMutex *PyQtSlotProxy::mutex;
PyQtSlotProxy::ProxyHash PyQtSlotProxy::proxy_slots;


const QMetaObject *PyQtSlotProxy::metaObject() const
{
    return meta_object;
}


void *PyQtSlotProxy::qt_metacast(const char *_clname)
{
    if (!_clname)
        return 0;

    if (qstrcmp(_clname, "PyQtSlotProxy") == 0)
        return static_cast<void *>(const_cast<PyQtSlotProxy *>(this));

    return QObject::qt_metacast(_clname);
}


int PyQtSlotProxy::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);

    if (_id < 0)
        return _id;

    if (_c == QMetaObject::InvokeMetaMethod)
    {
        switch (_id)
        {
        case 0:
            unislot(_a);
            break;

        case 1:
            disable();
            break;
        }

        _id -= 2;
    }

    return _id;
}


// This is the universal slot itself that dispatches to the real slot.
void PyQtSlotProxy::unislot(void **qargs)
{
    // If we are marked as disabled (possible if a queued signal has been
    // disconnected but there is still a signal in the event queue) then just
    // ignore the call.
    if (proxy_flags & PROXY_SLOT_DISABLED)
        return;

    // sender() must be called without the GIL to avoid possible deadlocks
    // between the GIL and Qt's internal thread data mutex.
    QObject *new_last_sender = sender();

    SIP_BLOCK_THREADS

    QObject *saved_last_sender = last_sender;
    last_sender = new_last_sender;

    proxy_flags |= PROXY_SLOT_INVOKED;

    switch (real_slot->invoke(qargs, (proxy_flags & PROXY_NO_RCVR_CHECK)))
    {
    case PyQtSlot::Succeeded:
        break;

    case PyQtSlot::Failed:
        pyqt5_err_print();
        break;

    case PyQtSlot::Ignored:
        proxy_flags |= PROXY_SLOT_DISABLED;
        break;
    }

    proxy_flags &= ~PROXY_SLOT_INVOKED;

    // Self destruct if we are a single shot or disabled.
    if (proxy_flags & (PROXY_SINGLE_SHOT|PROXY_SLOT_DISABLED))
    {
        // See the comment in disable() for why we deleteLater().
        deleteLater();
    }

    last_sender = saved_last_sender;

    SIP_UNBLOCK_THREADS
}


// Disable the slot by destroying it if possible, or delaying its destruction
// until the proxied slot returns.
void PyQtSlotProxy::disable()
{
    proxy_flags |= PROXY_SLOT_DISABLED;

    // Delete it if the slot isn't currently executing, otherwise it will be
    // done after the slot returns.  Note that we don't rely on deleteLater()
    // providing the necessary delay because the slot could process the event
    // loop and the proxy would be deleted too soon.
    if ((proxy_flags & PROXY_SLOT_INVOKED) == 0)
    {
        // Despite what the Qt documentation suggests, if there are outstanding
        // queued signals then we may crash so we always delete later when the
        // event queue will have been flushed.
        deleteLater();
    }
}


// Find a slot proxy connected to a transmitter.
PyQtSlotProxy *PyQtSlotProxy::findSlotProxy(const QObject *transmitter,
        const QByteArray &signal_signature, PyObject *slot)
{
    PyQtSlotProxy *proxy = 0;

    mutex->lock();

    ProxyHash::const_iterator it(proxy_slots.find(transmitter));
    ProxyHash::const_iterator end(proxy_slots.end());

    while (it != end && it.key() == transmitter)
    {
        PyQtSlotProxy *sp = it.value();

        if (!(sp->proxy_flags & PROXY_SLOT_DISABLED) && sp->signature == signal_signature && *(sp->real_slot) == slot)
        {
            proxy = sp;
            break;
        }

        ++it;
    }

    mutex->unlock();

    return proxy;
}


// Delete any slot proxy for a particular connection.
void PyQtSlotProxy::deleteSlotProxy(const QMetaObject::Connection *connection)
{
    mutex->lock();

    ProxyHash::iterator it(proxy_slots.begin());
    ProxyHash::iterator end(proxy_slots.end());

    while (it != end)
    {
        PyQtSlotProxy *sp = it.value();

        if (sp->connection == *connection)
        {
            proxy_slots.erase(it);
            sp->disable();

            break;
        }

        ++it;
    }

    mutex->unlock();
}


// Delete any slot proxies for a particular transmitter and optional signal
// signature.
void PyQtSlotProxy::deleteSlotProxies(const QObject *transmitter,
        const QByteArray &signal_signature)
{
    mutex->lock();

    ProxyHash::iterator it(proxy_slots.find(transmitter));
    ProxyHash::iterator end(proxy_slots.end());

    while (it != end && it.key() == transmitter)
    {
        PyQtSlotProxy *sp = it.value();

        if (signal_signature.isEmpty() || signal_signature == sp->signature)
        {
            it = proxy_slots.erase(it);
            sp->disable();
        }
        else
        {
            ++it;
        }
    }

    mutex->unlock();
}


// Clear the extra references of any slots connected to a transmitter.  This is
// called with the GIL.
int PyQtSlotProxy::clearSlotProxies(const QObject *transmitter)
{
    ProxyHash::iterator it(proxy_slots.find(transmitter));
    ProxyHash::iterator end(proxy_slots.end());

    while (it != end && it.key() == transmitter)
    {
        it.value()->real_slot->clearOther();

        ++it;
    }

    return 0;
}


// A thing wrapper available to the generated code.
int qpycore_clearSlotProxies(const QObject *transmitter)
{
    return PyQtSlotProxy::clearSlotProxies(transmitter);
}


// Visit the extra references of any slots connected to a transmitter.  This is
// called with the GIL.
int PyQtSlotProxy::visitSlotProxies(const QObject *transmitter,
        visitproc visit, void *arg)
{
    int vret = 0;

    ProxyHash::iterator it(proxy_slots.find(transmitter));
    ProxyHash::iterator end(proxy_slots.end());

    while (it != end && it.key() == transmitter)
    {
        if ((vret = it.value()->real_slot->visitOther(visit, arg)) != 0)
            break;

        ++it;
    }

    return vret;
}


// A thing wrapper available to the generated code.
int qpycore_visitSlotProxies(const QObject *transmitter, visitproc visit,
        void *arg)
{
    return PyQtSlotProxy::visitSlotProxies(transmitter, visit, arg);
}


// Get the last sender.
QObject *PyQtSlotProxy::lastSender()
{
    return last_sender;
}


// Disable the check that the receiver C++ object exists before invoking a
// slot.
void PyQtSlotProxy::disableReceiverCheck()
{
    proxy_flags |= PROXY_NO_RCVR_CHECK;
}