Update version number and release date.
[python/dscho.git] / Lib / test / test_exceptions.py
blob24ae9e307a77878c4c30d21d699a1109868dd8ab
1 # Python test set -- part 5, built-in exceptions
3 from test.test_support import TestFailed, TESTFN, unlink
4 from types import ClassType
5 import warnings
6 import sys, traceback
8 print '5. Built-in exceptions'
9 # XXX This is not really enough, each *operation* should be tested!
11 # Reloading the built-in exceptions module failed prior to Py2.2, while it
12 # should act the same as reloading built-in sys.
13 try:
14 import exceptions
15 reload(exceptions)
16 except ImportError, e:
17 raise TestFailed, e
19 def 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
30 def r(thing):
31 test_raise_catch(thing)
32 if isinstance(thing, ClassType):
33 print thing.__name__
34 else:
35 print thing
37 r(AttributeError)
38 import sys
39 try: x = sys.undefined_attribute
40 except AttributeError: pass
42 r(EOFError)
43 import sys
44 fp = open(TESTFN, 'w')
45 fp.close()
46 fp = open(TESTFN, 'r')
47 savestdin = sys.stdin
48 try:
49 try:
50 sys.stdin = fp
51 x = raw_input()
52 except EOFError:
53 pass
54 finally:
55 sys.stdin = savestdin
56 fp.close()
58 r(IOError)
59 try: open('this file does not exist', 'r')
60 except IOError: pass
62 r(ImportError)
63 try: import undefined_module
64 except ImportError: pass
66 r(IndexError)
67 x = []
68 try: a = x[10]
69 except IndexError: pass
71 r(KeyError)
72 x = {}
73 try: a = x['key']
74 except KeyError: pass
76 r(KeyboardInterrupt)
77 print '(not testable in a script)'
79 r(MemoryError)
80 print '(not safe to test)'
82 r(NameError)
83 try: x = undefined_variable
84 except NameError: pass
86 r(OverflowError)
87 # 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.
92 warnings.filterwarnings("error", "", OverflowWarning, __name__)
93 x = 1
94 try:
95 while 1: x = x+x
96 except OverflowError: pass
98 r(RuntimeError)
99 print '(not used any more?)'
101 r(SyntaxError)
102 try: exec '/\n'
103 except SyntaxError: pass
105 # make sure the right exception message is raised for each of these
106 # code fragments:
108 def 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"
120 s = '''\
121 while 1:
122 try:
123 pass
124 finally:
125 continue
127 if sys.platform.startswith('java'):
128 print "'continue' not supported inside 'finally' clause"
129 print "ok"
130 else:
131 ckmsg(s, "'continue' not supported inside 'finally' clause")
132 s = '''\
133 try:
134 continue
135 except:
136 pass
138 ckmsg(s, "'continue' not properly in loop")
139 ckmsg("continue\n", "'continue' not properly in loop")
141 r(IndentationError)
143 r(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
149 r(SystemError)
150 print '(hard to reproduce)'
152 r(SystemExit)
153 import sys
154 try: sys.exit(0)
155 except SystemExit: pass
157 r(TypeError)
158 try: [] + ()
159 except TypeError: pass
161 r(ValueError)
162 try: x = chr(10000)
163 except ValueError: pass
165 r(ZeroDivisionError)
166 try: x = 1/0
167 except ZeroDivisionError: pass
169 r(Exception)
170 try: x = 1/0
171 except Exception, e: pass
173 # test that setting an exception at the C level works even if the
174 # exception object can't be constructed.
176 class BadException:
177 def __init__(self):
178 raise RuntimeError, "can't instantiate BadException"
180 def test_capi1():
181 import _testcapi
182 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"
188 assert co.co_filename.endswith('test_exceptions.py')
189 else:
190 print "Expected exception"
192 def test_capi2():
193 import _testcapi
194 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__"
200 assert co.co_filename.endswith('test_exceptions.py')
201 co2 = tb.tb_frame.f_back.f_code
202 assert co2.co_name == "test_capi2"
203 else:
204 print "Expected exception"
206 if not sys.platform.startswith('java'):
207 test_capi1()
208 test_capi2()
210 unlink(TESTFN)