セッション間でpdb(Pythonデバッガー)コマンド履歴を保存する方法はありますか?また、履歴の長さを指定できますか?
これは、gdbにコマンド履歴を保存させるにはどうすればよいですか?という質問に似ています。ただし、gdbではなくpdbの場合。
-どうもありがとう
セッション間でpdb(Pythonデバッガー)コマンド履歴を保存する方法はありますか?また、履歴の長さを指定できますか?
これは、gdbにコマンド履歴を保存させるにはどうすればよいですか?という質問に似ています。ただし、gdbではなくpdbの場合。
-どうもありがとう
この投稿を参照してください。履歴を pdb に保存することができます。デフォルトでは、pdb は複数行を読み取りません。したがって、すべての関数は 1 行に記述する必要があります。
~/.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)
クレジット: 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
...
現在の 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 に入力されたものを含まない 2 つのライナー:
from pprint import pprint; import readline
pprint([readline.get_history_item(x) for x in range(readline.get_current_history_length() + 1)])
「ストック」pdbを使用する方法があるとは思いません。しかし、私はそれを行う代替デバッガーを書きました。
ソースから Pycopia をインストールするだけです: http://code.google.com/p/pycopia/source/checkoutで、pycopia.debugger にあります。
IPythonでこれを行うことができると思います:
http://ipython.org/ipython-doc/stable/interactive/tutorial.html#history
pdbのipdb置換: