Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | import __builtin__ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3 | import warnings |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 4 | from test.test_support import run_unittest, guard_warnings_filter |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 5 | import os |
| 6 | from platform import system as platform_system |
| 7 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 8 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 9 | class ExceptionClassTests(unittest.TestCase): |
| 10 | |
| 11 | """Tests for anything relating to exception objects themselves (e.g., |
| 12 | inheritance hierarchy)""" |
| 13 | |
| 14 | def test_builtins_new_style(self): |
| 15 | self.failUnless(issubclass(Exception, object)) |
| 16 | |
| 17 | def verify_instance_interface(self, ins): |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 18 | for attr in ("args", "__str__", "__repr__"): |
| 19 | self.failUnless(hasattr(ins, attr), |
| 20 | "%s missing %s attribute" % |
| 21 | (ins.__class__.__name__, attr)) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 22 | |
| 23 | def test_inheritance(self): |
| 24 | # Make sure the inheritance hierarchy matches the documentation |
Brett Cannon | 4af7dcf | 2007-02-28 00:01:43 +0000 | [diff] [blame] | 25 | exc_set = set() |
Brett Cannon | 3695bf3 | 2007-02-28 00:32:07 +0000 | [diff] [blame] | 26 | for object_ in __builtin__.__dict__.values(): |
Brett Cannon | 4af7dcf | 2007-02-28 00:01:43 +0000 | [diff] [blame] | 27 | try: |
| 28 | if issubclass(object_, BaseException): |
| 29 | exc_set.add(object_.__name__) |
| 30 | except TypeError: |
| 31 | pass |
| 32 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 33 | inheritance_tree = open(os.path.join(os.path.split(__file__)[0], |
| 34 | 'exception_hierarchy.txt')) |
| 35 | try: |
| 36 | superclass_name = inheritance_tree.readline().rstrip() |
| 37 | try: |
| 38 | last_exc = getattr(__builtin__, superclass_name) |
| 39 | except AttributeError: |
| 40 | self.fail("base class %s not a built-in" % superclass_name) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 41 | self.failUnless(superclass_name in exc_set, |
| 42 | '%s not found' % superclass_name) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 43 | exc_set.discard(superclass_name) |
| 44 | superclasses = [] # Loop will insert base exception |
| 45 | last_depth = 0 |
| 46 | for exc_line in inheritance_tree: |
| 47 | exc_line = exc_line.rstrip() |
| 48 | depth = exc_line.rindex('-') |
| 49 | exc_name = exc_line[depth+2:] # Slice past space |
| 50 | if '(' in exc_name: |
| 51 | paren_index = exc_name.index('(') |
| 52 | platform_name = exc_name[paren_index+1:-1] |
Brett Cannon | 6b4ed74 | 2006-03-01 06:10:48 +0000 | [diff] [blame] | 53 | exc_name = exc_name[:paren_index-1] # Slice off space |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 54 | if platform_system() != platform_name: |
| 55 | exc_set.discard(exc_name) |
| 56 | continue |
| 57 | if '[' in exc_name: |
| 58 | left_bracket = exc_name.index('[') |
| 59 | exc_name = exc_name[:left_bracket-1] # cover space |
| 60 | try: |
| 61 | exc = getattr(__builtin__, exc_name) |
| 62 | except AttributeError: |
| 63 | self.fail("%s not a built-in exception" % exc_name) |
| 64 | if last_depth < depth: |
| 65 | superclasses.append((last_depth, last_exc)) |
| 66 | elif last_depth > depth: |
| 67 | while superclasses[-1][0] >= depth: |
| 68 | superclasses.pop() |
| 69 | self.failUnless(issubclass(exc, superclasses[-1][1]), |
| 70 | "%s is not a subclass of %s" % (exc.__name__, |
| 71 | superclasses[-1][1].__name__)) |
| 72 | try: # Some exceptions require arguments; just skip them |
| 73 | self.verify_instance_interface(exc()) |
| 74 | except TypeError: |
| 75 | pass |
| 76 | self.failUnless(exc_name in exc_set) |
| 77 | exc_set.discard(exc_name) |
| 78 | last_exc = exc |
| 79 | last_depth = depth |
| 80 | finally: |
| 81 | inheritance_tree.close() |
| 82 | self.failUnlessEqual(len(exc_set), 0, "%s not accounted for" % exc_set) |
| 83 | |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 84 | interface_tests = ("length", "args", "str", "repr") |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 85 | |
| 86 | def interface_test_driver(self, results): |
| 87 | for test_name, (given, expected) in zip(self.interface_tests, results): |
| 88 | self.failUnlessEqual(given, expected, "%s: %s != %s" % (test_name, |
| 89 | given, expected)) |
| 90 | |
| 91 | def test_interface_single_arg(self): |
| 92 | # Make sure interface works properly when given a single argument |
| 93 | arg = "spam" |
| 94 | exc = Exception(arg) |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 95 | results = ([len(exc.args), 1], [exc.args[0], arg], |
| 96 | [str(exc), str(arg)], |
| 97 | [repr(exc), exc.__class__.__name__ + repr(exc.args)]) |
| 98 | self.interface_test_driver(results) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 99 | |
| 100 | def test_interface_multi_arg(self): |
| 101 | # Make sure interface correct when multiple arguments given |
| 102 | arg_count = 3 |
| 103 | args = tuple(range(arg_count)) |
| 104 | exc = Exception(*args) |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 105 | results = ([len(exc.args), arg_count], [exc.args, args], |
| 106 | [str(exc), str(args)], |
| 107 | [repr(exc), exc.__class__.__name__ + repr(exc.args)]) |
| 108 | self.interface_test_driver(results) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 109 | |
| 110 | def test_interface_no_arg(self): |
| 111 | # Make sure that with no args that interface is correct |
| 112 | exc = Exception() |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 113 | results = ([len(exc.args), 0], [exc.args, tuple()], |
| 114 | [str(exc), ''], |
| 115 | [repr(exc), exc.__class__.__name__ + '()']) |
| 116 | self.interface_test_driver(results) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 117 | |
| 118 | class UsageTests(unittest.TestCase): |
| 119 | |
| 120 | """Test usage of exceptions""" |
| 121 | |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 122 | def raise_fails(self, object_): |
| 123 | """Make sure that raising 'object_' triggers a TypeError.""" |
| 124 | try: |
| 125 | raise object_ |
| 126 | except TypeError: |
| 127 | return # What is expected. |
| 128 | self.fail("TypeError expected for raising %s" % type(object_)) |
| 129 | |
| 130 | def catch_fails(self, object_): |
| 131 | """Catching 'object_' should raise a TypeError.""" |
| 132 | try: |
| 133 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame^] | 134 | raise Exception |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 135 | except object_: |
| 136 | pass |
| 137 | except TypeError: |
| 138 | pass |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame^] | 139 | except Exception: |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 140 | self.fail("TypeError expected when catching %s" % type(object_)) |
| 141 | |
| 142 | try: |
| 143 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame^] | 144 | raise Exception |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 145 | except (object_,): |
| 146 | pass |
| 147 | except TypeError: |
| 148 | return |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame^] | 149 | except Exception: |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 150 | self.fail("TypeError expected when catching %s as specified in a " |
| 151 | "tuple" % type(object_)) |
| 152 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 153 | def test_raise_new_style_non_exception(self): |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 154 | # You cannot raise a new-style class that does not inherit from |
| 155 | # BaseException; the ability was not possible until BaseException's |
| 156 | # introduction so no need to support new-style objects that do not |
| 157 | # inherit from it. |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 158 | class NewStyleClass(object): |
| 159 | pass |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 160 | self.raise_fails(NewStyleClass) |
| 161 | self.raise_fails(NewStyleClass()) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 162 | |
| 163 | def test_raise_string(self): |
| 164 | # Raising a string raises TypeError. |
Thomas Wouters | fa35365 | 2007-02-23 20:24:22 +0000 | [diff] [blame] | 165 | self.raise_fails("spam") |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 166 | |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 167 | def test_catch_non_BaseException(self): |
| 168 | # Tryinng to catch an object that does not inherit from BaseException |
| 169 | # is not allowed. |
| 170 | class NonBaseException(object): |
| 171 | pass |
| 172 | self.catch_fails(NonBaseException) |
| 173 | self.catch_fails(NonBaseException()) |
| 174 | |
Brett Cannon | ba7bf49 | 2007-02-27 00:15:55 +0000 | [diff] [blame] | 175 | def test_catch_BaseException_instance(self): |
| 176 | # Catching an instance of a BaseException subclass won't work. |
| 177 | self.catch_fails(BaseException()) |
| 178 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 179 | def test_catch_string(self): |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 180 | # Catching a string is bad. |
| 181 | self.catch_fails("spam") |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 182 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 183 | def test_main(): |
| 184 | run_unittest(ExceptionClassTests, UsageTests) |
| 185 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 186 | if __name__ == '__main__': |
| 187 | test_main() |