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

Better error messages from PyObject.AsManagedObject and DelegateManager.TrueDispatch by tminka · Pull Request #1344 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (3) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 9 additions & 1 deletion src/embed_tests/TestPyObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL