Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197558

Re: Drop into REPL when your program crashes.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups comp.lang.python
Subject Re: Drop into REPL when your program crashes.
Date 8 Sep 2025 12:34:16 GMT
Organization Stefan Ram
Lines 71
Expires 1 Jun 2026 11:59:58 GMT
Message-ID <debugging-20250908133058@ram.dialup.fu-berlin.de> (permalink)
References <7a0dbcc10639dce0c40a6c4b3972f19bde2fc14e.camel@tilde.green>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de HdeQiMacyIrzqcAdk3rx0g87SaJfmRlnvmntxk7N0h623H
Cancel-Lock sha1:64eT+MA7MIHTKFoHJs71eGwfrpY= sha256:qXzZJghUdR7gUcbExvIK3bzlfECSaA4fR2GtWCyVL98=
X-Copyright (C) Copyright 2025 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed.
X-No-Archive Yes
Archive no
X-No-Archive-Readme "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access.
X-No-Html yes
Content-Language en
Xref csiph.com comp.lang.python:197558

Show key headers only | View raw


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) 

  .

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Drop into REPL when your program crashes. Annada Behera <annada@tilde.green> - 2025-09-08 16:37 +0530
  Re: Drop into REPL when your program crashes. ram@zedat.fu-berlin.de (Stefan Ram) - 2025-09-08 12:34 +0000
    Re: Drop into REPL when your program crashes. Annada Behera <annada@tilde.green> - 2025-09-09 11:53 +0530
  Re: Drop into REPL when your program crashes. Ethan Carter <ec1828@somewhere.edu> - 2025-09-08 21:21 -0300

csiph-web