Detecting arguments of a function - possible?

C

Chance Ginger

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.

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)?

Thanks in advance...
Chas.
 
K

Kent Johnson

Chance said:
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.

Can I determine the number of arguments required of a function?

See inspect.getargspec()
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)?

If you mean something like Java's throws declaration, no, Python doesn't
have that. Any function can raise an exception. IIUC even something as
simple as
def foo(): pass
can raise KeyboardInterrupt

Kent
 
D

Diez B. Roggisch

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
.... c = a * b
....(['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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,290
Messages
2,571,452
Members
48,129
Latest member
DianneCarn

Latest Threads

Top