pythonnet / pythonnet/pythonnet

StackOverflowException on calling super().method()

Aperta
#2,574 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
C#
Stelle
5.5k
Fork
780
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il reproducer C# e Python nell’issue e verifica il comportamento diverso di super().Foo() e super().Bar(). Traccia il dispatch di Python.NET per un metodo sovrascritto assente dal genitore immediato, quindi conferma che super().Bar() raggiunga DerivedClass.Bar(self) senza ricorsione e che lo script termini con l’output previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, python
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.