Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
koubaa wants to merge11 commits intopythonnet:masterfromkoubaa:ienumerable
Closed
Changes from1 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
PrevPrevious commit
NextNext commit
Implement list converter
  • Loading branch information
@koubaa
koubaa committedDec 1, 2019
commit9c3e1c51ff38d92ae15c45304b04e03a68616a5c
53 changes: 53 additions & 0 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp