Skip to content

Better error messages from PyObject.AsManagedObject and DelegateManager.TrueDispatch #1344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/embed_tests/TestPyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ def add(self, x, y):
}

[Test]
public void InvokeNull() {
public void InvokeNull()
{
var list = PythonEngine.Eval("list");
Assert.Throws<ArgumentNullException>(() => list.Invoke(new PyObject[] {null}));
}

[Test]
public void AsManagedObjectInvalidCast()
{
var list = PythonEngine.Eval("list");
Assert.Throws<InvalidCastException>(() => list.AsManagedObject(typeof(int)));
}
}
}
24 changes: 5 additions & 19 deletions src/runtime/delegatemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,17 @@ public void Dispose()
public object Dispatch(ArrayList args)
{
IntPtr gs = PythonEngine.AcquireLock();
object ob = null;
object ob;

try
{
ob = TrueDispatch(args);
}
catch (Exception e)
finally
{
PythonEngine.ReleaseLock(gs);
throw e;
}

PythonEngine.ReleaseLock(gs);
return ob;
}

Expand Down Expand Up @@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args)
return null;
}

object result = null;
if (!Converter.ToManaged(op, rtype, out result, false))
object result;
if (!Converter.ToManaged(op, rtype, out result, true))
{
Runtime.XDecref(op);
throw new ConversionException($"could not convert Python result to {rtype}");
throw new PythonException();
}

Runtime.XDecref(op);
return result;
}
}


public class ConversionException : Exception
{
public ConversionException()
{
}

public ConversionException(string msg) : base(msg)
{
}
}
}
11 changes: 3 additions & 8 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob)
public object AsManagedObject(Type t)
{
object result;
if (!Converter.ToManaged(obj, t, out result, false))
if (!Converter.ToManaged(obj, t, out result, true))
{
throw new InvalidCastException("cannot convert object to target type");
throw new InvalidCastException("cannot convert object to target type", new PythonException());
}
return result;
}
Expand All @@ -154,12 +154,7 @@ public T As<T>()
{
return (T)(this as object);
}
object result;
if (!Converter.ToManaged(obj, typeof(T), out result, false))
{
throw new InvalidCastException("cannot convert object to target type");
}
return (T)result;
return (T)AsManagedObject(typeof(T));
}


Expand Down
21 changes: 20 additions & 1 deletion src/tests/test_delegate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
# TODO: Add test for ObjectDelegate

"""Test CLR delegate support."""

Expand Down Expand Up @@ -257,6 +256,26 @@ def always_so_negative():
assert not d()
assert not ob.CallBoolDelegate(d)

def test_object_delegate():
"""Test object delegate."""
from Python.Test import ObjectDelegate

def create_object():
return DelegateTest()

d = ObjectDelegate(create_object)
ob = DelegateTest()
ob.CallObjectDelegate(d)

def test_invalid_object_delegate():
"""Test invalid object delegate with mismatched return type."""
from Python.Test import ObjectDelegate

d = ObjectDelegate(hello_func)
ob = DelegateTest()
with pytest.raises(TypeError):
ob.CallObjectDelegate(d)

# test async delegates

# test multicast delegates
Expand Down