[Python-Dev] Can I introspect/reflect to get arguments exec()? (original) (raw)

Rocky Bernstein rocky at gnu.org
Wed Mar 27 04:00:55 CET 2013


On Tue, Mar 26, 2013 at 10:18 PM, Benjamin Peterson <benjamin at python.org>wrote:

2013/3/26 Rocky Bernstein <rocky at gnu.org>: > [asked on comp.lang.python but no takers. So I'm bumping it up a notch.] > > I have ported 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 when the 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 > " 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 myexec(string): > showargs(inspect.currentframe()) # simulate exec(string) > > def showargs(frame): > print(inspect.getargvalues(frame)) > > myexec("showargs(inspect.currentframe())") > exec("showargs(inspect.currentframe())") > > > When run this is the output: > > python3 exec-args.py > ArgInfo(args=['string'], varargs=None, keywords=None, locals={'string': > 'showargs(inspect.currentframe())'}) > ArgInfo(args=[], varargs=None, keywords=None, locals={'myexec': > <function myexec at 0xb6f828ec>,, ... > > > In a different setting, CPython byte-code assembly that gets generated for > running exec() is: > > 25 88 LOADGLOBAL 10 (exec) > 91 LOADCONST 4 > ('showargs(inspect.currentframe())') > --> 94 CALLFUNCTION 1 > 97 POPTOP > > What's going on?

execing something is not the same as calling it, so there are no arguments.

Okay. But is the string is still somewhere in the CPython VM stack? (The result of LOAD_CONST 4 above). Is there a way to pick it up from there? At the point that we are stopped the exec action hasn't taken place yet.

-- Regards, Benjamin -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130326/a92cbe26/attachment-0001.html>



More information about the Python-Dev mailing list