@@ -416,6 +416,15 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
416
416
return true ;
417
417
}
418
418
419
+ if ( obType . IsGenericType && Runtime . PyObject_TYPE ( value ) == Runtime . PyListType )
420
+ {
421
+ var typeDefinition = obType . GetGenericTypeDefinition ( ) ;
422
+ if ( typeDefinition == typeof ( List < > ) )
423
+ {
424
+ return ToList ( value , obType , out result , setError ) ;
425
+ }
426
+ }
427
+
419
428
// Common case: if the Python value is a wrapped managed object
420
429
// instance, just return the wrapped object.
421
430
ManagedType mt = ManagedType . GetManagedObject ( value ) ;
@@ -948,7 +957,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
948
957
result = null ;
949
958
950
959
IntPtr IterObject = Runtime . PyObject_GetIter ( value ) ;
951
- if ( IterObject == IntPtr . Zero )
960
+ if ( IterObject == IntPtr . Zero || elementType . IsGenericType )
952
961
{
953
962
if ( setError )
954
963
{
@@ -962,6 +971,43 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
962
971
return false ;
963
972
}
964
973
974
+ var list = MakeList ( value , IterObject , obType , elementType , setError ) ;
975
+ if ( list == null )
976
+ {
977
+ return false ;
978
+ }
979
+
980
+ Array items = Array . CreateInstance ( elementType , list . Count ) ;
981
+ list . CopyTo ( items , 0 ) ;
982
+
983
+ result = items ;
984
+ return true ;
985
+ }
986
+
987
+ /// <summary>
988
+ /// Convert a Python value to a correctly typed managed list instance.
989
+ /// The Python value must support the Python sequence protocol and the
990
+ /// items in the sequence must be convertible to the target list type.
991
+ /// </summary>
992
+ private static bool ToList ( IntPtr value , Type obType , out object result , bool setError )
993
+ {
994
+ var elementType = obType . GetGenericArguments ( ) [ 0 ] ;
995
+ IntPtr IterObject = Runtime . PyObject_GetIter ( value ) ;
996
+ result = MakeList ( value , IterObject , obType , elementType , setError ) ;
997
+ return result != null ;
998
+ }
999
+
1000
+ /// <summary>
1001
+ /// Helper function for ToArray and ToList that creates a IList out of iterable objects
1002
+ /// </summary>
1003
+ /// <param name="value"></param>
1004
+ /// <param name="IterObject"></param>
1005
+ /// <param name="obType"></param>
1006
+ /// <param name="elementType"></param>
1007
+ /// <param name="setError"></param>
1008
+ /// <returns></returns>
1009
+ private static IList MakeList ( IntPtr value , IntPtr IterObject , Type obType , Type elementType , bool setError )
1010
+ {
965
1011
IList list ;
966
1012
try
967
1013
{
@@ -989,7 +1035,8 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
989
1035
Exceptions . SetError ( e ) ;
990
1036
SetConversionError ( value , obType ) ;
991
1037
}
992
- return false ;
1038
+
1039
+ return null ;
993
1040
}
994
1041
995
1042
IntPtr item ;
@@ -1001,19 +1048,15 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
1001
1048
if ( ! Converter . ToManaged ( item , elementType , out obj , setError ) )
1002
1049
{
1003
1050
Runtime . XDecref ( item ) ;
1004
- return false ;
1051
+ return null ;
1005
1052
}
1006
1053
1007
1054
list . Add ( obj ) ;
1008
1055
Runtime . XDecref ( item ) ;
1009
1056
}
1010
1057
Runtime . XDecref ( IterObject ) ;
1011
1058
1012
- Array items = Array . CreateInstance ( elementType , list . Count ) ;
1013
- list . CopyTo ( items , 0 ) ;
1014
-
1015
- result = items ;
1016
- return true ;
1059
+ return list ;
1017
1060
}
1018
1061
1019
1062
0 commit comments