Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save command history in pdb

Is there a way to save pdb (python debugger) command history across sessions? Also, can I specify history length?

This is similar to the question How can I make gdb save the command history?, however for pdb instead of gdb.

-Many thanks

like image 408
vkontori Avatar asked Apr 27 '12 07:04

vkontori


2 Answers

See this post. It is possible to save history in pdb. By default, pdb does not read multiple lines. So all functions need to be on a single line.

In ~/.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)
like image 139
irritable_phd_syndrome Avatar answered Nov 03 '22 09:11

irritable_phd_syndrome


Credits: https://wiki.python.org/moin/PdbRcIdea

pdb uses readline so we can instruct readline to save history:

.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

For the drop-in replacement pdb++, copy the above function code into the setup() method:

from pdb import DefaultConfig, Pdb


class Config(DefaultConfig):
    def setup(self, pdb):
        ## Save history across sessions
        #
        import readline
        ...
like image 31
olejorgenb Avatar answered Nov 03 '22 09:11

olejorgenb