forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
32 lines (25 loc) · 965 Bytes
/
testing.py
File metadata and controls
32 lines (25 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from functools import wraps
def passthrough(x):
return x
def raises(*allowed_types):
"""Document and enforce all possible exceptions a function might raise.
All other exceptions raised by the decorated functions are replaced
with AssertionErrors. This avoids masking the error if an
inappropriate exception type is caught at a higher level. (Your code
*never* handles AssertionErrors... *right*?)
Since this functionality relies on assertions, the decorator is
replaced with a zero-overhead passthrough if assertions are
disabled.
"""
if not __debug__:
return passthrough
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except BaseException as exc: # Gotta catch 'em all
assert isinstance(exc, allowed_types), type(exc)
raise
return wrapper
return decorator