Skip to content

Commit

Permalink
Merge a1c17b8 into a118ee5
Browse files Browse the repository at this point in the history
  • Loading branch information
yakobu committed Apr 14, 2019
2 parents a118ee5 + a1c17b8 commit 5d32df8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 16 additions & 6 deletions ipdbugger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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[:]
Expand Down
23 changes: 23 additions & 0 deletions tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d32df8

Please sign in to comment.