Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add guide to install ipdb to sys.excepthook #272

Open
hacker-DOM opened this issue Jul 20, 2023 · 4 comments
Open

Feature request: Add guide to install ipdb to sys.excepthook #272

hacker-DOM opened this issue Jul 20, 2023 · 4 comments

Comments

@hacker-DOM
Copy link

hacker-DOM commented Jul 20, 2023

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 cansarigol/pdbr#66.

@Jasha10
Copy link

Jasha10 commented Jul 26, 2023

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

import sys
import ipdb

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

@hacker-DOM
Copy link
Author

hacker-DOM commented Jul 29, 2023

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 ?

@Jasha10
Copy link

Jasha10 commented Jul 29, 2023

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.

@hacker-DOM
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants