Skip to content

Commit 5205f84

Browse files
committed
Handle managed exception
1 parent 14d0003 commit 5205f84

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

src/runtime/exceptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ public static void SetError(Exception e)
267267
Runtime.XDecref(op);
268268
}
269269

270+
public static void SetErrorWithoutOverride(Exception e)
271+
{
272+
if (ErrorOccurred())
273+
{
274+
return;
275+
}
276+
if (e.InnerException != null)
277+
{
278+
e = e.InnerException;
279+
}
280+
SetError(e);
281+
}
282+
270283
/// <summary>
271284
/// ErrorOccurred Method
272285
/// </summary>

src/runtime/methodobject.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,7 @@ public IntPtr PyCall(IntPtr self, IntPtr args)
393393
}
394394
catch (Exception e)
395395
{
396-
if (e.InnerException != null)
397-
{
398-
e = e.InnerException;
399-
}
400-
Console.WriteLine(e);
401-
Exceptions.SetError(e);
396+
Exceptions.SetErrorWithoutOverride(e);
402397
return IntPtr.Zero;
403398
}
404399
}

src/runtime/propertyobject.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ public StaticPropertyObject(PropertyInfo md)
181181
public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
182182
{
183183
var self = (StaticPropertyObject<T>)GetManagedObject(ds);
184-
return PyValueConverter<T>.Convert(self.getter());
184+
try
185+
{
186+
return PyValueConverter<T>.Convert(self.getter());
187+
}
188+
catch (Exception e)
189+
{
190+
Exceptions.SetErrorWithoutOverride(e);
191+
return IntPtr.Zero;
192+
}
185193
}
186194

187195
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
@@ -192,8 +200,16 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
192200
Exceptions.RaiseTypeError("property is read-only");
193201
return -1;
194202
}
195-
T value = ValueConverter<T>.Get(val);
196-
self.setter(value);
203+
try
204+
{
205+
T value = ValueConverter<T>.Get(val);
206+
self.setter(value);
207+
}
208+
catch (Exception e)
209+
{
210+
Exceptions.SetErrorWithoutOverride(e);
211+
return -1;
212+
}
197213
return 0;
198214
}
199215
}
@@ -231,8 +247,16 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
231247
{
232248
return Exceptions.RaiseTypeError("invalid target");
233249
}
234-
T value = self.getter((Cls)co.inst);
235-
return value.ToPythonPtr();
250+
try
251+
{
252+
T value = self.getter((Cls)co.inst);
253+
return value.ToPythonPtr();
254+
}
255+
catch (Exception e)
256+
{
257+
Exceptions.SetErrorWithoutOverride(e);
258+
return IntPtr.Zero;
259+
}
236260
}
237261

238262
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
@@ -256,7 +280,15 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
256280
Exceptions.RaiseTypeError("invalid target");
257281
return -1;
258282
}
259-
self.setter((Cls)co.inst, ValueConverter<T>.Get(val));
283+
try
284+
{
285+
self.setter((Cls)co.inst, ValueConverter<T>.Get(val));
286+
}
287+
catch (Exception e)
288+
{
289+
Exceptions.SetErrorWithoutOverride(e);
290+
return -1;
291+
}
260292
return 0;
261293
}
262294
}

0 commit comments

Comments
 (0)