blob: c157122b5b0c668f5d685ccbf836f2de9fd81ea9 [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
Tim Petersc8854432004-08-25 02:14:08 +000088# Obscure: in 2.2 and 2.3, this test relied on changing OverflowWarning
89# into an error, in order to trigger OverflowError. In 2.4, OverflowWarning
90# should no longer be generated, so the focus of the test shifts to showing
91# that OverflowError *isn't* generated. OverflowWarning should be gone
92# in Python 2.5, and then the filterwarnings() call, and this comment,
93# should go away.
Tim Petersd3925062002-04-16 01:27:44 +000094warnings.filterwarnings("error", "", OverflowWarning, __name__)
Guido van Rossum3bead091992-01-27 17:00:37 +000095x = 1
Tim Petersc8854432004-08-25 02:14:08 +000096for dummy in range(128):
97 x += x # this simply shouldn't blow up
Guido van Rossum3bead091992-01-27 17:00:37 +000098
99r(RuntimeError)
100print '(not used any more?)'
101
102r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +0000103try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +0000104except SyntaxError: pass
105
Fred Drake72e48bd2000-09-08 16:32:34 +0000106# make sure the right exception message is raised for each of these
107# code fragments:
108
109def ckmsg(src, msg):
110 try:
111 compile(src, '<fragment>', 'exec')
112 except SyntaxError, e:
113 print e.msg
114 if e.msg == msg:
115 print "ok"
116 else:
117 print "expected:", msg
118 else:
119 print "failed to get expected SyntaxError"
120
121s = '''\
122while 1:
123 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000124 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000125 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000126 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000127'''
Finn Bockaa3dc452001-12-08 10:15:48 +0000128if sys.platform.startswith('java'):
129 print "'continue' not supported inside 'finally' clause"
130 print "ok"
131else:
132 ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000133s = '''\
134try:
135 continue
136except:
137 pass
138'''
139ckmsg(s, "'continue' not properly in loop")
140ckmsg("continue\n", "'continue' not properly in loop")
141
Fred Drake85f36392000-07-11 17:53:00 +0000142r(IndentationError)
143
144r(TabError)
145# can only be tested under -tt, and is the only test for -tt
146#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
147#except TabError: pass
148#else: raise TestFailed
149
Guido van Rossum3bead091992-01-27 17:00:37 +0000150r(SystemError)
151print '(hard to reproduce)'
152
153r(SystemExit)
154import sys
155try: sys.exit(0)
156except SystemExit: pass
157
158r(TypeError)
159try: [] + ()
160except TypeError: pass
161
162r(ValueError)
163try: x = chr(10000)
164except ValueError: pass
165
166r(ZeroDivisionError)
167try: x = 1/0
168except ZeroDivisionError: pass
169
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000170r(Exception)
171try: x = 1/0
172except Exception, e: pass
173
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000174# test that setting an exception at the C level works even if the
175# exception object can't be constructed.
176
177class BadException:
178 def __init__(self):
179 raise RuntimeError, "can't instantiate BadException"
180
181def test_capi1():
Finn Bockaa3dc452001-12-08 10:15:48 +0000182 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000183 try:
184 _testcapi.raise_exception(BadException, 1)
185 except TypeError, err:
186 exc, err, tb = sys.exc_info()
187 co = tb.tb_frame.f_code
188 assert co.co_name == "test_capi1"
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000189 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000190 else:
191 print "Expected exception"
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000192
193def test_capi2():
Finn Bockaa3dc452001-12-08 10:15:48 +0000194 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000195 try:
196 _testcapi.raise_exception(BadException, 0)
197 except RuntimeError, err:
198 exc, err, tb = sys.exc_info()
199 co = tb.tb_frame.f_code
200 assert co.co_name == "__init__"
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000201 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000202 co2 = tb.tb_frame.f_back.f_code
203 assert co2.co_name == "test_capi2"
204 else:
205 print "Expected exception"
Finn Bockaa3dc452001-12-08 10:15:48 +0000206
207if not sys.platform.startswith('java'):
208 test_capi1()
209 test_capi2()
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000210
Guido van Rossum3bead091992-01-27 17:00:37 +0000211unlink(TESTFN)