Skip to content

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

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

Closed
testrunner123 opened this issue Sep 15, 2017 · 2 comments

Comments

@testrunner123
Copy link
Contributor

testrunner123 commented Sep 15, 2017

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.

@testrunner123 testrunner123 changed the title Python granchild of .NET class is not created correctly if "__namespace__" attribute is set Python grandchild of .NET class is not created correctly if "__namespace__" attribute is set Sep 15, 2017
@testrunner123
Copy link
Contributor Author

I have made a unit test for this on a branch. Does some one knows how it is designed to work?

@lostmsu
Copy link
Member

lostmsu commented Sep 17, 2022

This works as expected:

import System.Windows.Forms as WinForms

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

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

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

/ grp1 = PythonGroupBox()
PythonGroupBox.init
PythonGroupBox MyInit
/ grp2 = PythonGroupBoxDerived()
PythonGroupBox.init
PythonGroupBoxDerived MyInit

However, without explicitly redeclared __init__ in the PythonGroupBoxDerived base constructor is not called, for which I opened a separate issue: #1945

@lostmsu lostmsu closed this as completed Sep 17, 2022
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

2 participants