Use explanatory functions for gnarly exception handling logic.

This commit is contained in:
Daniel Watkins
2013-12-04 11:32:43 +00:00
parent 30876c0a17
commit 1b9591be7e

View File

@@ -23,6 +23,14 @@ from ._impl import (
_error_repr = BaseException.__repr__
def _is_exception(exc):
return isinstance(exc, BaseException)
def _is_user_exception(exc):
return isinstance(exc, Exception)
class MatchesException(Matcher):
"""Match an exc_info tuple against an exception instance or type."""
@@ -104,10 +112,10 @@ class Raises(Matcher):
else:
mismatch = None
# The exception did not match, or no explicit matching logic was
# performed. If the exception is a non-user exception (that is, not
# a subclass of Exception) then propagate it.
if (isinstance(exc_info[1], BaseException)
and not isinstance(exc_info[1], Exception)):
# performed. If the exception is a non-user exception then
# propagate it.
exception = exc_info[1]
if _is_exception(exception) and not _is_user_exception(exception):
del exc_info
raise
return mismatch