Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Make it possible to use inherited indexers#1248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromdanabr:inherited-indexers
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/runtime/classmanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/testing/indexertest.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/tests/test_indexer.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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"

[8]ページ先頭

©2009-2025 Movatter.jp