Skip to content

Inheritance not working with non-abstract base methods #756

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

Merged
merged 4 commits into from
Oct 29, 2018
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 AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
- Simon Mourier ([@smourier](https://github.com/smourier))
- Viktoria Kovescses ([@vkovec](https://github.com/vkovec))
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed errors breaking .NET Remoting on method invoke ([#276][i276])
- Fixed PyObject.GetHashCode ([#676][i676])
- Fix memory leaks due to spurious handle incrementation ([#691][i691])
- Fix inheritance of non-abstract base methods ([#755][i755])


## [2.3.0][] - 2017-03-11
Expand Down Expand Up @@ -691,3 +692,4 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
[p625]: https://github.com/pythonnet/pythonnet/pull/625
[i131]: https://github.com/pythonnet/pythonnet/issues/131
[p531]: https://github.com/pythonnet/pythonnet/pull/531
[i755]: https://github.com/pythonnet/pythonnet/pull/755
13 changes: 13 additions & 0 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,19 @@ internal class MethodSorter : IComparer
{
int IComparer.Compare(object m1, object m2)
{
var me1 = (MethodBase)m1;
var me2 = (MethodBase)m2;
if (me1.DeclaringType != me2.DeclaringType)
{
// m2's type derives from m1's type, favor m2
if (me1.DeclaringType.IsAssignableFrom(me2.DeclaringType))
return 1;

// m1's type derives from m2's type, favor m1
if (me2.DeclaringType.IsAssignableFrom(me1.DeclaringType))
return -1;
}

int p1 = MethodBinder.GetPrecedence((MethodBase)m1);
int p2 = MethodBinder.GetPrecedence((MethodBase)m2);
if (p1 < p2)
Expand Down
14 changes: 14 additions & 0 deletions src/testing/InheritanceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Python.Test
{
public class BaseClass
{
public bool IsBase() => true;
}

public class DerivedClass : BaseClass
{
public new bool IsBase() => false;
}
}
5 changes: 3 additions & 2 deletions src/testing/Python.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -83,6 +83,7 @@
<Compile Include="generictest.cs" />
<Compile Include="globaltest.cs" />
<Compile Include="indexertest.cs" />
<Compile Include="InheritanceTest.cs" />
<Compile Include="interfacetest.cs" />
<Compile Include="methodtest.cs" />
<Compile Include="moduletest.cs" />
Expand Down Expand Up @@ -110,4 +111,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(SolutionDir)\src\tests\fixtures" />
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
</Target>
</Project>
</Project>
11 changes: 11 additions & 0 deletions src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,14 @@ def PyCallback(self, self2):
testobj.DoCallback()
assert testobj.PyCallbackWasCalled
assert testobj.SameReference


def test_method_inheritance():
"""Ensure that we call the overridden method instead of the one provided in
the base class."""

base = Test.BaseClass()
derived = Test.DerivedClass()

assert base.IsBase() == True
assert derived.IsBase() == False