11"""
2+ Libraries are not as fine-grained with exception classes as one would like. By
3+ using this hack, you can make create your own exceptions which behave as if
4+ they are superclasses of the ones raised by the standard library.
5+
6+ Tread with caution.
7+
28Defines the base `instance_checking_exception` creator.
39"""
410
@@ -37,19 +43,7 @@ def wrapped(instance_checker):
3743 handling logic.
3844 """
3945 class TemporaryClass (base_exception ):
40- # def __new__(cls, *args):
41- # print('TemporaryClass.__new__: args are: {0}'.format(args))
42- # if len(args) == 1 and isinstance(args[0], TemporaryClass):
43- # unwrap_me = args[0]
44- # err = super(TemporaryClass, cls).__new__(cls)
45- # for attr in dir(unwrap_me):
46- # if not attr.startswith('__'):
47- # setattr(err, attr, getattr(unwrap_me, attr))
48- # else:
49- # return super(base_exception, cls).__new__(cls, *args)
50-
5146 def __init__ (self , * args , ** kwargs ):
52- # print('TemporaryClass.__init__: args are: {0}'.format(args))
5347 if len (args ) == 1 and isinstance (args [0 ], TemporaryClass ):
5448 unwrap_me = args [0 ]
5549 for attr in dir (unwrap_me ):
@@ -60,35 +54,13 @@ def __init__(self, *args, **kwargs):
6054
6155 class __metaclass__ (type ):
6256 def __instancecheck__ (cls , inst ):
63- # print('FileNotFoundError.__isinstance__: type(inst) = {0}\ninst = {1}'.format(type(inst), inst))
6457 return instance_checker (inst )
6558
6659 def __subclasscheck__ (cls , classinfo ):
67- # print('In __subclasscheck__:\n\tcls = {0}\n\tclassinfo = {1}'.format(cls, classinfo))
68- # return instance_checker(classinfo)
69- # This hook is called during the exception handling.
70- # Unfortunately, we would rather have exception handling call
71- # __instancecheck__, so we have to do that ourselves. But,
72- # that's not how it currently is. If you feel like proposing a
73- # patch for Python, check the function
74- # `PyErr_GivenExceptionMatches` in `Python/error.c`.
7560 value = sys .exc_info ()[1 ]
76- # if value is None:
77- # print('Exception value is None!!')
78-
79- # Double-check that the exception given actually somewhat
80- # matches the classinfo we received. If not, people may be using
81- # `issubclass` directly, which is of course prone to errors,
82- # or they are raising the exception with `raise ...`
83- # if value.__class__ != classinfo:
84- # print('Mismatch!\nvalue: {0}\nvalue.__class__: {1}\nclassinfo: {2}'.format(
85- # value, value.__class__, classinfo)
86- # )
8761 return isinstance (value , cls )
8862
8963 TemporaryClass .__name__ = instance_checker .__name__
9064 TemporaryClass .__doc__ = instance_checker .__doc__
9165 return TemporaryClass
9266 return wrapped
93-
94-
0 commit comments