If you want to wrap only some internal part of the function, or if you need to decorate several functions, you can use the context manager as an alternative to the accepted answer. I am currently using this simple version that catches all exceptions. I would also recommend using pudb
from contextlib import contextmanager @contextmanager def postmortem_pudb(): try: yield except Exception as exc: pudb.post_mortem()
Use like this
with postmortem_pudb(): function_that_might_throw_some() ... another_function_that_might_throw_some() ... yet_another_function_that_might_throw_some()
smido source share