Skip to content

Commit

Permalink
Merge 8244d66 into 58011a3
Browse files Browse the repository at this point in the history
  • Loading branch information
osherdp committed Apr 28, 2019
2 parents 58011a3 + 8244d66 commit 2f44a79
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
39 changes: 23 additions & 16 deletions ipdbugger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def start_debugging():
IPDBugger(exc_info=sys.exc_info()).set_trace(test_frame)


def get_value(ast_node):
"""Return a comparable object for the ast node."""
return ast.dump(ast_node)


class ErrorsCatchTransformer(ast.NodeTransformer):
"""Surround each statement with a try/except block to catch errors."""

Expand All @@ -114,11 +119,13 @@ def __init__(self, ignore_exceptions=(), catch_exception=None, depth=0):
self.ignore_exceptions = None

if ignore_exceptions is not None:
self.ignore_exceptions = [exception_class.__name__
for exception_class in ignore_exceptions]
self.ignore_exceptions = [
ast.Name(exception_class.__name__, ast.Load())
for exception_class in ignore_exceptions]

if catch_exception is not None:
self.catch_exception = catch_exception.__name__
self.catch_exception = ast.Name(catch_exception.__name__,
ast.Load())

@property
def ast_try_except(self):
Expand All @@ -134,24 +141,26 @@ def wrap_with_try(self, node):
body=[ast.Raise()]))

else:
ignores_nodes = [ast.Name(exception_class, ast.Load())
for exception_class in self.ignore_exceptions]
ignores_nodes = self.ignore_exceptions

handlers.append(ast.ExceptHandler(type=ast.Tuple(ignores_nodes,
ast.Load()),
name=None,
body=[ast.Raise()]))

if self.catch_exception not in self.ignore_exceptions:
if self.catch_exception is None or \
get_value(self.catch_exception) not in \
(get_value(ast_node)
for ast_node in self.ignore_exceptions):

call_extra_parameters = [] if IS_PYTHON_3 else [None, None]
start_debug_cmd = ast.Expr(
value=ast.Call(ast.Name("start_debugging", ast.Load()),
[], [], *call_extra_parameters))

catch_exception_type = None
if self.catch_exception is not None:
catch_exception_type = ast.Name(self.catch_exception,
ast.Load())
catch_exception_type = self.catch_exception

handlers.append(ast.ExceptHandler(type=catch_exception_type,
name=None,
Expand All @@ -175,11 +184,11 @@ def try_except_handler(self, node):
break

if isinstance(handler.type, ast.Tuple):
excepted_types.extends([exception_type.id for exception_type
in handler.type.elts])
excepted_types.extend([exception_type for exception_type
in handler.type.elts])

else:
excepted_types.append(handler.type.id)
excepted_types.append(handler.type)

new_exception_list = self.ignore_exceptions

Expand Down Expand Up @@ -214,12 +223,10 @@ def visit_Call(self, node):
ignore_exceptions = ast.Name("None", ast.Load())

else:
exception_names = [ast.Name(exception, ast.Load())
for exception in self.ignore_exceptions]
ignore_exceptions = ast.List(exception_names, ast.Load())
ignore_exceptions = ast.List(self.ignore_exceptions, ast.Load())

catch_exception_type = self.catch_exception if self.catch_exception \
else "None"
catch_exception_type = self.catch_exception \
if self.catch_exception else "None"

catch_exception = ast.Name(catch_exception_type, ast.Load())
depth = ast.Num(self.depth - 1 if self.depth > 0 else -1)
Expand Down
53 changes: 51 additions & 2 deletions tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from IPython.utils.capture import capture_output

from tests import utils
from ipdbugger import debug

try:
Expand Down Expand Up @@ -138,8 +139,8 @@ def func():
func()


def test_ignoring_excepted_exceptions():
"""Test ignoring exceptions that should be excepted."""
def test_ignoring_excepted_exception():
"""Test ignoring an exception that's excepted."""
@debug
def func():
try:
Expand All @@ -150,6 +151,54 @@ def func():
func()


def test_ignoring_excepted_specific_exception():
"""Test ignoring a specific exception that's excepted."""
def func():
try:
raise ValueError()
except ValueError:
pass

debug(func, catch_exception=ValueError)
func()


def test_ignoring_excepted_multiple_exception():
"""Test ignoring exceptions that are excepted."""
@debug
def func():
try:
raise ValueError()
except (ValueError, KeyError):
pass

func()


def test_ignoring_attribute_exception():
"""Test ignoring an exception that's excepted."""
@debug
def func():
try:
raise utils.TestError()
except utils.TestError:
pass

func()


def test_ignoring_excepted_specific_attribute_exception():
"""Test ignoring a specific exception that's excepted."""
def func():
try:
raise utils.TestError()
except utils.TestError:
pass

debug(func, catch_exception=utils.TestError)
func()


def test_ignoring_excepted_exceptions_only_on_try_except_scope():
"""Test ignoring exceptions that should be only on try except scope."""
def func():
Expand Down
5 changes: 5 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Utils for the Ipdbugger unittests."""


class TestError(Exception):
"""Exception class for the tests."""

0 comments on commit 2f44a79

Please sign in to comment.