inspecting a callable for the arguments it accepts

Cameron Simpson cs at cskk.id.au
Tue Nov 14 16:05:05 EST 2017


I know that this isn't generally solvable, but I'm wondering if it is partially 
solvable.

I have a task manager which accepts callables, usually functions or generators, 
and calls them on data items as needed. For reasons which, frankly, suggest 
user interface ergonomics failure to me it happens that inappropriate functions 
get submtted to this system. For example, functions accepting no arguments but 
which are handed one by the system.

I would like to inspect submitted functions' signatures for suitability at 
submission time, without calling the function. For example to see if this 
function accepts exactly one argument, or to see if it is a generator, etc.

Is this possible, even in a limited way?

The present situation is that when I get this wrong I get highly cryptic 
exceptions at runtime, some of which I think I could be FAR better understood 
if caught at submittion time.

Here's an example failure at runtime:

    pilfer.py: WorkerThreadPool:Later-1:WorkerThreadPool: worker thread: ran task: exception! (<class 'TypeError'>, TypeError('<lambda>() takes 0 positional arguments but 1 was given',), <traceback object at 0x101a510c8>)
    Traceback (most recent call last):
      File "/Users/cameron/hg/css/cs/threads.py", line 163, in _handler
        result = func()
      File "/Users/cameron/hg/css/cs/app/pilfer.py", line 1550, in retry_func
        return func(P, *a, **kw)
    TypeError: <lambda>() takes 0 positional arguments but 1 was given
    pilfer.py: MAIN:0:0:retriable(<lambda>).put(Pilfer-0[http://www.smh.com.au/]): (<class 'TypeError'>, TypeError('<lambda>() takes 0 positional arguments but 1 was given',), <traceback object at 0x101a510c8>)

Ignoring the slight repetition you can see that (a) there's no direct clue as 
to what the actual submitted function was originally, as it is a lambda and (b) 
there's no direct clue as to where the function came from, because the 
submission call stack is long gone and the function is being called via a 
thread pool.

Had I caught the function as unsuitable at submission time, debugging would be 
far easier.

To be clear: the task manager is my own project, as is the submitted function.  
I'm trying to improve its debuggability.

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)



More information about the Python-list mailing list