pythonnet / pythonnet/pythonnet

StackOverflowException on calling super().method()

Open
#2,574 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
5.5k
Forks
780
PR merge metrics
No merged PRs in 30d

Description

Environment
  • Python.NET version: 3.0.5
  • Tested on two configurations:
    • Windows 10, Python 3.12.4, .NET Framework
    • Windows 10, Python 3.12.4, .NET 9.0.201
Details

Code to reproduce the issue is below.

.NET library code:

namespace TestLib
{
    public class BaseClass
    {
        public virtual string Foo()
        {
            return "Foo";
        }

        public virtual string Bar()
        {
            return "Bar";
        }
    }

    public class DerivedClass : BaseClass
    {
        public override string Foo()
        {
            return "DerivedFoo";
        }
    }
}

Python code:

from pathlib import Path

import clr

clr.AddReference(str(Path(__file__).parent / 'TestLib.dll'))

from TestLib import BaseClass, DerivedClass


class TestClass(DerivedClass):
    __namespace__ = 'CrlTest'

    def Foo(self):
        return 'Test' + super().Foo()

    def Bar(self):
        # This work fine
        # return 'Test' + DerivedClass.Bar(self)
        # This calls TestClass::Bar for some reason
        return 'Test' + super().Bar()


if __name__ == '__main__':
    stc = TestClass()
    print('Foo() -> ' + stc.Foo())
    print('Bar() -> ' + stc.Bar())

Python script output:

Foo() -> TestDerivedFoo

Process is terminated due to StackOverflowException.

For some reason calling super().Bar() results in calling TestClass.Bar(self) instead of DerivedClass.Bar(self), which in the end results in infinite recursion. Calling DerivedClass.Bar(self) directly works as expected.

It seem to be related to the fact that the method you try to override in Python is not defined in the immediate parent class, since Foo in the example works fine.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C# and Python reproducer in the issue and verify the differing behavior of super().Foo() and super().Bar(). Trace Python.NET's dispatch for an overridden method missing from the immediate parent, then confirm that super().Bar() reaches DerivedClass.Bar(self) without recursion and that the script completes with the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.