forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonexception.cs
More file actions
executable file
·124 lines (100 loc) · 2.72 KB
/
pythonexception.cs
File metadata and controls
executable file
·124 lines (100 loc) · 2.72 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
// ==========================================================================
// 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.
// ==========================================================================
using System;
namespace Python.Runtime {
/// <summary>
/// Provides a managed interface to exceptions thrown by the Python
/// runtime.
/// </summary>
public class PythonException : System.Exception {
private IntPtr excType = IntPtr.Zero;
private IntPtr excValue = IntPtr.Zero;
private IntPtr excTb = IntPtr.Zero;
private bool disposed = false;
public PythonException() : base() {
Runtime.PyErr_Fetch(ref excType, ref excValue, ref excTb);
Runtime.Incref(excType);
Runtime.Incref(excValue);
Runtime.Incref(excTb);
}
// Ensure that encapsulated Python objects are decref'ed appropriately
// when the managed exception wrapper is garbage-collected.
~PythonException() {
Dispose();
}
/// <summary>
/// Type Property
/// </summary>
///
/// <remarks>
/// Returns the exception type as a Python object.
/// </remarks>
public IntPtr Type {
get {
return excType;
}
}
/// <summary>
/// Value Property
/// </summary>
///
/// <remarks>
/// Returns the exception value as a Python object.
/// </remarks>
public IntPtr Value {
get {
return excValue;
}
}
/// <summary>
/// Traceback Property
/// </summary>
///
/// <remarks>
/// Returns the exception traceback as a Python object.
/// </remarks>
public IntPtr Traceback {
get {
return excTb;
}
}
/// <summary>
/// Dispose Method
/// </summary>
///
/// <remarks>
/// The Dispose method provides a way to explicitly release the
/// Python objects represented by a PythonException.
/// </remarks>
public void Dispose() {
if (!disposed) {
if (Runtime.Py_IsInitialized() > 0) {
IntPtr gs = PythonEngine.AcquireLock();
Runtime.Decref(excType);
Runtime.Decref(excValue);
Runtime.Decref(excTb);
PythonEngine.ReleaseLock(gs);
}
GC.SuppressFinalize(this);
disposed = true;
}
}
/// <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;
}
}
}