Created
April 12, 2022 08:29
-
-
Save lucaswiman/fbf0ac2168da7b8d358a4d1128aee230 to your computer and use it in GitHub Desktop.
Context manager to add a breakpoint at a particular code location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pdb import Pdb, set_trace | |
from contextlib import contextmanager | |
@contextmanager | |
def debug_at(filename, lineno, condition=None): | |
p = Pdb() | |
filename = p.canonic(filename) | |
break_command = f"break {filename}:{lineno}" + (f', {condition}' if condition else '') | |
p.rcLines.append(break_command) | |
p.rcLines.append('continue') | |
try: | |
p.set_trace() | |
yield | |
finally: | |
p.clear_break(filename, lineno) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g.: