annotate Lib/test/test_exceptions.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 226ba34bd998
children 90342f395721
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- part 5, built-in exceptions
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
24442
226ba34bd998 Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents: 23073
diff changeset
3 from test.test_support import TestFailed, TESTFN, unlink
6194
cff105a3a384 Expanded r() function to handle class exceptions.
Barry Warsaw <barry@python.org>
parents: 2970
diff changeset
4 from types import ClassType
19437
5fec1c9a21ea Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents: 19239
diff changeset
5 import warnings
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
6 import sys, traceback
19437
5fec1c9a21ea Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents: 19239
diff changeset
7
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 print '5. Built-in exceptions'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # XXX This is not really enough, each *operation* should be tested!
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
19239
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
11 # Reloading the built-in exceptions module failed prior to Py2.2, while it
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
12 # should act the same as reloading built-in sys.
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
13 try:
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
14 import exceptions
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
15 reload(exceptions)
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
16 except ImportError, e:
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
17 raise TestFailed, e
a08d0e5861cc reload(exceptions) should not raise an ImportError, but should act
Barry Warsaw <barry@python.org>
parents: 16398
diff changeset
18
12969
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
19 def test_raise_catch(exc):
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
20 try:
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
21 raise exc, "spam"
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
22 except exc, err:
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
23 buf = str(err)
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
24 try:
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
25 raise exc("spam")
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
26 except exc, err:
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
27 buf = str(err)
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
28 print buf
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
29
6194
cff105a3a384 Expanded r() function to handle class exceptions.
Barry Warsaw <barry@python.org>
parents: 2970
diff changeset
30 def r(thing):
12969
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
31 test_raise_catch(thing)
14342
d41c41371dc1 Applying patch #100994 to allow JPython to use more of the standard
Barry Warsaw <barry@python.org>
parents: 13539
diff changeset
32 if isinstance(thing, ClassType):
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6194
diff changeset
33 print thing.__name__
6194
cff105a3a384 Expanded r() function to handle class exceptions.
Barry Warsaw <barry@python.org>
parents: 2970
diff changeset
34 else:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6194
diff changeset
35 print thing
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 r(AttributeError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 import sys
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 try: x = sys.undefined_attribute
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 except AttributeError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 r(EOFError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 import sys
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 fp = open(TESTFN, 'w')
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 fp.close()
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 fp = open(TESTFN, 'r')
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 savestdin = sys.stdin
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 try:
15304
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
49 try:
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
50 sys.stdin = fp
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
51 x = raw_input()
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
52 except EOFError:
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
53 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 finally:
15304
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
55 sys.stdin = savestdin
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
56 fp.close()
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 r(IOError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 try: open('this file does not exist', 'r')
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 except IOError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 r(ImportError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 try: import undefined_module
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 except ImportError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 r(IndexError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 x = []
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 try: a = x[10]
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 except IndexError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 r(KeyError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 x = {}
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 try: a = x['key']
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 except KeyError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 r(KeyboardInterrupt)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 print '(not testable in a script)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 r(MemoryError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 print '(not safe to test)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 r(NameError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 try: x = undefined_variable
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 except NameError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 r(OverflowError)
23073
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
87 # XXX
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
88 # Obscure: this test relies on int+int raising OverflowError if the
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
89 # ints are big enough. But ints no longer do that by default. This
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
90 # test will have to go away someday. For now, we can convert the
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
91 # transitional OverflowWarning into an error.
21d252028404 Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents: 21309
diff changeset
92 warnings.filterwarnings("error", "", OverflowWarning, __name__)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 x = 1
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 try:
15304
a6f3ef10815e Use 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 14472
diff changeset
95 while 1: x = x+x
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 except OverflowError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 r(RuntimeError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 print '(not used any more?)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 r(SyntaxError)
2970
cfc66b8ca202 exec() -> exec
Guido van Rossum <guido@python.org>
parents: 853
diff changeset
102 try: exec '/\n'
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 except SyntaxError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
105 # make sure the right exception message is raised for each of these
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
106 # code fragments:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
107
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
108 def ckmsg(src, msg):
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
109 try:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
110 compile(src, '<fragment>', 'exec')
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
111 except SyntaxError, e:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
112 print e.msg
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
113 if e.msg == msg:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
114 print "ok"
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
115 else:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
116 print "expected:", msg
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
117 else:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
118 print "failed to get expected SyntaxError"
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
119
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
120 s = '''\
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
121 while 1:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
122 try:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
123 pass
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
124 finally:
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15304
diff changeset
125 continue
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
126 '''
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
127 if sys.platform.startswith('java'):
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
128 print "'continue' not supported inside 'finally' clause"
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
129 print "ok"
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
130 else:
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
131 ckmsg(s, "'continue' not supported inside 'finally' clause")
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
132 s = '''\
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
133 try:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
134 continue
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
135 except:
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
136 pass
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
137 '''
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
138 ckmsg(s, "'continue' not properly in loop")
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
139 ckmsg("continue\n", "'continue' not properly in loop")
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
140
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
141 r(IndentationError)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
142
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
143 r(TabError)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
144 # can only be tested under -tt, and is the only test for -tt
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
145 #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
146 #except TabError: pass
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
147 #else: raise TestFailed
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
148
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 r(SystemError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 print '(hard to reproduce)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 r(SystemExit)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 import sys
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 try: sys.exit(0)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 except SystemExit: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 r(TypeError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 try: [] + ()
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 except TypeError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 r(ValueError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 try: x = chr(10000)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 except ValueError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 r(ZeroDivisionError)
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 try: x = 1/0
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 except ZeroDivisionError: pass
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
12758
406b0e24d62e Added a test to catch the base class.
Barry Warsaw <barry@python.org>
parents: 7747
diff changeset
169 r(Exception)
406b0e24d62e Added a test to catch the base class.
Barry Warsaw <barry@python.org>
parents: 7747
diff changeset
170 try: x = 1/0
406b0e24d62e Added a test to catch the base class.
Barry Warsaw <barry@python.org>
parents: 7747
diff changeset
171 except Exception, e: pass
406b0e24d62e Added a test to catch the base class.
Barry Warsaw <barry@python.org>
parents: 7747
diff changeset
172
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
173 # test that setting an exception at the C level works even if the
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
174 # exception object can't be constructed.
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
175
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
176 class BadException:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
177 def __init__(self):
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
178 raise RuntimeError, "can't instantiate BadException"
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
179
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
180 def test_capi1():
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
181 import _testcapi
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
182 try:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
183 _testcapi.raise_exception(BadException, 1)
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
184 except TypeError, err:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
185 exc, err, tb = sys.exc_info()
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
186 co = tb.tb_frame.f_code
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
187 assert co.co_name == "test_capi1"
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
188 assert co.co_filename.endswith('test_exceptions.py')
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
189 else:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
190 print "Expected exception"
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
191
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
192 def test_capi2():
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
193 import _testcapi
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
194 try:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
195 _testcapi.raise_exception(BadException, 0)
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
196 except RuntimeError, err:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
197 exc, err, tb = sys.exc_info()
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
198 co = tb.tb_frame.f_code
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
199 assert co.co_name == "__init__"
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
200 assert co.co_filename.endswith('test_exceptions.py')
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
201 co2 = tb.tb_frame.f_back.f_code
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
202 assert co2.co_name == "test_capi2"
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
203 else:
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
204 print "Expected exception"
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
205
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
206 if not sys.platform.startswith('java'):
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
207 test_capi1()
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
208 test_capi2()
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
209
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 unlink(TESTFN)