1 # Python test set -- part 5, built-in exceptions
3 from test
.test_support
import TestFailed
, TESTFN
, unlink
4 from types
import ClassType
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: 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__
)
96 except OverflowError: pass
99 print '(not used any more?)'
103 except SyntaxError: pass
105 # make sure the right exception message is raised for each of these
110 compile(src
, '<fragment>', 'exec')
111 except SyntaxError, e
:
116 print "expected:", msg
118 print "failed to get expected SyntaxError"
127 if sys
.platform
.startswith('java'):
128 print "'continue' not supported inside 'finally' clause"
131 ckmsg(s
, "'continue' not supported inside 'finally' clause")
138 ckmsg(s
, "'continue' not properly in loop")
139 ckmsg("continue\n", "'continue' not properly in loop")
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
150 print '(hard to reproduce)'
155 except SystemExit: pass
159 except TypeError: pass
163 except ValueError: pass
167 except ZeroDivisionError: pass
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.
178 raise RuntimeError, "can't instantiate BadException"
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')
190 print "Expected exception"
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"
204 print "Expected exception"
206 if not sys
.platform
.startswith('java'):