|
1 # Python test set -- part 5, built-in exceptions |
|
2 |
|
3 import os |
|
4 import sys |
|
5 import unittest |
|
6 import warnings |
|
7 import pickle, cPickle |
|
8 |
|
9 from test.test_support import TESTFN, unlink, run_unittest |
|
10 |
|
11 # XXX This is not really enough, each *operation* should be tested! |
|
12 |
|
13 class ExceptionTests(unittest.TestCase): |
|
14 |
|
15 def testReload(self): |
|
16 # Reloading the built-in exceptions module failed prior to Py2.2, while it |
|
17 # should act the same as reloading built-in sys. |
|
18 try: |
|
19 import exceptions |
|
20 reload(exceptions) |
|
21 except ImportError, e: |
|
22 self.fail("reloading exceptions: %s" % e) |
|
23 |
|
24 def raise_catch(self, exc, excname): |
|
25 try: |
|
26 raise exc, "spam" |
|
27 except exc, err: |
|
28 buf1 = str(err) |
|
29 try: |
|
30 raise exc("spam") |
|
31 except exc, err: |
|
32 buf2 = str(err) |
|
33 self.assertEquals(buf1, buf2) |
|
34 self.assertEquals(exc.__name__, excname) |
|
35 |
|
36 def testRaising(self): |
|
37 self.raise_catch(AttributeError, "AttributeError") |
|
38 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") |
|
39 |
|
40 self.raise_catch(EOFError, "EOFError") |
|
41 fp = open(TESTFN, 'w') |
|
42 fp.close() |
|
43 fp = open(TESTFN, 'r') |
|
44 savestdin = sys.stdin |
|
45 try: |
|
46 try: |
|
47 sys.stdin = fp |
|
48 x = raw_input() |
|
49 except EOFError: |
|
50 pass |
|
51 finally: |
|
52 sys.stdin = savestdin |
|
53 fp.close() |
|
54 unlink(TESTFN) |
|
55 |
|
56 self.raise_catch(IOError, "IOError") |
|
57 self.assertRaises(IOError, open, 'this file does not exist', 'r') |
|
58 |
|
59 self.raise_catch(ImportError, "ImportError") |
|
60 self.assertRaises(ImportError, __import__, "undefined_module") |
|
61 |
|
62 self.raise_catch(IndexError, "IndexError") |
|
63 x = [] |
|
64 self.assertRaises(IndexError, x.__getitem__, 10) |
|
65 |
|
66 self.raise_catch(KeyError, "KeyError") |
|
67 x = {} |
|
68 self.assertRaises(KeyError, x.__getitem__, 'key') |
|
69 |
|
70 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt") |
|
71 |
|
72 self.raise_catch(MemoryError, "MemoryError") |
|
73 |
|
74 self.raise_catch(NameError, "NameError") |
|
75 try: x = undefined_variable |
|
76 except NameError: pass |
|
77 |
|
78 self.raise_catch(OverflowError, "OverflowError") |
|
79 x = 1 |
|
80 for dummy in range(128): |
|
81 x += x # this simply shouldn't blow up |
|
82 |
|
83 self.raise_catch(RuntimeError, "RuntimeError") |
|
84 |
|
85 self.raise_catch(SyntaxError, "SyntaxError") |
|
86 try: exec '/\n' |
|
87 except SyntaxError: pass |
|
88 |
|
89 self.raise_catch(IndentationError, "IndentationError") |
|
90 |
|
91 self.raise_catch(TabError, "TabError") |
|
92 # can only be tested under -tt, and is the only test for -tt |
|
93 #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec') |
|
94 #except TabError: pass |
|
95 #else: self.fail("TabError not raised") |
|
96 |
|
97 self.raise_catch(SystemError, "SystemError") |
|
98 |
|
99 self.raise_catch(SystemExit, "SystemExit") |
|
100 self.assertRaises(SystemExit, sys.exit, 0) |
|
101 |
|
102 self.raise_catch(TypeError, "TypeError") |
|
103 try: [] + () |
|
104 except TypeError: pass |
|
105 |
|
106 self.raise_catch(ValueError, "ValueError") |
|
107 self.assertRaises(ValueError, chr, 10000) |
|
108 |
|
109 self.raise_catch(ZeroDivisionError, "ZeroDivisionError") |
|
110 try: x = 1/0 |
|
111 except ZeroDivisionError: pass |
|
112 |
|
113 self.raise_catch(Exception, "Exception") |
|
114 try: x = 1/0 |
|
115 except Exception, e: pass |
|
116 |
|
117 def testSyntaxErrorMessage(self): |
|
118 # make sure the right exception message is raised for each of |
|
119 # these code fragments |
|
120 |
|
121 def ckmsg(src, msg): |
|
122 try: |
|
123 compile(src, '<fragment>', 'exec') |
|
124 except SyntaxError, e: |
|
125 if e.msg != msg: |
|
126 self.fail("expected %s, got %s" % (msg, e.msg)) |
|
127 else: |
|
128 self.fail("failed to get expected SyntaxError") |
|
129 |
|
130 s = '''while 1: |
|
131 try: |
|
132 pass |
|
133 finally: |
|
134 continue''' |
|
135 |
|
136 if not sys.platform.startswith('java'): |
|
137 ckmsg(s, "'continue' not supported inside 'finally' clause") |
|
138 |
|
139 s = '''if 1: |
|
140 try: |
|
141 continue |
|
142 except: |
|
143 pass''' |
|
144 |
|
145 ckmsg(s, "'continue' not properly in loop") |
|
146 ckmsg("continue\n", "'continue' not properly in loop") |
|
147 |
|
148 def testSettingException(self): |
|
149 # test that setting an exception at the C level works even if the |
|
150 # exception object can't be constructed. |
|
151 |
|
152 class BadException: |
|
153 def __init__(self_): |
|
154 raise RuntimeError, "can't instantiate BadException" |
|
155 |
|
156 def test_capi1(): |
|
157 import _testcapi |
|
158 try: |
|
159 _testcapi.raise_exception(BadException, 1) |
|
160 except TypeError, err: |
|
161 exc, err, tb = sys.exc_info() |
|
162 co = tb.tb_frame.f_code |
|
163 self.assertEquals(co.co_name, "test_capi1") |
|
164 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) |
|
165 else: |
|
166 self.fail("Expected exception") |
|
167 |
|
168 def test_capi2(): |
|
169 import _testcapi |
|
170 try: |
|
171 _testcapi.raise_exception(BadException, 0) |
|
172 except RuntimeError, err: |
|
173 exc, err, tb = sys.exc_info() |
|
174 co = tb.tb_frame.f_code |
|
175 self.assertEquals(co.co_name, "__init__") |
|
176 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) |
|
177 co2 = tb.tb_frame.f_back.f_code |
|
178 self.assertEquals(co2.co_name, "test_capi2") |
|
179 else: |
|
180 self.fail("Expected exception") |
|
181 |
|
182 if not sys.platform.startswith('java'): |
|
183 test_capi1() |
|
184 test_capi2() |
|
185 |
|
186 def test_WindowsError(self): |
|
187 try: |
|
188 WindowsError |
|
189 except NameError: |
|
190 pass |
|
191 else: |
|
192 self.failUnlessEqual(str(WindowsError(1001)), |
|
193 "1001") |
|
194 self.failUnlessEqual(str(WindowsError(1001, "message")), |
|
195 "[Error 1001] message") |
|
196 self.failUnlessEqual(WindowsError(1001, "message").errno, 22) |
|
197 self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001) |
|
198 |
|
199 def testAttributes(self): |
|
200 # test that exception attributes are happy |
|
201 |
|
202 exceptionList = [ |
|
203 (BaseException, (), {'message' : '', 'args' : ()}), |
|
204 (BaseException, (1, ), {'message' : 1, 'args' : (1,)}), |
|
205 (BaseException, ('foo',), |
|
206 {'message' : 'foo', 'args' : ('foo',)}), |
|
207 (BaseException, ('foo', 1), |
|
208 {'message' : '', 'args' : ('foo', 1)}), |
|
209 (SystemExit, ('foo',), |
|
210 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}), |
|
211 (IOError, ('foo',), |
|
212 {'message' : 'foo', 'args' : ('foo',), 'filename' : None, |
|
213 'errno' : None, 'strerror' : None}), |
|
214 (IOError, ('foo', 'bar'), |
|
215 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None, |
|
216 'errno' : 'foo', 'strerror' : 'bar'}), |
|
217 (IOError, ('foo', 'bar', 'baz'), |
|
218 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz', |
|
219 'errno' : 'foo', 'strerror' : 'bar'}), |
|
220 (IOError, ('foo', 'bar', 'baz', 'quux'), |
|
221 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}), |
|
222 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
|
223 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'), |
|
224 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', |
|
225 'filename' : 'filenameStr'}), |
|
226 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
|
227 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1, |
|
228 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), |
|
229 (SyntaxError, ('msgStr',), |
|
230 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None, |
|
231 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
232 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
233 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
|
234 'textStr')), |
|
235 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr', |
|
236 'args' : ('msgStr', ('filenameStr', 'linenoStr', |
|
237 'offsetStr', 'textStr')), |
|
238 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
239 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), |
|
240 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
241 'textStr', 'print_file_and_lineStr'), |
|
242 {'message' : '', 'text' : None, |
|
243 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
244 'textStr', 'print_file_and_lineStr'), |
|
245 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
246 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
247 (UnicodeError, (), {'message' : '', 'args' : (),}), |
|
248 (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'), |
|
249 {'message' : '', 'args' : ('ascii', u'a', 0, 1, |
|
250 'ordinal not in range'), |
|
251 'encoding' : 'ascii', 'object' : u'a', |
|
252 'start' : 0, 'reason' : 'ordinal not in range'}), |
|
253 (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'), |
|
254 {'message' : '', 'args' : ('ascii', '\xff', 0, 1, |
|
255 'ordinal not in range'), |
|
256 'encoding' : 'ascii', 'object' : '\xff', |
|
257 'start' : 0, 'reason' : 'ordinal not in range'}), |
|
258 (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), |
|
259 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'), |
|
260 'object' : u'\u3042', 'reason' : 'ouch', |
|
261 'start' : 0, 'end' : 1}), |
|
262 ] |
|
263 try: |
|
264 exceptionList.append( |
|
265 (WindowsError, (1, 'strErrorStr', 'filenameStr'), |
|
266 {'message' : '', 'args' : (1, 'strErrorStr'), |
|
267 'strerror' : 'strErrorStr', 'winerror' : 1, |
|
268 'errno' : 22, 'filename' : 'filenameStr'}) |
|
269 ) |
|
270 except NameError: |
|
271 pass |
|
272 |
|
273 for exc, args, expected in exceptionList: |
|
274 try: |
|
275 raise exc(*args) |
|
276 except BaseException, e: |
|
277 if type(e) is not exc: |
|
278 raise |
|
279 # Verify module name |
|
280 self.assertEquals(type(e).__module__, 'exceptions') |
|
281 # Verify no ref leaks in Exc_str() |
|
282 s = str(e) |
|
283 for checkArgName in expected: |
|
284 self.assertEquals(repr(getattr(e, checkArgName)), |
|
285 repr(expected[checkArgName]), |
|
286 'exception "%s", attribute "%s"' % |
|
287 (repr(e), checkArgName)) |
|
288 |
|
289 # test for pickling support |
|
290 for p in pickle, cPickle: |
|
291 for protocol in range(p.HIGHEST_PROTOCOL + 1): |
|
292 new = p.loads(p.dumps(e, protocol)) |
|
293 for checkArgName in expected: |
|
294 got = repr(getattr(new, checkArgName)) |
|
295 want = repr(expected[checkArgName]) |
|
296 self.assertEquals(got, want, |
|
297 'pickled "%r", attribute "%s' % |
|
298 (e, checkArgName)) |
|
299 |
|
300 def testSlicing(self): |
|
301 # Test that you can slice an exception directly instead of requiring |
|
302 # going through the 'args' attribute. |
|
303 args = (1, 2, 3) |
|
304 exc = BaseException(*args) |
|
305 self.failUnlessEqual(exc[:], args) |
|
306 |
|
307 def testKeywordArgs(self): |
|
308 # test that builtin exception don't take keyword args, |
|
309 # but user-defined subclasses can if they want |
|
310 self.assertRaises(TypeError, BaseException, a=1) |
|
311 |
|
312 class DerivedException(BaseException): |
|
313 def __init__(self, fancy_arg): |
|
314 BaseException.__init__(self) |
|
315 self.fancy_arg = fancy_arg |
|
316 |
|
317 x = DerivedException(fancy_arg=42) |
|
318 self.assertEquals(x.fancy_arg, 42) |
|
319 |
|
320 def testInfiniteRecursion(self): |
|
321 def f(): |
|
322 return f() |
|
323 self.assertRaises(RuntimeError, f) |
|
324 |
|
325 def g(): |
|
326 try: |
|
327 return g() |
|
328 except ValueError: |
|
329 return -1 |
|
330 self.assertRaises(RuntimeError, g) |
|
331 |
|
332 def testUnicodeStrUsage(self): |
|
333 # Make sure both instances and classes have a str and unicode |
|
334 # representation. |
|
335 self.failUnless(str(Exception)) |
|
336 self.failUnless(unicode(Exception)) |
|
337 self.failUnless(str(Exception('a'))) |
|
338 self.failUnless(unicode(Exception(u'a'))) |
|
339 |
|
340 |
|
341 def test_main(): |
|
342 run_unittest(ExceptionTests) |
|
343 |
|
344 if __name__ == '__main__': |
|
345 test_main() |