【问题标题】:Save command history in pdb在 pdb 中保存命令历史记录
【发布时间】:2012-04-27 07:21:51
【问题描述】:

有没有办法跨会话保存 pdb(python 调试器)命令历史记录?另外,我可以指定历史长度吗?

这与问题 How can I make gdb save the command history? 类似,但是是针对 pdb 而不是 gdb。

-非常感谢

【问题讨论】:

  • Ubuntu 11.04 Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2
  • 您解决了这个问题吗?我也想有类似的功能。

标签: python debugging


【解决方案1】:

this 帖子。可以将历史记录保存在 pdb 中。默认情况下,pdb 不会读取多行。所以所有函数都需要在一行中。

在~/.pdbrc:

import atexit
import os
import readline

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath): import readline; readline.write_history_file(historyPath)

if os.path.exists(historyPath): readline.read_history_file(historyPath)

atexit.register(save_history, historyPath=historyPath)

【讨论】:

  • 确保你也从这个文件中删除了旧条目,在我的例子中,它可以在一个月内增长到 2GB 左右。
【解决方案2】:

致谢:https://wiki.python.org/moin/PdbRcIdea

pdb 使用 readline,因此我们可以指示 readline 保存历史记录:

.pdbrc

# NB: `pdb` only accepts single-line statements
import os
with open(os.path.expanduser("~/.pdbrc.py")) as _f: _f = _f.read()
exec(_f)
del _f

.pdbrc.py

def _pdbrc_init():
    # Save history across sessions
    import readline
    histfile = ".pdb-pyhist"
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    import atexit
    atexit.register(readline.write_history_file, histfile)
    readline.set_history_length(500)

_pdbrc_init()
del _pdbrc_init

对于直接替换pdb++,将上述函数代码复制到setup()方法中:

from pdb import DefaultConfig, Pdb


class Config(DefaultConfig):
    def setup(self, pdb):
        ## Save history across sessions
        #
        import readline
        ...

【讨论】:

  • 这对pdbpp / pdb++ 调试器非常有效。谢谢!
  • 要将历史文件放在您的主目录中,请参阅this answer
【解决方案3】:

清除/读取/打印当前pdb历史的示例:

(Pdb) readline.clear_history()
(Pdb) print('hello pdb')
hello pdb
(Pdb) from pprint import pprint; import readline
(Pdb) y = range(readline.get_current_history_length() + 2)
(Pdb) print([readline.get_history_item(x) for x in y])

输出:

[None, 
"print('hello pdb')", 
'from pprint import pprint; import readline', 
'y = range(readline.get_current_history_length() + 2)',
'print([readline.get_history_item(x) for x in y])']

参考:

没有readline.clear_history 的两个班轮到目前为止输入到 pdb 的内容:

from pprint import pprint; import readline
pprint([readline.get_history_item(x) for x in range(readline.get_current_history_length() + 1)])

【讨论】:

  • 这是一个有用的例子,说明如何访问和删除 readline 库记录的历史记录。
【解决方案4】:

我不相信“股票” pdb 有办法。但是我写了一个替代调试器来做到这一点。

只需从源代码安装 Pycopia:http://code.google.com/p/pycopia/source/checkout,它位于 pycopia.debugger 中。

【讨论】:

    【解决方案5】:

    扩展@olejorgenb 的优秀answer,我希望历史文件在我的主目录中而不是在当前目录中,所以我使用了pathlib.Path.expanduser

    import pdb
    
    class Config(pdb.DefaultConfig):
    
        def setup(self, pdb):
            # Save history across sessions
            import readline
            from pathlib import Path
            histfile_path = Path("~/.pdb-pyhist").expanduser()
    
            try:
                readline.read_history_file(histfile_path)
            except IOError:
                pass
    
            import atexit
            atexit.register(readline.write_history_file, histfile_path)
            readline.set_history_length(500)
    
    

    这是我用于配置pdbpp(改进的调试器,又名pdb++https://pypi.org/project/pdbpp/)的设置。您可以将相同的想法与@olejorgenb 的answer 一起使用来配置常规的pdb

    【讨论】:

      【解决方案6】:

      我认为你可以用 IPython 做到这一点:

      http://ipython.org/ipython-doc/stable/interactive/tutorial.html#history

      pdb 的 ipdb 替换:

      http://pypi.python.org/pypi/ipdb

      【讨论】:

      • 你确定这可以用 ipdb 完成吗?我无法从旧的 ipdb 会话中获取历史记录。
      • IPDB 不支持与 IPython 相同的魔术命令。除非你能告诉我一种在 IPDB 中输入 %magic-commands 的方法,否则不赞成投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 2021-02-10
      • 2013-08-17
      相关资源
      最近更新 更多