Can I reflect to get arguments exec()?


R

rocky

I have been porting my Python debugger pydbgr to Python3. See [1] or [2].

Inside the debugger, when there is an exec() somewhere in the call stack, I'd like to be able to retrieve the string parameter. With this, the debugger can show part of the string in a call stack. Or it can show the text whenthe frame is set to that exec() frame.

Going further, the debugger could write the exec string out to a temporary file. And when reporting locations, it could report not just something like"<string> line 4", but also give that temporary file name which a front-end could use as well.

So consider this code using inspect.getargvalues() and inspect.currentframe():

import inspect
def my_exec(string):
show_args(inspect.currentframe()) # simulate exec(string)

def show_args(frame):
print(inspect.getargvalues(frame))

my_exec("show_args(inspect.currentframe())")
exec("show_args(inspect.currentframe())")


When run this is the output:

python3 exec-args.py
ArgInfo(args=['string'], varargs=None, keywords=None, locals={'string': 'show_args(inspect.currentframe())'})
ArgInfo(args=[], varargs=None, keywords=None, locals={'my_exec': <function my_exec at 0xb6f828ec>,, ...


In a different setting, CPython byte-code assembly that gets generated for running exec() is:

25 88 LOAD_GLOBAL 10 (exec)
91 LOAD_CONST 4 ('show_args(inspect.currentframe())')
--> 94 CALL_FUNCTION 1
97 POP_TOP

What's going on?

Also, I have the same question for CPython 2.6 or Python 2.7.

Thanks!

[1] http://code.google.com/p/pydbgr/
[2] http://code.google.com/p/python3-trepan/
 
Ad

Advertisements


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

Top