blob: d194c8b030e35cf3dd498d8d53cb1778133f9849 [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
7import _testcapi
Guido van Rossum83b120d2001-08-23 03:23:03 +00008
9warnings.filterwarnings("error", "", OverflowWarning, __name__)
Guido van Rossum3bead091992-01-27 17:00:37 +000010
11print '5. Built-in exceptions'
12# XXX This is not really enough, each *operation* should be tested!
13
Barry Warsawb9c1d3d2001-08-13 23:07:00 +000014# Reloading the built-in exceptions module failed prior to Py2.2, while it
15# should act the same as reloading built-in sys.
16try:
17 import exceptions
18 reload(exceptions)
19except ImportError, e:
20 raise TestFailed, e
21
Jeremy Hylton56c807d2000-06-20 18:52:57 +000022def test_raise_catch(exc):
23 try:
24 raise exc, "spam"
25 except exc, err:
26 buf = str(err)
27 try:
28 raise exc("spam")
29 except exc, err:
30 buf = str(err)
31 print buf
32
Barry Warsaw6ed41a01997-08-29 21:58:25 +000033def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000034 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000035 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000036 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000037 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000038 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000039
40r(AttributeError)
41import sys
42try: x = sys.undefined_attribute
43except AttributeError: pass
44
45r(EOFError)
46import sys
47fp = open(TESTFN, 'w')
48fp.close()
49fp = open(TESTFN, 'r')
50savestdin = sys.stdin
51try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000052 try:
53 sys.stdin = fp
54 x = raw_input()
55 except EOFError:
56 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000057finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +000058 sys.stdin = savestdin
59 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000060
61r(IOError)
62try: open('this file does not exist', 'r')
63except IOError: pass
64
65r(ImportError)
66try: import undefined_module
67except ImportError: pass
68
69r(IndexError)
70x = []
71try: a = x[10]
72except IndexError: pass
73
74r(KeyError)
75x = {}
76try: a = x['key']
77except KeyError: pass
78
79r(KeyboardInterrupt)
80print '(not testable in a script)'
81
82r(MemoryError)
83print '(not safe to test)'
84
85r(NameError)
86try: x = undefined_variable
87except NameError: pass
88
89r(OverflowError)
90x = 1
91try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000092 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000093except OverflowError: pass
94
95r(RuntimeError)
96print '(not used any more?)'
97
98r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +000099try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +0000100except SyntaxError: pass
101
Fred Drake72e48bd2000-09-08 16:32:34 +0000102# make sure the right exception message is raised for each of these
103# code fragments:
104
105def ckmsg(src, msg):
106 try:
107 compile(src, '<fragment>', 'exec')
108 except SyntaxError, e:
109 print e.msg
110 if e.msg == msg:
111 print "ok"
112 else:
113 print "expected:", msg
114 else:
115 print "failed to get expected SyntaxError"
116
117s = '''\
118while 1:
119 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000120 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000121 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000122 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000123'''
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000124ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000125s = '''\
126try:
127 continue
128except:
129 pass
130'''
131ckmsg(s, "'continue' not properly in loop")
132ckmsg("continue\n", "'continue' not properly in loop")
133
Fred Drake85f36392000-07-11 17:53:00 +0000134r(IndentationError)
135
136r(TabError)
137# can only be tested under -tt, and is the only test for -tt
138#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
139#except TabError: pass
140#else: raise TestFailed
141
Guido van Rossum3bead091992-01-27 17:00:37 +0000142r(SystemError)
143print '(hard to reproduce)'
144
145r(SystemExit)
146import sys
147try: sys.exit(0)
148except SystemExit: pass
149
150r(TypeError)
151try: [] + ()
152except TypeError: pass
153
154r(ValueError)
155try: x = chr(10000)
156except ValueError: pass
157
158r(ZeroDivisionError)
159try: x = 1/0
160except ZeroDivisionError: pass
161
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000162r(Exception)
163try: x = 1/0
164except Exception, e: pass
165
Jeremy Hyltonede049b2001-09-26 20:01:13 +0000166# test that setting an exception at the C level works even if the
167# exception object can't be constructed.
168
169class BadException:
170 def __init__(self):
171 raise RuntimeError, "can't instantiate BadException"
172
173def test_capi1():
174 try:
175 _testcapi.raise_exception(BadException, 1)
176 except TypeError, err:
177 exc, err, tb = sys.exc_info()
178 co = tb.tb_frame.f_code
179 assert co.co_name == "test_capi1"
180 assert co.co_filename.endswith('test_exceptions.py')
181 else:
182 print "Expected exception"
183test_capi1()
184
185def test_capi2():
186 try:
187 _testcapi.raise_exception(BadException, 0)
188 except RuntimeError, err:
189 exc, err, tb = sys.exc_info()
190 co = tb.tb_frame.f_code
191 assert co.co_name == "__init__"
192 assert co.co_filename.endswith('test_exceptions.py')
193 co2 = tb.tb_frame.f_back.f_code
194 assert co2.co_name == "test_capi2"
195 else:
196 print "Expected exception"
197test_capi2()
198
Guido van Rossum3bead091992-01-27 17:00:37 +0000199unlink(TESTFN)