1import unittest 2import builtins 3import os 4from platform import system as platform_system 5 6 7class 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 self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set) 82 83 interface_tests = ("length", "args", "str", "repr") 84 85 def interface_test_driver(self, results): 86 for test_name, (given, expected) in zip(self.interface_tests, results): 87 self.assertEqual(given, expected, "%s: %s != %s" % (test_name, 88 given, expected)) 89 90 def test_interface_single_arg(self): 91 # Make sure interface works properly when given a single argument 92 arg = "spam" 93 exc = Exception(arg) 94 results = ([len(exc.args), 1], [exc.args[0], arg], 95 [str(exc), str(arg)], 96 [repr(exc), '%s(%r)' % (exc.__class__.__name__, arg)]) 97 self.interface_test_driver(results) 98 99 def test_interface_multi_arg(self): 100 # Make sure interface correct when multiple arguments given 101 arg_count = 3 102 args = tuple(range(arg_count)) 103 exc = Exception(*args) 104 results = ([len(exc.args), arg_count], [exc.args, args], 105 [str(exc), str(args)], 106 [repr(exc), exc.__class__.__name__ + repr(exc.args)]) 107 self.interface_test_driver(results) 108 109 def test_interface_no_arg(self): 110 # Make sure that with no args that interface is correct 111 exc = Exception() 112 results = ([len(exc.args), 0], [exc.args, tuple()], 113 [str(exc), ''], 114 [repr(exc), exc.__class__.__name__ + '()']) 115 self.interface_test_driver(results) 116 117class UsageTests(unittest.TestCase): 118 119 """Test usage of exceptions""" 120 121 def raise_fails(self, object_): 122 """Make sure that raising 'object_' triggers a TypeError.""" 123 try: 124 raise object_ 125 except TypeError: 126 return # What is expected. 127 self.fail("TypeError expected for raising %s" % type(object_)) 128 129 def catch_fails(self, object_): 130 """Catching 'object_' should raise a TypeError.""" 131 try: 132 try: 133 raise Exception 134 except object_: 135 pass 136 except TypeError: 137 pass 138 except Exception: 139 self.fail("TypeError expected when catching %s" % type(object_)) 140 141 try: 142 try: 143 raise Exception 144 except (object_,): 145 pass 146 except TypeError: 147 return 148 except Exception: 149 self.fail("TypeError expected when catching %s as specified in a " 150 "tuple" % type(object_)) 151 152 def test_raise_new_style_non_exception(self): 153 # You cannot raise a new-style class that does not inherit from 154 # BaseException; the ability was not possible until BaseException's 155 # introduction so no need to support new-style objects that do not 156 # inherit from it. 157 class NewStyleClass(object): 158 pass 159 self.raise_fails(NewStyleClass) 160 self.raise_fails(NewStyleClass()) 161 162 def test_raise_string(self): 163 # Raising a string raises TypeError. 164 self.raise_fails("spam") 165 166 def test_catch_non_BaseException(self): 167 # Trying to catch an object that does not inherit from BaseException 168 # is not allowed. 169 class NonBaseException(object): 170 pass 171 self.catch_fails(NonBaseException) 172 self.catch_fails(NonBaseException()) 173 174 def test_catch_BaseException_instance(self): 175 # Catching an instance of a BaseException subclass won't work. 176 self.catch_fails(BaseException()) 177 178 def test_catch_string(self): 179 # Catching a string is bad. 180 self.catch_fails("spam") 181 182 183if __name__ == '__main__': 184 unittest.main() 185