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
share common code between ToArray and ToList
  • Loading branch information
@koubaa
koubaa committedDec 2, 2019
commita4599d922bf0742f35e76f720ba0fefcdadf86af
88 changes: 34 additions & 54 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -864,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@@ -910,12 +894,34 @@ 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>
Expand DownExpand Up@@ -963,39 +969,13 @@ private static bool ToAction(IntPtr value, Type obType, out object result, bool
/// </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) {
var success = ToListOfT(value, elementType, out result);
if (!success)
{
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;
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp