Repository navigation
Run a .NET constructor once when it is instantiated from Python #166
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
base: master
Are you sure you want to change the base?
Changes from all commits
366886b
5757cec
6089cc0
2692e63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1107,6 +1107,37 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a | |
| return default; | ||
| } | ||
|
|
||
| if (binding.info is ConstructorInfo && ManagedType.GetManagedObject(inst) is CLRObject co) | ||
| { | ||
| if (co.awaitingCtor) | ||
| { | ||
| // tp_new suppressed finalization until a constructor runs; register it before the | ||
| // constructor body, as for a .NET 'new', so the object is finalized even if it throws. | ||
| co.awaitingCtor = false; | ||
| GC.ReRegisterForFinalize(co.inst); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is now the only thing that makes a .NET object constructed from Python finalizable, because
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pinned in 2692e63. |
||
| } | ||
| else if (co.ctorRanInNew != null) | ||
| { | ||
| var ranInNew = co.ctorRanInNew; | ||
| co.ctorRanInNew = null; | ||
| // A zero-argument base __init__() call that binds the constructor tp_new already ran on this | ||
| // Python subclass instance must not run it twice. A __namespace__ subclass's generated type | ||
| // mirrors each base constructor and chains to it, so its parameterless constructor has | ||
| // already run the base type's parameterless one. | ||
| var ranInNewAlready = binding.info == ranInNew | ||
| || co.inst is IPythonDerivedType | ||
| && binding.info.DeclaringType == ranInNew.DeclaringType.BaseType | ||
| && binding.args.Length == 0 | ||
| && ranInNew.GetParameters().Length == 0; | ||
| if (ranInNewAlready | ||
| && Runtime.PyTuple_Size(args) == 0 | ||
| && (kw == null || Runtime.PyDict_Size(kw) == 0)) | ||
| { | ||
| return new NewReference(Runtime.PyNone); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (allow_threads) | ||
| { | ||
| ts = PythonEngine.BeginAllowThreads(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.