Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Two objects created when calling C# constructor #2555

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
jhonabreul opened this issue Feb 26, 2025 · 0 comments
Open

Two objects created when calling C# constructor #2555

jhonabreul opened this issue Feb 26, 2025 · 0 comments

Comments

@jhonabreul
Copy link

jhonabreul commented Feb 26, 2025

Environment

  • Pythonnet version: 3.0.5
  • Python version: 3.11
  • Operating System: Windows 11
  • .NET Runtime: 9.0

Details

When instantiating a C# class in a Python module, two C# objects are created:

C# class:

class SomeClass
{
  public SomeClass()
  { 
  }

  public SomeClass(int arg1, string arg2)
  {
  }
}

Python module

obj = SomeClass()

The default constructor SomeClass() is called twice in MetaType.tp_call , first by using NewReference obj = NativeCall.Call_3(tp_new, tp, args, kw); and then by type.Init(obj.Borrow(), args, kw).

If Python does this:

obj = SomeClass(1,  "str")

Then using NewReference obj = NativeCall.Call_3(tp_new, tp, args, kw); calls the default constructor and then type.Init(obj.Borrow(), args, kw) calls the one with arguments.

Is this an expected behaviour? Is there a workaround or fix for this? I am working with a case where I really don't whant the constructor to be called twice.

Is this related to #495?

Thank you in advance.

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