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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
// This contains the implementation of the PyQtProxy class.
//
// Copyright (c) 2016 Riverbank Computing Limited <info@riverbankcomputing.com>
//
// This file is part of PyQt4.
//
// 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_chimera.h"
#include "qpycore_qmetaobjectbuilder.h"
#include "qpycore_qtlib.h"
#include "qpycore_pyqtproxy.h"
#include "qpycore_pyqtpyobject.h"
#include "qpycore_sip.h"
// Proxy flags. Note that SIP_SINGLE_SHOT is part of the same flag-space.
#define PROXY_OWNS_SLOT_SIG 0x10 // The proxy owns the slot signature.
#define PROXY_SLOT_INVOKED 0x20 // The proxied slot is executing.
#define PROXY_SLOT_DISABLED 0x40 // The proxy deleteLater() has been called
// or should be called after the slot has
// finished executing.
#define PROXY_NO_RCVR_CHECK 0x80 // The existence of the receiver C++ object
// should not be checked.
// The last QObject sender.
QObject *PyQtProxy::last_sender = 0;
#if QT_VERSION < 0x050000
static const uint slot_meta_data[] = {
// content:
1, // revision
0, // classname
0, 0, // classinfo
2, 10, // methods (number, offset in this array of first one)
0, 0, // properties
0, 0, // enums/sets
// slots: signature, parameters, type, tag, flags
21, 10, 10, 10, 0x0a,
11, 10, 10, 10, 0x0a,
0 // eod
};
static const char slot_meta_stringdata[] = {
"PyQtProxy\0\0disable()\0unislot()\0"
};
const QMetaObject PyQtProxy::staticMetaObject = {
{
&QObject::staticMetaObject,
slot_meta_stringdata,
slot_meta_data,
0
}
};
#endif
// Create a universal proxy used as a signal.
PyQtProxy::PyQtProxy(QObject *qtx, const char *sig)
: QObject(), type(PyQtProxy::ProxySignal), proxy_flags(0),
signature(QMetaObject::normalizedSignature(sig)), meta_object(0)
{
init(qtx, &proxy_signals, qtx);
}
// Create a universal proxy used as a slot. Note that this will leak if there
// is no signal transmitter (ie. no parent) and not marked as single shot.
// There will be no meta-object if there was a problem creating the proxy.
PyQtProxy::PyQtProxy(sipWrapper *txObj, const char *sig, PyObject *rxObj,
const char *slot, const char **member, int flags)
: QObject(), type(PyQtProxy::ProxySlot),
proxy_flags(PROXY_OWNS_SLOT_SIG | flags),
signature(QMetaObject::normalizedSignature(sig)), meta_object(0)
{
void *tx = 0;
QObject *qtx = 0;
// Parse the signature.
SIP_BLOCK_THREADS
real_slot.signature = Chimera::parse(signature, "a slot argument");
if (real_slot.signature)
{
// Save the slot.
if (qtlib_save_slot(&real_slot.sip_slot, rxObj, slot) < 0)
{
delete real_slot.signature;
real_slot.signature = 0;
}
else
{
// See if there is a transmitter and that it is a QObject.
if (txObj)
{
tx = sipGetCppPtr((sipSimpleWrapper *)txObj, 0);
if (tx && PyObject_TypeCheck((PyObject *)txObj, sipTypeAsPyTypeObject(sipType_QObject)))
qtx = reinterpret_cast<QObject *>(tx);
}
}
}
SIP_UNBLOCK_THREADS
if (real_slot.signature)
{
// Return the slot to connect to.
*member = SLOT(unislot());
init(qtx, &proxy_slots, tx);
}
}
// Create a universal proxy used as a slot being connected to a bound signal.
// There will be no meta-object if there was a problem creating the proxy.
PyQtProxy::PyQtProxy(QObject *qtx, const Chimera::Signature *signal_signature,
PyObject *rxObj, const char **member, int flags)
: QObject(), type(PyQtProxy::ProxySlot), proxy_flags(flags),
signature(signal_signature->signature)
{
SIP_BLOCK_THREADS
real_slot.signature = signal_signature;
// Save the slot.
if (qtlib_save_slot(&real_slot.sip_slot, rxObj, 0) < 0)
real_slot.signature = 0;
SIP_UNBLOCK_THREADS
if (real_slot.signature)
{
// Return the slot to connect to.
*member = SLOT(unislot());
init(qtx, &proxy_slots, qtx);
}
}
// Initialisation common to all ctors.
void PyQtProxy::init(QObject *qtx, PyQtProxy::ProxyHash *hash, void *key)
{
// Create a new meta-object on the heap so that it looks like it has a
// signal of the right name and signature.
#if QT_VERSION >= 0x050000
QMetaObjectBuilder builder;
builder.setClassName("PyQtProxy");
builder.setSuperClass(&QObject::staticMetaObject);
// Note that signals must be added before slots.
if (type == ProxySignal)
builder.addSignal(signature);
else
builder.addSlot("unislot()");
builder.addSlot("disable()");
meta_object = builder.toMetaObject();
#else
if (type == ProxySignal)
{
QMetaObject *mo = new QMetaObject;
mo->d.superdata = &QObject::staticMetaObject;
mo->d.extradata = 0;
// Calculate the size of the string meta-data as follows:
// - "PyQtProxy" and its terminating '\0' (ie. 9 + 1 bytes),
// - a '\0' used for any empty string,
// - "disable()" and its terminating '\0' (ie. 9 + 1 bytes),
// - the (non-existent) argument names (ie. use the empty string if
// there is less that two arguments, otherwise a comma between each
// argument and the terminating '\0'),
// - the name and full signature, less the initial type character, plus
// the terminating '\0'.
const size_t fixed_len = 9 + 1 + 1 + 9 + 1;
const size_t empty_str = 9 + 1;
int nr_commas = signature.count(',');
size_t len = fixed_len
+ (nr_commas >= 0 ? nr_commas + 1 : 0)
+ signature.size() + 1;
char *smd = new char[len];
memcpy(smd, slot_meta_stringdata, fixed_len);
uint i = fixed_len, args_pos;
if (nr_commas > 0)
{
args_pos = i;
for (int c = 0; c < nr_commas; ++c)
smd[i++] = ',';
smd[i++] = '\0';
}
else
{
args_pos = empty_str;
}
uint sig_pos = i;
qstrcpy(&smd[i], signature.constData());
mo->d.stringdata = smd;
// Add the non-string data.
uint *data = new uint[21];
memcpy(data, slot_meta_data, 21 * sizeof (uint));
// Replace the first method (ie. unislot()) with the new signal.
data[10] = sig_pos;
data[11] = args_pos;
data[14] = 0x05;
mo->d.data = data;
meta_object = mo;
}
else
{
meta_object = &staticMetaObject;
}
#endif
hashed = true;
saved_key = key;
transmitter = qtx;
// Add this one to the global hashes.
mutex->lock();
hash->insert(key, this);
mutex->unlock();
// Detect when the 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 (qtx)
connect(qtx, SIGNAL(destroyed(QObject *)), SLOT(disable()),
Qt::QueuedConnection);
}
// Destroy a universal proxy.
PyQtProxy::~PyQtProxy()
{
Q_ASSERT((proxy_flags & PROXY_SLOT_INVOKED) == 0);
if (hashed)
{
mutex->lock();
switch (type)
{
case ProxySlot:
{
ProxyHash::iterator it(proxy_slots.find(saved_key));
ProxyHash::iterator end(proxy_slots.end());
while (it != end && it.key() == saved_key)
{
if (it.value() == this)
it = proxy_slots.erase(it);
else
++it;
}
break;
}
case ProxySignal:
{
ProxyHash::iterator it(proxy_signals.find(saved_key));
ProxyHash::iterator end(proxy_signals.end());
while (it != end && it.key() == saved_key)
{
if (it.value() == this)
it = proxy_signals.erase(it);
else
++it;
}
break;
}
}
mutex->unlock();
}
if (type == ProxySlot && real_slot.signature)
{
// Qt can still be tidying up after Python has gone so make sure that
// it hasn't.
if (Py_IsInitialized())
{
SIP_BLOCK_THREADS
qtlib_free_slot(&real_slot.sip_slot);
SIP_UNBLOCK_THREADS
}
if (proxy_flags & PROXY_OWNS_SLOT_SIG)
delete real_slot.signature;
real_slot.signature = 0;
}
if (meta_object)
{
#if QT_VERSION >= 0x050000
free(const_cast<QMetaObject *>(meta_object));
#else
if (meta_object != &staticMetaObject)
{
// The casts are needed for MSVC 6.
delete[] const_cast<char *>(meta_object->d.stringdata);
delete[] const_cast<uint *>(meta_object->d.data);
delete meta_object;
}
#endif
}
}
// The static members of PyQtProxy.
QMutex *PyQtProxy::mutex;
PyQtProxy::ProxyHash PyQtProxy::proxy_slots;
PyQtProxy::ProxyHash PyQtProxy::proxy_signals;
const QMetaObject *PyQtProxy::metaObject() const
{
return meta_object;
}
void *PyQtProxy::qt_metacast(const char *_clname)
{
if (!_clname)
return 0;
if (qstrcmp(_clname, "PyQtProxy") == 0)
return static_cast<void *>(const_cast<PyQtProxy *>(this));
return QObject::qt_metacast(_clname);
}
int PyQtProxy::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:
if (type == ProxySignal)
QMetaObject::activate(this, meta_object, _id, _a);
else
unislot(_a);
break;
case 1:
disable();
break;
}
_id -= 2;
}
return _id;
}
// This is the universal slot itself that dispatches to the real slot.
void PyQtProxy::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;
int no_receiver_check = (proxy_flags & PROXY_NO_RCVR_CHECK);
PyObject *res;
// See if the sender was a short-circuit signal. */
if (last_sender && PyQtShortcircuitSignalProxy::shortcircuitSignal(last_sender))
{
// The Python arguments will be the only argument.
PyObject *pyargs = reinterpret_cast<PyQt_PyObject *>(qargs[1])->pyobject;
res = qtlib_invoke_slot(&real_slot.sip_slot, pyargs,
no_receiver_check);
}
else
{
proxy_flags |= PROXY_SLOT_INVOKED;
res = invokeSlot(real_slot, qargs, no_receiver_check);
proxy_flags &= ~PROXY_SLOT_INVOKED;
// Self destruct if we are a single shot or disabled.
if (proxy_flags & (SIP_SINGLE_SHOT|PROXY_SLOT_DISABLED))
{
// See the comment in disable() for why we deleteLater().
deleteLater();
}
}
if (res)
Py_DECREF(res);
else
PyErr_Print();
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 PyQtProxy::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();
}
}
// Invoke a slot on behalf of C++.
PyObject *PyQtProxy::invokeSlot(const qpycore_slot &slot, void **qargs,
int no_receiver_check)
{
const QList<const Chimera *> &args = slot.signature->parsed_arguments;
PyObject *argtup = PyTuple_New(args.size());
if (!argtup)
return 0;
QList<const Chimera *>::const_iterator it = args.constBegin();
for (int a = 0; it != args.constEnd(); ++a)
{
PyObject *arg = (*it)->toPyObject(*++qargs);
if (!arg)
{
Py_DECREF(argtup);
return 0;
}
PyTuple_SET_ITEM(argtup, a, arg);
++it;
}
// Dispatch to the real slot.
PyObject *res = qtlib_invoke_slot(&slot.sip_slot, argtup,
no_receiver_check);
Py_DECREF(argtup);
return res;
}
// Disable the check that the receiver C++ object exists before invoking a
// slot.
void PyQtProxy::disableReceiverCheck()
{
proxy_flags |= PROXY_NO_RCVR_CHECK;
}
// Find a slot proxy connected to a transmitter.
PyQtProxy *PyQtProxy::findSlotProxy(void *tx, const char *sig, PyObject *rxObj,
const char *slot, const char **member)
{
PyQtProxy *proxy = 0;
mutex->lock();
ProxyHash::const_iterator it(proxy_slots.find(tx));
ProxyHash::const_iterator end(proxy_slots.end());
while (it != end && it.key() == tx)
{
PyQtProxy *up = it.value();
if (up->signature == sig && qtlib_same_slot(&up->real_slot.sip_slot, rxObj, slot))
{
*member = SLOT(unislot());
proxy = up;
break;
}
++it;
}
mutex->unlock();
return proxy;
}
// Delete any slot proxies for a particular signal.
void PyQtProxy::deleteSlotProxies(void *tx, const char *sig)
{
mutex->lock();
ProxyHash::iterator it(proxy_slots.find(tx));
ProxyHash::iterator end(proxy_slots.end());
while (it != end && it.key() == tx)
{
PyQtProxy *up = it.value();
if (up->signature == sig)
{
up->hashed = false;
it = proxy_slots.erase(it);
up->disable();
}
else
++it;
}
mutex->unlock();
}
#if SIP_VERSION >= 0x050000
// Clear the extra references of any slots connected to a transmitter. This is
// called with the GIL.
int PyQtProxy::clearSlotProxies(const QObject *transmitter)
{
ProxyHash::iterator it(
proxy_slots.find(const_cast<QObject *>(transmitter)));
ProxyHash::iterator end(proxy_slots.end());
while (it != end && it.key() == transmitter)
{
sipSlot *slot = &it.value()->real_slot.sip_slot;
if (slot->weakSlot == Py_True)
{
PyObject *xref = slot->pyobj;
// Replace the slot with None. We don't use NULL as this has
// another meaning.
Py_INCREF(Py_None);
slot->pyobj = Py_None;
Py_DECREF(xref);
}
++it;
}
return 0;
}
#endif
// A thing wrapper available to the generated code.
int qpycore_clearSlotProxies(const QObject *transmitter)
{
#if SIP_VERSION >= 0x050000
return PyQtProxy::clearSlotProxies(transmitter);
#else
Q_UNUSED(transmitter);
return 0;
#endif
}
#if SIP_VERSION >= 0x050000
// Visit the extra references of any slots connected to a transmitter. This is
// called with the GIL.
int PyQtProxy::visitSlotProxies(const QObject *transmitter,
visitproc visit, void *arg)
{
int vret = 0;
ProxyHash::iterator it(
proxy_slots.find(const_cast<QObject *>(transmitter)));
ProxyHash::iterator end(proxy_slots.end());
while (it != end && it.key() == transmitter)
{
sipSlot *slot = &it.value()->real_slot.sip_slot;
// See if the slot has an extra reference.
if (slot->weakSlot == Py_True && slot->pyobj != Py_None)
{
if ((vret = visit(slot->pyobj, arg)) != 0)
break;
}
++it;
}
return vret;
}
#endif
// A thing wrapper available to the generated code.
int qpycore_visitSlotProxies(const QObject *transmitter, visitproc visit,
void *arg)
{
#if SIP_VERSION >= 0x050000
return PyQtProxy::visitSlotProxies(transmitter, visit, arg);
#else
Q_UNUSED(transmitter);
Q_UNUSED(visit);
Q_UNUSED(arg);
return 0;
#endif
}
// This is set if a PyQtShortcircuitSignalProxy has never been created.
bool PyQtShortcircuitSignalProxy::no_shortcircuit_signals = true;
// Create the short-circuit signal proxy.
PyQtShortcircuitSignalProxy::PyQtShortcircuitSignalProxy(QObject *parent)
: QObject()
{
no_shortcircuit_signals = false;
// We can't just set the parent because we might be in a different thread.
moveToThread(parent->thread());
setParent(parent);
}
// Find an object's short-circuit signal proxy for a signature, if any.
PyQtShortcircuitSignalProxy *PyQtShortcircuitSignalProxy::find(QObject *tx,
const char *sig)
{
if (no_shortcircuit_signals)
return 0;
// Only check immediate children.
const QObjectList &kids = tx->children();
for (int i = 0; i < kids.size(); ++i)
{
PyQtShortcircuitSignalProxy *proxy = qobject_cast<PyQtShortcircuitSignalProxy *>(kids.at(i));
if (proxy && proxy->objectName() == sig)
return proxy;
}
return 0;
}
// Return true if an object is a PyQtShortcircuitSignalProxy instance. This is
// done without calling qobject_cast() unless absolutely necessary in case the
// object has been deleted.
PyQtShortcircuitSignalProxy *PyQtShortcircuitSignalProxy::shortcircuitSignal(QObject *obj)
{
if (no_shortcircuit_signals)
return 0;
return qobject_cast<PyQtShortcircuitSignalProxy *>(obj);
}
|