Skip to content

Improve method binding #974

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added automatic NuGet package generation in appveyor and local builds
- Added function that sets Py_NoSiteFlag to 1.
- Added support for Jetson Nano.
- Added support for methodbinding to IEnumerable, ICollection, IList, Task, Action, Func

### Changed

Expand Down
126 changes: 102 additions & 24 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return ToArray(value, obType, out result, setError);
}

if (obType.IsGenericType)
{
if (obType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
obType.GetGenericTypeDefinition() == typeof(ICollection<>) ||
obType.GetGenericTypeDefinition() == typeof(IList<>))
{
//We could probably convert to a thin IEnumerable/IList/ICollection implementation around a python array
//but for simplicity right now convert to a List<T> which is a copy of the python iterable.
return ToList(value, obType, out result, setError);
}
}

if (obType.FullName == "System.Action")
{
return ToAction(value, obType, out result, setError);
}

if (obType.IsEnum)
{
return ToEnum(value, obType, out result, setError);
Expand Down Expand Up @@ -847,44 +864,28 @@ private static void SetConversionError(IntPtr value, Type target)
Exceptions.SetError(Exceptions.TypeError, $"Cannot convert {src} to {target}");
}


/// <summary>
/// Convert a Python value to a correctly typed managed array instance.
/// The Python value must support the Python iterator protocol or and the
/// items in the sequence must be convertible to the target array type.
/// </summary>
private static bool ToArray(IntPtr value, Type obType, out object result, bool setError)
private static bool ToListOfT(IntPtr value, Type elementType, out object result)
{
Type elementType = obType.GetElementType();
result = null;

bool IsSeqObj = Runtime.PySequence_Check(value);
var len = IsSeqObj ? Runtime.PySequence_Size(value) : -1;

IntPtr IterObject = Runtime.PyObject_GetIter(value);

if(IterObject==IntPtr.Zero) {
if (setError)
{
SetConversionError(value, obType);
}
if (IterObject == IntPtr.Zero)
return false;
}

Array items;

var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(elementType);
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
(IList) Activator.CreateInstance(constructedListType);
IList list = IsSeqObj ? (IList)Activator.CreateInstance(constructedListType, new Object[] { (int)len }) :
(IList)Activator.CreateInstance(constructedListType);
IntPtr item;

while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero)
{
while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero) {
object obj = null;

if (!Converter.ToManaged(item, elementType, out obj, true))
{
if (!Converter.ToManaged(item, elementType, out obj, true)) {
Runtime.XDecref(item);
return false;
}
Expand All @@ -893,12 +894,89 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
Runtime.XDecref(item);
}
Runtime.XDecref(IterObject);
result = list;
return true;
}

/// <summary>
/// Convert a Python value to a correctly typed managed array instance.
/// The Python value must support the Python iterator protocol or and the
/// items in the sequence must be convertible to the target array type.
/// </summary>
private static bool ToArray(IntPtr value, Type obType, out object result, bool setError)
{
Type elementType = obType.GetElementType();
result = null;
bool success = ToListOfT(value, elementType, out result);
if (!success)
{
if (setError)
SetConversionError(value, obType);
return false;
}

items = Array.CreateInstance(elementType, list.Count);
IList list = (IList)result;
Array items = Array.CreateInstance(elementType, list.Count);
list.CopyTo(items, 0);

result = items;
return true;

}

/// <summary>
/// Convert a Python function to a System.Action.
/// The Python value must support the Python "BLAH" protocol.
/// </summary>
private static bool ToAction(IntPtr value, Type obType, out object result, bool setError) {

result = null;

PyObject obj = new PyObject(value);

var args = obType.GetGenericArguments();

//Temporarily only deal with non-generic actions!
if (!obj.IsCallable() || args.Length != 0) {
if (setError) {
SetConversionError(value, obType);
}
obj.Dispose();
return false;
}

Action action = () => {
PyObject py_action = new PyObject(value);
var py_args = new PyObject[0];
var py_result = py_action.Invoke(py_args);

//Discard the result since this is being converted to an Action
py_result.Dispose();
py_action.Dispose();
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The object pointed to by value can be collected between the construction of this action, and the moment it is invoked. It would lead to a memory corruption.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had forgotten to push the latest change set, please check again


result = action;
obj.Dispose();
return true;
}


/// <summary>
/// Convert a Python value to a correctly typed managed list instance.
/// The Python value must support the Python iterator protocol or and the
/// items in the sequence must be convertible to the target array type.
/// TODO - remove duplication with ToArray!
/// </summary>
private static bool ToList(IntPtr value, Type obType, out object result, bool setError) {
Type elementType = obType.GetGenericArguments()[0];
var success = ToListOfT(value, elementType, out result);
if (!success)
{
if (setError)
SetConversionError(value, obType);
return false;
}
return true;
}


Expand Down
33 changes: 33 additions & 0 deletions src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ public static int[] TestOverloadedParams(int v, int[] args)
return args;
}

public static int TestIList(System.Collections.Generic.IList<object> arg)
{
return 1;
}

public static int TestICollection(System.Collections.Generic.ICollection<object> arg)
{
return 1;
}

public int TestAction(System.Action action)
{
action();
return 1;
}

public int TestFuncObj(System.Func<object, int> func, object arg)
{
return func.Invoke(arg);

}

public int TestTask(System.Threading.Tasks.Task<int> task)
{
task.RunSynchronously();
return task.Result;
}

public int TestIEnumerable(System.Collections.Generic.IEnumerable<object> arg)
{
return 1;
}

public static string TestOverloadedNoObject(int i)
{
return "Got int";
Expand Down
34 changes: 34 additions & 0 deletions src/tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ def test_null_array_conversion():
r = ob.TestNullArrayConversion(None)
assert r is None

def test_action():
"""Test python lambda as an Action"""
ob = MethodTest()
def func():
return
r = ob.TestAction(func)
assert r == 1

def test_func_object():
"""Test python lambda as a Func"""
ob = MethodTest()
def func(arg):
return arg + 1
r = ob.TestFunc(func, 0)
assert r == 1

def test_task():
"""Test python lambda as a Task"""
ob = MethodTest()
def func():
return 1
r = ob.TestTask(func)
assert r == 1

def test_ienumerable_args():
"""Test conversion of python lists and tuples to IEnumerable<object>"""
ob = MethodTest()
x = ob.TestIEnumerable([1,2,3])
y = ob.TestIEnumerable((1,2,3))

def test_icollection_args():
"""Test conversion of python lists and tuples to ICollection<object>"""
ob = MethodTest()
x = ob.TestICollection([1,2,3])

def test_string_params_args():
"""Test use of string params."""
Expand Down