Skip to content

Commit 2857071

Browse files
authored
Merge pull request #756 from smourier/master
Inheritance not working with non-abstract base methods
2 parents e5d299e + 2fff248 commit 2857071

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
4343
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
4444
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
45+
- Simon Mourier ([@smourier](https://github.com/smourier))
4546
- Viktoria Kovescses ([@vkovec](https://github.com/vkovec))
4647
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
4748
- Virgil Dupras ([@hsoft](https://github.com/hsoft))

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
4141
- Fixed errors breaking .NET Remoting on method invoke ([#276][i276])
4242
- Fixed PyObject.GetHashCode ([#676][i676])
4343
- Fix memory leaks due to spurious handle incrementation ([#691][i691])
44+
- Fix inheritance of non-abstract base methods ([#755][i755])
4445

4546

4647
## [2.3.0][] - 2017-03-11
@@ -691,3 +692,4 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
691692
[p625]: https://github.com/pythonnet/pythonnet/pull/625
692693
[i131]: https://github.com/pythonnet/pythonnet/issues/131
693694
[p531]: https://github.com/pythonnet/pythonnet/pull/531
695+
[i755]: https://github.com/pythonnet/pythonnet/pull/755

src/runtime/methodbinder.cs

+13
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ internal class MethodSorter : IComparer
601601
{
602602
int IComparer.Compare(object m1, object m2)
603603
{
604+
var me1 = (MethodBase)m1;
605+
var me2 = (MethodBase)m2;
606+
if (me1.DeclaringType != me2.DeclaringType)
607+
{
608+
// m2's type derives from m1's type, favor m2
609+
if (me1.DeclaringType.IsAssignableFrom(me2.DeclaringType))
610+
return 1;
611+
612+
// m1's type derives from m2's type, favor m1
613+
if (me2.DeclaringType.IsAssignableFrom(me1.DeclaringType))
614+
return -1;
615+
}
616+
604617
int p1 = MethodBinder.GetPrecedence((MethodBase)m1);
605618
int p2 = MethodBinder.GetPrecedence((MethodBase)m2);
606619
if (p1 < p2)

src/testing/InheritanceTest.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace Python.Test
4+
{
5+
public class BaseClass
6+
{
7+
public bool IsBase() => true;
8+
}
9+
10+
public class DerivedClass : BaseClass
11+
{
12+
public new bool IsBase() => false;
13+
}
14+
}

src/testing/Python.Test.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -83,6 +83,7 @@
8383
<Compile Include="generictest.cs" />
8484
<Compile Include="globaltest.cs" />
8585
<Compile Include="indexertest.cs" />
86+
<Compile Include="InheritanceTest.cs" />
8687
<Compile Include="interfacetest.cs" />
8788
<Compile Include="methodtest.cs" />
8889
<Compile Include="moduletest.cs" />
@@ -110,4 +111,4 @@
110111
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(SolutionDir)\src\tests\fixtures" />
111112
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
112113
</Target>
113-
</Project>
114+
</Project>

src/tests/test_class.py

+11
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,14 @@ def PyCallback(self, self2):
281281
testobj.DoCallback()
282282
assert testobj.PyCallbackWasCalled
283283
assert testobj.SameReference
284+
285+
286+
def test_method_inheritance():
287+
"""Ensure that we call the overridden method instead of the one provided in
288+
the base class."""
289+
290+
base = Test.BaseClass()
291+
derived = Test.DerivedClass()
292+
293+
assert base.IsBase() == True
294+
assert derived.IsBase() == False

0 commit comments

Comments
 (0)