From a1c17b89347096c1aefb89eef248ae4df0730ccb Mon Sep 17 00:00:00 2001 From: ronen-y Date: Sun, 14 Apr 2019 08:59:22 +0300 Subject: [PATCH 1/2] add option of using debug as decorator with params --- ipdbugger/__init__.py | 22 ++++++++++++++++------ tests/test_debug.py | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ipdbugger/__init__.py b/ipdbugger/__init__.py index 9137d17..334dc3b 100644 --- a/ipdbugger/__init__.py +++ b/ipdbugger/__init__.py @@ -280,7 +280,7 @@ def get_last_lineno(node): return max_lineno -def debug(victim, ignore_exceptions=(), catch_exception=None, depth=0): +def debug(victim=None, ignore_exceptions=(), catch_exception=None, depth=0): """A decorator function to catch exceptions and enter debug mode. Args: @@ -297,6 +297,15 @@ def debug(victim, ignore_exceptions=(), catch_exception=None, depth=0): Note: This wrapper avoids recursion by setting a flag to each wrapped item. """ + if victim is None: + # Debug is used as a decorator so we need to return wrap function to + # get the real victim + def wrapper(real_victim): + return debug(real_victim, ignore_exceptions, + catch_exception, depth) + + return wrapper + if inspect.isfunction(victim): if hasattr(victim, '_ipdebug_wrapped'): # Don't wrap the function more than once @@ -342,12 +351,13 @@ def debug(victim, ignore_exceptions=(), catch_exception=None, depth=0): tree.body[0].body.insert(1, import_exception_cmd) - for exception_class in ignore_exceptions: - import_exception_cmd = ast.ImportFrom( - exception_class.__module__, - [ast.alias(exception_class.__name__, None)], 0) + if ignore_exceptions is not None: + for exception_class in ignore_exceptions: + import_exception_cmd = ast.ImportFrom( + exception_class.__module__, + [ast.alias(exception_class.__name__, None)], 0) - tree.body[0].body.insert(1, import_exception_cmd) + tree.body[0].body.insert(1, import_exception_cmd) # Delete the debugger decorator of the function del tree.body[0].decorator_list[:] diff --git a/tests/test_debug.py b/tests/test_debug.py index 9189dae..572746f 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -301,3 +301,26 @@ def func_upper(): SaveFuncName()) as name_saver: func_upper() assert name_saver.func_name == "func_lowest" + + +def test_ignore_all_exceptions(): + """Test ignoring all exceptions.""" + def func(): + raise Exception() + + func = debug(func, ignore_exceptions=None) + + with pytest.raises(Exception): + func() + + +def test_using_debug_as_decorator_with_kwargs(): + """Test using debug function as decorator with kwargs.""" + + @debug(catch_exception=ValueError) + def func(): + raise ValueError() + + with capture_output(), patch('bdb.Bdb.set_trace') as set_trace: + func() + assert set_trace.called From dbe80cc6e7c9e8504b19eb3fa678017599138648 Mon Sep 17 00:00:00 2001 From: ronen-y Date: Sun, 14 Apr 2019 14:00:34 +0300 Subject: [PATCH 2/2] bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fca8ef3..fea80a5 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ """Setup file for handling packaging and distribution.""" from setuptools import setup -__version__ = "2.3.0" +__version__ = "2.4.0" setup( name="ipdbugger",