From 11f743439806eb189f2cf2919d16f02eacecb702 Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Tue, 6 Apr 2021 22:00:27 -0500 Subject: [PATCH] Add 'iex' convenience function as a decorator version of launch_ipdb_on_exception --- ipdb/__init__.py | 2 +- ipdb/__main__.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ipdb/__init__.py b/ipdb/__init__.py index fb0970e..75e8fed 100644 --- a/ipdb/__init__.py +++ b/ipdb/__init__.py @@ -4,7 +4,7 @@ # Redistributable under the revised BSD license # https://opensource.org/licenses/BSD-3-Clause -from ipdb.__main__ import set_trace, post_mortem, pm, run # noqa +from ipdb.__main__ import set_trace, post_mortem, pm, run, iex # noqa from ipdb.__main__ import runcall, runeval, launch_ipdb_on_exception # noqa from ipdb.stdout import sset_trace, spost_mortem, spm # noqa diff --git a/ipdb/__main__.py b/ipdb/__main__.py index f8f877c..3965725 100644 --- a/ipdb/__main__.py +++ b/ipdb/__main__.py @@ -228,6 +228,29 @@ def launch_ipdb_on_exception(): pass +class Decontext(object): + """ + Makes a context manager also act as decorator. + """ + def __init__(self, context_manager): + self._cm = context_manager + + def __enter__(self): + return self._cm.__enter__() + + def __exit__(self, *args, **kwds): + return self._cm.__exit__(*args, **kwds) + + def __call__(self, func): + def wrapper(*args, **kwds): + with self: + return func(*args, **kwds) + return wrapper + + +iex = Decontext(launch_ipdb_on_exception()) + + _usage = """\ usage: python -m ipdb [-m] [-c command] ... pyfile [arg] ...