Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supporting callables? #7

Open
TTimo opened this issue Feb 13, 2015 · 4 comments
Open

supporting callables? #7

TTimo opened this issue Feb 13, 2015 · 4 comments
Labels

Comments

@TTimo
Copy link

TTimo commented Feb 13, 2015

nifty little module!

Would it be possible to support callables instead of just functions - I ran into this use case:

class FuncToolsTest( unittest.TestCase ):
    def test( self ):
        def f( a, b ):
            logging.info( 'a: %s b: %s' % ( repr( a ), repr( b ) ) )

        g = functools.partial( f, 'a' )
        g('b')

        def fp( a, **kwargs ):
            logging.info( 'a: %s kwargs: %s' % ( repr( a ), repr( kwargs ) ) )
        gp = functools.partial( fp, 'a' )
        gp( b = 'b', c = 'c' )

        s = signalslot.Signal()
        s.connect( gp )
        s.emit()

Basically I wanted to specialize a signal handler from a generic function with a partial application in between. Error is:

INFO:root:a: 'a' b: 'b'
INFO:root:a: 'a' kwargs: {'c': 'c', 'b': 'b'}
E..
======================================================================
ERROR: test (main.FuncToolsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "main.py", line 1410, in test
    s.connect( gp )
  File "lib/signalslot/signal.py", line 57, in connect
    if inspect.getargspec(slot).keywords is None:
  File "/usr/lib/python2.7/inspect.py", line 816, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <functools.partial object at 0x7fe65f4889f0> is not a Python function
@sjlongland
Copy link
Contributor

I suspect the problem is knowing whether a callable supports keyword arguments. The inspect library doesn't seem to know how to check this.

One possibility is to take the Erlang approach and just "let it crash" if it turns out a callable doesn't support keywords. Trouble is the signal in question might be a very rare one and it'll only be some time later that you find there's a problem.

I made a fork of signalslot that accepts a Slot object which doesn't actually bother to check whether the thing it is passed takes keywords, then I found I had to add a special case to handle this very problem in the Signal class.

@jpic
Copy link
Member

jpic commented Apr 22, 2015 via email

@sjlongland
Copy link
Contributor

Okay, I'm a little lost, what is meant by "curry your callable"?

@jpic
Copy link
Member

jpic commented Apr 22, 2015 via email

@Sliim Sliim added the question label Jul 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants