Skip to content

StackOverflowException on calling super().method() #2574

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

Open
Eswcvlad opened this issue Apr 9, 2025 · 0 comments
Open

StackOverflowException on calling super().method() #2574

Eswcvlad opened this issue Apr 9, 2025 · 0 comments

Comments

@Eswcvlad
Copy link

Eswcvlad commented Apr 9, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant