-
Notifications
You must be signed in to change notification settings - Fork 48
/
warnings.py
34 lines (19 loc) · 872 Bytes
/
warnings.py
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
33
34
import warnings, contextlib
class NutilsWarning(Warning):
'Base class for warnings from Nutils.'
class NutilsDeprecationWarning(NutilsWarning):
'Warning about deprecated Nutils features.'
class NutilsInefficiencyWarning(NutilsWarning):
'Warning about inefficient runtime.'
def warn(message, category=NutilsWarning, stacklevel=1):
warnings.warn(message, category, stacklevel=stacklevel)
def deprecation(message):
warnings.warn(message, NutilsDeprecationWarning, stacklevel=2)
@contextlib.contextmanager
def via(print):
'''context manager to set/reset warnings.showwarning'''
oldshowwarning = warnings.showwarning
warnings.showwarning = lambda message, category, filename, lineno, *args: print(f'{category.__name__}: {message}\n In {filename}:{lineno}')
yield
warnings.showwarning = oldshowwarning
# vim:sw=4:sts=4:et