Issue 27565: Offer error context manager for code.interact (original) (raw)

When debugging code that raises unexpected exceptions, I often find myself doing this:

try:
    some_code
except:
    import code; code.interact(local=locals())
    raise

My suggestion is a context manager to make this less verbose:

with code.interact_on_error():
    some_code

The effect would be the same: if an exception is caught, code.interact is called with the locals of the function, and re-raised when code.interact ends.

Hi Claudiu, thanks for the report, but I don't think this is a common use case that needs to be supported in the standard library. Other people may prefer to use different solutions in a similar case (e.g. using a debugger) Plus, it's not hard to implement this as a custom context manager in your code.