• Drop into REPL when your program crashes.

    From Annada Behera@annada@tilde.green to comp.lang.python on Mon Sep 8 16:37:23 2025
    From Newsgroup: comp.lang.python

    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 = 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 = debug_hook
    Even when not debugging, it helps me salvage the results if a 3 week
    running experiment fails at 90% completion.
    Annada
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Mon Sep 8 12:34:16 2025
    From Newsgroup: comp.lang.python

    Annada Behera <annada@tilde.green> wrote or quoted:
    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 = debug_hook

    Thanks!

    I have changed it a bit to work without "ipdb" and added demo code.

    import sys
    import os
    import pdb

    i = 1

    def debug_exception_hook(exc_type, exc_value, tb):
    i = 2
    if issubclass(exc_type, KeyboardInterrupt):
    # Call the default excepthook for KeyboardInterrupt to allow clean exit
    sys.__excepthook__(exc_type, exc_value, tb)
    return
    print(f"Uncaught exception: {exc_type.__name__}: {exc_value}")
    pdb.post_mortem(tb)

    def cause_exception():
    # Function to demonstrate an unhandled exception
    i = 3
    return 1 / 0 # Will raise ZeroDivisionError

    def f():
    # Function to demonstrate an unhandled exception
    i = 4
    cause_exception()
    return i

    if __name__ == "__main__": # demo code
    import os; os.environ['DEBUG'] = 'true'

    if os.environ.get('DEBUG'):
    sys.excepthook = debug_exception_hook
    print("Debug mode enabled: Using pdb post-mortem on uncaught exceptions.")
    else:
    print("Debug mode not enabled: Regular exception handling.")

    # Run demo to trigger exception
    i = 5
    f()

    Then, I got this dialog:

    Debug mode enabled: Using pdb post-mortem on uncaught exceptions.
    Uncaught exception: ZeroDivisionError: division by zero
    debug_hook.py(19)cause_exception()
    return 1 / 0 # Will raise ZeroDivisionError
    (Pdb) i
    3
    (Pdb)

    .


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ethan Carter@ec1828@somewhere.edu to comp.lang.python on Mon Sep 8 21:21:20 2025
    From Newsgroup: comp.lang.python

    Annada Behera <annada@tilde.green> writes:

    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 = debug_hook

    Pretty nice contribution! I had no idea such thing was possible with
    Python. The more Common Lispy it gets, the better it feels! :>
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Annada Behera@annada@tilde.green to comp.lang.python on Tue Sep 9 11:53:07 2025
    From Newsgroup: comp.lang.python

    So, ipdb is the ipython version of pdb. In fact, post_mortem is a
    pdb function. I use ipdb because its REPL is a bit nicer to work
    with then pdb.
    -----Original Message-----
    From: Stefan Ram <ram@zedat.fu-berlin.de>
    Subject: Re: Drop into REPL when your program crashes.
    Date: 09/08/2025 06:04:16 PM
    Newsgroups: comp.lang.python
    Annada Behera <annada@tilde.green> wrote or quoted:
    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 = debug_hook
      Thanks!
      I have changed it a bit to work without "ipdb" and added demo code.
    import sys
    import os
    import pdb
    i = 1
    def debug_exception_hook(exc_type, exc_value, tb):
        i = 2
        if issubclass(exc_type, KeyboardInterrupt):
            # Call the default excepthook for KeyboardInterrupt to allow clean exit
            sys.__excepthook__(exc_type, exc_value, tb)
            return
        print(f"Uncaught exception: {exc_type.__name__}: {exc_value}")
        pdb.post_mortem(tb)
    def cause_exception():
        # Function to demonstrate an unhandled exception
        i = 3
        return 1 / 0  # Will raise ZeroDivisionError
    def f():
        # Function to demonstrate an unhandled exception
        i = 4
        cause_exception()
        return i
    if __name__ == "__main__": # demo code
        import os; os.environ['DEBUG'] = 'true'
        if os.environ.get('DEBUG'):
            sys.excepthook = debug_exception_hook
            print("Debug mode enabled: Using pdb post-mortem on uncaught exceptions.")
        else:
            print("Debug mode not enabled: Regular exception handling.")    
        # Run demo to trigger exception
        i = 5
        f()
      Then, I got this dialog:
    Debug mode enabled: Using pdb post-mortem on uncaught exceptions.
    Uncaught exception: ZeroDivisionError: division by zero
    debug_hook.py(19)cause_exception()
    return 1 / 0  # Will raise ZeroDivisionError
    (Pdb) i
    3
    (Pdb)
      .
    --- Synchronet 3.21a-Linux NewsLink 1.2