Skip to content

Conversation

lostmsu
Copy link
Member

@lostmsu lostmsu commented Dec 30, 2021

What does this implement/fix? Explain your changes.

Was:
tp_new implementation would call .NET constructor and return a fully constructed object

Now:
Except for some special .NET types tp_new creates uninitialized .NET object, which is later initialized by calling uninitialized_inst.__init__(...).

__init__ is set in type dictionary to a MethodObject, that contains ConstructorInfo[] instead of MethodInfo[]

This allows Python to:

  1. freely override __init__ when deriving from .NET types
  2. when doing so call base __init__ (e.g. .NET constructor), and choose the overload as needed

Failing to call base __init__ in this scenario will lead to an undefined behavior.

Does this close any currently open issues?

fixes #238

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • Updated the CHANGELOG

@lostmsu lostmsu force-pushed the bugs/DefaultConstructorFallback branch from 1d8c1f4 to 25a94e4 Compare January 4, 2022 17:34
was: tp_new implementation would call .NET constructor and return a fully constructed object

now:
Except for some special .NET types tp_new creates uninitialized .NET object, which is later initialized by calling __init__.

__init__ is set using type dictionary to a MethodObject, that contains ConstructorInfo[] instead of MethodInfo[]

This allows Python to:
1) freely override __init__
2) when deriving from .NET types call base __init__ (e.g. .NET constructor), and choose the overload as needed

fixes pythonnet#238
@lostmsu lostmsu force-pushed the bugs/DefaultConstructorFallback branch from 25a94e4 to c091caf Compare January 4, 2022 17:38
@lostmsu lostmsu merged commit c4238d9 into pythonnet:master Jan 4, 2022
@lostmsu lostmsu deleted the bugs/DefaultConstructorFallback branch January 4, 2022 20:23
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

Successfully merging this pull request may close these issues.

Constructor failing argument matching, silently selects default constructor
1 participant