ipdb icon indicating copy to clipboard operation
ipdb copied to clipboard

Feature request: Add guide to install `ipdb` to `sys.excepthook`

Open hacker-DOM opened this issue 2 years ago • 5 comments

Hi guys!

It seems like this is possible with with launch_ipdb_on_exception, but it would be great if there was a way to do it globally, for debugging a third-party library. Thanks in advance 👍

Ps: This issue is analogous to https://github.com/cansarigol/pdbr/issues/66.

hacker-DOM avatar Jul 20 '23 19:07 hacker-DOM

@hacker-DOM I've had success with the following:

import sys
import ipdb

sys.breakpointhook = ipdb.set_trace
sys.excepthook = lambda *args: ipdb.pm()

Jasha10 avatar Jul 26 '23 17:07 Jasha10

Hmm, yeah it seems to work. The only weird thing is they don't work together :D

import sys
import ipdb

sys.breakpointhook = ipdb.set_trace
sys.excepthook = lambda *args: ipdb.pm()

def main():
    breakpoint()
    print("Hello World!")
    print("Hello World!")
    print("Hello World!")
    assert False
    print("Hello World!")

main()

breakpoint launches ipdb, I enter continue and get a plain old assertion error (w/o entering ipdb)

I'm on python==3.8.12 and ipdb==0.13.13; can you reproduce this behavior @Jasha10 ?

hacker-DOM avatar Jul 29 '23 07:07 hacker-DOM

Yes, I can reproduce the behavior. Once the breakpoint() gets hit and sys.breakpointhook is called, ipdb.set_trace overwrites sys.excepthook.

# tmp.py
import sys
import ipdb

sys.breakpointhook = ipdb.set_trace
sys.excepthook = lambda *args: ipdb.pm()

print("BEFORE BREAKPOINT")
print(f"{sys.excepthook=}")
print(f"{sys.excepthook.__module__=}")
breakpoint()
print("AFTER BREAKPOINT")
print(f"{sys.excepthook=}")
print(f"{sys.excepthook.__module__=}")
$ python tmp.py
BEFORE BREAKPOINT
sys.excepthook=<function <lambda> at 0x7faa313a0540>
sys.excepthook.__module__='__main__'
> /home/homestar/tmp/tmp.py(12)<module>()
     11 breakpoint()
---> 12 print("AFTER BREAKPOINT")
     13 print(f"{sys.excepthook=}")

ipdb> continue
AFTER BREAKPOINT
sys.excepthook=<bound method BaseIPythonApplication.excepthook of <IPython.terminal.ipapp.TerminalIPythonApp object at 0x7faa2f522a50>>
sys.excepthook.__module__='IPython.core.application'

This happens because the ipdb.set_trace function makes a call to wrap_sys_excepthook.

Jasha10 avatar Jul 29 '23 16:07 Jasha10

Interesting. @gotcha do you think there would be a way to enable ipdb globally w/o this gotcha? Thanks in advance

hacker-DOM avatar Aug 01 '23 07:08 hacker-DOM