pythonparsinglocal-variablespdb

More parsing-friendly Python debugger locals() output


I need the locals() text output to be easier to parse then what the default output is. I am using a IDE that I wrote in C that starts a tty() to run pdb and it captures the output from locals() and parses it into useable data for my IDE.

I wrote a few Python functions that can output the format I am looking for but I can't get the debugger to use those functions without including it directly in the source code I am trying to debug. I made a package (pdbext) from them and sending the import pdbext to pdb injects that into the code I am debugging in the current function as an another local instead of pdb loading it outside into the debugger itself. I want to avoid having to include a debug package in every source file.

I also tried to extend the pdb.Pdb class and include those functions as members in that class and use a do_parse_locals () class method. However pdb no longer worked. Typing a simple next would drop it back to the shell prompt.

What could I do to achieve this?


Solution

  • Put your custom formatting behind a pdb alias in a .pdbrc file, so you don’t have to touch the code being debugged.

    Example ~/.pdbrc:

    !import pdbext
    alias pl !pdbext.print_locals(locals())
    

    pdb loads .pdbrc on start, so now in any pdb session you can just type:

    (Pdb) pl
    

    and get your parse-friendly locals() output, without injecting imports into the target program or subclassing Pdb.