Skip to content

Commit

Permalink
Use special exception hook for any exception object that defines a _h…
Browse files Browse the repository at this point in the history
…andle_() method. Version => 0.1.4
  • Loading branch information
st-pasha committed Jun 15, 2017
1 parent c3f52d3 commit d6ec4ef
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
# Runtime dependencies
install_requires=[
"colorama>=0.3",
"typesentry==" + version,
],
tests_require=[
"pytest>=3.0",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def foo(x):
foo("just a flesh wound!")
assert False, "Expected a failure above"
except TE as e:
e.report()
e._handle_()


def test_custom_exceptions():
class CustomError(TypeError):
def report(self):
def _handle_(self):
pass

conf = typesentry.Config(type_error=CustomError, value_error=CustomError,
Expand All @@ -37,7 +37,7 @@ def foo(x):
foo("bazinga!")
assert False, "Expected a failure"
except CustomError as e:
e.report()
e._handle_()


def test_disabled():
Expand Down
2 changes: 1 addition & 1 deletion typesentry/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"""

version = "0.1.3"
version = "0.1.4"
33 changes: 19 additions & 14 deletions typesentry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@


# Derive from the builtin TypeError
class TypeError(TypeError):
def report(self):
class TsTypeError(TypeError):
def _handle_(self):
_handle_tc_error(self)


# Derive from the builtin ValueError
class ValueError(ValueError):
def report(self):
class TsValueError(ValueError):
def _handle_(self):
_handle_tc_error(self)


TsTypeError.__name__ = "TypeError"
TsValueError.__name__ = "ValueError"



class Config(object):
"""
Expand All @@ -39,7 +43,7 @@ def foo(x):
"""

def __init__(self, type_error=TypeError, value_error=ValueError,
def __init__(self, type_error=TsTypeError, value_error=TsValueError,
disabled=False, soft_exceptions=True):
"""
Create new type-checking configuration.
Expand All @@ -51,18 +55,19 @@ def __init__(self, type_error=TypeError, value_error=ValueError,
error occurs. It is recommended that this class derives from the
standard ``ValueError``.
:param disabled: if True, then all type-checking will be disabled.
:param soft_exceptions: if True, then exceptions raised by this module
will use a custom exception handler (method ``.report()`` on the
exception object).
:param soft_exceptions: if True, then a custom exceptions handler will
be installed at the console level, which will catch any exception
with method ``._handle_()`` and use that method to report the
error.
"""
self.TypeError = type_error
self.ValueError = value_error
self.typed = self._make_typed(disabled)
if soft_exceptions:
assert callable(getattr(type_error, "report")), \
"Class %s missing method .report()" % type_error.__name__
assert callable(getattr(value_error, "report")), \
"Class %s missing method .report()" % value_error.__name__
assert callable(getattr(type_error, "_handle_")), \
"Class %s missing method ._handle_()" % type_error.__name__
assert callable(getattr(value_error, "_handle_")), \
"Class %s missing method ._handle_()" % value_error.__name__
self._install_exception_hooks()


Expand Down Expand Up @@ -94,8 +99,8 @@ def _install_exception_hooks(self):
def except_hook(exc_type, exc_value, exc_tb): # pragma: no cover
# This function cannot be tested, becauses it can only be executed
# by the Python's console
if isinstance(exc_value, (self.TypeError, self.ValueError)):
exc_value.report()
if hasattr(exc_value, "_handle_"):
exc_value._handle_()
else:
previous_except_hook(exc_type, exc_value, exc_tb)

Expand Down

0 comments on commit d6ec4ef

Please sign in to comment.