473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Detecting arguments of a function - possible?

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.
Mar 17 '06 #1
3 1102
Chance Ginger wrote:
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
Mar 17 '06 #2
> 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
Mar 17 '06 #3
>>>> inspect.getargs(foo)

Copy-and-paste error. Ignore that line.
Diez
Mar 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.