Debian Codegraph @

Lib/test/test_baseexception.py

raw
1 import unittest
2 import builtins
3 import os
4 from platform import system as platform_system
5
6
7 class ExceptionClassTests(unittest.TestCase):
8
9 """Tests for anything relating to exception objects themselves (e.g.,
10 inheritance hierarchy)"""
11
12 def test_builtins_new_style(self):
13 self.assertTrue(issubclass(Exception, object))
14
15 def verify_instance_interface(self, ins):
16 for attr in ("args", "__str__", "__repr__"):
17 self.assertTrue(hasattr(ins, attr),
18 "%s missing %s attribute" %
19 (ins.__class__.__name__, attr))
20
21 def test_inheritance(self):
22 # Make sure the inheritance hierarchy matches the documentation
23 exc_set = set()
24 for object_ in builtins.__dict__.values():
25 try:
26 if issubclass(object_, BaseException):
27 exc_set.add(object_.__name__)
28 except TypeError:
29 pass
30
31 inheritance_tree = open(
32 os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'),
33 encoding="utf-8")
34 try:
35 superclass_name = inheritance_tree.readline().rstrip()
36 try:
37 last_exc = getattr(builtins, superclass_name)
38 except AttributeError:
39 self.fail("base class %s not a built-in" % superclass_name)
40 self.assertIn(superclass_name, exc_set,
41 '%s not found' % superclass_name)
42 exc_set.discard(superclass_name)
43 superclasses = [] # Loop will insert base exception
44 last_depth = 0
45 for exc_line in inheritance_tree:
46 exc_line = exc_line.rstrip()
47 depth = exc_line.rindex('─')
48 exc_name = exc_line[depth+2:] # Slice past space
49 if '(' in exc_name:
50 paren_index = exc_name.index('(')
51 platform_name = exc_name[paren_index+1:-1]
52 exc_name = exc_name[:paren_index-1] # Slice off space
53 if platform_system() != platform_name:
54 exc_set.discard(exc_name)
55 continue
56 if '[' in exc_name:
57 left_bracket = exc_name.index('[')
58 exc_name = exc_name[:left_bracket-1] # cover space
59 try:
60 exc = getattr(builtins, exc_name)
61 except AttributeError:
62 self.fail("%s not a built-in exception" % exc_name)
63 if last_depth < depth:
64 superclasses.append((last_depth, last_exc))
65 elif last_depth > depth:
66 while superclasses[-1][0] >= depth:
67 superclasses.pop()
68 self.assertTrue(issubclass(exc, superclasses[-1][1]),
69 "%s is not a subclass of %s" % (exc.__name__,
70 superclasses[-1][1].__name__))
71 try: # Some exceptions require arguments; just skip them
72 self.verify_instance_interface(exc())
73 except TypeError:
74 pass
75 self.assertIn(exc_name, exc_set)
76 exc_set.discard(exc_name)
77 last_exc = exc
78 last_depth = depth
79 finally:
80 inheritance_tree.close()
81
82 # Underscore-prefixed (private) exceptions don't need to be documented
83 exc_set = set(e for e in exc_set if not e.startswith('_'))
84 self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
85
86 interface_tests = ("length", "args", "str", "repr")
87
88 def interface_test_driver(self, results):
89 for test_name, (given, expected) in zip(self.interface_tests, results):
90 self.assertEqual(given, expected, "%s: %s != %s" % (test_name,
91 given, expected))
92
93 def test_interface_single_arg(self):
94 # Make sure interface works properly when given a single argument
95 arg = "spam"
96 exc = Exception(arg)
97 results = ([len(exc.args), 1], [exc.args[0], arg],
98 [str(exc), str(arg)],
99 [repr(exc), '%s(%r)' % (exc.__class__.__name__, arg)])
100 self.interface_test_driver(results)
101
102 def test_interface_multi_arg(self):
103 # Make sure interface correct when multiple arguments given
104 arg_count = 3
105 args = tuple(range(arg_count))
106 exc = Exception(*args)
107 results = ([len(exc.args), arg_count], [exc.args, args],
108 [str(exc), str(args)],
109 [repr(exc), exc.__class__.__name__ + repr(exc.args)])
110 self.interface_test_driver(results)
111
112 def test_interface_no_arg(self):
113 # Make sure that with no args that interface is correct
114 exc = Exception()
115 results = ([len(exc.args), 0], [exc.args, tuple()],
116 [str(exc), ''],
117 [repr(exc), exc.__class__.__name__ + '()'])
118 self.interface_test_driver(results)
119
120 def test_setstate_refcount_no_crash(self):
121 # gh-97591: Acquire strong reference before calling tp_hash slot
122 # in PyObject_SetAttr.
123 import gc
124 d = {}
125 class HashThisKeyWillClearTheDict(str):
126 def __hash__(self) -> int:
127 d.clear()
128 return super().__hash__()
129 class Value(str):
130 pass
131 exc = Exception()
132
133 d[HashThisKeyWillClearTheDict()] = Value() # refcount of Value() is 1 now
134
135 # Exception.__setstate__ should acquire a strong reference of key and
136 # value in the dict. Otherwise, Value()'s refcount would go below
137 # zero in the tp_hash call in PyObject_SetAttr(), and it would cause
138 # crash in GC.
139 exc.__setstate__(d) # __hash__() is called again here, clearing the dict.
140
141 # This GC would crash if the refcount of Value() goes below zero.
142 gc.collect()
143
144
145 class UsageTests(unittest.TestCase):
146
147 """Test usage of exceptions"""
148
149 def raise_fails(self, object_):
150 """Make sure that raising 'object_' triggers a TypeError."""
151 try:
152 raise object_
153 except TypeError:
154 return # What is expected.
155 self.fail("TypeError expected for raising %s" % type(object_))
156
157 def catch_fails(self, object_):
158 """Catching 'object_' should raise a TypeError."""
159 try:
160 try:
161 raise Exception
162 except object_:
163 pass
164 except TypeError:
165 pass
166 except Exception:
167 self.fail("TypeError expected when catching %s" % type(object_))
168
169 try:
170 try:
171 raise Exception
172 except (object_,):
173 pass
174 except TypeError:
175 return
176 except Exception:
177 self.fail("TypeError expected when catching %s as specified in a "
178 "tuple" % type(object_))
179
180 def test_raise_new_style_non_exception(self):
181 # You cannot raise a new-style class that does not inherit from
182 # BaseException; the ability was not possible until BaseException's
183 # introduction so no need to support new-style objects that do not
184 # inherit from it.
185 class NewStyleClass(object):
186 pass
187 self.raise_fails(NewStyleClass)
188 self.raise_fails(NewStyleClass())
189
190 def test_raise_string(self):
191 # Raising a string raises TypeError.
192 self.raise_fails("spam")
193
194 def test_catch_non_BaseException(self):
195 # Trying to catch an object that does not inherit from BaseException
196 # is not allowed.
197 class NonBaseException(object):
198 pass
199 self.catch_fails(NonBaseException)
200 self.catch_fails(NonBaseException())
201
202 def test_catch_BaseException_instance(self):
203 # Catching an instance of a BaseException subclass won't work.
204 self.catch_fails(BaseException())
205
206 def test_catch_string(self):
207 # Catching a string is bad.
208 self.catch_fails("spam")
209
210
211 if __name__ == '__main__':
212 unittest.main()