From 3f8def796af3827b8e5fde0aebd6bf948b0165ac Mon Sep 17 00:00:00 2001 From: Daniel Abrahamsson Date: Tue, 6 Oct 2020 08:46:52 +0200 Subject: [PATCH 1/2] Make `len` work for `ICollection<>` interface objects --- CHANGELOG.md | 1 + src/runtime/slots/mp_length.cs | 4 ++++ src/tests/test_mp_length.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bdf5e32b..d27c136a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ details about the cause of the failure - Fix non-delegate types incorrectly appearing as callable. - Indexers can now be used with interface objects - Fixed a bug where indexers could not be used if they were inherited +- Made it possible to use `__len__` also on `ICollection<>` interface objects ## [2.5.0][] - 2020-06-14 diff --git a/src/runtime/slots/mp_length.cs b/src/runtime/slots/mp_length.cs index 42448a2e9..11278ca8f 100644 --- a/src/runtime/slots/mp_length.cs +++ b/src/runtime/slots/mp_length.cs @@ -36,6 +36,10 @@ public static bool CanAssgin(Type clrType) { return true; } + if (clrType.IsInterface && clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(ICollection<>)) + { + return true; + } return false; } diff --git a/src/tests/test_mp_length.py b/src/tests/test_mp_length.py index c96ac77d1..e86fff288 100644 --- a/src/tests/test_mp_length.py +++ b/src/tests/test_mp_length.py @@ -47,3 +47,17 @@ def test_custom_generic_collection_explicit___len__(): s.Add(1) s.Add(10) assert len(s) == 2 + +def test_len_through_interface_generic(): + """Test __len__ for ICollection""" + import System.Collections.Generic + l = System.Collections.Generic.List[int]() + coll = System.Collections.Generic.ICollection[int](l) + assert len(coll) == 0 + +def test_len_through_interface(): + """Test __len__ for ICollection""" + import System.Collections + l = System.Collections.ArrayList() + coll = System.Collections.ICollection(l) + assert len(coll) == 0 From 3c267c62ba5dd5d29edd87c65e44cda2c9bdc13f Mon Sep 17 00:00:00 2001 From: Daniel Abrahamsson Date: Fri, 9 Oct 2020 07:14:36 +0200 Subject: [PATCH 2/2] Fix typo --- src/runtime/slots/mp_length.cs | 2 +- src/runtime/typemanager.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/slots/mp_length.cs b/src/runtime/slots/mp_length.cs index 11278ca8f..a13c7b6f8 100644 --- a/src/runtime/slots/mp_length.cs +++ b/src/runtime/slots/mp_length.cs @@ -26,7 +26,7 @@ public static MethodInfo Method } } - public static bool CanAssgin(Type clrType) + public static bool CanAssign(Type clrType) { if (typeof(ICollection).IsAssignableFrom(clrType)) { diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index c00247ca4..78c34a027 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -225,7 +225,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType) InitializeSlots(type, impl.GetType(), slotsHolder); if (Marshal.ReadIntPtr(type, TypeOffset.mp_length) == IntPtr.Zero - && mp_length_slot.CanAssgin(clrType)) + && mp_length_slot.CanAssign(clrType)) { InitializeSlot(type, TypeOffset.mp_length, mp_length_slot.Method, slotsHolder); } 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