8000 Explicit interface implementations by danabr · Pull Request #1233 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Explicit interface implementations #1233

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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 AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Christoph Gohlke ([@cgohlke](https://github.com/cgohlke))
- Christopher Bremner ([@chrisjbremner](https://github.com/chrisjbremner))
- Christopher Pow ([@christopherpow](https://github.com/christopherpow))
- Daniel Abrahamsson ([@danabr](https://github.com/danabr))
- Daniel Fernandez ([@fdanny](https://github.com/fdanny))
- Daniel Santana ([@dgsantana](https://github.com/dgsantana))
- Dave Hirschfeld ([@dhirschfeld](https://github.com/dhirschfeld))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ details about the cause of the failure
- Fix incorrect dereference of wrapper object in `tp_repr`, which may result in a program crash
- Fix incorrect dereference in params array handling
- Fix `object[]` parameters taking precedence when should not in overload resolution
- Fix methods and properties of explictly implemented interfaces being invisible

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

Expand Down
43 changes: 20 additions & 23 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,32 +241,29 @@ private static ClassInfo GetClassInfo(Type type)
}
}

if (type.IsInterface)
// Interface inheritance seems to be a different animal:
// more contractual, less structural. Thus, a Type that
// represents an interface that inherits from another
// interface does not return the inherited interface's
// methods in GetMembers. For example ICollection inherits
// from IEnumerable, but ICollection's GetMemebers does not
// return GetEnumerator.
//
// Not sure if this is the correct way to fix this, but it
// seems to work. Thanks to Bruce Dodson for the fix.

Type[] inheritedInterfaces = type.GetInterfaces();

for (i = 0; i < inheritedInterfaces.Length; ++i)
{
// Interface inheritance seems to be a different animal:
// more contractual, less structural. Thus, a Type that
// represents an interface that inherits from another
// interface does not return the inherited interface's
// methods in GetMembers. For example ICollection inherits
// from IEnumerable, but ICollection's GetMemebers does not
// return GetEnumerator.
//
// Not sure if this is the correct way to fix this, but it
// seems to work. Thanks to Bruce Dodson for the fix.

Type[] inheritedInterfaces = type.GetInterfaces();

for (i = 0; i < inheritedInterfaces.Length; ++i)
Type inheritedType = inheritedInterfaces[i];
MemberInfo[] imembers = inheritedType.GetMembers(flags);
for (n = 0; n < imembers.Length; n++)
{
Type inheritedType = inheritedInterfaces[i];
MemberInfo[] imembers = inheritedType.GetMembers(flags);
for (n = 0; n < imembers.Length; n++)
m = imembers[n];
if (local[m.Name] == null)
{
m = imembers[n];
if (local[m.Name] == null)
{
items.Add(m);
}
items.Add(m);
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/testing/interfacetest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ public interface ISayHello2
string SayHello();
}

public class InterfaceTest : ISayHello1, ISayHello2
public interface IInterfaceResolutionTest
{
string TestMethod1();
string TestMethod2();

string TestProperty1 { get; }
string TestProperty2 { get; }
}

public class InterfaceTest : ISayHello1, ISayHello2, IInterfaceResolutionTest
{
public InterfaceTest()
{
Expand All @@ -43,6 +52,20 @@ string ISayHello2.SayHello()
return "hello 2";
}

public string TestMethod1()
{
return "TestMethod1";
}

string IInterfaceResolutionTest.TestMethod2()
{
return "TestMethod2";
}

public string TestProperty1 => "TestProperty1";

string IInterfaceResolutionTest.TestProperty2 => "TestProperty2";

public interface IPublic
{
}
Expand Down
16 changes: 16 additions & 0 deletions src/tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ def test_explicit_cast_to_interface():
assert i2.SayHello() == 'hello 2'
assert hasattr(i2, 'SayHello')
assert not hasattr(i2, 'HelloProperty')


def test_interface_method_and_property_lookup():
"""Test methods and properties in interfaces can be accessed"""
from Python.Test import InterfaceTest

ob = InterfaceTest()
assert hasattr(ob, 'TestMethod1')
assert ob.TestMethod1() == 'TestMethod1'
assert hasattr(ob, 'TestMethod2')
assert ob.TestMethod2() == 'TestMethod2'
assert hasattr(ob, 'TestProperty1')
assert ob.TestProperty1 == 'TestProperty1'
assert hasattr(ob, 'TestProperty2')
assert ob.TestProperty2 == 'TestProperty2'

0