Why not use $XDG_CONFIG_HOME/pdb/.pdbrc?

This question may come to mind when fine-tuning pdb.

For example, setting up .pdb_history. The widespread approach is to put instructions to .pdbrc that will load some code from, say, pdbrc.py. Storing both in $HOME? Too much clutter. Moving pdbrc.py to ~/.config/pdb/ sounds better, but why not move both?

The problem is that pdb supports only ./.pdbrc and ~/.pdbrc locations at the moment. Yes, adding support for new .pdbrc location will cost some lines of code python/cpython#133733, but user’s $HOME will be more organized.

Example ~/.pdbrc (:sad:) and ~/.config/pdb/pdbrc.py
# ~/.pdbrc
import os as _os
with open(_os.path.expanduser('~/.config/pdb/pdbrc.py')) as _f: _f = _f.read()
exec(_f)
del _f, _os
# ~/.config/pdb/pdbrc.py
def _pdbrc_init():
    """
    Save history across sessions.
    """
    import atexit
    import os
    import readline

    HISTFILE = '~/.pdb_history'
    HISTFSIZE = 1000

    readline.parse_and_bind('tab: complete')
    try:
        readline.read_init_file()
    except OSError:
        pass

    history = os.path.expanduser(os.getenv('PDB_HISTFILE', HISTFILE))
    if readline.get_current_history_length() == 0:
        if os.path.exists(history):
            readline.read_history_file(history)

    try:
        size = int(os.getenv('PDB_HISTSIZE', ''))
    except ValueError:
        size = HISTFSIZE
    readline.set_history_length(size)

    def save_history(path):
        try:
            readline.write_history_file(path)
        except (FileNotFoundError, PermissionError):
            from warnings import warn
            warn(f'Unable to write Pdb history file {path}')

    atexit.register(save_history, history)


_pdbrc_init()
del _pdbrc_init

What do you think?

I wish all the .rc was in .config as well.

For backwards compatibility if .config/pdb/pdbrc.py is missing then fallback to the current ~/.pdbrc?

@barry-scott yep, this is how it could look like: https://cpython-previews–133733.org.readthedocs.build/en/133733/library/pdb.html#debugger-commands (need to scroll down a little bit)