diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 7cee0890c..936f8b248 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -882,17 +882,26 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s // See https://docs.microsoft.com/en-us/dotnet/api/system.type.makegenerictype#System_Type_MakeGenericType_System_Type var constructedListType = typeof(List<>).MakeGenericType(elementType); bool IsSeqObj = Runtime.PySequence_Check(value); + object[] constructorArgs = Array.Empty(); if (IsSeqObj) { var len = Runtime.PySequence_Size(value); - list = (IList)Activator.CreateInstance(constructedListType, new Object[] { (int)len }); - } - else - { - // CreateInstance can throw even if MakeGenericType succeeded. - // See https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance#System_Activator_CreateInstance_System_Type_ - list = (IList)Activator.CreateInstance(constructedListType); + if (len >= 0) + { + if (len <= int.MaxValue) + { + constructorArgs = new object[] { (int)len }; + } + } + else + { + // for the sequences, that explicitly deny calling __len__() + Exceptions.Clear(); + } } + // CreateInstance can throw even if MakeGenericType succeeded. + // See https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance#System_Activator_CreateInstance_System_Type_ + list = (IList)Activator.CreateInstance(constructedListType, args: constructorArgs); } catch (Exception e) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy