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

Commit51a1868

Browse files
jmlidbetterfilmor
authored andcommitted
Adds support to convert iterators to arrays (#928)
1 parentc97a380 commit51a1868

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

‎AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
3535
- Jeff Reback ([@jreback](https://github.com/jreback))
3636
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
37+
- Joe Lidbetter ([@jmlidbetter](https://github.com/jmlidbetter))
3738
- John Burnett ([@johnburnett](https://github.com/johnburnett))
3839
- John Wilkes ([@jbw3](https://github.com/jbw3))
3940
- Luke Stratman ([@lstratman](https://github.com/lstratman))

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1616
- Added argument types information to "No method matches given arguments" message
1717
- Moved wheel import in setup.py inside of a try/except to prevent pip collection failures
1818
- Removes PyLong_GetMax and PyClass_New when targetting Python3
19+
- Added support for converting python iterators to C# arrays
1920

2021
###Fixed
2122

‎src/runtime/converter.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -837,50 +837,53 @@ private static void SetConversionError(IntPtr value, Type target)
837837

838838
/// <summary>
839839
/// Convert a Python value to a correctly typed managed array instance.
840-
/// The Python value must support the Pythonsequence protocol and the
840+
/// The Python value must support the Pythoniterator protocol or and the
841841
/// items in the sequence must be convertible to the target array type.
842842
/// </summary>
843843
privatestaticboolToArray(IntPtrvalue,TypeobType,outobjectresult,boolsetError)
844844
{
845845
TypeelementType=obType.GetElementType();
846-
varsize=Runtime.PySequence_Size(value);
847846
result=null;
848847

849-
if(size<0)
850-
{
848+
boolIsSeqObj=Runtime.PySequence_Check(value);
849+
varlen=IsSeqObj?Runtime.PySequence_Size(value):-1;
850+
851+
IntPtrIterObject=Runtime.PyObject_GetIter(value);
852+
853+
if(IterObject==IntPtr.Zero){
851854
if(setError)
852855
{
853856
SetConversionError(value,obType);
854857
}
855858
returnfalse;
856859
}
857860

858-
Arrayitems=Array.CreateInstance(elementType,size);
861+
Arrayitems;
862+
863+
varlistType=typeof(List<>);
864+
varconstructedListType=listType.MakeGenericType(elementType);
865+
IListlist=IsSeqObj?(IList)Activator.CreateInstance(constructedListType,newObject[]{(int)len}):
866+
(IList)Activator.CreateInstance(constructedListType);
867+
IntPtritem;
859868

860-
// XXX - is there a better way to unwrap this if it is a real array?
861-
for(vari=0;i<size;i++)
869+
while((item=Runtime.PyIter_Next(IterObject))!=IntPtr.Zero)
862870
{
863871
objectobj=null;
864-
IntPtritem=Runtime.PySequence_GetItem(value,i);
865-
if(item==IntPtr.Zero)
866-
{
867-
if(setError)
868-
{
869-
SetConversionError(value,obType);
870-
returnfalse;
871-
}
872-
}
873872

874873
if(!Converter.ToManaged(item,elementType,outobj,true))
875874
{
876875
Runtime.XDecref(item);
877876
returnfalse;
878877
}
879878

880-
items.SetValue(obj,i);
879+
list.Add(obj);
881880
Runtime.XDecref(item);
882881
}
882+
Runtime.XDecref(IterObject);
883883

884+
items=Array.CreateInstance(elementType,list.Count);
885+
list.CopyTo(items,0);
886+
884887
result=items;
885888
returntrue;
886889
}

‎src/tests/test_array.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,3 +1337,34 @@ def test_array_abuse():
13371337
withpytest.raises(TypeError):
13381338
desc=Test.PublicArrayTest.__dict__['__setitem__']
13391339
desc(0,0,0)
1340+
1341+
1342+
@pytest.mark.skipif(PY2,reason="Only applies in Python 3")
1343+
deftest_iterator_to_array():
1344+
fromSystemimportArray,String
1345+
1346+
d= {"a":1,"b":2,"c":3}
1347+
keys_iterator=iter(d.keys())
1348+
arr=Array[String](keys_iterator)
1349+
1350+
Array.Sort(arr)
1351+
1352+
assertarr[0]=="a"
1353+
assertarr[1]=="b"
1354+
assertarr[2]=="c"
1355+
1356+
1357+
@pytest.mark.skipif(PY2,reason="Only applies in Python 3")
1358+
deftest_dict_keys_to_array():
1359+
fromSystemimportArray,String
1360+
1361+
d= {"a":1,"b":2,"c":3}
1362+
d_keys=d.keys()
1363+
arr=Array[String](d_keys)
1364+
1365+
Array.Sort(arr)
1366+
1367+
assertarr[0]=="a"
1368+
assertarr[1]=="b"
1369+
assertarr[2]=="c"
1370+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp