1
# Copyright 2012, Kay Hayen, mailto:kayhayen@gmx.de
3
# Python tests originally created or extracted from other peoples work. The
4
# parts were too small to be protected.
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
20
def raiseExceptionClass():
26
print e, repr(e), type(e)
28
def raiseExceptionInstance():
29
raise ValueError( "hallo" )
34
raiseExceptionInstance()
36
print f, repr(f), type(f)
40
def raiseExceptionAndReraise():
50
raiseExceptionAndReraise()
52
print "Catched reraised"
54
def raiseNonGlobalError():
55
return undefined_value
62
print "NameError caught"
64
def raiseIllegalError():
75
print "New style class exception correctly rejected:", E
78
assert False, "Error, new style class exception was not rejected"
81
def raiseCustomError():
82
raise ClassicClassException()
84
class ClassicClassException:
91
except ClassicClassException:
92
print "Caught classic class exception"
95
assert False, "Error, old style class exception was not caught"
103
assert sys.exc_info()[0] is not None
104
assert sys.exc_info()[1] is not None
105
assert sys.exc_info()[2] is not None
107
print "Check traceback:"
109
traceback.print_tb( sys.exc_info()[2], file = sys.stdout )
111
print "End of traceback"
113
print "Type is", sys.exc_info()[0]
114
print "Value is", sys.exc_info()[1]
120
def checkExceptionConversion():
122
raise Exception( "some string")
123
except Exception, err:
124
print "Catched raised object", err, type( err )
127
raise Exception, "some string"
128
except Exception, err:
129
print "Catched raised type, value pair", err, type( err )
133
checkExceptionConversion()
135
def checkExcInfoScope():
139
assert sys.exc_info()[0] is not None
140
assert sys.exc_info()[1] is not None
141
assert sys.exc_info()[2] is not None
143
assert sys.exc_info()[0] is not None
144
assert sys.exc_info()[1] is not None
145
assert sys.exc_info()[2] is not None
147
print "Exc_info remains visible after exception handler"
150
assert sys.exc_info()[0] is not None
151
assert sys.exc_info()[1] is not None
152
assert sys.exc_info()[2] is not None
162
# Check that the sys.exc_info is cleared again, after being set inside the
163
# function checkExcInfoScope, it should now be clear again.
164
assert sys.exc_info()[0] is None
165
assert sys.exc_info()[1] is None
166
assert sys.exc_info()[2] is None
168
def checkDerivedCatch():
182
print "Not caught A class"
187
print "TypeError with pair form for class not taking args:", e
194
def checkNonCatch1():
195
print "Testing if the else branch is executed in the optimizable case"
199
print "Should not catch"
201
print "Executed else branch correctly"
204
def checkNonCatch2():
207
print "Testing if the else branch is executed in the non-optimizable case"
209
print "Should not catch"
211
print "Executed else branch correctly"
219
def checkRaisingRaise():
220
print "Checking raise that has exception arg that raises an error itself."
226
print "Had exception", e
232
print "Had exception", e
236
raise TypeError, 7, 1/0
239
print "Had exception", e
250
print "Without exception, raise gives:", e
253
def nestedExceptions( a, b ):
256
except ZeroDivisionError:
260
nestedExceptions( 1, 0 )
262
print "Nested exception gives", e