annotate Lib/test/test_exceptions.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 67bfadf4f954
children 0e41c4466d58
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
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
7
63634
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
8 from test.test_support import (TESTFN, unlink, run_unittest, captured_output,
62597
c0a49b6a6fb1 testcapi tests are definitely cpython only
Benjamin Peterson <benjamin@python.org>
parents: 60116
diff changeset
9 check_warnings, cpython_only)
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
10 from test.test_pep352 import ignore_deprecation_warnings
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:
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
20 from imp import reload
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
21 import exceptions
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
22 reload(exceptions)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
23 except ImportError, e:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
24 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
25
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
26 def raise_catch(self, exc, excname):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
27 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
28 raise exc, "spam"
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
29 except exc, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
30 buf1 = str(err)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
31 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
32 raise exc("spam")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
33 except exc, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
34 buf2 = str(err)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
35 self.assertEqual(buf1, buf2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
36 self.assertEqual(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
37
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
38 def testRaising(self):
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
39 self.raise_catch(AttributeError, "AttributeError")
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
40 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
42 self.raise_catch(EOFError, "EOFError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
43 fp = open(TESTFN, 'w')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
44 fp.close()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
45 fp = open(TESTFN, 'r')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
46 savestdin = sys.stdin
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 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
49 sys.stdin = fp
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
50 x = raw_input()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
51 except EOFError:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
52 pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
53 finally:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
54 sys.stdin = savestdin
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
55 fp.close()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
56 unlink(TESTFN)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
58 self.raise_catch(IOError, "IOError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
59 self.assertRaises(IOError, open, 'this file does not exist', 'r')
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
61 self.raise_catch(ImportError, "ImportError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
62 self.assertRaises(ImportError, __import__, "undefined_module")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
64 self.raise_catch(IndexError, "IndexError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
65 x = []
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
66 self.assertRaises(IndexError, x.__getitem__, 10)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
67
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
68 self.raise_catch(KeyError, "KeyError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
69 x = {}
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
70 self.assertRaises(KeyError, x.__getitem__, 'key')
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
72 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
73
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
74 self.raise_catch(MemoryError, "MemoryError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
76 self.raise_catch(NameError, "NameError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
77 try: x = undefined_variable
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
78 except NameError: pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
80 self.raise_catch(OverflowError, "OverflowError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
81 x = 1
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
82 for dummy in range(128):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
83 x += x # this simply shouldn't blow up
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
85 self.raise_catch(RuntimeError, "RuntimeError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
86
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
87 self.raise_catch(SyntaxError, "SyntaxError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
88 try: exec '/\n'
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
89 except SyntaxError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
90
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
91 self.raise_catch(IndentationError, "IndentationError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
93 self.raise_catch(TabError, "TabError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
94 # 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
95 #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
96 #except TabError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
97 #else: self.fail("TabError not raised")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
99 self.raise_catch(SystemError, "SystemError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
101 self.raise_catch(SystemExit, "SystemExit")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
102 self.assertRaises(SystemExit, sys.exit, 0)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
103
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
104 self.raise_catch(TypeError, "TypeError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
105 try: [] + ()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
106 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
107
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
108 self.raise_catch(ValueError, "ValueError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
109 self.assertRaises(ValueError, chr, 10000)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
110
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
111 self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
112 try: x = 1 // 0
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
113 except ZeroDivisionError: pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
114
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
115 self.raise_catch(Exception, "Exception")
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
116 try: x = 1 // 0
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
117 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
118
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
119 def testSyntaxErrorMessage(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
120 # 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
121 # 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
122
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
123 def ckmsg(src, msg):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
124 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
125 compile(src, '<fragment>', 'exec')
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
126 except SyntaxError, e:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
127 if e.msg != msg:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
128 self.fail("expected %s, got %s" % (msg, e.msg))
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
129 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
130 self.fail("failed to get expected SyntaxError")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
131
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
132 s = '''while 1:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
133 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
134 pass
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
135 finally:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
136 continue'''
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
137
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
138 if not sys.platform.startswith('java'):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
139 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
140
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
141 s = '''if 1:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
142 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
143 continue
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
144 except:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
145 pass'''
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
147 ckmsg(s, "'continue' not properly in loop")
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
148 ckmsg("continue\n", "'continue' not properly in loop")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
62597
c0a49b6a6fb1 testcapi tests are definitely cpython only
Benjamin Peterson <benjamin@python.org>
parents: 60116
diff changeset
150 @cpython_only
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
151 def testSettingException(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
152 # 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
153 # exception object can't be constructed.
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
155 class BadException:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
156 def __init__(self_):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
157 raise RuntimeError, "can't instantiate BadException"
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
159 def test_capi1():
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
160 import _testcapi
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
161 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
162 _testcapi.raise_exception(BadException, 1)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
163 except TypeError, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
164 exc, err, tb = sys.exc_info()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
165 co = tb.tb_frame.f_code
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
166 self.assertEqual(co.co_name, "test_capi1")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
167 self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
168 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
169 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
170
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
171 def test_capi2():
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
172 import _testcapi
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
173 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
174 _testcapi.raise_exception(BadException, 0)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
175 except RuntimeError, err:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
176 exc, err, tb = sys.exc_info()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
177 co = tb.tb_frame.f_code
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
178 self.assertEqual(co.co_name, "__init__")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
179 self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
180 co2 = tb.tb_frame.f_back.f_code
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
181 self.assertEqual(co2.co_name, "test_capi2")
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
182 else:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
183 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
184
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
185 if not sys.platform.startswith('java'):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
186 test_capi1()
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
187 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
188
40210
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
189 def test_WindowsError(self):
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
190 try:
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
191 WindowsError
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
192 except NameError:
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
193 pass
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
194 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
195 self.assertEqual(str(WindowsError(1001)),
40210
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
196 "1001")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
197 self.assertEqual(str(WindowsError(1001, "message")),
40210
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
198 "[Error 1001] message")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
199 self.assertEqual(WindowsError(1001, "message").errno, 22)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
200 self.assertEqual(WindowsError(1001, "message").winerror, 1001)
40210
18871a10ffc1 WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents: 39925
diff changeset
201
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
202 @ignore_deprecation_warnings
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
203 def testAttributes(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
204 # test that exception attributes are happy
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
205
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
206 exceptionList = [
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
207 (BaseException, (), {'message' : '', 'args' : ()}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
208 (BaseException, (1, ), {'message' : 1, 'args' : (1,)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
209 (BaseException, ('foo',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
210 {'message' : 'foo', 'args' : ('foo',)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
211 (BaseException, ('foo', 1),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
212 {'message' : '', 'args' : ('foo', 1)}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
213 (SystemExit, ('foo',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
214 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
215 (IOError, ('foo',),
39925
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
216 {'message' : 'foo', 'args' : ('foo',), 'filename' : None,
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
217 'errno' : None, 'strerror' : None}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
218 (IOError, ('foo', 'bar'),
39925
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
219 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None,
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
220 'errno' : 'foo', 'strerror' : 'bar'}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
221 (IOError, ('foo', 'bar', 'baz'),
39925
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
222 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz',
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
223 'errno' : 'foo', 'strerror' : 'bar'}),
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
224 (IOError, ('foo', 'bar', 'baz', 'quux'),
3986bfe53101 Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents: 39867
diff changeset
225 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
226 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
227 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
228 'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
229 'filename' : 'filenameStr'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
230 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
231 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
232 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
40987
9dd86ead51c3 Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents: 40725
diff changeset
233 (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
234 '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
235 'print_file_and_line' : None}),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
236 (SyntaxError, ('msgStr',),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
237 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
238 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
239 'filename' : None, 'lineno' : None, 'offset' : None}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
240 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
241 'textStr')),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
242 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
243 'args' : ('msgStr', ('filenameStr', 'linenoStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
244 'offsetStr', 'textStr')),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
245 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
246 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
247 (SyntaxError, ('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 {'message' : '', 'text' : None,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
250 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
251 'textStr', 'print_file_and_lineStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
252 'print_file_and_line' : None, 'msg' : 'msgStr',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
253 'filename' : None, 'lineno' : None, 'offset' : None}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
254 (UnicodeError, (), {'message' : '', 'args' : (),}),
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
255 (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
256 {'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
257 'ordinal not in range'),
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
258 'encoding' : 'ascii', 'object' : u'a',
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
259 '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
260 (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
261 {'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
262 'ordinal not in range'),
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
263 'encoding' : 'ascii', 'object' : '\xff',
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
264 '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
265 (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
266 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
267 'object' : u'\u3042', 'reason' : 'ouch',
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
268 'start' : 0, 'end' : 1}),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
269 ]
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
270 try:
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
271 exceptionList.append(
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
272 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
273 {'message' : '', 'args' : (1, 'strErrorStr'),
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
274 'strerror' : 'strErrorStr', 'winerror' : 1,
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
275 'errno' : 22, 'filename' : 'filenameStr'})
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
276 )
38638
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
277 except NameError:
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
278 pass
38506
0aba63ff7f73 Add a test case for exception pickling. args is never NULL.
Georg Brandl <georg@python.org>
parents: 38431
diff changeset
279
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
280 for exc, args, expected in exceptionList:
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
281 try:
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
282 raise exc(*args)
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
283 except BaseException, e:
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
284 if type(e) is not exc:
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
285 raise
39781
cb883e7e0b7e Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
286 # Verify module name
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
287 self.assertEqual(type(e).__module__, 'exceptions')
38542
2632bd55c29f Fix memory leak found by valgrind.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38541
diff changeset
288 # Verify no ref leaks in Exc_str()
2632bd55c29f Fix memory leak found by valgrind.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38541
diff changeset
289 s = str(e)
2632bd55c29f Fix memory leak found by valgrind.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38541
diff changeset
290 for checkArgName in expected:
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
291 self.assertEqual(repr(getattr(e, checkArgName)),
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
292 repr(expected[checkArgName]),
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
293 'exception "%s", attribute "%s"' %
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
294 (repr(e), checkArgName))
38517
d2ae580880b4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38511
diff changeset
295
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
296 # test for pickling support
38638
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
297 for p in pickle, cPickle:
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
298 for protocol in range(p.HIGHEST_PROTOCOL + 1):
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
299 new = p.loads(p.dumps(e, protocol))
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
300 for checkArgName in expected:
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
301 got = repr(getattr(new, checkArgName))
ea9966c88657 SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents: 38542
diff changeset
302 want = repr(expected[checkArgName])
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
303 self.assertEqual(got, want,
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
304 'pickled "%r", attribute "%s"' %
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
305 (e, checkArgName))
38508
99564e62aea9 Disallow keyword args for exceptions.
Georg Brandl <georg@python.org>
parents: 38506
diff changeset
306
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
307
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
308 def testDeprecatedMessageAttribute(self):
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
309 # Accessing BaseException.message and relying on its value set by
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
310 # BaseException.__init__ triggers a deprecation warning.
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
311 exc = BaseException("foo")
63634
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
312 with check_warnings(("BaseException.message has been deprecated "
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
313 "as of Python 2.6", DeprecationWarning)) as w:
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
314 self.assertEqual(exc.message, "foo")
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
315 self.assertEqual(len(w.warnings), 1)
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
316
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
317 def testRegularMessageAttribute(self):
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
318 # Accessing BaseException.message after explicitly setting a value
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
319 # for it does not trigger a deprecation warning.
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
320 exc = BaseException("foo")
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
321 exc.message = "bar"
63634
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
322 with check_warnings(quiet=True) as w:
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
323 self.assertEqual(exc.message, "bar")
762d89faceaa Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63534
diff changeset
324 self.assertEqual(len(w.warnings), 0)
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
325 # Deleting the message is supported, too.
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
326 del exc.message
56011
0f09d38f852e #6844: do not emit DeprecationWarnings on access if Exception.message has been set by the user.
Georg Brandl <georg@python.org>
parents: 55002
diff changeset
327 with self.assertRaises(AttributeError):
0f09d38f852e #6844: do not emit DeprecationWarnings on access if Exception.message has been set by the user.
Georg Brandl <georg@python.org>
parents: 55002
diff changeset
328 exc.message
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
329
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
330 @ignore_deprecation_warnings
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
331 def testPickleMessageAttribute(self):
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
332 # Pickling with message attribute must work, as well.
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
333 e = Exception("foo")
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
334 f = Exception("foo")
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
335 f.message = "bar"
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
336 for p in pickle, cPickle:
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
337 ep = p.loads(p.dumps(e))
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
338 self.assertEqual(ep.message, "foo")
56014
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
339 fp = p.loads(p.dumps(f))
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
340 self.assertEqual(fp.message, "bar")
8ab304034021 Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49099
diff changeset
341
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
342 @ignore_deprecation_warnings
39867
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
343 def testSlicing(self):
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
344 # Test that you can slice an exception directly instead of requiring
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
345 # going through the 'args' attribute.
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
346 args = (1, 2, 3)
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
347 exc = BaseException(*args)
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
348 self.assertEqual(exc[:], args)
63534
2dc0b7131c72 Merged revisions 77871,77910,77913 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59109
diff changeset
349 self.assertEqual(exc.args[:], args)
39867
35bf16e7f11c Backport of fix to allow exception instances to be sliced once again.
Brett Cannon <bcannon@gmail.com>
parents: 39818
diff changeset
350
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
351 def testKeywordArgs(self):
38541
f37e67bd4393 Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents: 38535
diff changeset
352 # 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
353 # but user-defined subclasses can if they want
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
354 self.assertRaises(TypeError, BaseException, a=1)
38535
cf677e978852 Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents: 38517
diff changeset
355
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
356 class DerivedException(BaseException):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
357 def __init__(self, fancy_arg):
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
358 BaseException.__init__(self)
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
359 self.fancy_arg = fancy_arg
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
360
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
361 x = DerivedException(fancy_arg=42)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62597
diff changeset
362 self.assertEqual(x.fancy_arg, 42)
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
363
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
364 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
365 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
366 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
367 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
368
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38638
diff changeset
369 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
370 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
371 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
372 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
373 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
374
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
375 # 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
376 # 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
377 # 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
378 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
379 try:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
380 g()
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
381 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
382 pass
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
383 except:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
384 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
385 else:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
386 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
387
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
388 def testUnicodeStrUsage(self):
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
389 # 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
390 # representation.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
391 self.assertTrue(str(Exception))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
392 self.assertTrue(unicode(Exception))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
393 self.assertTrue(str(Exception('a')))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
394 self.assertTrue(unicode(Exception(u'a')))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
395 self.assertTrue(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
396
59109
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
397 def testUnicodeChangeAttributes(self):
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
398 # See issue 7309. This was a crasher.
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
399
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
400 u = UnicodeEncodeError('baz', u'xxxxx', 1, 5, 'foo')
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
401 self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
402 u.end = 2
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
403 self.assertEqual(str(u), "'baz' codec can't encode character u'\\x78' in position 1: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
404 u.end = 5
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
405 u.reason = 0x345345345345345345
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
406 self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
407 u.encoding = 4000
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
408 self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
409 u.start = 1000
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
410 self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
411
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
412 u = UnicodeDecodeError('baz', 'xxxxx', 1, 5, 'foo')
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
413 self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
414 u.end = 2
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
415 self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
416 u.end = 5
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
417 u.reason = 0x345345345345345345
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
418 self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
419 u.encoding = 4000
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
420 self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
421 u.start = 1000
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
422 self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
423
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
424 u = UnicodeTranslateError(u'xxxx', 1, 5, 'foo')
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
425 self.assertEqual(str(u), "can't translate characters in position 1-4: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
426 u.end = 2
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
427 self.assertEqual(str(u), "can't translate character u'\\x78' in position 1: foo")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
428 u.end = 5
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
429 u.reason = 0x345345345345345345
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
430 self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
431 u.start = 1000
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
432 self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997")
5ac349c322fd Merged revisions 78418 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57928
diff changeset
433
48107
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
434 def test_badisinstance(self):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
435 # 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
436 # it should be ignored
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
437 class Meta(type):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
438 def __subclasscheck__(cls, subclass):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
439 raise ValueError()
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
440
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
441 class MyException(Exception):
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
442 __metaclass__ = Meta
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
443 pass
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
444
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
445 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
446 try:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
447 raise KeyError()
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
448 except MyException, e:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
449 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
450 except KeyError:
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
451 pass
0fbddc04aee1 #2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47914
diff changeset
452 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
453 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
454 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
455 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
456
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
457 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
458 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
459 try:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
460 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
461 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
462 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
463 e, v, tb = g()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49099
diff changeset
464 self.assertTrue(e is RuntimeError, e)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58231
diff changeset
465 self.assertIn("maximum recursion depth exceeded", str(v))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48116
diff changeset
466
71354
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
467 def test_new_returns_invalid_instance(self):
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
468 # See issue #11627.
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
469 class MyException(Exception):
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
470 def __new__(cls, *args):
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
471 return object()
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
472
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
473 with self.assertRaises(TypeError):
45b1ae1ef318 port 8d05f697acd4 (#11627)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
474 raise MyException
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
475
73142
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
476 def test_assert_with_tuple_arg(self):
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
477 try:
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
478 assert False, (3,)
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
479 except AssertionError as e:
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
480 self.assertEqual(str(e), "(3,)")
67bfadf4f954 add a test for an assertion with tuple msg
Benjamin Peterson <benjamin@python.org>
parents: 71354
diff changeset
481
57928
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
482
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
483 # Helper class used by TestSameStrAndUnicodeMsg
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
484 class ExcWithOverriddenStr(Exception):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
485 """Subclass of Exception that accepts a keyword 'msg' arg that is
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
486 returned by __str__. 'msg' won't be included in self.args"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
487 def __init__(self, *args, **kwargs):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
488 self.msg = kwargs.pop('msg') # msg should always be present
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
489 super(ExcWithOverriddenStr, self).__init__(*args, **kwargs)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
490 def __str__(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
491 return self.msg
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
492
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
493
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
494 class TestSameStrAndUnicodeMsg(unittest.TestCase):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
495 """unicode(err) should return the same message of str(err). See #6108"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
496
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
497 def check_same_msg(self, exc, msg):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
498 """Helper function that checks if str(exc) == unicode(exc) == msg"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
499 self.assertEqual(str(exc), msg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
500 self.assertEqual(str(exc), unicode(exc))
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
501
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
502 def test_builtin_exceptions(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
503 """Check same msg for built-in exceptions"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
504 # These exceptions implement a __str__ method that uses the args
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
505 # to create a better error message. unicode(e) should return the same
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
506 # message.
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
507 exceptions = [
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
508 SyntaxError('invalid syntax', ('<string>', 1, 3, '2+*3')),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
509 IOError(2, 'No such file or directory'),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
510 KeyError('both should have the same quotes'),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
511 UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1,
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
512 'ordinal not in range(128)'),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
513 UnicodeEncodeError('ascii', u'\u1234', 0, 1,
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
514 'ordinal not in range(128)')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
515 ]
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
516 for exception in exceptions:
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
517 self.assertEqual(str(exception), unicode(exception))
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
518
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
519 def test_0_args(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
520 """Check same msg for Exception with 0 args"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
521 # str() and unicode() on an Exception with no args should return an
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
522 # empty string
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
523 self.check_same_msg(Exception(), '')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
524
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
525 def test_0_args_with_overridden___str__(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
526 """Check same msg for exceptions with 0 args and overridden __str__"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
527 # str() and unicode() on an exception with overridden __str__ that
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
528 # returns an ascii-only string should return the same string
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
529 for msg in ('foo', u'foo'):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
530 self.check_same_msg(ExcWithOverriddenStr(msg=msg), msg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
531
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
532 # if __str__ returns a non-ascii unicode string str() should fail
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
533 # but unicode() should return the unicode string
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
534 e = ExcWithOverriddenStr(msg=u'f\xf6\xf6') # no args
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
535 self.assertRaises(UnicodeEncodeError, str, e)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
536 self.assertEqual(unicode(e), u'f\xf6\xf6')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
537
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
538 def test_1_arg(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
539 """Check same msg for Exceptions with 1 arg"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
540 for arg in ('foo', u'foo'):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
541 self.check_same_msg(Exception(arg), arg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
542
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
543 # if __str__ is not overridden and self.args[0] is a non-ascii unicode
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
544 # string, str() should try to return str(self.args[0]) and fail.
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
545 # unicode() should return unicode(self.args[0]) and succeed.
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
546 e = Exception(u'f\xf6\xf6')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
547 self.assertRaises(UnicodeEncodeError, str, e)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
548 self.assertEqual(unicode(e), u'f\xf6\xf6')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
549
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
550 def test_1_arg_with_overridden___str__(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
551 """Check same msg for exceptions with overridden __str__ and 1 arg"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
552 # when __str__ is overridden and __unicode__ is not implemented
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
553 # unicode(e) returns the same as unicode(e.__str__()).
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
554 for msg in ('foo', u'foo'):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
555 self.check_same_msg(ExcWithOverriddenStr('arg', msg=msg), msg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
556
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
557 # if __str__ returns a non-ascii unicode string, str() should fail
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
558 # but unicode() should succeed.
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
559 e = ExcWithOverriddenStr('arg', msg=u'f\xf6\xf6') # 1 arg
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
560 self.assertRaises(UnicodeEncodeError, str, e)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
561 self.assertEqual(unicode(e), u'f\xf6\xf6')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
562
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
563 def test_many_args(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
564 """Check same msg for Exceptions with many args"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
565 argslist = [
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
566 (3, 'foo'),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
567 (1, u'foo', 'bar'),
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
568 (4, u'f\xf6\xf6', u'bar', 'baz')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
569 ]
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
570 # both str() and unicode() should return a repr() of the args
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
571 for args in argslist:
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
572 self.check_same_msg(Exception(*args), repr(args))
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
573
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
574 def test_many_args_with_overridden___str__(self):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
575 """Check same msg for exceptions with overridden __str__ and many args"""
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
576 # if __str__ returns an ascii string / ascii unicode string
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
577 # both str() and unicode() should succeed
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
578 for msg in ('foo', u'foo'):
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
579 e = ExcWithOverriddenStr('arg1', u'arg2', u'f\xf6\xf6', msg=msg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
580 self.check_same_msg(e, msg)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
581
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
582 # if __str__ returns a non-ascii unicode string, str() should fail
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
583 # but unicode() should succeed
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
584 e = ExcWithOverriddenStr('arg1', u'f\xf6\xf6', u'arg3', # 3 args
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
585 msg=u'f\xf6\xf6')
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
586 self.assertRaises(UnicodeEncodeError, str, e)
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
587 self.assertEqual(unicode(e), u'f\xf6\xf6')
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
588
62597
c0a49b6a6fb1 testcapi tests are definitely cpython only
Benjamin Peterson <benjamin@python.org>
parents: 60116
diff changeset
589 @cpython_only
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
590 def test_exception_with_doc(self):
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
591 import _testcapi
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
592 doc2 = "This is a test docstring."
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
593 doc4 = "This is another test docstring."
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
594
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
595 self.assertRaises(SystemError, _testcapi.make_exception_with_doc,
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
596 "error1")
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
597
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
598 # test basic usage of PyErr_NewException
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
599 error1 = _testcapi.make_exception_with_doc("_testcapi.error1")
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
600 self.assertIs(type(error1), type)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
601 self.assertTrue(issubclass(error1, Exception))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
602 self.assertIsNone(error1.__doc__)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
603
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
604 # test with given docstring
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
605 error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
606 self.assertEqual(error2.__doc__, doc2)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
607
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
608 # test with explicit base (without docstring)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
609 error3 = _testcapi.make_exception_with_doc("_testcapi.error3",
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
610 base=error2)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
611 self.assertTrue(issubclass(error3, error2))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
612
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
613 # test with explicit base tuple
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
614 class C(object):
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
615 pass
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
616 error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
617 (error3, C))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
618 self.assertTrue(issubclass(error4, error3))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
619 self.assertTrue(issubclass(error4, C))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
620 self.assertEqual(error4.__doc__, doc4)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
621
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
622 # test with explicit dictionary
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
623 error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "",
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
624 error4, {'a': 1})
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
625 self.assertTrue(issubclass(error5, error4))
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
626 self.assertEqual(error5.a, 1)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
627 self.assertEqual(error5.__doc__, "")
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 57927
diff changeset
628
39818
c2fd978df392 Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents: 39781
diff changeset
629
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
630 def test_main():
57928
9424b84a2ec9 Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56014
diff changeset
631 run_unittest(ExceptionTests, TestSameStrAndUnicodeMsg)
38511
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
632
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
633 if __name__ == '__main__':
c747ad53e09e Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents: 38510
diff changeset
634 test_main()