Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d00e4c

Browse files
authored
Merge branch 'master' into soft-shutdown
2 parents 3203457 + c79be84 commit 8d00e4c

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ details about the cause of the failure
3030
- Fix incorrect choice of method to invoke when using keyword arguments.
3131
- Fix non-delegate types incorrectly appearing as callable.
3232
- Indexers can now be used with interface objects
33+
- Fixed a bug where indexers could not be used if they were inherited
3334

3435
## [2.5.0][] - 2020-06-14
3536

src/runtime/classmanager.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,25 @@ private static ClassInfo GetClassInfo(Type type)
461461
ci.members[name] = ob;
462462
}
463463

464+
if (ci.indexer == null && type.IsClass)
465+
{
466+
// Indexer may be inherited.
467+
var parent = type.BaseType;
468+
while (parent != null && ci.indexer == null)
469+
{
470+
foreach (var prop in parent.GetProperties()) {
471+
var args = prop.GetIndexParameters();
472+
if (args.GetLength(0) > 0)
473+
{
474+
ci.indexer = new Indexer();
475+
ci.indexer.AddProperty(prop);
476+
break;
477+
}
478+
}
479+
parent = parent.BaseType;
480+
}
481+
}
482+
464483
return ci;
465484
}
466485

src/testing/indexertest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,29 @@ public MultiDefaultKeyIndexerTest() : base()
411411
}
412412
}
413413
}
414+
415+
public class PublicInheritedIndexerTest : PublicIndexerTest { }
416+
417+
public class ProtectedInheritedIndexerTest : ProtectedIndexerTest { }
418+
419+
public class PrivateInheritedIndexerTest : ProtectedIndexerTest { }
420+
421+
public class InternalInheritedIndexerTest : InternalIndexerTest { }
422+
423+
public interface IIndexer
424+
{
425+
string this[int index] { get; set; }
426+
}
427+
428+
public interface IInheritedIndexer : IIndexer { }
429+
430+
public class InterfaceInheritedIndexerTest :IndexerBase, IInheritedIndexer {
431+
private System.Collections.Generic.IDictionary<int, string> d = new System.Collections.Generic.Dictionary<int, string>();
432+
433+
public string this[int index]
434+
{
435+
get { return GetValue(index); }
436+
set { t[index] = value; }
437+
}
438+
}
414439
}

src/tests/test_indexer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,35 @@ def test_using_indexer_on_object_without_indexer():
614614

615615
with pytest.raises(TypeError):
616616
o[0] = 1
617+
618+
619+
def test_inherited_indexer():
620+
"""Test that inherited indexers are accessible"""
621+
from Python.Test import PublicInheritedIndexerTest
622+
from Python.Test import ProtectedInheritedIndexerTest
623+
from Python.Test import PrivateInheritedIndexerTest
624+
from Python.Test import InternalInheritedIndexerTest
625+
626+
pub = PublicInheritedIndexerTest()
627+
pub[0] = "zero"
628+
assert pub[0] == "zero"
629+
630+
def assert_no_indexer(obj):
631+
with pytest.raises(TypeError):
632+
obj[0]
633+
with pytest.raises(TypeError):
634+
obj[0] = "zero"
635+
636+
assert_no_indexer(PrivateInheritedIndexerTest)
637+
assert_no_indexer(ProtectedInheritedIndexerTest)
638+
assert_no_indexer(InternalInheritedIndexerTest)
639+
640+
641+
def test_inherited_indexer_interface():
642+
"""Test that indexers inherited from other interfaces are accessible"""
643+
from Python.Test import InterfaceInheritedIndexerTest, IInheritedIndexer
644+
645+
impl = InterfaceInheritedIndexerTest()
646+
ifc = IInheritedIndexer(impl)
647+
ifc[0] = "zero"
648+
assert ifc[0] == "zero"

0 commit comments

Comments
 (0)