将变量保存到python调试器的交互式命名空间 [英] save variable to interactive namespace from python debugger

查看:127
本文介绍了将变量保存到python调试器的交互式命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在一个交互式会话(在我的情况下 ipython )运行时,目前正在一个调试器( ipdb pdb )我想能够在调试命名空间内将变量放入主要的交互式命名空间。

When I am running inside an interactive session (in my case ipython), and am currently inside a debugger (ipdb or pdb) I would like to be able to put a variable into the main interactive namespace from within the debugging namespace.

如果我的代码崩溃,但是已经做了很多工作,其中一些可以节省时间(例如从磁盘加载数据),这很有用。

This is useful if my code crashes, but has already done significant work, some of which is salvageable to save time (for example, loading data from disk).

所以我想要的是这样的,例如:

so what I'd like is something like this, for example:

>>> run -m my.module
loading data from disk...
done loading data.
processing data...
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
...

- >这里在处理过程中出错,但是加载数据工作正常...所以我会进入调试器检查发生了什么,并看到 loaded_data

-> here something goes wrong during processing, but the loading of data worked fine... so I'd go into the debugger to check out what happened, and to see the loaded_data:

>>> debug
ipdb> len(loaded_data)
100000

- >然后我想保存变量到交互式命名空间,以便在调试器之外使用,如下所示:

-> Then I would like to be able to save this variable to the interactive namespace for use outside the debugger, like so:

ipdb> save_to_interactive('loaded_data')
ipdb> exit
>>> len(loaded_data)
100000


推荐答案

通过获取对外部解释器的栈
框架的引用,并写入其框架全局变量来实现此目的。

You can accomplish this by getting a reference to the outer interpreter's stack frame, and writing to its frame globals.

给出一个带有断点的示例模块, pdb:

Given a sample module with a breakpoint that kicks us into pdb:

my_module.py

def fun(arg):
    import pdb; pdb.set_trace()
    print arg

演示基本概念的示例:

    >>> import my_module
    >>> my_module.fun(1)
    > /Users/lukasgraf/src/stackoverflow/my_module.py(3)fun()
    -> print arg
    (Pdb) import sys
    (Pdb) sys._getframe(0)
    <frame object at 0x1032ab290>
    # 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__'

所以这里是一个快速而肮脏的功能,可以爬上堆栈寻找'__ name__ '在框架全局变量中的值为'__ main __

So here's a quick and dirty function that walks up the stack looking for '__name__' with a value of '__main__' in frame globals:

debughelper.py

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

用法:

>>> import my_module
>>> my_module.fun('foo')
> /Users/lukasgraf/src/stackoverflow/my_module.py(3)fun()
-> 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
>>> print mykey
42
>>>

这篇关于将变量保存到python调试器的交互式命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆