Note that there are some explanatory texts on larger screens.

plurals
  1. POusing python inspect to view local variables in a function
    primarykey
    data
    text
    <p>I can't figure out how to inspect a currently executing function using <a href="http://docs.python.org/library/inspect.html" rel="nofollow">inspect</a> / <a href="https://github.com/amoffat/Inspect-Shell" rel="nofollow">inspect_shell</a></p> <p>I'm guessing it involves walking the frame hierarchy using getinnerframe and getouterframe, but I'm confused on several issues.</p> <p>Given this example nine.py:</p> <pre><code>import inspect_shell import time def number_nine(): x = 9 while x==9: time.sleep(1) number_nine() print x </code></pre> <p>I would like to inspect the value of <code>x</code> or even possibly change it to cause the function to return and print the new value.</p> <p>First I launch nine.py, then in a separate command window, using <a href="https://github.com/amoffat/Inspect-Shell" rel="nofollow">inspect_shell</a>, I see that <code>getinnerframes</code> does not work on the current frame (it needs a trace (perhaps?)) and the current frame has no "trace". and <code>getouterframes</code> (in case I'm thinking about this backwards) seems to get only frames that have nothing to do with my function.</p> <pre><code>&gt;&gt; Inspect Shell v1.0 &gt;&gt; https://github.com/amoffat/Inspect-Shell localhost:1234&gt; import inspect localhost:1234&gt; f = inspect.currentframe() localhost:1234&gt; inspect.getinnerframes(f) Traceback (most recent call last): File "C:\Users\Paul\Desktop\inspect_shell.py", line 143, in run_repl try: exec compile(data, "&lt;dummy&gt;", "single") in f_globals, f_globals File "&lt;dummy&gt;", line 1, in &lt;module&gt; File "C:\Python26\lib\inspect.py", line 942, in getinnerframes framelist.append((tb.tb_frame,) + getframeinfo(tb, context)) AttributeError: 'frame' object has no attribute 'tb_frame' localhost:1234&gt; dir(f) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_glo bals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace'] localhost:1234&gt; print f.f_trace None localhost:1234&gt; inspect.getouterframes(f) [(&lt;frame object at 0x0237D470&gt;, '&lt;dummy&gt;', 1, '&lt;module&gt;', None, None), (&lt;frame object at 0x02375938&gt;, 'C:\\Users\\Paul\\Desktop\\inspect_shell.py', 14 3, 'run_repl', [' try: exec compile(data, "&lt;dummy&gt;", "single") in f_globals, f_globals\n'], 0), (&lt;frame object at 0x023A2C30&gt;, 'C:\ \Python26\\lib\\threading.py', 484, 'run', [' self.__target(*self.__args, **self.__kwargs)\n'], 0), (&lt;frame object at 0x02397F28&gt;, 'C:\ \Python26\\lib\\threading.py', 532, '__bootstrap_inner', [' self.run()\n'], 0), (&lt;frame object at 0x023A9D68&gt;, 'C:\\Python26\\lib\\thre ading.py', 504, '__bootstrap', [' self.__bootstrap_inner()\n'], 0)] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload