| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,7 @@ details about the cause of the failure | |||
| 30 | 30 | - Fix incorrect choice of method to invoke when using keyword arguments. | |
| 31 | 31 | - Fix non-delegate types incorrectly appearing as callable. | |
| 32 | 32 | - Indexers can now be used with interface objects | |
| 33 | + - Fixed a bug where indexers could not be used if they were inherited | ||
| 33 | 34 | ||
| 34 | 35 | ## [2.5.0][] - 2020-06-14 | |
| 35 | 36 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -461,6 +461,25 @@ private static ClassInfo GetClassInfo(Type type) | |||
| 461 | 461 | ci.members[name] = ob; | |
| 462 | 462 | } | |
| 463 | 463 | ||
| 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 | + | ||
| 464 | 483 | return ci; | |
| 465 | 484 | } | |
| 466 | 485 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -411,4 +411,29 @@ public MultiDefaultKeyIndexerTest() : base() | |||
| 411 | 411 | } | |
| 412 | 412 | } | |
| 413 | 413 | } | |
| 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 | + } | ||
| 414 | 439 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -614,3 +614,35 @@ def test_using_indexer_on_object_without_indexer(): | |||
| 614 | 614 | ||
| 615 | 615 | with pytest.raises(TypeError): | |
| 616 | 616 | 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" | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments