Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db9478f

Browse files
filmordenfromufa
authored and
denfromufa
committed
Fix exception pickling (#286)
* Remove Python <2.5 exception wrapping. * Fix crashes when trying to pickle CLR exceptions. The "args" slot of BaseException was not filled, instead we provided the args through our __getattr__ implementation. This fails in BaseException_reduce which depends on "args" being not NULL. We fix this by explicitly setting the "args" slot on all CLR exception objects on creation. This also makes tp_repr obsolete. * Fix Python 2 compatibility. * Remove access to obsolete message attribute. * Fix the tests. * Use cPickle for Python 2. * Fix typo.
1 parent 35cdb12 commit db9478f

9 files changed

+67
-420
lines changed

src/runtime/clrobject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ internal CLRObject(Object ob, IntPtr tp) : base()
3030
this.pyHandle = py;
3131
this.gcHandle = gc;
3232
inst = ob;
33+
34+
// Fix the BaseException args slot if wrapping a CLR exception
35+
Exceptions.SetArgs(py);
3336
}
3437

3538

src/runtime/converter.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,7 @@ internal static IntPtr ToPython(Object value, Type type)
156156
switch (tc)
157157
{
158158
case TypeCode.Object:
159-
result = CLRObject.GetInstHandle(value, type);
160-
161-
// XXX - hack to make sure we convert new-style class based
162-
// managed exception instances to wrappers ;(
163-
if (Runtime.wrap_exceptions)
164-
{
165-
Exception e = value as Exception;
166-
if (e != null)
167-
{
168-
return Exceptions.GetExceptionInstanceWrapper(result);
169-
}
170-
}
171-
172-
return result;
159+
return CLRObject.GetInstHandle(value, type);
173160

174161
case TypeCode.String:
175162
return Runtime.PyUnicode_FromString((string)value);
@@ -283,36 +270,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
283270
ManagedType mt = ManagedType.GetManagedObject(value);
284271
result = null;
285272

286-
// XXX - hack to support objects wrapped in old-style classes
287-
// (such as exception objects).
288-
if (Runtime.wrap_exceptions)
289-
{
290-
if (mt == null)
291-
{
292-
if (Runtime.PyObject_IsInstance(
293-
value, Exceptions.Exception
294-
) > 0)
295-
{
296-
IntPtr p = Runtime.PyObject_GetAttrString(value, "_inner");
297-
if (p != IntPtr.Zero)
298-
{
299-
// This is safe because we know that the __dict__ of
300-
// value holds a reference to _inner.
301-
value = p;
302-
Runtime.XDecref(p);
303-
mt = ManagedType.GetManagedObject(value);
304-
}
305-
}
306-
IntPtr c = Exceptions.UnwrapExceptionClass(value);
307-
if ((c != IntPtr.Zero) && (c != value))
308-
{
309-
value = c;
310-
Runtime.XDecref(c);
311-
mt = ManagedType.GetManagedObject(value);
312-
}
313-
}
314-
}
315-
316273
if (mt != null)
317274
{
318275
if (mt is CLRObject)

0 commit comments

Comments
 (0)