Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Lib / test / test_exceptions.py
blobc157122b5b0c668f5d685ccbf836f2de9fd81ea9
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, os
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: 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.
94 warnings.filterwarnings("error", "", OverflowWarning, __name__)
95 x = 1
96 for dummy in range(128):
97 x += x # this simply shouldn't blow up
99 r(RuntimeError)
100 print '(not used any more?)'
102 r(SyntaxError)
103 try: exec '/\n'
104 except SyntaxError: pass
106 # make sure the right exception message is raised for each of these
107 # code fragments:
109 def 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"
121 s = '''\
122 while 1:
123 try:
124 pass
125 finally:
126 continue
128 if sys.platform.startswith('java'):
129 print "'continue' not supported inside 'finally' clause"
130 print "ok"
131 else:
132 ckmsg(s, "'continue' not supported inside 'finally' clause")
133 s = '''\
134 try:
135 continue
136 except:
137 pass
139 ckmsg(s, "'continue' not properly in loop")
140 ckmsg("continue\n", "'continue' not properly in loop")
142 r(IndentationError)
144 r(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
150 r(SystemError)
151 print '(hard to reproduce)'
153 r(SystemExit)
154 import sys
155 try: sys.exit(0)
156 except SystemExit: pass
158 r(TypeError)
159 try: [] + ()
160 except TypeError: pass
162 r(ValueError)
163 try: x = chr(10000)
164 except ValueError: pass
166 r(ZeroDivisionError)
167 try: x = 1/0
168 except ZeroDivisionError: pass
170 r(Exception)
171 try: x = 1/0
172 except Exception, e: pass
174 # test that setting an exception at the C level works even if the
175 # exception object can't be constructed.
177 class BadException:
178 def __init__(self):
179 raise RuntimeError, "can't instantiate BadException"
181 def 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"
193 def 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"
207 if not sys.platform.startswith('java'):
208 test_capi1()
209 test_capi2()
211 unlink(TESTFN)