Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can accomplish this by getting a reference to the outer interpreter's stack frame, and writing to its frame globals.</p> <p>Given a sample module with a breakpoint that kicks us into pdb:</p> <p><strong>my_module.py</strong>:</p> <pre><code>def fun(arg): import pdb; pdb.set_trace() print arg </code></pre> <p>Example demonstrating the basic concept:</p> <pre><code> &gt;&gt;&gt; import my_module &gt;&gt;&gt; my_module.fun(1) &gt; /Users/lukasgraf/src/stackoverflow/my_module.py(3)fun() -&gt; print arg (Pdb) import sys (Pdb) sys._getframe(0) &lt;frame object at 0x1032ab290&gt; # this is the current frame (Pdb) sys._getframe(0).f_globals['__name__'] 'my_module' # Next outer frame (Pdb) sys._getframe(1).f_globals['__name__'] 'pdb' # etc... # In this example, frame 10 happens to be # the one from the outer interpreter (Pdb) sys._getframe(10).f_globals['__name__'] '__main__' </code></pre> <p>So here's a quick and dirty function that walks up the stack looking for <code>'__name__'</code> with a value of <code>'__main__'</code> in frame globals:</p> <p><strong>debughelper.py</strong>:</p> <pre><code>import sys # Be safe and define a maximum of frames we're trying to walk up MAX_FRAMES = 20 def save_to_interactive(dct): n = 0 # Walk up the stack looking for '__name__' # with a value of '__main__' in frame globals for n in range(MAX_FRAMES): cur_frame = sys._getframe(n) name = cur_frame.f_globals.get('__name__') if name == '__main__': # Yay - we're in the stack frame of the interactive interpreter! # So we update its frame globals with the dict containing our data cur_frame.f_globals.update(dct) break </code></pre> <p>Usage:</p> <pre><code>&gt;&gt;&gt; import my_module &gt;&gt;&gt; my_module.fun('foo') &gt; /Users/lukasgraf/src/stackoverflow/my_module.py(3)fun() -&gt; print arg (Pdb) import debughelper (Pdb) debughelper.save_to_interactive({'mykey': 42}) (Pdb) c foo # We continued PDB, so we're in the outer interpreter again &gt;&gt;&gt; print mykey 42 &gt;&gt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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