Detecting arguments of a function - possible?

Diez B. Roggisch deets at nospam.web.de
Fri Mar 17 15:14:10 CET 2006


> I am trying to write a tool to examine a function (I'd like it
> to work with pyc files only). So here are a few questions I
> have; any pointers would be very welcome.

Try playing around with reflection - the function-code reveals some
properties of the code.

for example

>>> import inspect
>>> def foo(a, b=1):
...    c = a * b
...
>>> inspect.getargs(foo)
>>> inspect.getargspec(foo)
(['a', 'b'], None, None, (1,))


> Can I determine the number of arguments required of a function?
> Is there a way to detect is the function will throw an exception
> (I don't care under what conditions just that it is possible)?

No. You can disassemble a function and see if it contains a raise-statement.
But as it is perfectly legal to say:

raise some_random_value()

you have absolutely no idea what will be raised. And of course every call to
another function and even some bytecodes (like division, which may result
in ZeroDivsionError) can raise an exception.

Diez





More information about the Python-list mailing list