Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / lib / python2.4 / test / test_exceptions.py
CommitLineData
920dae64
AT
1# Python test set -- part 5, built-in exceptions
2
3from test.test_support import TestFailed, TESTFN, unlink
4from types import ClassType
5import warnings
6import sys, traceback, os
7
8print '5. Built-in exceptions'
9# XXX This is not really enough, each *operation* should be tested!
10
11# 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
19def 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
30def r(thing):
31 test_raise_catch(thing)
32 if isinstance(thing, ClassType):
33 print thing.__name__
34 else:
35 print thing
36
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:
49 try:
50 sys.stdin = fp
51 x = raw_input()
52 except EOFError:
53 pass
54finally:
55 sys.stdin = savestdin
56 fp.close()
57
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)
87# XXX
88# 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.
94warnings.filterwarnings("error", "", OverflowWarning, __name__)
95x = 1
96for dummy in range(128):
97 x += x # this simply shouldn't blow up
98
99r(RuntimeError)
100print '(not used any more?)'
101
102r(SyntaxError)
103try: exec '/\n'
104except SyntaxError: pass
105
106# 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:
124 pass
125 finally:
126 continue
127'''
128if 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")
133s = '''\
134try:
135 continue
136except:
137 pass
138'''
139ckmsg(s, "'continue' not properly in loop")
140ckmsg("continue\n", "'continue' not properly in loop")
141
142r(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
150r(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
170r(Exception)
171try: x = 1/0
172except Exception, e: pass
173
174# 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():
182 import _testcapi
183 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"
189 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
190 else:
191 print "Expected exception"
192
193def test_capi2():
194 import _testcapi
195 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__"
201 assert co.co_filename.endswith('test_exceptions'+os.extsep+'py')
202 co2 = tb.tb_frame.f_back.f_code
203 assert co2.co_name == "test_capi2"
204 else:
205 print "Expected exception"
206
207if not sys.platform.startswith('java'):
208 test_capi1()
209 test_capi2()
210
211unlink(TESTFN)