Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ed94f6

Browse files
committed
Fixed issue calling base-base class implementation of methods.
1 parent 9eaf35f commit 9ed94f6

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

src/python_tests_runner/PythonTestRunner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static IEnumerable<string[]> PythonTestCases()
4242
yield return new[] { "test_subclass", "test_class_with_attributes" };
4343
yield return new[] { "test_subclass", "test_class_with_advanced_attribute" };
4444
yield return new[] { "test_subclass", "test_more_subclasses" };
45+
yield return new[] { "test_subclass", "test_more_subclasses2" };
4546
yield return new[] { "test_subclass", "abstract_test" };
4647
}
4748

src/runtime/Types/ClassDerived.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -522,23 +522,26 @@ private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuild
522522
string? baseMethodName = null;
523523
if (!method.IsAbstract)
524524
{
525-
baseMethodName = "_" + baseType.Name + "__" + method.Name;
526-
MethodBuilder baseMethodBuilder = typeBuilder.DefineMethod(baseMethodName,
527-
MethodAttributes.Public |
528-
MethodAttributes.Final |
529-
MethodAttributes.HideBySig,
530-
method.ReturnType,
531-
parameterTypes);
532-
533-
// emit the assembly for calling the original method using call instead of callvirt
534-
ILGenerator baseIl = baseMethodBuilder.GetILGenerator();
535-
baseIl.Emit(OpCodes.Ldarg_0);
536-
for (var i = 0; i < parameters.Length; ++i)
525+
baseMethodName = "_" + method.DeclaringType.Name + "__" + method.Name;
526+
if (baseType.GetMethod(baseMethodName) == null)
537527
{
538-
baseIl.Emit(OpCodes.Ldarg, i + 1);
528+
MethodBuilder baseMethodBuilder = typeBuilder.DefineMethod(baseMethodName,
529+
MethodAttributes.Public |
530+
MethodAttributes.Final |
531+
MethodAttributes.HideBySig,
532+
method.ReturnType,
533+
parameterTypes);
534+
535+
// emit the assembly for calling the original method using call instead of callvirt
536+
ILGenerator baseIl = baseMethodBuilder.GetILGenerator();
537+
baseIl.Emit(OpCodes.Ldarg_0);
538+
for (var i = 0; i < parameters.Length; ++i)
539+
{
540+
baseIl.Emit(OpCodes.Ldarg, i + 1);
541+
}
542+
baseIl.Emit(OpCodes.Call, method);
543+
baseIl.Emit(OpCodes.Ret);
539544
}
540-
baseIl.Emit(OpCodes.Call, method);
541-
baseIl.Emit(OpCodes.Ret);
542545
}
543546

544547
// override the original method with a new one that dispatches to python

src/testing/subclasstest.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,29 @@ public TestAttributeAttribute(int x, int y, string z = "x")
151151
}
152152
}
153153

154+
public abstract class SimpleClassBase
155+
{
156+
private int counter;
157+
public virtual int IncrementThing()
158+
{
159+
return counter++;
160+
}
154161

155-
public class SimpleClass
162+
}
163+
164+
public abstract class SimpleClass : SimpleClassBase
156165
{
157166
public bool Initialized;
158167

159168
public SimpleClass()
160169
{
161170
Initialized = true;
162171
}
163-
private int counter = 0;
164-
public virtual int IncrementThing()
172+
173+
public int CallIncrementThing()
165174
{
166-
return ++counter;
175+
var x = IncrementThing();
176+
return x;
167177
}
168178

169179
public static void TestObject(object obj)

tests/test_subclass.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def __init__(self):
396396
SimpleClass.Pause();
397397
super().__init__()
398398
def IncrementThing(self):
399+
super().IncrementThing()
399400
return 6;
400401
SimpleClass.TestOnType(SubClass0)
401402
SimpleClass.TestOnType(SubClass1)
@@ -465,3 +466,26 @@ class Derived(BaseClass):
465466

466467
import gc
467468
gc.collect()
469+
def test_more_subclasses2():
470+
import clr
471+
class SubClass50(SimpleClass):
472+
def __init__(self):
473+
super().__init__()
474+
def IncrementThing(self):
475+
return super().IncrementThing()
476+
477+
@clr.attribute(DebuggerDisplay("X"))
478+
479+
class SubClass51(SubClass50):
480+
__namespace__ = "TestModule"
481+
def __init__(self):
482+
super().__init__()
483+
484+
def IncrementThing(self):
485+
return super().IncrementThing() + 10
486+
x = SubClass51()
487+
print(x.CallIncrementThing())
488+
print(x.CallIncrementThing())
489+
print(x.CallIncrementThing())
490+
491+

0 commit comments

Comments
 (0)