[PyQt] How to determine identity of bound PyQt signals

Phil Thompson phil at riverbankcomputing.com
Wed Jul 13 18:34:36 BST 2016


On 13 Jul 2016, at 5:33 pm, Marius Shekow <marius.shekow at fit.fraunhofer.de> wrote:
> 
> Hi,
> 
> I'm trying to extend the functionality of the pytest plugin "pytest-qt" to be able to have pytests that check that some QObject emits a list of signals with optional parameter checking. However, I'm having trouble discerning signals that are given to my wait-method as parameter.
> 
> For example, consider this (somewhere a class "SomeQObject" is defined which defines a signal "some_signal" - omitted here for brevity), using PyQt5:
> 
>>>> obj = SomeQObject()
>>>> obj.some_signal == obj.some_signal
> False <- I would have suspected this to be True, but it's False....

Bound signals (like bound methods) are created on the fly. In the above two different objects are created which have different values of id().

>>>> id(obj.some_signal) == id(obj.some_signal)
> True

I would guess that one of the bound signals is being garbage collected as soon as id() returns. The second bound signal is using the same piece of memory and therefore has the same id().

> Also:
>>>> x = obj.some_signal
>>>> y = obj.some_signal
>>>> x is y
> False
>>>> x == y
> False
>>>> id(x) == id(y)
> False
> 
> Now, suppose I have a method "wait_for_signals(signals: list)", then this method's code won't be able to reliably find out which of the variables inside the list "signals" actually point to the SAME signal. The code would think that in the call wait_for_signals([x, y]) two different signals were provided, which is incorrect. This would break the evaluation logic, which connects to x and y, effectly connecting to ONE signal (obj.some_signal) twice.
> 
> My question is: is there a better way for PyQt4 and PyQt5 to find out which variables in the "signals" list correspond to the same signal? So far the only solution that works is:
>>>> str(obj.some_signal) == str(obj.some_signal)
> True
>>>> str(x) == str(y)
> True
> 
> But I guess that's a very hacky solution!

...but will work. A better solution would be if bound signals implemented __eq__.

Phil


More information about the PyQt mailing list