Path: csiph.com!weretis.net!feeder9.news.weretis.net!news.quux.org!tilde.green!.POSTED.103.160.128.42!not-for-mail From: Annada Behera Newsgroups: comp.lang.python Subject: Drop into REPL when your program crashes. Date: Mon, 08 Sep 2025 16:37:23 +0530 Organization: tilde.green Sender: annada@tilde.green Message-ID: <7a0dbcc10639dce0c40a6c4b3972f19bde2fc14e.camel@tilde.green> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: tilde.green; posting-account="annada@tilde.green"; posting-host="103.160.128.42"; logging-data="1988464"; mail-complaints-to="admins@tilde.green" User-Agent: Evolution 3.56.2 Xref: csiph.com comp.lang.python:197557 Hi, Recently I have been increasingly adding this piece of code as a preamble to a lot of my code. import (sys, os, ipdb) def debug_hook(exc_type, exc_value, traceback): if exc_type is KeyboardInterrupt: sys.__excepthook__(exc_type, exc_value, traceback) return print(f"Uncaught exception: {exc_type.__name__}: {exc_value}") ipdb.post_mortem(traceback) if os.environ.get('DEBUG'): sys.excepthook =3D debug_hook This has been extemely helpful when debugging and even in prod. When degugging, this helps me go up and down my backtrace like I am in gdb. In fact, it is better than gdb. I can use evaluate any python expression directly, and verify the shape of my tensors and exactly what caused the error. It's like freezing the entire program right at the time the program failed. This way I don't have to second guess what exactly failed in the entire training loop and the next time I can fix it. if True or os.environ.get('DEBUG'): sys.excepthook =3D debug_hook Even when not debugging, it helps me salvage the results if a 3 week running experiment fails at 90% completion. Annada