debugging 在pdb中保存命令历史记录

o4tp2gmn  于 2023-04-30  发布在  其他
关注(0)|答案(6)|浏览(124)

有没有一种方法可以跨会话保存pdb(python调试器)命令历史记录?另外,我可以指定历史记录长度吗?
这类似于问题How can I make gdb save the command history?,但对于pdb而不是gdb。

gg0vcinb

gg0vcinb1#

我不相信有一种方法与“股票”pdb。但我写了一个替代调试器来实现这一点。
只需从源代码安装Pycopia:http://code.google.com/p/pycopia/source/checkout在pycopia调试器

xfyts7mz

xfyts7mz2#

我想你可以用IPython做到这一点:
http://ipython.org/ipython-doc/stable/interactive/tutorial.html#history
pdb的Ipdb替换:
http://pypi.python.org/pypi/ipdb

hpxqektj

hpxqektj3#

看这个帖子。可以将历史保存在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)
d7v8vwbk

d7v8vwbk4#

制作人: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

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

from pdb import DefaultConfig, Pdb

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

bq9c1y665#

清除/阅读/打印当前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)])
jm2pwxwz

jm2pwxwz6#

扩展@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

相关问题