MCPcopy Index your code
hub / github.com/python/cpython / test_inheritance

Method test_inheritance

Lib/test/test_baseexception.py:19–82  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

17 self.assertHasAttr(ins, attr)
18
19 def test_inheritance(self):
20 # Make sure the inheritance hierarchy matches the documentation
21 exc_set = set()
22 for object_ in builtins.__dict__.values():
23 try:
24 if issubclass(object_, BaseException):
25 exc_set.add(object_.__name__)
26 except TypeError:
27 pass
28
29 inheritance_tree = open(
30 os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'),
31 encoding="utf-8")
32 try:
33 superclass_name = inheritance_tree.readline().rstrip()
34 try:
35 last_exc = getattr(builtins, superclass_name)
36 except AttributeError:
37 self.fail("base class %s not a built-in" % superclass_name)
38 self.assertIn(superclass_name, exc_set,
39 '%s not found' % superclass_name)
40 exc_set.discard(superclass_name)
41 superclasses = [] # Loop will insert base exception
42 last_depth = 0
43 for exc_line in inheritance_tree:
44 exc_line = exc_line.rstrip()
45 depth = exc_line.rindex('─')
46 exc_name = exc_line[depth+2:] # Slice past space
47 if '(' in exc_name:
48 paren_index = exc_name.index('(')
49 platform_name = exc_name[paren_index+1:-1]
50 exc_name = exc_name[:paren_index-1] # Slice off space
51 if platform_system() != platform_name:
52 exc_set.discard(exc_name)
53 continue
54 if '[' in exc_name:
55 left_bracket = exc_name.index('[')
56 exc_name = exc_name[:left_bracket-1] # cover space
57 try:
58 exc = getattr(builtins, exc_name)
59 except AttributeError:
60 self.fail("%s not a built-in exception" % exc_name)
61 if last_depth < depth:
62 superclasses.append((last_depth, last_exc))
63 elif last_depth > depth:
64 while superclasses[-1][0] >= depth:
65 superclasses.pop()
66 self.assertIsSubclass(exc, superclasses[-1][1],
67 "%s is not a subclass of %s" % (exc.__name__,
68 superclasses[-1][1].__name__))
69 try: # Some exceptions require arguments; just skip them
70 self.verify_instance_interface(exc())
71 except TypeError:
72 pass
73 self.assertIn(exc_name, exc_set)
74 exc_set.discard(exc_name)
75 last_exc = exc
76 last_depth = depth

Callers

nothing calls this directly

Calls 15

setFunction · 0.85
assertInMethod · 0.80
assertIsSubclassMethod · 0.80
openFunction · 0.50
valuesMethod · 0.45
addMethod · 0.45
joinMethod · 0.45
splitMethod · 0.45
rstripMethod · 0.45
readlineMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected