1 # Python test set -- part 5, built-in exceptions
9 from test
.test_support
import TESTFN
, unlink
, run_unittest
11 # XXX This is not really enough, each *operation* should be tested!
13 class ExceptionTests(unittest
.TestCase
):
16 # Reloading the built-in exceptions module failed prior to Py2.2, while it
17 # should act the same as reloading built-in sys.
21 except ImportError, e
:
22 self
.fail("reloading exceptions: %s" % e
)
24 def raise_catch(self
, exc
, excname
):
33 self
.assertEquals(buf1
, buf2
)
34 self
.assertEquals(exc
.__name
__, excname
)
36 def testRaising(self
):
37 self
.raise_catch(AttributeError, "AttributeError")
38 self
.assertRaises(AttributeError, getattr, sys
, "undefined_attribute")
40 self
.raise_catch(EOFError, "EOFError")
41 fp
= open(TESTFN
, 'w')
43 fp
= open(TESTFN
, 'r')
56 self
.raise_catch(IOError, "IOError")
57 self
.assertRaises(IOError, open, 'this file does not exist', 'r')
59 self
.raise_catch(ImportError, "ImportError")
60 self
.assertRaises(ImportError, __import__, "undefined_module")
62 self
.raise_catch(IndexError, "IndexError")
64 self
.assertRaises(IndexError, x
.__getitem
__, 10)
66 self
.raise_catch(KeyError, "KeyError")
68 self
.assertRaises(KeyError, x
.__getitem
__, 'key')
70 self
.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
72 self
.raise_catch(MemoryError, "MemoryError")
74 self
.raise_catch(NameError, "NameError")
75 try: x
= undefined_variable
76 except NameError: pass
78 self
.raise_catch(OverflowError, "OverflowError")
80 for dummy
in range(128):
81 x
+= x
# this simply shouldn't blow up
83 self
.raise_catch(RuntimeError, "RuntimeError")
85 self
.raise_catch(SyntaxError, "SyntaxError")
87 except SyntaxError: pass
89 self
.raise_catch(IndentationError, "IndentationError")
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")
97 self
.raise_catch(SystemError, "SystemError")
99 self
.raise_catch(SystemExit, "SystemExit")
100 self
.assertRaises(SystemExit, sys
.exit
, 0)
102 self
.raise_catch(TypeError, "TypeError")
104 except TypeError: pass
106 self
.raise_catch(ValueError, "ValueError")
107 self
.assertRaises(ValueError, chr, 10000)
109 self
.raise_catch(ZeroDivisionError, "ZeroDivisionError")
111 except ZeroDivisionError: pass
113 self
.raise_catch(Exception, "Exception")
115 except Exception, e
: pass
117 def testSyntaxErrorMessage(self
):
118 # make sure the right exception message is raised for each of
119 # these code fragments
123 compile(src
, '<fragment>', 'exec')
124 except SyntaxError, e
:
126 self
.fail("expected %s, got %s" % (msg
, e
.msg
))
128 self
.fail("failed to get expected SyntaxError")
136 if not sys
.platform
.startswith('java'):
137 ckmsg(s
, "'continue' not supported inside 'finally' clause")
145 ckmsg(s
, "'continue' not properly in loop")
146 ckmsg("continue\n", "'continue' not properly in loop")
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.
154 raise RuntimeError, "can't instantiate BadException"
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'))
166 self
.fail("Expected exception")
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")
180 self
.fail("Expected exception")
182 if not sys
.platform
.startswith('java'):
186 def testAttributes(self
):
187 # test that exception attributes are happy
190 (BaseException
, (), {'message' : '', 'args' : ()}),
191 (BaseException
, (1, ), {'message' : 1, 'args' : (1,)}),
192 (BaseException
, ('foo',),
193 {'message' : 'foo', 'args' : ('foo',)}),
194 (BaseException
, ('foo', 1),
195 {'message' : '', 'args' : ('foo', 1)}),
196 (SystemExit, ('foo',),
197 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}),
199 {'message' : 'foo', 'args' : ('foo',)}),
200 (IOError, ('foo', 'bar'),
201 {'message' : '', 'args' : ('foo', 'bar')}),
202 (IOError, ('foo', 'bar', 'baz'),
203 {'message' : '', 'args' : ('foo', 'bar')}),
204 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
205 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'),
206 'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
207 'filename' : 'filenameStr'}),
208 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
209 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1,
210 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
211 (SyntaxError, ('msgStr',),
212 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None,
213 'print_file_and_line' : None, 'msg' : 'msgStr',
214 'filename' : None, 'lineno' : None, 'offset' : None}),
215 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
217 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr',
218 'args' : ('msgStr', ('filenameStr', 'linenoStr',
219 'offsetStr', 'textStr')),
220 'print_file_and_line' : None, 'msg' : 'msgStr',
221 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
222 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
223 'textStr', 'print_file_and_lineStr'),
224 {'message' : '', 'text' : None,
225 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
226 'textStr', 'print_file_and_lineStr'),
227 'print_file_and_line' : None, 'msg' : 'msgStr',
228 'filename' : None, 'lineno' : None, 'offset' : None}),
229 (UnicodeError, (), {'message' : '', 'args' : (),}),
230 (UnicodeEncodeError, ('ascii', u
'a', 0, 1, 'ordinal not in range'),
231 {'message' : '', 'args' : ('ascii', u
'a', 0, 1,
232 'ordinal not in range'),
233 'encoding' : 'ascii', 'object' : u
'a',
234 'start' : 0, 'reason' : 'ordinal not in range'}),
235 (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'),
236 {'message' : '', 'args' : ('ascii', '\xff', 0, 1,
237 'ordinal not in range'),
238 'encoding' : 'ascii', 'object' : '\xff',
239 'start' : 0, 'reason' : 'ordinal not in range'}),
240 (UnicodeTranslateError, (u
"\u3042", 0, 1, "ouch"),
241 {'message' : '', 'args' : (u
'\u3042', 0, 1, 'ouch'),
242 'object' : u
'\u3042', 'reason' : 'ouch',
243 'start' : 0, 'end' : 1}),
246 exceptionList
.append(
247 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
248 {'message' : '', 'args' : (1, 'strErrorStr'),
249 'strerror' : 'strErrorStr', 'winerror' : 1,
250 'errno' : 22, 'filename' : 'filenameStr'})
255 for exc
, args
, expected
in exceptionList
:
258 except BaseException
, e
:
259 if type(e
) is not exc
:
262 self
.assertEquals(type(e
).__module
__, 'exceptions')
263 # Verify no ref leaks in Exc_str()
265 for checkArgName
in expected
:
266 self
.assertEquals(repr(getattr(e
, checkArgName
)),
267 repr(expected
[checkArgName
]),
268 'exception "%s", attribute "%s"' %
269 (repr(e
), checkArgName
))
271 # test for pickling support
272 for p
in pickle
, cPickle
:
273 for protocol
in range(p
.HIGHEST_PROTOCOL
+ 1):
274 new
= p
.loads(p
.dumps(e
, protocol
))
275 for checkArgName
in expected
:
276 got
= repr(getattr(new
, checkArgName
))
277 want
= repr(expected
[checkArgName
])
278 self
.assertEquals(got
, want
,
279 'pickled "%r", attribute "%s' %
282 def testKeywordArgs(self
):
283 # test that builtin exception don't take keyword args,
284 # but user-defined subclasses can if they want
285 self
.assertRaises(TypeError, BaseException
, a
=1)
287 class DerivedException(BaseException
):
288 def __init__(self
, fancy_arg
):
289 BaseException
.__init
__(self
)
290 self
.fancy_arg
= fancy_arg
292 x
= DerivedException(fancy_arg
=42)
293 self
.assertEquals(x
.fancy_arg
, 42)
295 def testInfiniteRecursion(self
):
298 self
.assertRaises(RuntimeError, f
)
305 self
.assertRaises(RuntimeError, g
)
307 def testUnicodeStrUsage(self
):
308 # Make sure both instances and classes have a str and unicode
310 self
.failUnless(str(Exception))
311 self
.failUnless(unicode(Exception))
312 self
.failUnless(str(Exception('a')))
313 self
.failUnless(unicode(Exception(u
'a')))
317 run_unittest(ExceptionTests
)
319 if __name__
== '__main__':