Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_exceptions.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 5badebdb438c |
| children | 2b82b16a6375 |
| 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 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
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 | 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: |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
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) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
35 self.assertEquals(buf1, buf2) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
36 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
|
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 98 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
99 self.raise_catch(SystemError, "SystemError") |
| 653 | 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") |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
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") |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
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 | 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 | 149 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
150 def testSettingException(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
151 # 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
|
152 # exception object can't be constructed. |
| 653 | 153 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
154 class BadException: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
155 def __init__(self_): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
156 raise RuntimeError, "can't instantiate BadException" |
| 653 | 157 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
158 def test_capi1(): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
159 import _testcapi |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
160 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
161 _testcapi.raise_exception(BadException, 1) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
162 except TypeError, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
163 exc, err, tb = sys.exc_info() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
164 co = tb.tb_frame.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
165 self.assertEquals(co.co_name, "test_capi1") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
166 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
|
167 else: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
168 self.fail("Expected exception") |
|
20117
e026f517f655
Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19437
diff
changeset
|
169 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
170 def test_capi2(): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
171 import _testcapi |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
172 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
173 _testcapi.raise_exception(BadException, 0) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
174 except RuntimeError, err: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
175 exc, err, tb = sys.exc_info() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
176 co = tb.tb_frame.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
177 self.assertEquals(co.co_name, "__init__") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
178 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
|
179 co2 = tb.tb_frame.f_back.f_code |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
180 self.assertEquals(co2.co_name, "test_capi2") |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
181 else: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
182 self.fail("Expected exception") |
|
20117
e026f517f655
Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19437
diff
changeset
|
183 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
184 if not sys.platform.startswith('java'): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
185 test_capi1() |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
186 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
|
187 |
|
40209
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
188 def test_WindowsError(self): |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
189 try: |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
190 WindowsError |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
191 except NameError: |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
192 pass |
|
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
193 else: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
194 self.assertEqual(str(WindowsError(1001)), |
|
40209
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
195 "1001") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
196 self.assertEqual(str(WindowsError(1001, "message")), |
|
40209
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
197 "[Error 1001] message") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
198 self.assertEqual(WindowsError(1001, "message").errno, 22) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
199 self.assertEqual(WindowsError(1001, "message").winerror, 1001) |
|
40209
988d95ff1fdb
WindowsError.str should display the windows error code,
Thomas Heller <theller@ctypes.org>
parents:
39924
diff
changeset
|
200 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
201 @ignore_deprecation_warnings |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
202 def testAttributes(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
203 # test that exception attributes are happy |
|
38517
d2ae580880b4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38511
diff
changeset
|
204 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
205 exceptionList = [ |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
206 (BaseException, (), {'message' : '', 'args' : ()}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
207 (BaseException, (1, ), {'message' : 1, 'args' : (1,)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
208 (BaseException, ('foo',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
209 {'message' : 'foo', 'args' : ('foo',)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
210 (BaseException, ('foo', 1), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
211 {'message' : '', 'args' : ('foo', 1)}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
212 (SystemExit, ('foo',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
213 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
214 (IOError, ('foo',), |
|
39924
e1ab4653ebc0
Bug #1566800: make sure that EnvironmentError can be called with any
Georg Brandl <georg@python.org>
parents:
39818
diff
changeset
|
215 {'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
|
216 'errno' : None, 'strerror' : None}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
217 (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
|
218 {'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
|
219 'errno' : 'foo', 'strerror' : 'bar'}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
220 (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
|
221 {'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
|
222 '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
|
223 (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
|
224 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
225 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
226 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
227 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
228 'filename' : 'filenameStr'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
229 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
230 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
231 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), |
|
40987
9dd86ead51c3
Add a test for instantiating SyntaxError with no arguments.
Brett Cannon <bcannon@gmail.com>
parents:
40725
diff
changeset
|
232 (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
|
233 '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
|
234 'print_file_and_line' : None}), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
235 (SyntaxError, ('msgStr',), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
236 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
237 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
238 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
239 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
240 'textStr')), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
241 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
242 'args' : ('msgStr', ('filenameStr', 'linenoStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
243 'offsetStr', 'textStr')), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
244 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
245 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
246 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
247 'textStr', 'print_file_and_lineStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
248 {'message' : '', 'text' : None, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
249 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
250 'textStr', 'print_file_and_lineStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
251 'print_file_and_line' : None, 'msg' : 'msgStr', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
252 'filename' : None, 'lineno' : None, 'offset' : None}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
253 (UnicodeError, (), {'message' : '', 'args' : (),}), |
|
39781
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
254 (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
|
255 {'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
|
256 'ordinal not in range'), |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
257 'encoding' : 'ascii', 'object' : u'a', |
|
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
258 '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
|
259 (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
|
260 {'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
|
261 'ordinal not in range'), |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
262 'encoding' : 'ascii', 'object' : '\xff', |
|
39781
cb883e7e0b7e
Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack.
Georg Brandl <georg@python.org>
parents:
38904
diff
changeset
|
263 '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
|
264 (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
265 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
266 'object' : u'\u3042', 'reason' : 'ouch', |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
267 'start' : 0, 'end' : 1}), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
268 ] |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
269 try: |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
270 exceptionList.append( |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
271 (WindowsError, (1, 'strErrorStr', 'filenameStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
272 {'message' : '', 'args' : (1, 'strErrorStr'), |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
273 'strerror' : 'strErrorStr', 'winerror' : 1, |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
274 'errno' : 22, 'filename' : 'filenameStr'}) |
|
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
275 ) |
|
38638
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
276 except NameError: |
|
ea9966c88657
SF patch 1501987: Remove randomness from test_exceptions,
Tim Peters <tim.peters@gmail.com>
parents:
38542
diff
changeset
|
277 pass |
|
38506
0aba63ff7f73
Add a test case for exception pickling. args is never NULL.
Georg Brandl <georg@python.org>
parents:
38431
diff
changeset
|
278 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
279 for exc, args, expected in exceptionList: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
280 try: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
281 raise exc(*args) |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
282 except BaseException, e: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
283 if type(e) is not exc: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
284 raise |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
285 # Verify module name |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
286 self.assertEquals(type(e).__module__, 'exceptions') |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
287 # Verify no ref leaks in Exc_str() |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
288 s = str(e) |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
289 for checkArgName in expected: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
290 self.assertEquals(repr(getattr(e, checkArgName)), |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
291 repr(expected[checkArgName]), |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
292 'exception "%s", attribute "%s"' % |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
293 (repr(e), checkArgName)) |
|
38517
d2ae580880b4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38511
diff
changeset
|
294 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
295 # test for pickling support |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
296 for p in pickle, cPickle: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
297 for protocol in range(p.HIGHEST_PROTOCOL + 1): |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
298 new = p.loads(p.dumps(e, protocol)) |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
299 for checkArgName in expected: |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
300 got = repr(getattr(new, checkArgName)) |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
301 want = repr(expected[checkArgName]) |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
302 self.assertEquals(got, want, |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
303 'pickled "%r", attribute "%s"' % |
|
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
304 (e, checkArgName)) |
|
38508
99564e62aea9
Disallow keyword args for exceptions.
Georg Brandl <georg@python.org>
parents:
38506
diff
changeset
|
305 |
|
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
|
306 |
|
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
|
307 def testDeprecatedMessageAttribute(self): |
|
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
|
308 # Accessing BaseException.message and relying on its value set by |
|
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
|
309 # BaseException.__init__ triggers a deprecation warning. |
|
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
|
310 exc = BaseException("foo") |
|
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
|
311 with warnings.catch_warnings(record=True) as w: |
|
58231
1f177818f1d0
DeprecationWarning is now silent by default.
Brett Cannon <bcannon@gmail.com>
parents:
58198
diff
changeset
|
312 warnings.simplefilter('default') |
|
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
|
313 self.assertEquals(exc.message, "foo") |
|
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
|
314 self.assertEquals(len(w), 1) |
|
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
|
315 self.assertEquals(w[0].category, DeprecationWarning) |
|
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
|
316 self.assertEquals( |
|
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
|
317 str(w[0].message), |
|
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
|
318 "BaseException.message has been deprecated as of Python 2.6") |
|
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
|
319 |
|
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
|
320 |
|
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
|
321 def testRegularMessageAttribute(self): |
|
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
|
322 # Accessing BaseException.message after explicitly setting a value |
|
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
|
323 # for it does not trigger a deprecation warning. |
|
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
|
324 exc = BaseException("foo") |
|
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
|
325 exc.message = "bar" |
|
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
|
326 with warnings.catch_warnings(record=True) as w: |
|
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 self.assertEquals(exc.message, "bar") |
|
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 self.assertEquals(len(w), 0) |
|
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
|
329 # Deleting the message is supported, too. |
|
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
|
330 del exc.message |
|
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
|
331 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
|
332 exc.message |
|
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
|
333 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
334 @ignore_deprecation_warnings |
|
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
|
335 def testPickleMessageAttribute(self): |
|
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
|
336 # Pickling with message attribute must work, as well. |
|
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
|
337 e = Exception("foo") |
|
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
|
338 f = Exception("foo") |
|
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
|
339 f.message = "bar" |
|
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
|
340 for p in pickle, cPickle: |
|
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
|
341 ep = p.loads(p.dumps(e)) |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
342 self.assertEqual(ep.message, "foo") |
|
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
|
343 fp = p.loads(p.dumps(f)) |
|
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
|
344 self.assertEqual(fp.message, "bar") |
|
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
|
345 |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
346 @ignore_deprecation_warnings |
|
40725
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
347 def testSlicing(self): |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
348 # 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
|
349 # going through the 'args' attribute. |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
350 args = (1, 2, 3) |
|
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
351 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
|
352 self.assertEqual(exc[:], args) |
|
58640
5badebdb438c
#7092: Silence py3k warnings in test_exceptions and test_pep352. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
353 self.assertEqual(exc.args[:], args) |
|
40725
2d8239abfa60
Add a test for slicing an exception.
Brett Cannon <bcannon@gmail.com>
parents:
40454
diff
changeset
|
354 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
355 def testKeywordArgs(self): |
|
38541
f37e67bd4393
Convert docstrings to comments so regrtest -v prints method names
Neal Norwitz <nnorwitz@gmail.com>
parents:
38535
diff
changeset
|
356 # 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
|
357 # but user-defined subclasses can if they want |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
358 self.assertRaises(TypeError, BaseException, a=1) |
|
38535
cf677e978852
Some code style tweaks, and remove apply.
Georg Brandl <georg@python.org>
parents:
38517
diff
changeset
|
359 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
360 class DerivedException(BaseException): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
361 def __init__(self, fancy_arg): |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
362 BaseException.__init__(self) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
363 self.fancy_arg = fancy_arg |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
364 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
365 x = DerivedException(fancy_arg=42) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
366 self.assertEquals(x.fancy_arg, 42) |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
367 |
|
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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 |
|
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 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
379 # 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
|
380 # 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
|
381 # 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
|
382 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
|
383 try: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
384 g() |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
385 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
|
386 pass |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
387 except: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
388 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
|
389 else: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
390 self.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
|
391 |
|
39818
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
392 def testUnicodeStrUsage(self): |
|
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
393 # 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
|
394 # representation. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
395 self.assertTrue(str(Exception)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
396 self.assertTrue(unicode(Exception)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
397 self.assertTrue(str(Exception('a'))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
398 self.assertTrue(unicode(Exception(u'a'))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
399 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
|
400 |
|
48107
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
401 def test_badisinstance(self): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
402 # 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
|
403 # it should be ignored |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
404 class Meta(type): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
405 def __subclasscheck__(cls, subclass): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
406 raise ValueError() |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
407 |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
408 class MyException(Exception): |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
409 __metaclass__ = Meta |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
410 pass |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
411 |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
412 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
|
413 try: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
414 raise KeyError() |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
415 except MyException, e: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
416 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
|
417 except KeyError: |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
418 pass |
|
0fbddc04aee1
#2542: now that issubclass() may call arbitrary code,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47914
diff
changeset
|
419 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
|
420 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
|
421 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
|
422 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
|
423 |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
424 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
|
425 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
|
426 try: |
|
d01c1d768841
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents:
48116
diff
changeset
|
427 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
|
428 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
|
429 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
|
430 e, v, tb = g() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
431 self.assertTrue(e is RuntimeError, e) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58231
diff
changeset
|
432 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
|
433 |
|
39818
c2fd978df392
Remove the __unicode__ method from exceptions. Allows unicode() to be called
Brett Cannon <bcannon@gmail.com>
parents:
39781
diff
changeset
|
434 |
|
57927
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
435 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
436 # Helper class used by TestSameStrAndUnicodeMsg |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
437 class ExcWithOverriddenStr(Exception): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
438 """Subclass of Exception that accepts a keyword 'msg' arg that is |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
439 returned by __str__. 'msg' won't be included in self.args""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
440 def __init__(self, *args, **kwargs): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
441 self.msg = kwargs.pop('msg') # msg should always be present |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
442 super(ExcWithOverriddenStr, self).__init__(*args, **kwargs) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
443 def __str__(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
444 return self.msg |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
445 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
446 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
447 class TestSameStrAndUnicodeMsg(unittest.TestCase): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
448 """unicode(err) should return the same message of str(err). See #6108""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
449 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
450 def check_same_msg(self, exc, msg): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
451 """Helper function that checks if str(exc) == unicode(exc) == msg""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
452 self.assertEqual(str(exc), msg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
453 self.assertEqual(str(exc), unicode(exc)) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
454 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
455 def test_builtin_exceptions(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
456 """Check same msg for built-in exceptions""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
457 # These exceptions implement a __str__ method that uses the args |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
458 # to create a better error message. unicode(e) should return the same |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
459 # message. |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
460 exceptions = [ |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
461 SyntaxError('invalid syntax', ('<string>', 1, 3, '2+*3')), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
462 IOError(2, 'No such file or directory'), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
463 KeyError('both should have the same quotes'), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
464 UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
465 'ordinal not in range(128)'), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
466 UnicodeEncodeError('ascii', u'\u1234', 0, 1, |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
467 'ordinal not in range(128)') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
468 ] |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
469 for exception in exceptions: |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
470 self.assertEqual(str(exception), unicode(exception)) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
471 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
472 def test_0_args(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
473 """Check same msg for Exception with 0 args""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
474 # str() and unicode() on an Exception with no args should return an |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
475 # empty string |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
476 self.check_same_msg(Exception(), '') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
477 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
478 def test_0_args_with_overridden___str__(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
479 """Check same msg for exceptions with 0 args and overridden __str__""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
480 # str() and unicode() on an exception with overridden __str__ that |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
481 # returns an ascii-only string should return the same string |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
482 for msg in ('foo', u'foo'): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
483 self.check_same_msg(ExcWithOverriddenStr(msg=msg), msg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
484 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
485 # if __str__ returns a non-ascii unicode string str() should fail |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
486 # but unicode() should return the unicode string |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
487 e = ExcWithOverriddenStr(msg=u'f\xf6\xf6') # no args |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
488 self.assertRaises(UnicodeEncodeError, str, e) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
489 self.assertEqual(unicode(e), u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
490 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
491 def test_1_arg(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
492 """Check same msg for Exceptions with 1 arg""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
493 for arg in ('foo', u'foo'): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
494 self.check_same_msg(Exception(arg), arg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
495 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
496 # if __str__ is not overridden and self.args[0] is a non-ascii unicode |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
497 # string, str() should try to return str(self.args[0]) and fail. |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
498 # unicode() should return unicode(self.args[0]) and succeed. |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
499 e = Exception(u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
500 self.assertRaises(UnicodeEncodeError, str, e) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
501 self.assertEqual(unicode(e), u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
502 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
503 def test_1_arg_with_overridden___str__(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
504 """Check same msg for exceptions with overridden __str__ and 1 arg""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
505 # when __str__ is overridden and __unicode__ is not implemented |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
506 # unicode(e) returns the same as unicode(e.__str__()). |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
507 for msg in ('foo', u'foo'): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
508 self.check_same_msg(ExcWithOverriddenStr('arg', msg=msg), msg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
509 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
510 # if __str__ returns a non-ascii unicode string, str() should fail |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
511 # but unicode() should succeed. |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
512 e = ExcWithOverriddenStr('arg', msg=u'f\xf6\xf6') # 1 arg |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
513 self.assertRaises(UnicodeEncodeError, str, e) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
514 self.assertEqual(unicode(e), u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
515 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
516 def test_many_args(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
517 """Check same msg for Exceptions with many args""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
518 argslist = [ |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
519 (3, 'foo'), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
520 (1, u'foo', 'bar'), |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
521 (4, u'f\xf6\xf6', u'bar', 'baz') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
522 ] |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
523 # both str() and unicode() should return a repr() of the args |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
524 for args in argslist: |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
525 self.check_same_msg(Exception(*args), repr(args)) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
526 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
527 def test_many_args_with_overridden___str__(self): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
528 """Check same msg for exceptions with overridden __str__ and many args""" |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
529 # if __str__ returns an ascii string / ascii unicode string |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
530 # both str() and unicode() should succeed |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
531 for msg in ('foo', u'foo'): |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
532 e = ExcWithOverriddenStr('arg1', u'arg2', u'f\xf6\xf6', msg=msg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
533 self.check_same_msg(e, msg) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
534 |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
535 # if __str__ returns a non-ascii unicode string, str() should fail |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
536 # but unicode() should succeed |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
537 e = ExcWithOverriddenStr('arg1', u'f\xf6\xf6', u'arg3', # 3 args |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
538 msg=u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
539 self.assertRaises(UnicodeEncodeError, str, e) |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
540 self.assertEqual(unicode(e), u'f\xf6\xf6') |
|
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
541 |
|
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
|
542 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
|
543 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
|
544 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
|
545 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
|
546 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
547 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
|
548 "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
|
549 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
550 # 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
556 # 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
|
557 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
|
558 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
|
559 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
560 # 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
|
561 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
|
562 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
|
563 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
|
564 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
565 # 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
|
566 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
|
567 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
|
568 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
|
569 (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
|
570 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
|
571 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
|
572 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
|
573 |
|
cf39c5e462f9
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents:
57927
diff
changeset
|
574 # 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
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 |
|
57927
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
581 |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
582 def test_main(): |
|
57927
86387b8b9af8
#6108: unicode(exception) and str(exception) should return the same message
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56011
diff
changeset
|
583 run_unittest(ExceptionTests, TestSameStrAndUnicodeMsg) |
|
38511
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
584 |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
585 if __name__ == '__main__': |
|
c747ad53e09e
Convert test_exceptions to unittest.
Georg Brandl <georg@python.org>
parents:
38510
diff
changeset
|
586 test_main() |
