1 # Python test set -- part 5, built-in exceptions
3 from test
.test_support
import TestFailed
, TESTFN
, unlink
4 from types
import ClassType
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.
16 except ImportError, e
:
19 def test_raise_catch(exc
):
31 test_raise_catch(thing
)
32 if isinstance(thing
, ClassType
):
39 try: x
= sys
.undefined_attribute
40 except AttributeError: pass
44 fp
= open(TESTFN
, 'w')
46 fp
= open(TESTFN
, 'r')
59 try: open('this file does not exist', 'r')
63 try: import undefined_module
64 except ImportError: pass
69 except IndexError: pass
77 print '(not testable in a script)'
80 print '(not safe to test)'
83 try: x
= undefined_variable
84 except NameError: pass
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,
94 warnings
.filterwarnings("error", "", OverflowWarning, __name__
)
96 for dummy
in range(128):
97 x
+= x
# this simply shouldn't blow up
100 print '(not used any more?)'
104 except SyntaxError: pass
106 # make sure the right exception message is raised for each of these
111 compile(src
, '<fragment>', 'exec')
112 except SyntaxError, e
:
117 print "expected:", msg
119 print "failed to get expected SyntaxError"
128 if sys
.platform
.startswith('java'):
129 print "'continue' not supported inside 'finally' clause"
132 ckmsg(s
, "'continue' not supported inside 'finally' clause")
139 ckmsg(s
, "'continue' not properly in loop")
140 ckmsg("continue\n", "'continue' not properly in loop")
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
151 print '(hard to reproduce)'
156 except SystemExit: pass
160 except TypeError: pass
164 except ValueError: pass
168 except ZeroDivisionError: pass
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.
179 raise RuntimeError, "can't instantiate BadException"
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')
191 print "Expected exception"
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"
205 print "Expected exception"
207 if not sys
.platform
.startswith('java'):