[PyQt] PyQt should not ignore functools.partial signature

Luper Rouch luper.rouch at gmail.com
Wed Oct 12 14:57:17 BST 2011


PyQt seems to ignore the signature of functools.partial objects (the 'args'
and 'keywords' attributes [1]), when connecting a callable. Here is an
example demonstrating the problem :

import functools
from PyQt4.QtCore import QObject, pyqtSignal

class Sender(QObject):

    hello = pyqtSignal(bool)

def receiver():
    print "foo"

def decorator(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapped

decorated_receiver = decorator(receiver)

if __name__ == "__main__":
    sender = Sender()
    sender.hello.connect(receiver)
    sender.hello.connect(decorated_receiver)
    sender.hello.emit(True)

When executed, the script gives the following error :

$ python test_signature.py
foo
Traceback (most recent call last):
  File "test_signature.py", line 17, in wrapped
    return func(*args, **kwargs)
TypeError: receiver() takes no arguments (1 given)

Connecting to a lambda is not a good solution, because PyQt increases its
reference count to keep it alive ("However, if a slot is a lambda function
or a partial function then its reference count is automatically incremented
to prevent it from being immediately garbage collected", see [2]).

If you do this in a widget that is later deleted, the lambda stays alive,
leading to complex bugs. The only solution is to connect to a normal method
calling the decorated method, which can quickly become cumbersome.

It would be nice if PyQt did some additional checks when connecting to a
functools.partial object.

[1] http://docs.python.org/library/functools.html#partial-objects
[2]
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/old_style_signals_slots.html#pyqt-slots-and-qt-slots

-- 
Luper Rouch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20111012/96525630/attachment.html>


More information about the PyQt mailing list