~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to tests/basics/ExceptionRaising.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2012-01-10 22:21:56 UTC
  • Revision ID: package-import@ubuntu.com-20120110222156-fuyjhnhomqm6609e
Tags: upstream-0.3.18~pre2+ds
ImportĀ upstreamĀ versionĀ 0.3.18~pre2+ds

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#     Copyright 2012, Kay Hayen, mailto:kayhayen@gmx.de
 
2
#
 
3
#     Python tests originally created or extracted from other peoples work. The
 
4
#     parts were too small to be protected.
 
5
#
 
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
 
9
#
 
10
#        http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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.
 
17
#
 
18
import sys
 
19
 
 
20
def raiseExceptionClass():
 
21
    raise ValueError
 
22
 
 
23
try:
 
24
    raiseExceptionClass()
 
25
except Exception, e:
 
26
    print e, repr(e), type(e)
 
27
 
 
28
def raiseExceptionInstance():
 
29
    raise ValueError( "hallo" )
 
30
 
 
31
print "*" * 20
 
32
 
 
33
try:
 
34
    raiseExceptionInstance()
 
35
except Exception, f:
 
36
    print f, repr(f), type(f)
 
37
 
 
38
print sys.exc_info()
 
39
 
 
40
def raiseExceptionAndReraise():
 
41
    try:
 
42
        x = 0
 
43
        y = x / x
 
44
    except:
 
45
        raise
 
46
 
 
47
print "*" * 20
 
48
 
 
49
try:
 
50
    raiseExceptionAndReraise()
 
51
except:
 
52
    print "Catched reraised"
 
53
 
 
54
def raiseNonGlobalError():
 
55
    return undefined_value
 
56
 
 
57
print "*" * 20
 
58
 
 
59
try:
 
60
   raiseNonGlobalError()
 
61
except:
 
62
   print "NameError caught"
 
63
 
 
64
def raiseIllegalError():
 
65
    class X(object):
 
66
        pass
 
67
 
 
68
    raise X()
 
69
 
 
70
print "*" * 20
 
71
 
 
72
try:
 
73
    raiseIllegalError()
 
74
except TypeError, E:
 
75
    print "New style class exception correctly rejected:", E
 
76
except:
 
77
    print sys.exc_info()
 
78
    assert False, "Error, new style class exception was not rejected"
 
79
 
 
80
 
 
81
def raiseCustomError():
 
82
    raise ClassicClassException()
 
83
 
 
84
class ClassicClassException:
 
85
    pass
 
86
 
 
87
print "*" * 20
 
88
 
 
89
try:
 
90
    raiseCustomError()
 
91
except ClassicClassException:
 
92
    print "Caught classic class exception"
 
93
except:
 
94
    print sys.exc_info()
 
95
    assert False, "Error, old style class exception was not caught"
 
96
 
 
97
def checkTraceback():
 
98
    import sys, traceback
 
99
 
 
100
    try:
 
101
        raise "me"
 
102
    except:
 
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
 
106
 
 
107
        print "Check traceback:"
 
108
 
 
109
        traceback.print_tb( sys.exc_info()[2], file = sys.stdout )
 
110
 
 
111
        print "End of traceback"
 
112
 
 
113
        print "Type is", sys.exc_info()[0]
 
114
        print "Value is", sys.exc_info()[1]
 
115
 
 
116
print "*" * 20
 
117
 
 
118
checkTraceback()
 
119
 
 
120
def checkExceptionConversion():
 
121
    try:
 
122
        raise Exception( "some string")
 
123
    except Exception, err:
 
124
        print "Catched raised object", err, type( err )
 
125
 
 
126
    try:
 
127
        raise Exception, "some string"
 
128
    except Exception, err:
 
129
        print "Catched raised type, value pair", err, type( err )
 
130
 
 
131
 
 
132
print "*" * 20
 
133
checkExceptionConversion()
 
134
 
 
135
def checkExcInfoScope():
 
136
    try:
 
137
        raise "me"
 
138
    except:
 
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
 
142
 
 
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
 
146
 
 
147
    print "Exc_info remains visible after exception handler"
 
148
 
 
149
    def subFunction():
 
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
 
153
 
 
154
    subFunction()
 
155
 
 
156
print "*" * 20
 
157
 
 
158
sys.exc_clear()
 
159
 
 
160
checkExcInfoScope()
 
161
 
 
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
 
167
 
 
168
def checkDerivedCatch():
 
169
    class A:
 
170
        pass
 
171
    class B(A):
 
172
        pass
 
173
 
 
174
    a = A()
 
175
    b = B()
 
176
 
 
177
    try:
 
178
        raise A, b
 
179
    except B, v:
 
180
        print "Caught B", v
 
181
    else:
 
182
        print "Not caught A class"
 
183
 
 
184
    try:
 
185
        raise B, a
 
186
    except TypeError, e:
 
187
        print "TypeError with pair form for class not taking args:", e
 
188
 
 
189
 
 
190
print "*" * 20
 
191
 
 
192
checkDerivedCatch()
 
193
 
 
194
def checkNonCatch1():
 
195
    print "Testing if the else branch is executed in the optimizable case"
 
196
    try:
 
197
        0
 
198
    except TypeError:
 
199
        print "Should not catch"
 
200
    else:
 
201
        print "Executed else branch correctly"
 
202
 
 
203
 
 
204
def checkNonCatch2():
 
205
 
 
206
    try:
 
207
        print "Testing if the else branch is executed in the non-optimizable case"
 
208
    except TypeError:
 
209
        print "Should not catch"
 
210
    else:
 
211
        print "Executed else branch correctly"
 
212
 
 
213
 
 
214
print "*" * 20
 
215
checkNonCatch1()
 
216
checkNonCatch2()
 
217
 
 
218
print "*" * 20
 
219
def checkRaisingRaise():
 
220
    print "Checking raise that has exception arg that raises an error itself."
 
221
 
 
222
    try:
 
223
        raise 1/0
 
224
 
 
225
    except Exception, e:
 
226
        print "Had exception", e
 
227
 
 
228
    try:
 
229
        raise TypeError, 1/0
 
230
 
 
231
    except Exception, e:
 
232
        print "Had exception", e
 
233
 
 
234
 
 
235
    try:
 
236
        raise TypeError, 7, 1/0
 
237
 
 
238
    except Exception, e:
 
239
        print "Had exception", e
 
240
 
 
241
 
 
242
checkRaisingRaise()
 
243
 
 
244
def checkMisRaise():
 
245
    raise
 
246
 
 
247
try:
 
248
    checkMisRaise()
 
249
except Exception, e:
 
250
    print "Without exception, raise gives:", e
 
251
 
 
252
 
 
253
def nestedExceptions( a, b ):
 
254
    try:
 
255
        a / b
 
256
    except ZeroDivisionError:
 
257
        a / b
 
258
 
 
259
try:
 
260
    nestedExceptions( 1, 0 )
 
261
except Exception, e:
 
262
    print "Nested exception gives", e