8000 Improve method binding by koubaa · Pull Request #974 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement list converter
  • Loading branch information
koubaa committed Dec 1, 2019
commit 9c3e1c51ff38d92ae15c45304b04e03a68616a5c
53 changes: 53 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ 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.IsEnum)
{
return ToEnum(value, obType, out result, setError);
Expand Down Expand Up @@ -901,6 +912,48 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
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.
/// </summary>
private static bool ToList(IntPtr value, Type obType, out object result, bool setError) {
Type elementType = obType.GetGenericArguments()[0];
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);
}
return false;
}

var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(elementType);
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) {
object obj = null;

if (!Converter.ToManaged(item, elementType, out obj, true)) {
Runtime.XDecref(item);
return false;
}

list.Add(obj);
Runtime.XDecref(item);
}
Runtime.XDecref(IterObject);
result = list;
return true;
}

/// <summary>
/// Convert a Python value to a correctly typed managed enum instance.
Expand Down
0