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

Commitc79be84

Browse files
danabrlostmsu
authored andcommitted
Make it possible to use inherited indexers
If a class A had indexer, and class B derived from it, Python.NET wouldnot consider class B to have any indexer.
1 parentf808166 commitc79be84

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
@@ -389,6 +389,25 @@ private static ClassInfo GetClassInfo(Type type)
389389
ci.members[name]=ob;
390390
}
391391

392+
if(ci.indexer==null&&type.IsClass)
393+
{
394+
// Indexer may be inherited.
395+
varparent=type.BaseType;
396+
while(parent!=null&&ci.indexer==null)
397+
{
398+
foreach(varpropinparent.GetProperties()){
399+
varargs=prop.GetIndexParameters();
400+
if(args.GetLength(0)>0)
401+
{
402+
ci.indexer=newIndexer();
403+
ci.indexer.AddProperty(prop);
404+
break;
405+
}
406+
}
407+
parent=parent.BaseType;
408+
}
409+
}
410+
392411
returnci;
393412
}
394413
}

‎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+
publicclassPublicInheritedIndexerTest:PublicIndexerTest{}
416+
417+
publicclassProtectedInheritedIndexerTest:ProtectedIndexerTest{}
418+
419+
publicclassPrivateInheritedIndexerTest:ProtectedIndexerTest{}
420+
421+
publicclassInternalInheritedIndexerTest:InternalIndexerTest{}
422+
423+
publicinterfaceIIndexer
424+
{
425+
stringthis[intindex]{get;set;}
426+
}
427+
428+
publicinterfaceIInheritedIndexer:IIndexer{}
429+
430+
publicclassInterfaceInheritedIndexerTest:IndexerBase,IInheritedIndexer{
431+
privateSystem.Collections.Generic.IDictionary<int,string>d=newSystem.Collections.Generic.Dictionary<int,string>();
432+
433+
publicstringthis[intindex]
434+
{
435+
get{returnGetValue(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
withpytest.raises(TypeError):
616616
o[0]=1
617+
618+
619+
deftest_inherited_indexer():
620+
"""Test that inherited indexers are accessible"""
621+
fromPython.TestimportPublicInheritedIndexerTest
622+
fromPython.TestimportProtectedInheritedIndexerTest
623+
fromPython.TestimportPrivateInheritedIndexerTest
624+
fromPython.TestimportInternalInheritedIndexerTest
625+
626+
pub=PublicInheritedIndexerTest()
627+
pub[0]="zero"
628+
assertpub[0]=="zero"
629+
630+
defassert_no_indexer(obj):
631+
withpytest.raises(TypeError):
632+
obj[0]
633+
withpytest.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+
deftest_inherited_indexer_interface():
642+
"""Test that indexers inherited from other interfaces are accessible"""
643+
fromPython.TestimportInterfaceInheritedIndexerTest,IInheritedIndexer
644+
645+
impl=InterfaceInheritedIndexerTest()
646+
ifc=IInheritedIndexer(impl)
647+
ifc[0]="zero"
648+
assertifc[0]=="zero"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp