forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
66 lines (53 loc) · 2.25 KB
/
base.py
File metadata and controls
66 lines (53 loc) · 2.25 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Libraries are not as fine-grained with exception classes as one would like. By
using this hack, you can make create your own exceptions which behave as if
they are superclasses of the ones raised by the standard library.
Tread with caution.
Defines the base `instance_checking_exception` creator.
"""
import re
import sys
def instance_checking_exception(base_exception=Exception):
def wrapped(instance_checker):
"""
Create an exception class which inspects the exception being raised.
This is most easily used as a decorator:
>>> @instance_checking_exception()
... def Foo(inst):
... return "Foo" in inst.message
>>> try:
... raise Exception("Something Fooish")
... except Foo as e:
... print "True"
... except Exception:
... print "False"
True
This is quite a powerful tool, mind you.
Arguments:
instance_checker (callable): A function which checks if the given
instance should be treated as an instance of a (subclass) of this
exception.
Returns:
Exception: (Actually: a new subclass of it), which calls the argument
`instance_checker` when it is checked against during exception
handling logic.
"""
class TemporaryClass(base_exception):
def __init__(self, *args, **kwargs):
if len(args) == 1 and isinstance(args[0], TemporaryClass):
unwrap_me = args[0]
for attr in dir(unwrap_me):
if not attr.startswith('__'):
setattr(self, attr, getattr(unwrap_me, attr))
else:
super(TemporaryClass, self).__init__(*args, **kwargs)
class __metaclass__(type):
def __instancecheck__(cls, inst):
return instance_checker(inst)
def __subclasscheck__(cls, classinfo):
value = sys.exc_info()[1]
return isinstance(value, cls)
TemporaryClass.__name__ = instance_checker.__name__
TemporaryClass.__doc__ = instance_checker.__doc__
return TemporaryClass
return wrapped