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 pdb
try:
import readline
historyPath = os.path.expanduser("~/.pdb_history")
readline.set_history_length(1000)
readline.clear_history()
if os.path.exists(historyPath):
try:
readline.read_history_file(historyPath)
except OSError:
pass
def save_history(historyPath=historyPath):
import readline
class PdbWithHistory(pdb.Pdb):
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)
except OSError:
pass
pdb.Pdb = PdbWithHistory
atexit.register(save_history)
except ImportError:
pass