0

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?

3
  • What I did was something similar to Lavi Kumar's answer without the aliases. In ~/.pdbrc I loaded the package with "_pdbext = imp.import_module ("pdbext.pdbext")" and accessed it at the (Pdb) prompt with "_pdbext.debug_disp_var_format (locals ())". It worked and that is also OK correct? Sorry I don't know how to format it properly and I don't think I want to answer it myself since I want to give credit to Lavi Kumar. Thank you also S Bonnet. Commented Nov 10 at 2:27
  • I ran into an issue. When pdb goes to another py file, it cannot find the _pdbext module: *** NameError: name '_pdbext' is not defined. Commented Nov 10 at 3:13
  • A related question was posted at stackoverflow.com/questions/79817159/… Commented Nov 12 at 9:33

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The simplest way:


import sys
import os

# Add your pdbext to path if needed
sys.path.insert(0, '/path/to/your/pdbext')
import pdbext

def _format_locals_cmd():
    """Function accessible to pdb commands"""
    # Get the current frame from the debugger
    frame = sys._getframe(1)
    output = []
    for name, value in sorted(frame.f_locals.items()):
        if not name.startswith('__'):
            output.append(f"{name}|{type(value).__name__}|{repr(value)[:100]}")
    return '\n'.join(output)

# Now create pdb alias that calls it
alias pl !print(_format_locals_cmd())

1 Comment

Where would the alias lines need to go? That's not valid Python syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.