blob: dc47737feb3a81bfc127e192a44c37828398b65d [file] [log] [blame]
Brett Cannonbf364092006-03-01 04:25:17 +00001import unittest
2import __builtin__
Brett Cannonbf364092006-03-01 04:25:17 +00003import warnings
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004from test.test_support import run_unittest, guard_warnings_filter
Brett Cannonbf364092006-03-01 04:25:17 +00005import os
6from platform import system as platform_system
7
Guido van Rossum360e4b82007-05-14 22:51:27 +00008
Brett Cannonbf364092006-03-01 04:25:17 +00009class 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 Rossumebe3e162007-05-17 18:20:34 +000018 for attr in ("args", "__str__", "__repr__"):
19 self.failUnless(hasattr(ins, attr),
20 "%s missing %s attribute" %
21 (ins.__class__.__name__, attr))
Brett Cannonbf364092006-03-01 04:25:17 +000022
23 def test_inheritance(self):
24 # Make sure the inheritance hierarchy matches the documentation
Brett Cannon4af7dcf2007-02-28 00:01:43 +000025 exc_set = set()
Brett Cannon3695bf32007-02-28 00:32:07 +000026 for object_ in __builtin__.__dict__.values():
Brett Cannon4af7dcf2007-02-28 00:01:43 +000027 try:
28 if issubclass(object_, BaseException):
29 exc_set.add(object_.__name__)
30 except TypeError:
31 pass
32
Brett Cannonbf364092006-03-01 04:25:17 +000033 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 Rossum805365e2007-05-07 22:24:25 +000041 self.failUnless(superclass_name in exc_set,
42 '%s not found' % superclass_name)
Brett Cannonbf364092006-03-01 04:25:17 +000043 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 Cannon6b4ed742006-03-01 06:10:48 +000053 exc_name = exc_name[:paren_index-1] # Slice off space
Brett Cannonbf364092006-03-01 04:25:17 +000054 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 Rossumebe3e162007-05-17 18:20:34 +000084 interface_tests = ("length", "args", "str", "repr")
Brett Cannonbf364092006-03-01 04:25:17 +000085
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 Rossumebe3e162007-05-17 18:20:34 +000095 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 Cannonbf364092006-03-01 04:25:17 +000099
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 Rossumebe3e162007-05-17 18:20:34 +0000105 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 Cannonbf364092006-03-01 04:25:17 +0000109
110 def test_interface_no_arg(self):
111 # Make sure that with no args that interface is correct
112 exc = Exception()
Guido van Rossumebe3e162007-05-17 18:20:34 +0000113 results = ([len(exc.args), 0], [exc.args, tuple()],
114 [str(exc), ''],
115 [repr(exc), exc.__class__.__name__ + '()'])
116 self.interface_test_driver(results)
Brett Cannonbf364092006-03-01 04:25:17 +0000117
118class UsageTests(unittest.TestCase):
119
120 """Test usage of exceptions"""
121
Thomas Woutersfa353652007-02-23 20:24:22 +0000122 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 Rossumcd16bf62007-06-13 18:07:49 +0000134 raise Exception
Thomas Woutersfa353652007-02-23 20:24:22 +0000135 except object_:
136 pass
137 except TypeError:
138 pass
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000139 except Exception:
Thomas Woutersfa353652007-02-23 20:24:22 +0000140 self.fail("TypeError expected when catching %s" % type(object_))
141
142 try:
143 try:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000144 raise Exception
Thomas Woutersfa353652007-02-23 20:24:22 +0000145 except (object_,):
146 pass
147 except TypeError:
148 return
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000149 except Exception:
Thomas Woutersfa353652007-02-23 20:24:22 +0000150 self.fail("TypeError expected when catching %s as specified in a "
151 "tuple" % type(object_))
152
Brett Cannonbf364092006-03-01 04:25:17 +0000153 def test_raise_new_style_non_exception(self):
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000154 # 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 Cannonbf364092006-03-01 04:25:17 +0000158 class NewStyleClass(object):
159 pass
Thomas Woutersfa353652007-02-23 20:24:22 +0000160 self.raise_fails(NewStyleClass)
161 self.raise_fails(NewStyleClass())
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000162
163 def test_raise_string(self):
164 # Raising a string raises TypeError.
Thomas Woutersfa353652007-02-23 20:24:22 +0000165 self.raise_fails("spam")
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000166
Brett Cannonf74225d2007-02-26 21:10:16 +0000167 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 Cannonba7bf492007-02-27 00:15:55 +0000175 def test_catch_BaseException_instance(self):
176 # Catching an instance of a BaseException subclass won't work.
177 self.catch_fails(BaseException())
178
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000179 def test_catch_string(self):
Brett Cannonf74225d2007-02-26 21:10:16 +0000180 # Catching a string is bad.
181 self.catch_fails("spam")
Brett Cannonbf364092006-03-01 04:25:17 +0000182
Brett Cannonbf364092006-03-01 04:25:17 +0000183def test_main():
184 run_unittest(ExceptionClassTests, UsageTests)
185
Brett Cannonbf364092006-03-01 04:25:17 +0000186if __name__ == '__main__':
187 test_main()