python: improve pdb readline history

This commit is contained in:
J. Fernando Sánchez
2026-06-10 23:06:27 +02:00
parent 7d4f31a6b4
commit bdf93421b1

View File

@@ -1,17 +1,33 @@
import atexit
import os import os
import pdb
try: try:
import readline import readline
historyPath = os.path.expanduser("~/.pdb_history") historyPath = os.path.expanduser("~/.pdb_history")
readline.set_history_length(1000)
readline.clear_history() readline.clear_history()
if os.path.exists(historyPath): if os.path.exists(historyPath):
try:
readline.read_history_file(historyPath) readline.read_history_file(historyPath)
except OSError:
pass
def save_history(historyPath=historyPath): class PdbWithHistory(pdb.Pdb):
import readline def default(self, line):
readline.append_history_file(1, historyPath)
return super().default(line)
def cmdloop(self, intro=None):
try:
super().cmdloop(intro)
finally:
try:
readline.write_history_file(historyPath) readline.write_history_file(historyPath)
except OSError:
pass
pdb.Pdb = PdbWithHistory
atexit.register(save_history)
except ImportError: except ImportError:
pass pass