Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_exceptions.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 9424b84a2ec9 |
| children | 5ac349c322fd |
| rev | line source |
|---|---|
| 653 | 1 # Python test set -- part 5, built-in exceptions |
| 2 | |
|
38638
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
3 import os |
|
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
4 import sys |
|
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
5 import unittest |
|
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
6 import pickle, cPickle |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48876
diff
changeset
|
7 import warnings |
|
38638
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
8 |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48876
diff
changeset
|
9 from test.test_support import TESTFN, unlink, run_unittest, captured_output |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
10 from test.test_pep352 import ignore_message_warning |
|
19437
5fec1c9a21ea
Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents:
19239
diff
changeset
|
11 |
| 653 | 12 # XXX This is not really enough, each *operation* should be tested! |
| 13 | |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
14 class ExceptionTests(unittest.TestCase): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
15 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
16 def testReload(self): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
17 # Reloading the built-in exceptions module failed prior to Py2.2, while it |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
18 # should act the same as reloading built-in sys. |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
19 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
20 import exceptions |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
21 reload(exceptions) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
22 except ImportError, e: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
23 self.fail("reloading exceptions: %s" % e) |
|
19239
a08d0e5861cc
reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents:
16398
diff
changeset
|
24 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
25 def raise_catch(self, exc, excname): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
26 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
27 raise exc, "spam" |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
28 except exc, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
29 buf1 = str(err) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
30 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
31 raise exc("spam") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
32 except exc, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
33 buf2 = str(err) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
34 self.assertEquals(buf1, buf2) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
35 self.assertEquals(exc.__name__, excname) |
|
12969
e6b407ba4715
add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12758
diff
changeset
|
36 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
37 def testRaising(self): |
|
38517
d2ae580880b4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38511
diff
changeset
|
38 self.raise_catch(AttributeError, "AttributeError") |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
39 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") |
| 653 | 40 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
41 self.raise_catch(EOFError, "EOFError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
42 fp = open(TESTFN, 'w') |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
43 fp.close() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
44 fp = open(TESTFN, 'r') |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
45 savestdin = sys.stdin |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
46 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
47 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
48 sys.stdin = fp |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
49 x = raw_input() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
50 except EOFError: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
51 pass |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
52 finally: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
53 sys.stdin = savestdin |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
54 fp.close() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
55 unlink(TESTFN) |
| 653 | 56 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
57 self.raise_catch(IOError, "IOError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
58 self.assertRaises(IOError, open, 'this file does not exist', 'r') |
| 653 | 59 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
60 self.raise_catch(ImportError, "ImportError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
61 self.assertRaises(ImportError, __import__, "undefined_module") |
| 653 | 62 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
63 self.raise_catch(IndexError, "IndexError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
64 x = [] |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
65 self.assertRaises(IndexError, x.__getitem__, 10) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
66 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
67 self.raise_catch(KeyError, "KeyError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
68 x = {} |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
69 self.assertRaises(KeyError, x.__getitem__, 'key') |
| 653 | 70 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
71 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
72 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
73 self.raise_catch(MemoryError, "MemoryError") |
| 653 | 74 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
75 self.raise_catch(NameError, "NameError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
76 try: x = undefined_variable |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
77 except NameError: pass |
| 653 | 78 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
79 self.raise_catch(OverflowError, "OverflowError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
80 x = 1 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
81 for dummy in range(128): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
82 x += x # this simply shouldn't blow up |
| 653 | 83 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
84 self.raise_catch(RuntimeError, "RuntimeError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
85 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
86 self.raise_catch(SyntaxError, "SyntaxError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
87 try: exec '/\n' |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
88 except SyntaxError: pass |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
89 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
90 self.raise_catch(IndentationError, "IndentationError") |
| 653 | 91 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
92 self.raise_catch(TabError, "TabError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
93 # can only be tested under -tt, and is the only test for -tt |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
94 #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec') |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
95 #except TabError: pass |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
96 #else: self.fail("TabError not raised") |
| 653 | 97 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
98 self.raise_catch(SystemError, "SystemError") |
| 653 | 99 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
100 self.raise_catch(SystemExit, "SystemExit") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
101 self.assertRaises(SystemExit, sys.exit, 0) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
102 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
103 self.raise_catch(TypeError, "TypeError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
104 try: [] + () |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
105 except TypeError: pass |
|
14472
f6b86e2dc55b
Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents:
14342
diff
changeset
|
106 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
107 self.raise_catch(ValueError, "ValueError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
108 self.assertRaises(ValueError, chr, 10000) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
109 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
110 self.raise_catch(ZeroDivisionError, "ZeroDivisionError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
111 try: x = 1/0 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
112 except ZeroDivisionError: pass |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
113 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
114 self.raise_catch(Exception, "Exception") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
115 try: x = 1/0 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
116 except Exception, e: pass |
|
14472
f6b86e2dc55b
Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents:
14342
diff
changeset
|
117 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
118 def testSyntaxErrorMessage(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
119 # make sure the right exception message is raised for each of |
|
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
120 # these code fragments |
|
14472
f6b86e2dc55b
Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents:
14342
diff
changeset
|
121 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
122 def ckmsg(src, msg): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
123 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
124 compile(src, '<fragment>', 'exec') |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
125 except SyntaxError, e: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
126 if e.msg != msg: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
127 self.fail("expected %s, got %s" % (msg, e.msg)) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
128 else: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
129 self.fail("failed to get expected SyntaxError") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
130 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
131 s = '''while 1: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
132 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
133 pass |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
134 finally: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
135 continue''' |
|
13539
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
12969
diff
changeset
|
136 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
137 if not sys.platform.startswith('java'): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
138 ckmsg(s, "'continue' not supported inside 'finally' clause") |
|
13539
feab677269e0
Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents:
12969
diff
changeset
|
139 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
140 s = '''if 1: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
141 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
142 continue |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
143 except: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
144 pass''' |
| 653 | 145 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
146 ckmsg(s, "'continue' not properly in loop") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
147 ckmsg("continue\n", "'continue' not properly in loop") |
| 653 | 148 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
149 def testSettingException(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
150 # test that setting an exception at the C level works even if the |
|
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
151 # exception object can't be constructed. |
| 653 | 152 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
153 class BadException: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
154 def __init__(self_): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
155 raise RuntimeError, "can't instantiate BadException" |
| 653 | 156 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
157 def test_capi1(): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
158 import _testcapi |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
159 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
160 _testcapi.raise_exception(BadException, 1) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
161 except TypeError, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
162 exc, err, tb = sys.exc_info() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
163 co = tb.tb_frame.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
164 self.assertEquals(co.co_name, "test_capi1") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
165 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
166 else: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
167 self.fail("Expected exception") |
|
20117
e026f517f655
Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19437
diff
changeset
|
168 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
169 def test_capi2(): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
170 import _testcapi |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
171 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
172 _testcapi.raise_exception(BadException, 0) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
173 except RuntimeError, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
174 exc, err, tb = sys.exc_info() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
175 co = tb.tb_frame.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
176 self.assertEquals(co.co_name, "__init__") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
177 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
178 co2 = tb.tb_frame.f_back.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
179 self.assertEquals(co2.co_name, "test_capi2") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
180 else: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
181 self.fail("Expected exception") |
|
20117
e026f517f655
Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19437
diff
changeset
|
182 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
183 if not sys.platform.startswith('java'): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
184 test_capi1() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
185 test_capi2() |
|
38431
4c0823f0a0a1
Conversion of exceptions over from faked-up classes to new-style C types.
Richard Jones <richard@commonground.com.au>
parents:
36751
diff
changeset
|
186 |
|
40209
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
187 def test_WindowsError(self): |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
188 try: |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
189 WindowsError |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
190 except NameError: |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
191 pass |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
192 else: |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
193 self.failUnlessEqual(str(WindowsError(1001)), |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
194 "1001") |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
195 self.failUnlessEqual(str(WindowsError(1001, "message")), |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
196 "[Error 1001] message") |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
197 self.failUnlessEqual(WindowsError(1001, "message").errno, 22) |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
198 self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001) |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
199 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
200 def testAttributes(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
201 # test that exception attributes are happy |
|
38517
d2ae580880b4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38511
diff
changeset
|
202 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
203 exceptionList = [ |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
204 (BaseException, (), {'message' : '', 'args' : ()}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
205 (BaseException, (1, ), {'message' : 1, 'args' : (1,)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
206 (BaseException, ('foo',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
207 {'message' : 'foo', 'args' : ('foo',)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
208 (BaseException, ('foo', 1), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
209 {'message' : '', 'args' : ('foo', 1)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
210 (SystemExit, ('foo',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
211 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
212 (IOError, ('foo',), |
|
39924
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
213 {'message' : 'foo', 'args' : ('foo',), 'filename' : None, |
|
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
214 'errno' : None, 'strerror' : None}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
215 (IOError, ('foo', 'bar'), |
|
39924
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
216 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None, |
|
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
217 'errno' : 'foo', 'strerror' : 'bar'}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
218 (IOError, ('foo', 'bar', 'baz'), |
|
39924
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
219 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz', |
|
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
220 'errno' : 'foo', 'strerror' : 'bar'}), |
|
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
221 (IOError, ('foo', 'bar', 'baz', 'quux'), |
|
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
222 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
223 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
224 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
225 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
226 'filename' : 'filenameStr'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
227 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
228 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
229 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), |
|
40987
9dd86ead51c3
Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents:
40725
diff
changeset
|
230 (SyntaxError, (), {'message' : '', 'msg' : None, 'text' : None, |
|
9dd86ead51c3
Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents:
40725
diff
changeset
|
231 'filename' : None, 'lineno' : None, 'offset' : None, |
|
9dd86ead51c3
Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents:
40725
diff
changeset
|
232 'print_file_and_line' : None}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
233 (SyntaxError, ('msgStr',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
234 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
235 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
236 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
237 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
238 'textStr')), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
239 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
240 'args' : ('msgStr', ('filenameStr', 'linenoStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
241 'offsetStr', 'textStr')), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
242 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
243 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
244 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
245 'textStr', 'print_file_and_lineStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
246 {'message' : '', 'text' : None, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
247 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
248 'textStr', 'print_file_and_lineStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
249 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
250 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
251 (UnicodeError, (), {'message' : '', 'args' : (),}), |
|
39781
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
252 (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'), |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
253 {'message' : '', 'args' : ('ascii', u'a', 0, 1, |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
254 'ordinal not in range'), |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
255 'encoding' : 'ascii', 'object' : u'a', |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
256 'start' : 0, 'reason' : 'ordinal not in range'}), |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
257 (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
258 {'message' : '', 'args' : ('ascii', '\xff', 0, 1, |
|
39781
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
259 'ordinal not in range'), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
260 'encoding' : 'ascii', 'object' : '\xff', |
|
39781
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
261 'start' : 0, 'reason' : 'ordinal not in range'}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
262 (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
263 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
264 'object' : u'\u3042', 'reason' : 'ouch', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
265 'start' : 0, 'end' : 1}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
266 ] |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
267 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
268 exceptionList.append( |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
269 (WindowsError, (1, 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
270 {'message' : '', 'args' : (1, 'strErrorStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
271 'strerror' : 'strErrorStr', 'winerror' : 1, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
272 'errno' : 22, 'filename' : 'filenameStr'}) |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
273 ) |
|
38638
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
274 except NameError: |
|
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
275 pass |
|
38506
0aba63ff7f73
Add a test case for exception pickling. args is never NULL.
Georg Brandl <georg@python.org>
parents:
38431
diff
changeset
|
276 |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48876
diff
changeset
|
277 with warnings.catch_warnings(): |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
278 ignore_message_warning() |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
279 for exc, args, expected in exceptionList: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
280 try: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
281 raise exc(*args) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
282 except BaseException, e: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
283 if type(e) is not exc: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
284 raise |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
285 # Verify module name |
|
42688
62eee79de923
Revert accidental checkins from last commit.
Georg Brandl <georg@python.org>
parents:
42687
diff
changeset
|
286 self.assertEquals(type(e).__module__, 'exceptions') |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
287 # Verify no ref leaks in Exc_str() |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
288 s = str(e) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
289 for checkArgName in expected: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
290 self.assertEquals(repr(getattr(e, checkArgName)), |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
291 repr(expected[checkArgName]), |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
292 'exception "%s", attribute "%s"' % |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
293 (repr(e), checkArgName)) |
|
38517
d2ae580880b4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38511
diff
changeset
|
294 |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
295 # test for pickling support |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
296 for p in pickle, cPickle: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
297 for protocol in range(p.HIGHEST_PROTOCOL + 1): |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
298 new = p.loads(p.dumps(e, protocol)) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
299 for checkArgName in expected: |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
300 got = repr(getattr(new, checkArgName)) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
301 want = repr(expected[checkArgName]) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
302 self.assertEquals(got, want, |
|
43758
6e491d12765c
Add a missing quotation mark.
Brett Cannon <bcannon@gmail.com>
parents:
42688
diff
changeset
|
303 'pickled "%r", attribute "%s"' % |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
40987
diff
changeset
|
304 (e, checkArgName)) |
|
38508
99564e62aea9
Disallow keyword args for exceptions.
Georg Brandl <georg@python.org>
parents:
38506
diff
changeset
|
305 |
|
56014
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
306 |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
307 def testDeprecatedMessageAttribute(self): |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
308 # 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
|
309 # BaseException.__init__ triggers a deprecation warning. |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
310 exc = BaseException("foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
311 with warnings.catch_warnings(record=True) as w: |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
312 self.assertEquals(exc.message, "foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
313 self.assertEquals(len(w), 1) |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
314 self.assertEquals(w[0].category, DeprecationWarning) |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
315 self.assertEquals( |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
316 str(w[0].message), |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
317 "BaseException.message has been deprecated as of Python 2.6") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
318 |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
319 |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
320 def testRegularMessageAttribute(self): |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
321 # Accessing BaseException.message after explicitly setting a value |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
322 # for it does not trigger a deprecation warning. |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
323 exc = BaseException("foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
324 exc.message = "bar" |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
325 with warnings.catch_warnings(record=True) as w: |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
326 self.assertEquals(exc.message, "bar") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
327 self.assertEquals(len(w), 0) |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
328 # Deleting the message is supported, too. |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
329 del exc.message |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
330 self.assertRaises(AttributeError, getattr, exc, "message") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
331 |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
332 def testPickleMessageAttribute(self): |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
333 # Pickling with message attribute must work, as well. |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
334 e = Exception("foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
335 f = Exception("foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
336 f.message = "bar" |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
337 for p in pickle, cPickle: |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
338 ep = p.loads(p.dumps(e)) |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
339 with warnings.catch_warnings(): |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
340 ignore_message_warning() |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
341 self.assertEqual(ep.message, "foo") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
342 fp = p.loads(p.dumps(f)) |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
343 self.assertEqual(fp.message, "bar") |
|
8ab304034021
Merged revisions 74845 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49099
diff
changeset
|
344 |
|
40725
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
345 def testSlicing(self): |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
346 # Test that you can slice an exception directly instead of requiring |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
347 # going through the 'args' attribute. |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
348 args = (1, 2, 3) |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
349 exc = BaseException(*args) |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
350 self.failUnlessEqual(exc[:], args) |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
351 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
352 def testKeywordArgs(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
353 # 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
|
354 # but user-defined subclasses can if they want |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
355 self.assertRaises(TypeError, BaseException, a=1) |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
356 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
357 class DerivedException(BaseException): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
358 def __init__(self, fancy_arg): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
359 BaseException.__init__(self) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
360 self.fancy_arg = fancy_arg |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
361 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
362 x = DerivedException(fancy_arg=42) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
363 self.assertEquals(x.fancy_arg, 42) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
364 |
|
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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 |
|
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 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
376 # 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
|
377 # 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
|
378 # 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
|
379 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
|
380 try: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
381 g() |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
382 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
|
383 pass |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
384 except: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
385 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
|
386 else: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
387 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
|
388 |
|
39818
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
389 def testUnicodeStrUsage(self): |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
390 # 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
|
391 # representation. |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
392 self.failUnless(str(Exception)) |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
393 self.failUnless(unicode(Exception)) |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
394 self.failUnless(str(Exception('a'))) |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
395 self.failUnless(unicode(Exception(u'a'))) |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
43758
diff
changeset
|
396 self.failUnless(unicode(Exception(u'\xe1'))) |
|
39818
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
397 |
|
48107
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
398 def test_badisinstance(self): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
399 # 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
|
400 # it should be ignored |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
401 class Meta(type): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
402 def __subclasscheck__(cls, subclass): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
403 raise ValueError() |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
404 |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
405 class MyException(Exception): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
406 __metaclass__ = Meta |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
407 pass |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
408 |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
409 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
|
410 try: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
411 raise KeyError() |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
412 except MyException, e: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
413 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
|
414 except KeyError: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
415 pass |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
416 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
|
417 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
|
418 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
|
419 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
|
420 |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
421 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
|
422 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
|
423 try: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
424 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
|
425 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
|
426 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
|
427 e, v, tb = g() |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
428 self.assert_(e is RuntimeError, e) |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
429 self.assert_("maximum recursion depth exceeded" in str(v), v) |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
430 |
|
39818
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
431 |
|
57928
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
432 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
433 # Helper class used by TestSameStrAndUnicodeMsg |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
434 class ExcWithOverriddenStr(Exception): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
435 """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
|
436 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
|
437 def __init__(self, *args, **kwargs): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
438 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
|
439 super(ExcWithOverriddenStr, self).__init__(*args, **kwargs) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
440 def __str__(self): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
441 return self.msg |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
442 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
443 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
444 class TestSameStrAndUnicodeMsg(unittest.TestCase): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
445 """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
|
446 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
447 def check_same_msg(self, exc, msg): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
448 """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
|
449 self.assertEqual(str(exc), msg) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
450 self.assertEqual(str(exc), unicode(exc)) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
451 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
452 def test_builtin_exceptions(self): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
453 """Check same msg for built-in exceptions""" |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
454 # 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
|
455 # 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
|
456 # message. |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
457 exceptions = [ |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
458 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
|
459 IOError(2, 'No such file or directory'), |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
460 KeyError('both should have the same quotes'), |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
461 UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
462 'ordinal not in range(128)'), |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
463 UnicodeEncodeError('ascii', u'\u1234', 0, 1, |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
464 'ordinal not in range(128)') |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
465 ] |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
466 for exception in exceptions: |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
467 self.assertEqual(str(exception), unicode(exception)) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
468 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
469 def test_0_args(self): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
470 """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
|
471 # 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
|
472 # empty string |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
473 self.check_same_msg(Exception(), '') |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
474 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
475 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
|
476 """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
|
477 # 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
|
478 # 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
|
479 for msg in ('foo', u'foo'): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
480 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
|
481 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
482 # 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
|
483 # but unicode() should return the unicode string |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
484 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
|
485 self.assertRaises(UnicodeEncodeError, str, e) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
486 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
|
487 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
488 def test_1_arg(self): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
489 """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
|
490 for arg in ('foo', u'foo'): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
491 self.check_same_msg(Exception(arg), arg) |
|
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 # 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
|
494 # 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
|
495 # 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
|
496 e = Exception(u'f\xf6\xf6') |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
497 self.assertRaises(UnicodeEncodeError, str, e) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
498 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
|
499 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
500 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
|
501 """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
|
502 # 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
|
503 # 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
|
504 for msg in ('foo', u'foo'): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
505 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
|
506 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
507 # 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
|
508 # but unicode() should succeed. |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
509 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
|
510 self.assertRaises(UnicodeEncodeError, str, e) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
511 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
|
512 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
513 def test_many_args(self): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
514 """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
|
515 argslist = [ |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
516 (3, 'foo'), |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
517 (1, u'foo', 'bar'), |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
518 (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
|
519 ] |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
520 # 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
|
521 for args in argslist: |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
522 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
|
523 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
524 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
|
525 """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
|
526 # 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
|
527 # both str() and unicode() should succeed |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
528 for msg in ('foo', u'foo'): |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
529 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
|
530 self.check_same_msg(e, 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 succeed |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
534 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
|
535 msg=u'f\xf6\xf6') |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
536 self.assertRaises(UnicodeEncodeError, str, e) |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
537 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
|
538 |
|
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
539 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
540 def test_main(): |
|
57928
9424b84a2ec9
Merged revisions 77045 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56014
diff
changeset
|
541 run_unittest(ExceptionTests, TestSameStrAndUnicodeMsg) |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
542 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
543 if __name__ == '__main__': |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
544 test_main() |
