# Python test set -- part 5, built-in exceptions
from test
.test_support
import TestFailed
, TESTFN
, unlink
from types
import ClassType
import sys
, traceback
, os
print '5. Built-in exceptions'
# XXX This is not really enough, each *operation* should be tested!
# Reloading the built-in exceptions module failed prior to Py2.2, while it
# should act the same as reloading built-in sys.
def test_raise_catch(exc
):
if isinstance(thing
, ClassType
):
try: x
= sys
.undefined_attribute
except AttributeError: pass
try: open('this file does not exist', 'r')
try: import undefined_module
print '(not testable in a script)'
print '(not safe to test)'
try: x
= undefined_variable
# Obscure: in 2.2 and 2.3, this test relied on changing OverflowWarning
# into an error, in order to trigger OverflowError. In 2.4, OverflowWarning
# should no longer be generated, so the focus of the test shifts to showing
# that OverflowError *isn't* generated. OverflowWarning should be gone
# in Python 2.5, and then the filterwarnings() call, and this comment,
warnings
.filterwarnings("error", "", OverflowWarning, __name__
)
x
+= x
# this simply shouldn't blow up
print '(not used any more?)'
# make sure the right exception message is raised for each of these
compile(src
, '<fragment>', 'exec')
print "failed to get expected SyntaxError"
if sys
.platform
.startswith('java'):
print "'continue' not supported inside 'finally' clause"
ckmsg(s
, "'continue' not supported inside 'finally' clause")
ckmsg(s
, "'continue' not properly in loop")
ckmsg("continue\n", "'continue' not properly in loop")
# can only be tested under -tt, and is the only test for -tt
#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
print '(hard to reproduce)'
except ZeroDivisionError: pass
except Exception, e
: pass
# test that setting an exception at the C level works even if the
# exception object can't be constructed.
raise RuntimeError, "can't instantiate BadException"
_testcapi
.raise_exception(BadException
, 1)
exc
, err
, tb
= sys
.exc_info()
assert co
.co_name
== "test_capi1"
assert co
.co_filename
.endswith('test_exceptions'+os
.extsep
+'py')
print "Expected exception"
_testcapi
.raise_exception(BadException
, 0)
except RuntimeError, err
:
exc
, err
, tb
= sys
.exc_info()
assert co
.co_name
== "__init__"
assert co
.co_filename
.endswith('test_exceptions'+os
.extsep
+'py')
co2
= tb
.tb_frame
.f_back
.f_code
assert co2
.co_name
== "test_capi2"
print "Expected exception"
if not sys
.platform
.startswith('java'):