FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet-rawsvnmig/pythonnet/src/tests/test_exceptions.py at master · pythonnet/pythonnet-rawsvnmig · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jun 4, 2019. It is now read-only.
pythonnet
/
pythonnet-rawsvnmig
Public archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet-rawsvnmig
/
pythonnet
/
src
/
tests
/
test_exceptions.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
354 lines (253 loc) · 11.9 KB
Breadcrumbs
pythonnet-rawsvnmig
/
pythonnet
/
src
/
tests
/
test_exceptions.py
Copy path
File metadata and controls
354 lines (253 loc) · 11.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# ===========================================================================
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================
import
sys
,
os
,
string
,
unittest
,
types
import
System
# Note: all of these tests are known to fail because Python currently
# doesn't allow new-style classes to be used as exceptions. I'm leaving
# the tests in place in to document 'how it ought to work' in the hopes
# that they'll all pass one day...
class
ExceptionTests
(
unittest
.
TestCase
):
"""Test exception support."""
def
testUnifiedExceptionSemantics
(
self
):
"""Test unified exception semantics."""
from
System
import
Exception
,
Object
import
exceptions
e
=
Exception
(
'Something bad happened'
)
self
.
assertTrue
(
isinstance
(
e
,
exceptions
.
Exception
))
self
.
assertTrue
(
isinstance
(
e
,
Exception
))
def
testStandardExceptionAttributes
(
self
):
"""Test accessing standard exception attributes."""
from
System
import
OverflowException
from
Python
.
Test
import
ExceptionTest
e
=
ExceptionTest
.
GetExplicitException
()
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
self
.
assertTrue
(
e
.
Message
==
'error'
)
e
.
Source
=
'Test Suite'
self
.
assertTrue
(
e
.
Source
==
'Test Suite'
)
v
=
e
.
ToString
()
self
.
assertTrue
(
len
(
v
)
>
0
)
def
testExtendedExceptionAttributes
(
self
):
"""Test accessing extended exception attributes."""
from
Python
.
Test
import
ExceptionTest
,
ExtendedException
from
System
import
Exception
,
OverflowException
import
exceptions
e
=
ExceptionTest
.
GetExtendedException
()
self
.
assertTrue
(
isinstance
(
e
,
ExtendedException
))
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
self
.
assertTrue
(
isinstance
(
e
,
Exception
))
self
.
assertTrue
(
e
.
Message
==
'error'
)
e
.
Source
=
'Test Suite'
self
.
assertTrue
(
e
.
Source
==
'Test Suite'
)
v
=
e
.
ToString
()
self
.
assertTrue
(
len
(
v
)
>
0
)
self
.
assertTrue
(
e
.
ExtraProperty
==
'extra'
)
e
.
ExtraProperty
=
'changed'
self
.
assertTrue
(
e
.
ExtraProperty
==
'changed'
)
self
.
assertTrue
(
e
.
GetExtraInfo
()
==
'changed'
)
def
testRaiseClassException
(
self
):
"""Test class exception propagation."""
from
System
import
NullReferenceException
def
test
():
raise
NullReferenceException
self
.
assertRaises
(
NullReferenceException
,
test
)
try
:
raise
NullReferenceException
except
:
type
,
value
,
tb
=
sys
.
exc_info
()
self
.
assertTrue
(
type
is
NullReferenceException
)
self
.
assertTrue
(
isinstance
(
value
,
NullReferenceException
))
def
testRaiseClassExceptionWithValue
(
self
):
"""Test class exception propagation with associated value."""
from
System
import
NullReferenceException
def
test
():
raise
NullReferenceException
,
'Aiiieee!'
self
.
assertRaises
(
NullReferenceException
,
test
)
try
:
raise
NullReferenceException
(
'Aiiieee!'
)
except
:
type
,
value
,
tb
=
sys
.
exc_info
()
self
.
assertTrue
(
type
is
NullReferenceException
)
self
.
assertTrue
(
isinstance
(
value
,
NullReferenceException
))
self
.
assertTrue
(
value
.
Message
==
'Aiiieee!'
)
def
testRaiseInstanceException
(
self
):
"""Test instance exception propagation."""
from
System
import
NullReferenceException
def
test
():
raise
NullReferenceException
()
self
.
assertRaises
(
NullReferenceException
,
test
)
try
:
raise
NullReferenceException
()
except
:
type
,
value
,
tb
=
sys
.
exc_info
()
self
.
assertTrue
(
type
is
NullReferenceException
)
self
.
assertTrue
(
isinstance
(
value
,
NullReferenceException
))
self
.
assertTrue
(
len
(
value
.
Message
)
>
0
)
def
testRaiseInstanceExceptionWithArgs
(
self
):
"""Test instance exception propagation with args."""
from
System
import
NullReferenceException
def
test
():
raise
NullReferenceException
(
"Aiieeee!"
)
self
.
assertRaises
(
NullReferenceException
,
test
)
try
:
raise
NullReferenceException
(
'Aiiieee!'
)
except
:
type
,
value
,
tb
=
sys
.
exc_info
()
self
.
assertTrue
(
type
is
NullReferenceException
)
self
.
assertTrue
(
isinstance
(
value
,
NullReferenceException
))
self
.
assertTrue
(
value
.
Message
==
'Aiiieee!'
)
def
testManagedExceptionPropagation
(
self
):
"""Test propagation of exceptions raised in managed code."""
from
System
import
Decimal
,
OverflowException
def
test
():
l
=
Decimal
.
ToInt64
(
Decimal
.
MaxValue
)
self
.
assertRaises
(
OverflowException
,
test
)
def
testManagedExceptionConversion
(
self
):
"""Test conversion of managed exceptions."""
from
System
import
Exception
,
OverflowException
from
Python
.
Test
import
ExceptionTest
e
=
ExceptionTest
.
GetBaseException
()
self
.
assertTrue
(
isinstance
(
e
,
Exception
))
e
=
ExceptionTest
.
GetExplicitException
()
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
self
.
assertTrue
(
isinstance
(
e
,
Exception
))
e
=
ExceptionTest
.
GetWidenedException
()
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
self
.
assertTrue
(
isinstance
(
e
,
Exception
))
v
=
ExceptionTest
.
SetBaseException
(
Exception
(
'error'
))
self
.
assertTrue
(
v
)
v
=
ExceptionTest
.
SetExplicitException
(
OverflowException
(
'error'
))
self
.
assertTrue
(
v
)
v
=
ExceptionTest
.
SetWidenedException
(
OverflowException
(
'error'
))
self
.
assertTrue
(
v
)
def
testCatchExceptionFromManagedMethod
(
self
):
"""Test catching an exception from a managed method."""
from
Python
.
Test
import
ExceptionTest
from
System
import
OverflowException
try
:
ExceptionTest
().
ThrowException
()
except
OverflowException
,
e
:
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
return
raise
SystemError
(
'failed to catch exception from managed method'
)
def
testCatchExceptionFromManagedProperty
(
self
):
"""Test catching an exception from a managed property."""
from
Python
.
Test
import
ExceptionTest
from
System
import
OverflowException
try
:
v
=
ExceptionTest
().
ThrowProperty
except
OverflowException
,
e
:
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
return
try
:
ExceptionTest
().
ThrowProperty
=
1
except
OverflowException
,
e
:
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
return
raise
SystemError
(
'failed to catch exception from managed property'
)
def
testCatchExceptionManagedClass
(
self
):
"""Test catching the managed class of an exception."""
from
System
import
OverflowException
try
:
raise
OverflowException
(
'overflow'
)
except
OverflowException
:
return
raise
SystemError
(
'failed to catch managed class exception'
)
def
testCatchExceptionPythonClass
(
self
):
"""Test catching the python class of an exception."""
from
System
import
OverflowException
from
exceptions
import
Exception
try
:
raise
OverflowException
(
'overflow'
)
except
Exception
:
return
raise
SystemError
(
'failed to catch python class exception'
)
def
testCatchExceptionBaseClass
(
self
):
"""Test catching the base of an exception."""
from
System
import
OverflowException
,
ArithmeticException
try
:
raise
OverflowException
(
'overflow'
)
except
ArithmeticException
:
return
raise
SystemError
(
'failed to catch base exception'
)
def
testCatchExceptionNestedBaseClass
(
self
):
"""Test catching the nested base of an exception."""
from
System
import
OverflowException
,
SystemException
try
:
raise
OverflowException
(
'overflow'
)
except
SystemException
:
return
raise
SystemError
(
'failed to catch nested base exception'
)
def
testCatchExceptionWithAssignment
(
self
):
"""Test catching an exception with assignment."""
from
System
import
OverflowException
try
:
raise
OverflowException
(
'overflow'
)
except
OverflowException
,
e
:
self
.
assertTrue
(
isinstance
(
e
,
OverflowException
))
def
testCatchExceptionUnqualified
(
self
):
"""Test catching an unqualified exception."""
from
System
import
OverflowException
try
:
raise
OverflowException
(
'overflow'
)
except
:
return
raise
SystemError
(
'failed to catch unqualified exception'
)
def
testApparentModuleOfException
(
self
):
"""Test the apparent module of an exception."""
from
System
import
Exception
,
OverflowException
self
.
assertTrue
(
Exception
.
__module__
==
'System'
)
self
.
assertTrue
(
OverflowException
.
__module__
==
'System'
)
def
testStrOfException
(
self
):
"""Test the str() representation of an exception."""
from
System
import
NullReferenceException
from
System
import
Convert
,
FormatException
e
=
NullReferenceException
(
''
)
self
.
assertEqual
(
str
(
e
),
''
)
e
=
NullReferenceException
(
'Something bad happened'
)
self
.
assertTrue
(
str
(
e
).
startswith
(
'Something bad happened'
))
try
:
Convert
.
ToDateTime
(
'this will fail'
)
except
FormatException
,
e
:
msg
=
unicode
(
e
).
encode
(
"utf8"
)
# fix for international installation
self
.
assertTrue
(
msg
.
find
(
'System.Convert.ToDateTime'
)
>
-
1
,
msg
)
def
testPythonCompatOfManagedExceptions
(
self
):
"""Test if managed exceptions are compatible with Python's implementation
"""
from
System
import
OverflowException
msg
=
"A simple message"
e
=
OverflowException
(
msg
)
self
.
assertEqual
(
e
.
message
,
msg
)
self
.
assertTrue
(
isinstance
(
e
.
message
,
unicode
))
# ???
self
.
assertEqual
(
str
(
e
),
msg
)
self
.
assertEqual
(
unicode
(
e
),
msg
)
self
.
assertEqual
(
e
.
args
, (
msg
,))
self
.
assertTrue
(
isinstance
(
e
.
args
,
tuple
))
self
.
assertEqual
(
repr
(
e
),
"OverflowException('A simple message',)"
)
def
testExceptionIsInstanceOfSystemObject
(
self
):
"""Test behavior of isinstance(<managed exception>, System.Object)."""
# This is an anti-test, in that this is a caveat of the current
# implementation. Because exceptions are not allowed to be new-style
# classes, we wrap managed exceptions in a general-purpose old-style
# class that delegates to the wrapped object. This makes _almost_
# everything work as expected, except that an isinstance check against
# CLR.System.Object will fail for a managed exception (because a new
# style class cannot appear in the __bases__ of an old-style class
# without causing a crash in the CPython interpreter). This test is
# here mainly to remind me to update the caveat in the documentation
# one day when when exceptions can be new-style classes.
from
System
import
OverflowException
from
System
import
Object
o
=
OverflowException
(
'error'
)
self
.
assertFalse
(
isinstance
(
o
,
Object
))
def
test_suite
():
return
unittest
.
makeSuite
(
ExceptionTests
)
def
main
():
unittest
.
TextTestRunner
().
run
(
test_suite
())
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL