gotcha / ipdb

Integration of IPython pdb
BSD 3-Clause "New" or "Revised" License
1.85k stars 146 forks source link

Add way to launch debugger once only #238

Closed stuaxo closed 2 years ago

stuaxo commented 2 years ago

Often I'm debugging something that may run several times (it may be some code in a unit test), I want a way to trigger ipdb only on the first time.

At the moment I might do something hacky like this:

def test_some_pytest_test():
    if not hasattr(test_some_pytest_test, "has_run"):
        setattr(test_some_pytest_test, "has_run", True)
        ipdb.set_trace()

But more often I put in a single set_trace(), continue everything with "c" then frantically hit Ctrl-C... (usually "q" doesn't actually quit in unit tests).

So it would be nice to have a proper way of just breaking once, this could be a new API call or maybe passing a parameter to launch_ipdb_on_exception.

alexandrebarbaruiva commented 2 years ago

You can use ipdb.set_trace(cond=first_run), it'll only run when first_run is True

stuaxo commented 2 years ago

Is first_run a variable I keep track of ?

alexandrebarbaruiva commented 2 years ago

@stuaxo you create a variable first_run or whatever you want to name it

alexandrebarbaruiva commented 2 years ago

cond checks if a given value is true to stop on breakpoint