Skip to content

Python grandchild of .NET class is not created correctly if "__namespace__" attribute is set #544

Closed
@testrunner123

Description

@testrunner123

Environment

Details

  • Describe what you were trying to get done.

I am trying to use python child of .NET class in python grandchild.

class A:
  def __init__(self):
      self.foo()
  def foo(self): print('A.foo')

class B(A):
  def foo(self): print('B.foo')

a = A()
b = B()

works like expected and prints:

A.foo
B.foo

Consider Python classes constructed with pythonnet:

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

works fine and prints:

PythonGroupBox.init
PythonGroupBox MyInit
PPythonGroupBox.init
PythonGroupBoxDerived MyInit

Now the same with "namespace":

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    __namespace__ = "System.Windows.Forms"
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

The output is:

PythonGroupBox.init
PythonGroupBox MyInit
PythonGroupBox.init
PythonGroupBox MyInit

Error (1), because MyInit Method didn't get overloaded !!!

One more try:

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    __namespace__ = "System.Windows.Forms"
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    __namespace__ = "System.Windows.Forms"
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

Output gets really weird:

PythonGroupBox.init
PythonGroupBox MyInit

Error (2): during construction of PythonGroupBoxDerived the init() method was not called at all !!!

Important: this issue has nothing to do with issue #503: if I undo that fix than constructors get called twice, but errors described here persist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions