FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/pythonexception.cs at python3.9 · gokmonk/pythonnet · GitHub
gokmonk
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
pythonexception.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (254 loc) · 8.25 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
pythonexception.cs
Copy path
File metadata and controls
277 lines (254 loc) · 8.25 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
using
System
;
using
System
.
Runtime
.
CompilerServices
;
using
System
.
Text
;
namespace
Python
.
Runtime
{
/// <summary>
/// Provides a managed interface to exceptions thrown by the Python
/// runtime.
/// </summary>
public
class
PythonException
:
System
.
Exception
,
IPyDisposable
{
private
IntPtr
_pyType
=
IntPtr
.
Zero
;
private
IntPtr
_pyValue
=
IntPtr
.
Zero
;
private
IntPtr
_pyTB
=
IntPtr
.
Zero
;
private
string
_tb
=
""
;
private
string
_message
=
""
;
private
string
_pythonTypeName
=
""
;
private
bool
disposed
=
false
;
private
bool
_finalized
=
false
;
public
PythonException
(
)
{
IntPtr
gs
=
PythonEngine
.
AcquireLock
(
)
;
Runtime
.
PyErr_Fetch
(
out
_pyType
,
out
_pyValue
,
out
_pyTB
)
;
if
(
_pyType
!=
IntPtr
.
Zero
&&
_pyValue
!=
IntPtr
.
Zero
)
{
string
type
;
string
message
;
Runtime
.
XIncref
(
_pyType
)
;
using
(
var
pyType
=
new
PyObject
(
_pyType
)
)
using
(
PyObject
pyTypeName
=
pyType
.
GetAttr
(
"__name__"
)
)
{
type
=
pyTypeName
.
ToString
(
)
;
}
_pythonTypeName
=
type
;
Runtime
.
XIncref
(
_pyValue
)
;
using
(
var
pyValue
=
new
PyObject
(
_pyValue
)
)
{
message
=
pyValue
.
ToString
(
)
;
}
_message
=
type
+
" : "
+
message
;
}
if
(
_pyTB
!=
IntPtr
.
Zero
)
{
using
(
PyObject
tb_module
=
PythonEngine
.
ImportModule
(
"traceback"
)
)
{
Runtime
.
XIncref
(
_pyTB
)
;
using
(
var
pyTB
=
new
PyObject
(
_pyTB
)
)
{
_tb
=
tb_module
.
InvokeMethod
(
"format_tb"
,
pyTB
)
.
ToString
(
)
;
}
}
}
PythonEngine
.
ReleaseLock
(
gs
)
;
}
// Ensure that encapsulated Python objects are decref'ed appropriately
// when the managed exception wrapper is garbage-collected.
~
PythonException
(
)
{
if
(
_finalized
||
disposed
)
{
return
;
}
_finalized
=
true
;
Finalizer
.
Instance
.
AddFinalizedObject
(
this
)
;
}
/// <summary>
/// Restores python error.
/// </summary>
public
void
Restore
(
)
{
IntPtr
gs
=
PythonEngine
.
AcquireLock
(
)
;
Runtime
.
PyErr_Restore
(
_pyType
,
_pyValue
,
_pyTB
)
;
_pyType
=
IntPtr
.
Zero
;
_pyValue
=
IntPtr
.
Zero
;
_pyTB
=
IntPtr
.
Zero
;
PythonEngine
.
ReleaseLock
(
gs
)
;
}
/// <summary>
/// PyType Property
/// </summary>
/// <remarks>
/// Returns the exception type as a Python object.
/// </remarks>
public
IntPtr
PyType
{
get
{
return
_pyType
;
}
}
/// <summary>
/// PyValue Property
/// </summary>
/// <remarks>
/// Returns the exception value as a Python object.
/// </remarks>
public
IntPtr
PyValue
{
get
{
return
_pyValue
;
}
}
/// <summary>
/// PyTB Property
/// </summary>
/// <remarks>
/// Returns the TraceBack as a Python object.
/// </remarks>
public
IntPtr
PyTB
{
get
{
return
_pyTB
;
}
}
/// <summary>
/// Message Property
/// </summary>
/// <remarks>
/// A string representing the python exception message.
/// </remarks>
public
override
string
Message
{
get
{
return
_message
;
}
}
/// <summary>
/// StackTrace Property
/// </summary>
/// <remarks>
/// A string representing the python exception stack trace.
/// </remarks>
public
override
string
StackTrace
{
get
{
return
_tb
+
base
.
StackTrace
;
}
}
/// <summary>
/// Python error type name.
/// </summary>
public
string
PythonTypeName
{
get
{
return
_pythonTypeName
;
}
}
/// <summary>
/// Formats this PythonException object into a message as would be printed
/// out via the Python console. See traceback.format_exception
/// </summary>
public
string
Format
(
)
{
string
res
;
IntPtr
gs
=
PythonEngine
.
AcquireLock
(
)
;
try
{
if
(
_pyTB
!=
IntPtr
.
Zero
&&
_pyType
!=
IntPtr
.
Zero
&&
_pyValue
!=
IntPtr
.
Zero
)
{
Runtime
.
XIncref
(
_pyType
)
;
Runtime
.
XIncref
(
_pyValue
)
;
Runtime
.
XIncref
(
_pyTB
)
;
using
(
PyObject
pyType
=
new
PyObject
(
_pyType
)
)
using
(
PyObject
pyValue
=
new
PyObject
(
_pyValue
)
)
using
(
PyObject
pyTB
=
new
PyObject
(
_pyTB
)
)
using
(
PyObject
tb_mod
=
PythonEngine
.
ImportModule
(
"traceback"
)
)
{
var
buffer
=
new
StringBuilder
(
)
;
var
values
=
tb_mod
.
InvokeMethod
(
"format_exception"
,
pyType
,
pyValue
,
pyTB
)
;
foreach
(
PyObject
val
in
values
)
{
buffer
.
Append
(
val
.
ToString
(
)
)
;
}
res
=
buffer
.
ToString
(
)
;
}
}
else
{
res
=
StackTrace
;
}
}
finally
{
PythonEngine
.
ReleaseLock
(
gs
)
;
}
return
res
;
}
public
bool
IsMatches
(
IntPtr
exc
)
{
return
Runtime
.
PyErr_GivenExceptionMatches
(
PyType
,
exc
)
!=
0
;
}
/// <summary>
/// Dispose Method
/// </summary>
/// <remarks>
/// The Dispose method provides a way to explicitly release the
/// Python objects represented by a PythonException.
/// If object not properly disposed can cause AppDomain unload issue.
/// See GH#397 and GH#400.
/// </remarks>
public
void
Dispose
(
)
{
if
(
!
disposed
)
{
if
(
Runtime
.
Py_IsInitialized
(
)
>
0
&&
!
Runtime
.
IsFinalizing
)
{
IntPtr
gs
=
PythonEngine
.
AcquireLock
(
)
;
if
(
_pyType
!=
IntPtr
.
Zero
)
{
Runtime
.
XDecref
(
_pyType
)
;
_pyType
=
IntPtr
.
Zero
;
}
if
(
_pyValue
!=
IntPtr
.
Zero
)
{
Runtime
.
XDecref
(
_pyValue
)
;
_pyValue
=
IntPtr
.
Zero
;
}
// XXX Do we ever get TraceBack? //
if
(
_pyTB
!=
IntPtr
.
Zero
)
{
Runtime
.
XDecref
(
_pyTB
)
;
_pyTB
=
IntPtr
.
Zero
;
}
PythonEngine
.
ReleaseLock
(
gs
)
;
}
GC
.
SuppressFinalize
(
this
)
;
disposed
=
true
;
}
}
public
IntPtr
[
]
GetTrackedHandles
(
)
{
return
new
IntPtr
[
]
{
_pyType
,
_pyValue
,
_pyTB
}
;
}
/// <summary>
/// Matches Method
/// </summary>
/// <remarks>
/// Returns true if the Python exception type represented by the
/// PythonException instance matches the given exception type.
/// </remarks>
public
static
bool
Matches
(
IntPtr
ob
)
{
return
Runtime
.
PyErr_ExceptionMatches
(
ob
)
!=
0
;
}
public
static
void
ThrowIfIsNull
(
IntPtr
ob
)
{
if
(
ob
==
IntPtr
.
Zero
)
{
throw
new
PythonException
(
)
;
}
}
internal
static
void
ThrowIfIsNull
(
BorrowedReference
reference
)
{
if
(
reference
.
IsNull
)
{
throw
new
PythonException
(
)
;
}
}
public
static
void
ThrowIfIsNotZero
(
int
value
)
{
if
(
value
!=
0
)
{
throw
new
PythonException
(
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL