FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

commit fix for noticing wrapped exception types · chid/pythonnet@3fccfd5 · GitHub

Repository navigation

Commit 3fccfd5

Browse files
committed
commit fix for noticing wrapped exception types
1 parent c11c50a commit 3fccfd5

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎pythonnet/src/runtime/converter.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
225225
mt = ManagedType.GetManagedObject(value);
226226
}
227227
}
228+
IntPtr c = Exceptions.UnwrapExceptionClass(value);
229+
if ((c != IntPtr.Zero) && (c != value)) {
230+
value = c;
231+
Runtime.Decref(c);
232+
mt = ManagedType.GetManagedObject(value);
233+
}
228234
}
229235
}
230236

‎pythonnet/src/runtime/exceptions.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ internal static IntPtr GetExceptionInstanceWrapper(IntPtr real) {
192192
return op;
193193
}
194194

195+
internal static IntPtr UnwrapExceptionClass(IntPtr op) {
196+
// In some cases its necessary to recognize an exception *class*,
197+
// and obtain the inner (wrapped) exception class. This method
198+
// returns the inner class if found, or a null pointer.
199+
200+
IntPtr d = Runtime.PyObject_GetAttrString(op, "__dict__");
201+
if (d == IntPtr.Zero) {
202+
Exceptions.Clear();
203+
return IntPtr.Zero;
204+
}
205+
IntPtr c = Runtime.PyDict_GetItemString(d, "_class");
206+
Runtime.Decref(d);
207+
if (c == IntPtr.Zero) {
208+
Exceptions.Clear();
209+
}
210+
return c;
211+
}
195212

196213

197214
/// <summary>

‎pythonnet/src/runtime/managedtype.cs‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@ internal static ManagedType GetManagedObject(IntPtr ob) {
4747
return (ManagedType)gc.Target;
4848
}
4949

50-
51-
// // Hmm - check to see if its a wrapped exception?
52-
// if (Runtime.wrap_exceptions) {
53-
// IntPtr p = Runtime.PyObject_GetAttrString(ob, "_inner");
54-
// if (p != IntPtr.Zero) {
55-
// ManagedType m = GetManagedObject(p);
56-
// Runtime.Decref(p);
57-
// if (m != null)
58-
// return m;
59-
// }
60-
// }
61-
50+
// In certain situations, we need to recognize a wrapped
51+
// exception class and be willing to unwrap the class :(
52+
53+
if (Runtime.wrap_exceptions) {
54+
IntPtr e = Exceptions.UnwrapExceptionClass(ob);
55+
if ((e != IntPtr.Zero) && (e != ob)) {
56+
ManagedType m = GetManagedObject(e);
57+
Runtime.Decref(e);
58+
return m;
59+
}
60+
}
6261
}
6362
return null;
6463
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL