@@ -882,17 +882,23 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
882
882
// See https://docs.microsoft.com/en-us/dotnet/api/system.type.makegenerictype#System_Type_MakeGenericType_System_Type
883
883
var constructedListType = typeof ( List < > ) . MakeGenericType ( elementType ) ;
884
884
bool IsSeqObj = Runtime . PySequence_Check ( value ) ;
885
+ object [ ] constructorArgs = Array . Empty < object > ( ) ;
885
886
if ( IsSeqObj )
886
887
{
887
888
var len = Runtime . PySequence_Size ( value ) ;
888
- list = ( IList ) Activator . CreateInstance ( constructedListType , new Object [ ] { ( int ) len } ) ;
889
- }
890
- else
891
- {
892
- // CreateInstance can throw even if MakeGenericType succeeded.
893
- // See https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance#System_Activator_CreateInstance_System_Type_
894
- list = ( IList ) Activator . CreateInstance ( constructedListType ) ;
889
+ if ( len >= 0 )
890
+ {
891
+ constructorArgs = new object [ ] { len } ;
892
+ }
893
+ else
894
+ {
895
+ // for the sequences, that explicitly deny calling __len__()
896
+ Exceptions . Clear ( ) ;
897
+ }
895
898
}
899
+ // CreateInstance can throw even if MakeGenericType succeeded.
900
+ // See https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance#System_Activator_CreateInstance_System_Type_
901
+ list = ( IList ) Activator . CreateInstance ( constructedListType , args : constructorArgs ) ;
896
902
}
897
903
catch ( Exception e )
898
904
{