forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexceptiontest.cs
More file actions
145 lines (121 loc) · 4.24 KB
/
exceptiontest.cs
File metadata and controls
145 lines (121 loc) · 4.24 KB
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
using Python.Runtime;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Python.Test
{
/// <summary>
/// Supports CLR Exception unit tests.
/// </summary>
public class ExceptionTest
{
public int ThrowProperty
{
get { throw new OverflowException("error"); }
set { throw new OverflowException("error"); }
}
public static Exception GetBaseException()
{
return new Exception("error");
}
public static OverflowException GetExplicitException()
{
return new OverflowException("error");
}
public static Exception GetWidenedException()
{
return new OverflowException("error");
}
public static ExtendedException GetExtendedException()
{
return new ExtendedException("error");
}
public static bool SetBaseException(Exception e)
{
return typeof(Exception).IsInstanceOfType(e);
}
public static bool SetExplicitException(OverflowException e)
{
return typeof(OverflowException).IsInstanceOfType(e);
}
public static bool SetWidenedException(Exception e)
{
return typeof(Exception).IsInstanceOfType(e);
}
public static bool ThrowException()
{
throw new OverflowException("error");
}
public static IEnumerable<int> ThrowExceptionInIterator(Exception e)
{
yield return 1;
yield return 2;
throw e;
}
public static void ThrowChainedExceptions()
{
try
{
try
{
throw new Exception("Innermost exception");
}
catch (Exception exc)
{
throw new Exception("Inner exception", exc);
}
}
catch (Exception exc2)
{
throw new Exception("Outer exception", exc2);
}
}
public static IntPtr DoThrowSimple()
{
using (Py.GIL())
{
dynamic builtins = Py.Import("builtins");
var typeErrorType = new PyType(builtins.TypeError);
var pyerr = new PythonException(typeErrorType, value:null, traceback:null, "Type error, the first", innerException:null);
throw new ArgumentException("Bogus bad parameter", pyerr);
}
}
public static void DoThrowWithInner()
{
using(Py.GIL())
{
// create a TypeError
dynamic builtins = Py.Import("builtins");
var pyerrFirst = new PythonException(new PyType(builtins.TypeError), value:null, traceback:null, "Type error, the first", innerException:null);
// Create an ArgumentException, but as a python exception, with the previous type error as the inner exception
var argExc = new ArgumentException("Bogus bad parameter", pyerrFirst);
var argExcPyObj = argExc.ToPython();
var pyArgExc = new PythonException(argExcPyObj.GetPythonType(), value:null, traceback:null, argExc.Message, innerException:argExc.InnerException);
// This object must be disposed explicitly or else we get a false-positive leak.
argExcPyObj.Dispose();
// Then throw a TypeError with the ArgumentException-as-python-error exception as inner.
var pyerrSecond = new PythonException(new PyType(builtins.TypeError), value:null, traceback:null, "Type error, the second", innerException:pyArgExc);
throw pyerrSecond;
}
}
}
public class ExtendedException : OverflowException
{
public ExtendedException()
{
}
public ExtendedException(string m) : base(m)
{
}
public string extra = "extra";
public string ExtraProperty
{
get { return extra; }
set { extra = value; }
}
public string GetExtraInfo()
{
return extra;
}
}
}