blob: e03abfa10e9ce35275e70f73ec1a387056154d3c [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 5, built-in exceptions
2
3from test_support import *
Barry Warsaw6ed41a01997-08-29 21:58:25 +00004from types import ClassType
Guido van Rossum83b120d2001-08-23 03:23:03 +00005import warnings
Jeremy Hyltonede049b2001-09-26 20:01:13 +00006import sys, traceback
Guido van Rossum83b120d2001-08-23 03:23:03 +00007
8warnings.filterwarnings("error", "", OverflowWarning, __name__)
Guido van Rossum3bead091992-01-27 17:00:37 +00009
10print '5. Built-in exceptions'
11# XXX This is not really enough, each *operation* should be tested!
12
Barry Warsawb9c1d3d2001-08-13 23:07:00 +000013# Reloading the built-in exceptions module failed prior to Py2.2, while it
14# should act the same as reloading built-in sys.
15try:
16 import exceptions
17 reload(exceptions)
18except ImportError, e:
19 raise TestFailed, e
20
Jeremy Hylton56c807d2000-06-20 18:52:57 +000021def test_raise_catch(exc):
22 try:
23 raise exc, "spam"
24 except exc, err:
25 buf = str(err)
26 try:
27 raise exc("spam")
28 except exc, err:
29 buf = str(err)
30 print buf
31
Barry Warsaw6ed41a01997-08-29 21:58:25 +000032def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000033 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000034 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000035 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000036 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000037 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000038
39r(AttributeError)
40import sys
41try: x = sys.undefined_attribute
42except AttributeError: pass
43
44r(EOFError)
45import sys
46fp = open(TESTFN, 'w')
47fp.close()
48fp = open(TESTFN, 'r')
49savestdin = sys.stdin
50try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000051 try:
52 sys.stdin = fp
53 x = raw_input()
54 except EOFError:
55 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000056finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +000057 sys.stdin = savestdin
58 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000059
60r(IOError)
61try: open('this file does not exist', 'r')
62except IOError: pass
63
64r(ImportError)
65try: import undefined_module
66except ImportError: pass
67
68r(IndexError)
69x = []
70try: a = x[10]
71except IndexError: pass
72
73r(KeyError)
74x = {}
75try: a = x['key']
76except KeyError: pass
77
78r(KeyboardInterrupt)
79print '(not testable in a script)'
80
81r(MemoryError)
82print '(not safe to test)'
83
84r(NameError)
85try: x = undefined_variable
86except NameError: pass
87
88r(OverflowError)
89x = 1
90try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000091 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000092except OverflowError: pass
93
94r(RuntimeError)
95print '(not used any more?)'
96
97r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +000098try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +000099except SyntaxError: pass
100
Fred Drake72e48bd2000-09-08 16:32:34 +0000101# make sure the right exception message is raised for each of these
102# code fragments:
103
104def ckmsg(src, msg):
105 try:
106 compile(src, '<fragment>', 'exec')
107 except SyntaxError, e:
108 print e.msg
109 if e.msg == msg:
110 print "ok"
111 else:
112 print "expected:", msg
113 else:
114 print "failed to get expected SyntaxError"
115
116s = '''\
117while 1:
118 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000119 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000120 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000121 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000122'''
Finn Bockaa3dc452001-12-08 10:15:48 +0000123if sys.platform.startswith('java'):
124 print "'continue' not supported inside 'finally' clause"
125 print "ok"
126else:
127 ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000128s = '''\
129try:
130 continue
131except:
132 pass
133'''
134ckmsg(s, "'continue' not properly in loop")
135ckmsg("continue\n", "'continue' not properly in loop")
136
Fred Drake85f36392000-07-11 17:53:00 +0000137r(IndentationError)
138
139r(TabError)
140# can only be tested under -tt, and is the only test for -tt
141#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
142#except TabError: pass
143#else: raise TestFailed
144
Guido van Rossum3bead091992-01-27 17:00:37 +0000145r(SystemError)
146print '(hard to reproduce)'
147
148r(SystemExit)
149import sys
150try: sys.exit(0)
151except SystemExit: pass
152
153r(TypeError)
154try: [] + ()
155except TypeError: pass
156
157r(ValueError)
158try: x = chr(10000)
159except ValueError: pass
160
161r(ZeroDivisionError)
162try: x = 1/0
163except ZeroDivisionError: pass
164
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000165r(Exception)
166try: x = 1/0
167except Exception, e: pass
168
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000169# test that setting an exception at the C level works even if the
170# exception object can't be constructed.
171
172class BadException:
173 def __init__(self):
174 raise RuntimeError, "can't instantiate BadException"
175
176def test_capi1():
Finn Bockaa3dc452001-12-08 10:15:48 +0000177 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000178 try:
179 _testcapi.raise_exception(BadException, 1)
180 except TypeError, err:
181 exc, err, tb = sys.exc_info()
182 co = tb.tb_frame.f_code
183 assert co.co_name == "test_capi1"
184 assert co.co_filename.endswith('test_exceptions.py')
185 else:
186 print "Expected exception"
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000187
188def test_capi2():
Finn Bockaa3dc452001-12-08 10:15:48 +0000189 import _testcapi
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000190 try:
191 _testcapi.raise_exception(BadException, 0)
192 except RuntimeError, err:
193 exc, err, tb = sys.exc_info()
194 co = tb.tb_frame.f_code
195 assert co.co_name == "__init__"
196 assert co.co_filename.endswith('test_exceptions.py')
197 co2 = tb.tb_frame.f_back.f_code
198 assert co2.co_name == "test_capi2"
199 else:
200 print "Expected exception"
Finn Bockaa3dc452001-12-08 10:15:48 +0000201
202if not sys.platform.startswith('java'):
203 test_capi1()
204 test_capi2()
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000205
Guido van Rossum3bead091992-01-27 17:00:37 +0000206unlink(TESTFN)