blob: 83e680f81a81ee70bad21aa1d47fbcb9c86909c3 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 5, built-in exceptions
2
Barry Warsaw408b6d32002-07-30 23:27:12 +00003from test.test_support import TestFailed, TESTFN, unlink
Barry Warsaw6ed41a01997-08-29 21:58:25 +00004from types import ClassType
Guido van Rossum83b120d2001-08-23 03:23:03 +00005import warnings
Martin v. Löwisa94568a2003-05-10 07:36:56 +00006import sys, traceback, os
Guido van Rossum83b120d2001-08-23 03:23:03 +00007
Guido van Rossum3bead091992-01-27 17:00:37 +00008print '5. Built-in exceptions'
9# XXX This is not really enough, each *operation* should be tested!
10
Barry Warsawb9c1d3d2001-08-13 23:07:00 +000011# Reloading the built-in exceptions module failed prior to Py2.2, while it
12# should act the same as reloading built-in sys.
13try:
14 import exceptions
15 reload(exceptions)
16except ImportError, e:
17 raise TestFailed, e
18
Jeremy Hylton56c807d2000-06-20 18:52:57 +000019def test_raise_catch(exc):
20 try:
21 raise exc, "spam"
22 except exc, err:
23 buf = str(err)
24 try:
25 raise exc("spam")
26 except exc, err:
27 buf = str(err)
28 print buf
29
Barry Warsaw6ed41a01997-08-29 21:58:25 +000030def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000031 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000032 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000033 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000034 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000035 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000036
37r(AttributeError)
38import sys
39try: x = sys.undefined_attribute
40except AttributeError: pass
41
42r(EOFError)
43import sys
44fp = open(TESTFN, 'w')
45fp.close()
46fp = open(TESTFN, 'r')
47savestdin = sys.stdin
48try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000049 try:
50 sys.stdin = fp
51 x = raw_input()
52 except EOFError:
53 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000054finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +000055 sys.stdin = savestdin
56 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000057
58r(IOError)
59try: open('this file does not exist', 'r')
60except IOError: pass
61
62r(ImportError)
63try: import undefined_module
64except ImportError: pass
65
66r(IndexError)
67x = []
68try: a = x[10]
69except IndexError: pass
70
71r(KeyError)
72x = {}
73try: a = x['key']
74except KeyError: pass
75
76r(KeyboardInterrupt)
77print '(not testable in a script)'
78
79r(MemoryError)
80print '(not safe to test)'
81
82r(NameError)
83try: x = undefined_variable
84except NameError: pass
85
86r(OverflowError)
Tim Petersd3925062002-04-16 01:27:44 +000087# XXX
88# Obscure: this test relies on int+int raising OverflowError if the
89# ints are big enough. But ints no longer do that by default. This
90# test will have to go away someday. For now, we can convert the
91# transitional OverflowWarning into an error.
92warnings.filterwarnings("error", "", OverflowWarning, __name__)
Guido van Rossum3bead091992-01-27 17:00:37 +000093x = 1
94try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000095 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000096except OverflowError: pass
97
98r(RuntimeError)
99print '(not used any more?)'
100
101r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +0000102try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +0000103except SyntaxError: pass
104
Fred Drake72e48bd2000-09-08 16:32:34 +0000105# make sure the right exception message is raised for each of these
106# code fragments:
107
108def ckmsg(src, msg):
109 try:
110 compile(src, '<fragment>', 'exec')
111 except SyntaxError, e:
112 print e.msg
113 if e.msg == msg:
114 print "ok"
115 else:
116 print "expected:", msg
117 else:
118 print "failed to get expected SyntaxError"
119
120s = '''\
121while 1:
122 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000123 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000124 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000125 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000126'''
Finn Bockaa3dc452001-12-08 10:15:48 +0000127if sys.platform.startswith('java'):
128 print "'continue' not supported inside 'finally' clause"
129 print "ok"
130else:
131 ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000132s = '''\
133try:
134 continue
135except:
136 pass
137'''
138ckmsg(s, "'continue' not properly in loop")
139ckmsg("continue\n", "'continue' not properly in loop")
140
Fred Drake85f36392000-07-11 17:53:00 +0000141r(IndentationError)
142
143r(TabError)
144# can only be tested under -tt, and is the only test for -tt
145#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
146#except TabError: pass
147#else: raise TestFailed
148
Guido van Rossum3bead091992-01-27 17:00:37 +0000149r(SystemError)
150print '(hard to reproduce)'
151
152r(SystemExit)
153import sys
154try: sys.exit(0)
155except SystemExit: pass
156
157r(TypeError)
158try: [] + ()
159except TypeError: pass
160
161r(ValueError)
162try: x = chr(10000)
163except ValueError: pass
164
165r(ZeroDivisionError)
166try: x = 1/0
167except ZeroDivisionError: pass
168
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000169r(Exception)
170try: x = 1/0
171except Exception, e: pass
172
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000173# test that setting an exception at the C level works even if the
174# exception object can't be constructed.
175
176class BadException:
177 def __init__(self):
178 raise RuntimeError, "can't instantiate BadException"
179
180def test_capi1():
Finn Bockaa3dc452001-12-08 10:15:48 +0000181 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000182 try:
183 _testcapi.raise_exception(BadException, 1)
184 except TypeError, err:
185 exc, err, tb = sys.exc_info()
186 co = tb.tb_frame.f_code
187 assert co.co_name == "test_capi1"
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000188 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000189 else:
190 print "Expected exception"
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000191
192def test_capi2():
Finn Bockaa3dc452001-12-08 10:15:48 +0000193 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000194 try:
195 _testcapi.raise_exception(BadException, 0)
196 except RuntimeError, err:
197 exc, err, tb = sys.exc_info()
198 co = tb.tb_frame.f_code
199 assert co.co_name == "__init__"
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000200 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000201 co2 = tb.tb_frame.f_back.f_code
202 assert co2.co_name == "test_capi2"
203 else:
204 print "Expected exception"
Finn Bockaa3dc452001-12-08 10:15:48 +0000205
206if not sys.platform.startswith('java'):
207 test_capi1()
208 test_capi2()
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000209
Guido van Rossum3bead091992-01-27 17:00:37 +0000210unlink(TESTFN)