annotate Lib/test/test_exceptions.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents ff96a313cd1c
children 8ab304034021 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- part 5, built-in exceptions
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
38638
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
3 import os
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
4 import sys
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
5 import unittest
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
6 import pickle, cPickle
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 48876
diff changeset
7 import warnings
38638
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
8
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 48876
diff changeset
9 from test.test_support import TESTFN, unlink, run_unittest, captured_output
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
10 from test.test_pep352 import ignore_message_warning
19437
5fec1c9a21ea Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents: 19239
diff changeset
11
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # XXX This is not really enough, each *operation* should be tested!
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
14 class ExceptionTests(unittest.TestCase):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
15
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
16 def testReload(self):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
17 # Reloading the built-in exceptions module failed prior to Py2.2, while it
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
18 # should act the same as reloading built-in sys.
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
19 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
20 import exceptions
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
21 reload(exceptions)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
22 except ImportError, e:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
23 self.fail("reloading exceptions: %s" % e)
19239
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
24
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
25 def raise_catch(self, exc, excname):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
26 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
27 raise exc, "spam"
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
28 except exc, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
29 buf1 = str(err)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
30 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
31 raise exc("spam")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
32 except exc, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
33 buf2 = str(err)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
34 self.assertEquals(buf1, buf2)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
35 self.assertEquals(exc.__name__, excname)
12969
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
36
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
37 def testRaising(self):
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
38 self.raise_catch(AttributeError, "AttributeError")
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
39 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
41 self.raise_catch(EOFError, "EOFError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
42 fp = open(TESTFN, 'w')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
43 fp.close()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
44 fp = open(TESTFN, 'r')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
45 savestdin = sys.stdin
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
46 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
47 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
48 sys.stdin = fp
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
49 x = raw_input()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
50 except EOFError:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
51 pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
52 finally:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
53 sys.stdin = savestdin
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
54 fp.close()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
55 unlink(TESTFN)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
57 self.raise_catch(IOError, "IOError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
58 self.assertRaises(IOError, open, 'this file does not exist', 'r')
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
60 self.raise_catch(ImportError, "ImportError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
61 self.assertRaises(ImportError, __import__, "undefined_module")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
63 self.raise_catch(IndexError, "IndexError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
64 x = []
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
65 self.assertRaises(IndexError, x.__getitem__, 10)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
66
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
67 self.raise_catch(KeyError, "KeyError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
68 x = {}
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
69 self.assertRaises(KeyError, x.__getitem__, 'key')
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
71 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
72
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
73 self.raise_catch(MemoryError, "MemoryError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
75 self.raise_catch(NameError, "NameError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
76 try: x = undefined_variable
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
77 except NameError: pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
79 self.raise_catch(OverflowError, "OverflowError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
80 x = 1
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
81 for dummy in range(128):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
82 x += x # this simply shouldn't blow up
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
84 self.raise_catch(RuntimeError, "RuntimeError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
85
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
86 self.raise_catch(SyntaxError, "SyntaxError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
87 try: exec '/\n'
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
88 except SyntaxError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
89
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
90 self.raise_catch(IndentationError, "IndentationError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
92 self.raise_catch(TabError, "TabError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
93 # can only be tested under -tt, and is the only test for -tt
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
94 #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
95 #except TabError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
96 #else: self.fail("TabError not raised")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
98 self.raise_catch(SystemError, "SystemError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
100 self.raise_catch(SystemExit, "SystemExit")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
101 self.assertRaises(SystemExit, sys.exit, 0)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
102
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
103 self.raise_catch(TypeError, "TypeError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
104 try: [] + ()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
105 except TypeError: pass
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
106
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
107 self.raise_catch(ValueError, "ValueError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
108 self.assertRaises(ValueError, chr, 10000)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
109
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
110 self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
111 try: x = 1/0
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
112 except ZeroDivisionError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
113
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
114 self.raise_catch(Exception, "Exception")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
115 try: x = 1/0
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
116 except Exception, e: pass
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
117
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
118 def testSyntaxErrorMessage(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
119 # make sure the right exception message is raised for each of
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
120 # these code fragments
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
121
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
122 def ckmsg(src, msg):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
123 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
124 compile(src, '<fragment>', 'exec')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
125 except SyntaxError, e:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
126 if e.msg != msg:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
127 self.fail("expected %s, got %s" % (msg, e.msg))
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
128 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
129 self.fail("failed to get expected SyntaxError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
130
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
131 s = '''while 1:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
132 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
133 pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
134 finally:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
135 continue'''
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
136
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
137 if not sys.platform.startswith('java'):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
138 ckmsg(s, "'continue' not supported inside 'finally' clause")
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
139
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
140 s = '''if 1:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
141 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
142 continue
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
143 except:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
144 pass'''
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
146 ckmsg(s, "'continue' not properly in loop")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
147 ckmsg("continue\n", "'continue' not properly in loop")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
149 def testSettingException(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
150 # test that setting an exception at the C level works even if the
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
151 # exception object can't be constructed.
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
153 class BadException:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
154 def __init__(self_):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
155 raise RuntimeError, "can't instantiate BadException"
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
157 def test_capi1():
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
158 import _testcapi
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
159 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
160 _testcapi.raise_exception(BadException, 1)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
161 except TypeError, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
162 exc, err, tb = sys.exc_info()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
163 co = tb.tb_frame.f_code
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
164 self.assertEquals(co.co_name, "test_capi1")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
165 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
166 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
167 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
168
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
169 def test_capi2():
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
170 import _testcapi
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
171 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
172 _testcapi.raise_exception(BadException, 0)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
173 except RuntimeError, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
174 exc, err, tb = sys.exc_info()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
175 co = tb.tb_frame.f_code
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
176 self.assertEquals(co.co_name, "__init__")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
177 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
178 co2 = tb.tb_frame.f_back.f_code
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
179 self.assertEquals(co2.co_name, "test_capi2")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
180 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
181 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
182
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
183 if not sys.platform.startswith('java'):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
184 test_capi1()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
185 test_capi2()
38431
4c0823f0a0a1 Conversion of exceptions over from faked-up classes to new-style C types.
Richard Jones <richard@commonground.com.au>
parents: 36751
diff changeset
186
40209
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
187 def test_WindowsError(self):
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
188 try:
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
189 WindowsError
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
190 except NameError:
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
191 pass
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
192 else:
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
193 self.failUnlessEqual(str(WindowsError(1001)),
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
194 "1001")
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
195 self.failUnlessEqual(str(WindowsError(1001, "message")),
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
196 "[Error 1001] message")
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
197 self.failUnlessEqual(WindowsError(1001, "message").errno, 22)
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
198 self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001)
988d95ff1fdb WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39924
diff changeset
199
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
200 def testAttributes(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
201 # test that exception attributes are happy
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
202
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
203 exceptionList = [
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
204 (BaseException, (), {'message' : '', 'args' : ()}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
205 (BaseException, (1, ), {'message' : 1, 'args' : (1,)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
206 (BaseException, ('foo',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
207 {'message' : 'foo', 'args' : ('foo',)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
208 (BaseException, ('foo', 1),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
209 {'message' : '', 'args' : ('foo', 1)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
210 (SystemExit, ('foo',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
211 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
212 (IOError, ('foo',),
39924
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
213 {'message' : 'foo', 'args' : ('foo',), 'filename' : None,
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
214 'errno' : None, 'strerror' : None}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
215 (IOError, ('foo', 'bar'),
39924
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
216 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None,
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
217 'errno' : 'foo', 'strerror' : 'bar'}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
218 (IOError, ('foo', 'bar', 'baz'),
39924
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
219 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz',
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
220 'errno' : 'foo', 'strerror' : 'bar'}),
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
221 (IOError, ('foo', 'bar', 'baz', 'quux'),
e1ab4653ebc0 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39818
diff changeset
222 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
223 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
224 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
225 'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
226 'filename' : 'filenameStr'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
227 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
228 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
229 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
40987
9dd86ead51c3 Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents: 40725
diff changeset
230 (SyntaxError, (), {'message' : '', 'msg' : None, 'text' : None,
9dd86ead51c3 Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents: 40725
diff changeset
231 'filename' : None, 'lineno' : None, 'offset' : None,
9dd86ead51c3 Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents: 40725
diff changeset
232 'print_file_and_line' : None}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
233 (SyntaxError, ('msgStr',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
234 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
235 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
236 'filename' : None, 'lineno' : None, 'offset' : None}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
237 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
238 'textStr')),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
239 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
240 'args' : ('msgStr', ('filenameStr', 'linenoStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
241 'offsetStr', 'textStr')),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
242 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
243 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
244 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
245 'textStr', 'print_file_and_lineStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
246 {'message' : '', 'text' : None,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
247 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
248 'textStr', 'print_file_and_lineStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
249 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
250 'filename' : None, 'lineno' : None, 'offset' : None}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
251 (UnicodeError, (), {'message' : '', 'args' : (),}),
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
252 (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'),
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
253 {'message' : '', 'args' : ('ascii', u'a', 0, 1,
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
254 'ordinal not in range'),
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
255 'encoding' : 'ascii', 'object' : u'a',
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
256 'start' : 0, 'reason' : 'ordinal not in range'}),
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
257 (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
258 {'message' : '', 'args' : ('ascii', '\xff', 0, 1,
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
259 'ordinal not in range'),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
260 'encoding' : 'ascii', 'object' : '\xff',
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
261 'start' : 0, 'reason' : 'ordinal not in range'}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
262 (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
263 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
264 'object' : u'\u3042', 'reason' : 'ouch',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
265 'start' : 0, 'end' : 1}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
266 ]
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
267 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
268 exceptionList.append(
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
269 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
270 {'message' : '', 'args' : (1, 'strErrorStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
271 'strerror' : 'strErrorStr', 'winerror' : 1,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
272 'errno' : 22, 'filename' : 'filenameStr'})
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
273 )
38638
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
274 except NameError:
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
275 pass
38506
0aba63ff7f73 Add a test case for exception pickling. args is never NULL.
Georg Brandl <georg@python.org>
parents: 38431
diff changeset
276
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 48876
diff changeset
277 with warnings.catch_warnings():
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
278 ignore_message_warning()
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
279 for exc, args, expected in exceptionList:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
280 try:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
281 raise exc(*args)
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
282 except BaseException, e:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
283 if type(e) is not exc:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
284 raise
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
285 # Verify module name
42688
62eee79de923 Revert accidental checkins from last commit.
Georg Brandl <georg@python.org>
parents: 42687
diff changeset
286 self.assertEquals(type(e).__module__, 'exceptions')
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
287 # Verify no ref leaks in Exc_str()
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
288 s = str(e)
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
289 for checkArgName in expected:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
290 self.assertEquals(repr(getattr(e, checkArgName)),
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
291 repr(expected[checkArgName]),
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
292 'exception "%s", attribute "%s"' %
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
293 (repr(e), checkArgName))
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
294
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
295 # test for pickling support
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
296 for p in pickle, cPickle:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
297 for protocol in range(p.HIGHEST_PROTOCOL + 1):
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
298 new = p.loads(p.dumps(e, protocol))
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
299 for checkArgName in expected:
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
300 got = repr(getattr(new, checkArgName))
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
301 want = repr(expected[checkArgName])
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
302 self.assertEquals(got, want,
43758
6e491d12765c Add a missing quotation mark.
Brett Cannon <bcannon@gmail.com>
parents: 42688
diff changeset
303 'pickled "%r", attribute "%s"' %
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 40987
diff changeset
304 (e, checkArgName))
38508
99564e62aea9 Disallow keyword args for exceptions.
Georg Brandl <georg@python.org>
parents: 38506
diff changeset
305
40725
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
306 def testSlicing(self):
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
307 # Test that you can slice an exception directly instead of requiring
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
308 # going through the 'args' attribute.
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
309 args = (1, 2, 3)
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
310 exc = BaseException(*args)
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
311 self.failUnlessEqual(exc[:], args)
2d8239abfa60 Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents: 40454
diff changeset
312
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
313 def testKeywordArgs(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
314 # test that builtin exception don't take keyword args,
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
315 # but user-defined subclasses can if they want
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
316 self.assertRaises(TypeError, BaseException, a=1)
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
317
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
318 class DerivedException(BaseException):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
319 def __init__(self, fancy_arg):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
320 BaseException.__init__(self)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
321 self.fancy_arg = fancy_arg
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
322
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
323 x = DerivedException(fancy_arg=42)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
324 self.assertEquals(x.fancy_arg, 42)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
325
38904
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
326 def testInfiniteRecursion(self):
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
327 def f():
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
328 return f()
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
329 self.assertRaises(RuntimeError, f)
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
330
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
331 def g():
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
332 try:
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
333 return g()
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
334 except ValueError:
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
335 return -1
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
336
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
337 # The test prints an unraisable recursion error when
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
338 # doing "except ValueError", this is because subclass
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
339 # checking has recursion checking too.
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
340 with captured_output("stderr"):
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
341 try:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
342 g()
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
343 except RuntimeError:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
344 pass
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
345 except:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
346 self.fail("Should have raised KeyError")
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
347 else:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
348 self.fail("Should have raised KeyError")
38904
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
349
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
350 def testUnicodeStrUsage(self):
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
351 # Make sure both instances and classes have a str and unicode
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
352 # representation.
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
353 self.failUnless(str(Exception))
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
354 self.failUnless(unicode(Exception))
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
355 self.failUnless(str(Exception('a')))
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
356 self.failUnless(unicode(Exception(u'a')))
47914
1bc543944daa Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents: 43758
diff changeset
357 self.failUnless(unicode(Exception(u'\xe1')))
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
358
48107
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
359 def test_badisinstance(self):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
360 # Bug #2542: if issubclass(e, MyException) raises an exception,
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
361 # it should be ignored
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
362 class Meta(type):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
363 def __subclasscheck__(cls, subclass):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
364 raise ValueError()
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
365
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
366 class MyException(Exception):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
367 __metaclass__ = Meta
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
368 pass
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
369
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
370 with captured_output("stderr") as stderr:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
371 try:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
372 raise KeyError()
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
373 except MyException, e:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
374 self.fail("exception should not be a MyException")
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
375 except KeyError:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
376 pass
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
377 except:
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
378 self.fail("Should have raised KeyError")
48107
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
379 else:
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
380 self.fail("Should have raised KeyError")
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
381
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
382 with captured_output("stderr") as stderr:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
383 def g():
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
384 try:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
385 return g()
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
386 except RuntimeError:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
387 return sys.exc_info()
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
388 e, v, tb = g()
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
389 self.assert_(e is RuntimeError, e)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
390 self.assert_("maximum recursion depth exceeded" in str(v), v)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
391
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
392
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
393 def test_main():
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
394 run_unittest(ExceptionTests)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
395
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
396 if __name__ == '__main__':
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
397 test_main()