forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Reworked ManagedType to not keep python references to self
#2
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
Merged
lostmsu
merged 45 commits into
precise-API-references
from
ManagedType-DontKeepBorrowedRefs
Dec 9, 2021
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
7ed0c7a
WIP
lostmsu d6a853f
avoid generating and handling useless SerializationException when May…
lostmsu b0c25c1
finalizer does not attempt to finalize objects when runtime is shut down
lostmsu 5ca474a
PyType Dict and MRO properties to assist debugging
lostmsu 48078b3
WIP 2
lostmsu a624dd8
fixed PyObject disposal crashing when runtime is still finalizing
lostmsu e7ab071
arrays: use 64 bit indexing, and avoid first chance .NET exceptions o…
lostmsu cbe1dd2
refactored conditional ClassBase slot initialization
lostmsu d5f1c48
removed DisposePythonWrappersForClrTypes
lostmsu 74d87c5
simplified outdated condition in ClassBase.tp_clear
lostmsu 82d6c33
sprinkled a few DebuggerHidden to make debugging easier
lostmsu eeebcd7
fixed derived classes not inheriting slots correctly
lostmsu 8ee8d3d
remove unused TypeManager._slotImpls
lostmsu 1a4ada7
fixed TestRuntime not building in Release mode
lostmsu a610aa3
can't really clear managed references to Python objects from ManagedT…
lostmsu 03f32cb
PythonException is serializable
lostmsu b1c9f5b
EventObject no longer used for static events. EventBinding is constru…
lostmsu cb4bb9a
use a special class to stub .NET types that no longer exist after a d…
lostmsu 652f946
make EventHandlerCollection serializable
lostmsu 84db670
fixed MaybeMemberInfo always failing for properties
lostmsu 56fafe3
fixed construct_removed_class domain reload test case
lostmsu d33dcdd
domain reload test runner can run test by index
lostmsu b737e10
minor docs change
lostmsu d3e4fba
assert check in GetUnmanagedBaseType for null base
lostmsu e003e12
PythonEngine .Exec and .Eval no longer work with raw pointers
lostmsu d0a6f44
a few annotation to ease debugging
lostmsu e31f7ba
ensure Python types continue to exist when registered decoders for th…
lostmsu 48c0dfc
GC-related WIP
lostmsu cb58147
merge latest changes from upstream
lostmsu 2fdbf0e
added TraceAlloc solution configuration
lostmsu a8ef06c
fixed sending PyObject across domain boundary
lostmsu 7167229
fixed accidental premature disposal of Runtime.PyNone
lostmsu ab11fa2
made freeing GCHandles more robust
lostmsu 7a4daeb
removed bad assert in generated constructor for derived classes
lostmsu e422367
fixed __pyobj__ access
lostmsu a74ea86
minor
lostmsu 0325a8c
fixed Python derived types trying to double-free GCHandle when collec…
lostmsu 85fab3b
reinstate collection assert on shutdown from Python
lostmsu 932fce2
fixed crash when Python derived class instances survive past early sh…
lostmsu c2e207a
delay nulling GC handles of reflected instances until the last moment…
lostmsu c8f0f09
fixed assert in XDecref in case _Py_IsFinalizing is not present
lostmsu e269cf0
when initialized from Python, reset slots implemented in CLR: CLR mig…
lostmsu d7d5cb7
fixed minor warnings
lostmsu d6edace
fixed line endings in intern_.cs
lostmsu a86994f
use NonCopyableAnalyzer 0.7.0-m05
lostmsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Runtime.Serialization; | ||
| using System.Security.Permissions; | ||
|
|
||
| namespace Python.Runtime; | ||
|
|
||
| [Serializable] | ||
| internal class EventHandlerCollection: Dictionary<object, List<Handler>> | ||
| { | ||
| readonly EventInfo info; | ||
| public EventHandlerCollection(EventInfo @event) | ||
| { | ||
| info = @event; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Register a new Python object event handler with the event. | ||
| /// </summary> | ||
| internal bool AddEventHandler(BorrowedReference target, PyObject handler) | ||
| { | ||
| object? obj = null; | ||
| if (target != null) | ||
| { | ||
| var co = (CLRObject)ManagedType.GetManagedObject(target)!; | ||
| obj = co.inst; | ||
| } | ||
|
|
||
| // Create a true delegate instance of the appropriate type to | ||
| // wrap the Python handler. Note that wrapper delegate creation | ||
| // always succeeds, though calling the wrapper may fail. | ||
| Type type = info.EventHandlerType; | ||
| Delegate d = PythonEngine.DelegateManager.GetDelegate(type, handler); | ||
|
|
||
| // Now register the handler in a mapping from instance to pairs | ||
| // of (handler hash, delegate) so we can lookup to remove later. | ||
| object key = obj ?? info.ReflectedType; | ||
| if (!TryGetValue(key, out var list)) | ||
| { | ||
| list = new List<Handler>(); | ||
| this[key] = list; | ||
| } | ||
| list.Add(new Handler(Runtime.PyObject_Hash(handler), d)); | ||
|
|
||
| // Note that AddEventHandler helper only works for public events, | ||
| // so we have to get the underlying add method explicitly. | ||
| object[] args = { d }; | ||
| MethodInfo mi = info.GetAddMethod(true); | ||
| mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Remove the given Python object event handler. | ||
| /// </summary> | ||
| internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference handler) | ||
| { | ||
| object? obj = null; | ||
| if (target != null) | ||
| { | ||
| var co = (CLRObject)ManagedType.GetManagedObject(target)!; | ||
| obj = co.inst; | ||
| } | ||
|
|
||
| nint hash = Runtime.PyObject_Hash(handler); | ||
| if (hash == -1 && Exceptions.ErrorOccurred()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| object key = obj ?? info.ReflectedType; | ||
|
|
||
| if (!TryGetValue(key, out var list)) | ||
| { | ||
| Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| return false; | ||
| } | ||
|
|
||
| object?[] args = { null }; | ||
| MethodInfo mi = info.GetRemoveMethod(true); | ||
|
|
||
| for (var i = 0; i < list.Count; i++) | ||
| { | ||
| var item = (Handler)list[i]; | ||
| if (item.hash != hash) | ||
| { | ||
| continue; | ||
| } | ||
| args[0] = item.del; | ||
| try | ||
| { | ||
| mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
| } | ||
| catch | ||
| { | ||
| continue; | ||
| } | ||
| list.RemoveAt(i); | ||
| return true; | ||
| } | ||
|
|
||
| Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| return false; | ||
| } | ||
|
|
||
| #region Serializable | ||
| [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] | ||
| protected EventHandlerCollection(SerializationInfo info, StreamingContext context) | ||
| : base(info, context) | ||
| { | ||
| this.info = (EventInfo)info.GetValue("event", typeof(EventInfo)); | ||
| } | ||
| [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] | ||
| public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
| { | ||
| base.GetObjectData(info, context); | ||
|
|
||
| info.AddValue("event", this.info); | ||
| } | ||
| #endregion | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly moved from eventobject.cs