Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ details about the cause of the failure
- Fix incorrect choice of method to invoke when using keyword arguments.
- 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

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

Expand Down
19 changes: 19 additions & 0 deletions 19 src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,25 @@ private static ClassInfo GetClassInfo(Type type)
ci.members[name] = ob;
}

if (ci.indexer == null && type.IsClass)
{
// Indexer may be inherited.
var parent = type.BaseType;
while (parent != null && ci.indexer == null)
{
foreach (var prop in parent.GetProperties()) {
var args = prop.GetIndexParameters();
if (args.GetLength(0) > 0)
{
ci.indexer = new Indexer();
ci.indexer.AddProperty(prop);
break;
}
}
parent = parent.BaseType;
}
}

return ci;
}
}
Expand Down
25 changes: 25 additions & 0 deletions 25 src/testing/indexertest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,29 @@ public MultiDefaultKeyIndexerTest() : base()
}
}
}

public class PublicInheritedIndexerTest : PublicIndexerTest { }

public class ProtectedInheritedIndexerTest : ProtectedIndexerTest { }

public class PrivateInheritedIndexerTest : ProtectedIndexerTest { }

public class InternalInheritedIndexerTest : InternalIndexerTest { }

public interface IIndexer
{
string this[int index] { get; set; }
}

public interface IInheritedIndexer : IIndexer { }

public class InterfaceInheritedIndexerTest :IndexerBase, IInheritedIndexer {
private System.Collections.Generic.IDictionary<int, string> d = new System.Collections.Generic.Dictionary<int, string>();

public string this[int index]
{
get { return GetValue(index); }
set { t[index] = value; }
}
}
}
32 changes: 32 additions & 0 deletions 32 src/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,35 @@ def test_using_indexer_on_object_without_indexer():

with pytest.raises(TypeError):
o[0] = 1


def test_inherited_indexer():
"""Test that inherited indexers are accessible"""
from Python.Test import PublicInheritedIndexerTest
from Python.Test import ProtectedInheritedIndexerTest
from Python.Test import PrivateInheritedIndexerTest
from Python.Test import InternalInheritedIndexerTest

pub = PublicInheritedIndexerTest()
pub[0] = "zero"
assert pub[0] == "zero"

def assert_no_indexer(obj):
with pytest.raises(TypeError):
obj[0]
with pytest.raises(TypeError):
obj[0] = "zero"

assert_no_indexer(PrivateInheritedIndexerTest)
assert_no_indexer(ProtectedInheritedIndexerTest)
assert_no_indexer(InternalInheritedIndexerTest)


def test_inherited_indexer_interface():
"""Test that indexers inherited from other interfaces are accessible"""
from Python.Test import InterfaceInheritedIndexerTest, IInheritedIndexer

impl = InterfaceInheritedIndexerTest()
ifc = IInheritedIndexer(impl)
ifc[0] = "zero"
assert ifc[0] == "zero"
Morty Proxy This is a proxified and sanitized view of the page, visit original site.