From bdf93421b151193ec1a5344045a30ef6a5f97a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Wed, 10 Jun 2026 23:06:27 +0200 Subject: [PATCH] python: improve pdb readline history --- python/.pdb_startup.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/python/.pdb_startup.py b/python/.pdb_startup.py index 164e8ad..2b91583 100644 --- a/python/.pdb_startup.py +++ b/python/.pdb_startup.py @@ -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): - readline.read_history_file(historyPath) + try: + readline.read_history_file(historyPath) + except OSError: + pass - def save_history(historyPath=historyPath): - import readline - readline.write_history_file(historyPath) + 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