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

Adds support to convert iterators to arrays#928

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

Merged
filmor merged 1 commit intopythonnet:masterfromjmlidbetter:iters_to_arrays
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsAUTHORS.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,7 @@
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
- Jeff Reback ([@jreback](https://github.com/jreback))
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- Joe Lidbetter ([@jmlidbetter](https://github.com/jmlidbetter))
- John Burnett ([@johnburnett](https://github.com/johnburnett))
- John Wilkes ([@jbw3](https://github.com/jbw3))
- Luke Stratman ([@lstratman](https://github.com/lstratman))
Expand Down
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added argument types information to "No method matches given arguments" message
- Moved wheel import in setup.py inside of a try/except to prevent pip collection failures
- Removes PyLong_GetMax and PyClass_New when targetting Python3
- Added support for converting python iterators to C# arrays

### Fixed

Expand Down
37 changes: 20 additions & 17 deletionssrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -837,50 +837,53 @@ private static void SetConversionError(IntPtr value, Type target)

/// <summary>
/// Convert a Python value to a correctly typed managed array instance.
/// The Python value must support the Pythonsequence protocol and the
/// The Python value must support the Pythoniterator 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();
var size = Runtime.PySequence_Size(value);
result = null;

if (size < 0)
{
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;
}

Array items = Array.CreateInstance(elementType, size);
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);
IntPtr item;

// XXX - is there a better way to unwrap this if it is a real array?
for (var i = 0; i < size; i++)
while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero)
{
object obj = null;
IntPtr item = Runtime.PySequence_GetItem(value, i);
if (item == IntPtr.Zero)
{
if (setError)
{
SetConversionError(value, obType);
return false;
}
}

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

items.SetValue(obj, i);
list.Add(obj);
Runtime.XDecref(item);
}
Runtime.XDecref(IterObject);

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

result = items;
return true;
}
Expand Down
31 changes: 31 additions & 0 deletionssrc/tests/test_array.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1337,3 +1337,34 @@ def test_array_abuse():
with pytest.raises(TypeError):
desc = Test.PublicArrayTest.__dict__['__setitem__']
desc(0, 0, 0)


@pytest.mark.skipif(PY2, reason="Only applies in Python 3")
def test_iterator_to_array():
from System import Array, String

d = {"a": 1, "b": 2, "c": 3}
keys_iterator = iter(d.keys())
arr = Array[String](keys_iterator)

Array.Sort(arr)

assert arr[0] == "a"
assert arr[1] == "b"
assert arr[2] == "c"


@pytest.mark.skipif(PY2, reason="Only applies in Python 3")
def test_dict_keys_to_array():
from System import Array, String

d = {"a": 1, "b": 2, "c": 3}
d_keys = d.keys()
arr = Array[String](d_keys)

Array.Sort(arr)

assert arr[0] == "a"
assert arr[1] == "b"
assert arr[2] == "c"


[8]ページ先頭

©2009-2025 Movatter.jp