Save breakpoints to file
Asked Answered
J

1

10

When debugging my Python code, I run a script through ipdb from the commandline, and set a number of breakpoints. Then I make some changes in one or more modules, and rerun. However, if I simply use run modules do not get reloaded. To make sure they do, I can exist and restart Python completely, but then I need to reset all breakpoints, which is tedious if I have many and if done over and over again.

Is there a way to save breakpoint to a file in (i)pdb, so that after small changes that do not change line numbers, I can dump my breakpoints, restart Python + pdb, and reload my breakpoints? The equivalent to Matlabs X = dbstatus, saving/loading X, and setting dbstop(X).

Jewbaiting answered 3/3, 2015 at 20:44 Comment(0)
M
10

You can save the breakpoints to .pdbrc file in a working path or globally to your home dir. File should have something like this:

# breakpoint 1
break /path/to/file:lineno

# breakpoint 2
break /path/to/file:lineno

You can define breakpoints various ways, just like in the interactive mode. So just break 4 or break method will work too.

This file works for both, pdb and ipdb, since later has everything pdb has and more.

Bonus:

You could use alias to more easily save breakpoints. For example:

# append breakpoint to .pdbrc in current working directory
# usage: bs lineno
alias bs with open(".pdbrc", "a") as pdbrc: pdbrc.write("break " + __file__ + ":%1\n")

Put above to your global .pdbrc and use it like this:

> bs 15

This will append a breakpoint statement to a local .pdbrc file for a line 15 of current file.

It is not perfect solution, but close enough for me. Tune the command to your needs.

Read more about aliases here.

Monstrosity answered 21/11, 2016 at 23:29 Comment(2)
Is there a direct way of writing/appending the current set of breakpoints, apart from typing break and copy-pasting the results to .pdbrc?Jewbaiting
@Jewbaiting no, but you could take advantage of aliases. Updated the answer.Monstrosity

© 2022 - 2024 — McMap. All rights reserved.