FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adds support to convert iterators to arrays by jmlidbetter · Pull Request #928 · pythonnet/pythonnet · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .md  (2) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions AUTHORS.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff 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 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff 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 deletions src/runtime/converter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff 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 Python sequence protocol and the
/// 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();
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 deletions src/tests/test_array.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff 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"


Back | FazBrowse Home | New Git URL