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

Skip to content

Commit 107c8b9

Browse files
tminkalostmsu
authored andcommitted
PyObject.AsManagedObject gives a better error message
DelegateManager.TrueDispatch gives a better error message
1 parent cac82a6 commit 107c8b9

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/embed_tests/TestPyObject.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ def add(self, x, y):
5959
}
6060

6161
[Test]
62-
public void InvokeNull() {
62+
public void InvokeNull()
63+
{
6364
var list = PythonEngine.Eval("list");
6465
Assert.Throws<ArgumentNullException>(() => list.Invoke(new PyObject[] {null}));
6566
}
67+
68+
[Test]
69+
public void AsManagedObjectInvalidCast()
70+
{
71+
var list = PythonEngine.Eval("list");
72+
Assert.Throws<InvalidCastException>(() => list.AsManagedObject(typeof(int)));
73+
}
6674
}
6775
}

src/runtime/delegatemanager.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,17 @@ public void Dispose()
221221
public object Dispatch(ArrayList args)
222222
{
223223
IntPtr gs = PythonEngine.AcquireLock();
224-
object ob = null;
224+
object ob;
225225

226226
try
227227
{
228228
ob = TrueDispatch(args);
229229
}
230-
catch (Exception e)
230+
finally
231231
{
232232
PythonEngine.ReleaseLock(gs);
233-
throw e;
234233
}
235234

236-
PythonEngine.ReleaseLock(gs);
237235
return ob;
238236
}
239237

@@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args)
266264
return null;
267265
}
268266

269-
object result = null;
270-
if (!Converter.ToManaged(op, rtype, out result, false))
267+
object result;
268+
if (!Converter.ToManaged(op, rtype, out result, true))
271269
{
272270
Runtime.XDecref(op);
273-
throw new ConversionException($"could not convert Python result to {rtype}");
271+
throw new PythonException();
274272
}
275273

276274
Runtime.XDecref(op);
277275
return result;
278276
}
279277
}
280-
281-
282-
public class ConversionException : Exception
283-
{
284-
public ConversionException()
285-
{
286-
}
287-
288-
public ConversionException(string msg) : base(msg)
289-
{
290-
}
291-
}
292278
}

src/runtime/pyobject.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob)
134134
public object AsManagedObject(Type t)
135135
{
136136
object result;
137-
if (!Converter.ToManaged(obj, t, out result, false))
137+
if (!Converter.ToManaged(obj, t, out result, true))
138138
{
139-
throw new InvalidCastException("cannot convert object to target type");
139+
throw new InvalidCastException("cannot convert object to target type", new PythonException());
140140
}
141141
return result;
142142
}
@@ -154,12 +154,7 @@ public T As<T>()
154154
{
155155
return (T)(this as object);
156156
}
157-
object result;
158-
if (!Converter.ToManaged(obj, typeof(T), out result, false))
159-
{
160-
throw new InvalidCastException("cannot convert object to target type");
161-
}
162-
return (T)result;
157+
return (T)AsManagedObject(typeof(T));
163158
}
164159

165160

src/tests/test_delegate.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
# TODO: Add test for ObjectDelegate
32

43
"""Test CLR delegate support."""
54

@@ -257,6 +256,26 @@ def always_so_negative():
257256
assert not d()
258257
assert not ob.CallBoolDelegate(d)
259258

259+
def test_object_delegate():
260+
"""Test object delegate."""
261+
from Python.Test import ObjectDelegate
262+
263+
def create_object():
264+
return DelegateTest()
265+
266+
d = ObjectDelegate(create_object)
267+
ob = DelegateTest()
268+
ob.CallObjectDelegate(d)
269+
270+
def test_invalid_object_delegate():
271+
"""Test invalid object delegate with mismatched return type."""
272+
from Python.Test import ObjectDelegate
273+
274+
d = ObjectDelegate(hello_func)
275+
ob = DelegateTest()
276+
with pytest.raises(TypeError):
277+
ob.CallObjectDelegate(d)
278+
260279
# test async delegates
261280

262281
# test multicast delegates

0 commit comments

Comments
 (0)