| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,105 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Reflection; | ||
| 4 | + | ||
| 5 | + namespace Python.Runtime; | ||
| 6 | + | ||
| 7 | + internal class EventHandlerCollection: Dictionary<object, List<Handler>> | ||
| 8 | + { | ||
| 9 | + readonly EventInfo info; | ||
| 10 | + public EventHandlerCollection(EventInfo @event) | ||
| 11 | + { | ||
| 12 | + info = @event; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + /// <summary> | ||
| 16 | + /// Register a new Python object event handler with the event. | ||
| 17 | + /// </summary> | ||
| 18 | + internal bool AddEventHandler(BorrowedReference target, PyObject handler) | ||
| 19 | + { | ||
| 20 | + object? obj = null; | ||
| 21 | + if (target != null) | ||
| 22 | + { | ||
| 23 | + var co = (CLRObject)ManagedType.GetManagedObject(target)!; | ||
| 24 | + obj = co.inst; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + // Create a true delegate instance of the appropriate type to | ||
| 28 | + // wrap the Python handler. Note that wrapper delegate creation | ||
| 29 | + // always succeeds, though calling the wrapper may fail. | ||
| 30 | + Type type = info.EventHandlerType; | ||
| 31 | + Delegate d = PythonEngine.DelegateManager.GetDelegate(type, handler); | ||
| 32 | + | ||
| 33 | + // Now register the handler in a mapping from instance to pairs | ||
| 34 | + // of (handler hash, delegate) so we can lookup to remove later. | ||
| 35 | + object key = obj ?? info.ReflectedType; | ||
| 36 | + if (!TryGetValue(key, out var list)) | ||
| 37 | + { | ||
| 38 | + list = new List<Handler>(); | ||
| 39 | + this[key] = list; | ||
| 40 | + } | ||
| 41 | + list.Add(new Handler(Runtime.PyObject_Hash(handler), d)); | ||
| 42 | + | ||
| 43 | + // Note that AddEventHandler helper only works for public events, | ||
| 44 | + // so we have to get the underlying add method explicitly. | ||
| 45 | + object[] args = { d }; | ||
| 46 | + MethodInfo mi = info.GetAddMethod(true); | ||
| 47 | + mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
| 48 | + | ||
| 49 | + return true; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + | ||
| 53 | + /// <summary> | ||
| 54 | + /// Remove the given Python object event handler. | ||
| 55 | + /// </summary> | ||
| 56 | + internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference handler) | ||
| 57 | + { | ||
| 58 | + object? obj = null; | ||
| 59 | + if (target != null) | ||
| 60 | + { | ||
| 61 | + var co = (CLRObject)ManagedType.GetManagedObject(target)!; | ||
| 62 | + obj = co.inst; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + nint hash = Runtime.PyObject_Hash(handler); | ||
| 66 | + if (hash == -1 && Exceptions.ErrorOccurred()) | ||
| 67 | + { | ||
| 68 | + return false; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + object key = obj ?? info.ReflectedType; | ||
| 72 | + | ||
| 73 | + if (!TryGetValue(key, out var list)) | ||
| 74 | + { | ||
| 75 | + Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + object?[] args = { null }; | ||
| 80 | + MethodInfo mi = info.GetRemoveMethod(true); | ||
| 81 | + | ||
| 82 | + for (var i = 0; i < list.Count; i++) | ||
| 83 | + { | ||
| 84 | + var item = (Handler)list[i]; | ||
| 85 | + if (item.hash != hash) | ||
| 86 | + { | ||
| 87 | + continue; | ||
| 88 | + } | ||
| 89 | + args[0] = item.del; | ||
| 90 | + try | ||
| 91 | + { | ||
| 92 | + mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
| 93 | + } | ||
| 94 | + catch | ||
| 95 | + { | ||
| 96 | + continue; | ||
| 97 | + } | ||
| 98 | + list.RemoveAt(i); | ||
| 99 | + return true; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| 103 | + return false; | ||
| 104 | + } | ||
| 105 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -491,7 +491,9 @@ private static ClassInfo GetClassInfo(Type type) | |||
| 491 | 491 | { | |
| 492 | 492 | continue; | |
| 493 | 493 | } | |
| 494 | - ob = new EventObject(ei); | ||
| 494 | + ob = ei.AddMethod.IsStatic | ||
| 495 | + ? new EventBinding(ei) | ||
| 496 | + : new EventObject(ei); | ||
| 495 | 497 | ci.members[ei.Name] = ob; | |
| 496 | 498 | continue; | |
| 497 | 499 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | + using System.Diagnostics; | ||
| 3 | + using System.Reflection; | ||
| 2 | 4 | ||
| 3 | 5 | namespace Python.Runtime | |
| 4 | 6 | { | |
@@ -8,15 +10,22 @@ namespace Python.Runtime | |||
| 8 | 10 | [Serializable] | |
| 9 | 11 | internal class EventBinding : ExtensionType | |
| 10 | 12 | { | |
| 11 | - private EventObject e; | ||
| 13 | + private readonly string name; | ||
| 14 | + private readonly EventHandlerCollection e; | ||
| 12 | 15 | private PyObject? target; | |
| 13 | 16 | ||
| 14 | - public EventBinding(EventObject e, PyObject? target) | ||
| 17 | + public EventBinding(string name, EventHandlerCollection e, PyObject? target) | ||
| 15 | 18 | { | |
| 19 | + this.name = name; | ||
| 16 | 20 | this.target = target; | |
| 17 | 21 | this.e = e; | |
| 18 | 22 | } | |
| 19 | 23 | ||
| 24 | + public EventBinding(EventInfo @event) : this(@event.Name, new EventHandlerCollection(@event), target: null) | ||
| 25 | + { | ||
| 26 | + Debug.Assert(@event.AddMethod.IsStatic); | ||
| 27 | + } | ||
| 28 | + | ||
| 20 | 29 | ||
| 21 | 30 | /// <summary> | |
| 22 | 31 | /// EventBinding += operator implementation. | |
@@ -61,6 +70,10 @@ public static NewReference nb_inplace_subtract(BorrowedReference ob, BorrowedRef | |||
| 61 | 70 | return new NewReference(ob); | |
| 62 | 71 | } | |
| 63 | 72 | ||
| 73 | + /// </summary> | ||
| 74 | + public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val) | ||
| 75 | + => EventObject.tp_descr_set(ds, ob, val); | ||
| 76 | + | ||
| 64 | 77 | ||
| 65 | 78 | /// <summary> | |
| 66 | 79 | /// EventBinding __hash__ implementation. | |
@@ -91,7 +104,7 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 91 | 104 | { | |
| 92 | 105 | var self = (EventBinding)GetManagedObject(ob)!; | |
| 93 | 106 | string type = self.target == null ? "unbound" : "bound"; | |
| 94 | - string s = string.Format("<{0} event '{1}'>", type, self.e.name); | ||
| 107 | + string s = string.Format("<{0} event '{1}'>", type, self.name); | ||
| 95 | 108 | return Runtime.PyString_FromString(s); | |
| 96 | 109 | } | |
| 97 | 110 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections; | |
| 3 | + using System.Diagnostics; | ||
| 3 | 4 | using System.Reflection; | |
| 4 | 5 | ||
| 5 | 6 | namespace Python.Runtime | |
@@ -10,124 +11,16 @@ namespace Python.Runtime | |||
| 10 | 11 | [Serializable] | |
| 11 | 12 | internal class EventObject : ExtensionType | |
| 12 | 13 | { | |
| 13 | - internal string name; | ||
| 14 | - internal PyObject? unbound; | ||
| 15 | - internal EventInfo info; | ||
| 16 | - internal Hashtable? reg; | ||
| 14 | + internal readonly string name; | ||
| 15 | + internal readonly EventHandlerCollection reg; | ||
| 17 | 16 | ||
| 18 | 17 | public EventObject(EventInfo info) | |
| 19 | 18 | { | |
| 19 | + Debug.Assert(!info.AddMethod.IsStatic); | ||
| 20 | 20 | this.name = info.Name; | |
| 21 | - this.info = info; | ||
| 21 | + this.reg = new EventHandlerCollection(info); | ||
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | - | ||
| 25 | - /// <summary> | ||
| 26 | - /// Register a new Python object event handler with the event. | ||
| 27 | - /// </summary> | ||
| 28 | - internal bool AddEventHandler(BorrowedReference target, PyObject handler) | ||
| 29 | - { | ||
| 30 | - object? obj = null; | ||
| 31 | - if (target != null) | ||
| 32 | - { | ||
| 33 | - var co = (CLRObject)GetManagedObject(target)!; | ||
| 34 | - obj = co.inst; | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - // Create a true delegate instance of the appropriate type to | ||
| 38 | - // wrap the Python handler. Note that wrapper delegate creation | ||
| 39 | - // always succeeds, though calling the wrapper may fail. | ||
| 40 | - Type type = info.EventHandlerType; | ||
| 41 | - Delegate d = PythonEngine.DelegateManager.GetDelegate(type, handler); | ||
| 42 | - | ||
| 43 | - // Now register the handler in a mapping from instance to pairs | ||
| 44 | - // of (handler hash, delegate) so we can lookup to remove later. | ||
| 45 | - // All this is done lazily to avoid overhead until an event is | ||
| 46 | - // actually subscribed to by a Python event handler. | ||
| 47 | - if (reg == null) | ||
| 48 | - { | ||
| 49 | - reg = new Hashtable(); | ||
| 50 | - } | ||
| 51 | - object key = obj ?? info.ReflectedType; | ||
| 52 | - var list = reg[key] as ArrayList; | ||
| 53 | - if (list == null) | ||
| 54 | - { | ||
| 55 | - list = new ArrayList(); | ||
| 56 | - reg[key] = list; | ||
| 57 | - } | ||
| 58 | - list.Add(new Handler(Runtime.PyObject_Hash(handler), d)); | ||
| 59 | - | ||
| 60 | - // Note that AddEventHandler helper only works for public events, | ||
| 61 | - // so we have to get the underlying add method explicitly. | ||
| 62 | - object[] args = { d }; | ||
| 63 | - MethodInfo mi = info.GetAddMethod(true); | ||
| 64 | - mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
| 65 | - | ||
| 66 | - return true; | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - /// <summary> | ||
| 71 | - /// Remove the given Python object event handler. | ||
| 72 | - /// </summary> | ||
| 73 | - internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference handler) | ||
| 74 | - { | ||
| 75 | - if (reg == null) | ||
| 76 | - { | ||
| 77 | - Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| 78 | - return false; | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - object? obj = null; | ||
| 82 | - if (target != null) | ||
| 83 | - { | ||
| 84 | - var co = (CLRObject)GetManagedObject(target)!; | ||
| 85 | - obj = co.inst; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - nint hash = Runtime.PyObject_Hash(handler); | ||
| 89 | - if (hash == -1 && Exceptions.ErrorOccurred()) | ||
| 90 | - { | ||
| 91 | - return false; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - object key = obj ?? info.ReflectedType; | ||
| 95 | - var list = reg[key] as ArrayList; | ||
| 96 | - | ||
| 97 | - if (list == null) | ||
| 98 | - { | ||
| 99 | - Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| 100 | - return false; | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - object?[] args = { null }; | ||
| 104 | - MethodInfo mi = info.GetRemoveMethod(true); | ||
| 105 | - | ||
| 106 | - for (var i = 0; i < list.Count; i++) | ||
| 107 | - { | ||
| 108 | - var item = (Handler)list[i]; | ||
| 109 | - if (item.hash != hash) | ||
| 110 | - { | ||
| 111 | - continue; | ||
| 112 | - } | ||
| 113 | - args[0] = item.del; | ||
| 114 | - try | ||
| 115 | - { | ||
| 116 | - mi.Invoke(obj, BindingFlags.Default, null, args, null); | ||
| 117 | - } | ||
| 118 | - catch | ||
| 119 | - { | ||
| 120 | - continue; | ||
| 121 | - } | ||
| 122 | - list.RemoveAt(i); | ||
| 123 | - return true; | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - Exceptions.SetError(Exceptions.ValueError, "unknown event handler"); | ||
| 127 | - return false; | ||
| 128 | - } | ||
| 129 | - | ||
| 130 | - | ||
| 131 | 24 | /// <summary> | |
| 132 | 25 | /// Descriptor __get__ implementation. A getattr on an event returns | |
| 133 | 26 | /// a "bound" event that keeps a reference to the object instance. | |
@@ -141,25 +34,17 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference | |||
| 141 | 34 | return Exceptions.RaiseTypeError("invalid argument"); | |
| 142 | 35 | } | |
| 143 | 36 | ||
| 144 | - // If the event is accessed through its type (rather than via | ||
| 145 | - // an instance) we return an 'unbound' EventBinding that will | ||
| 146 | - // be cached for future accesses through the type. | ||
| 147 | - | ||
| 148 | 37 | if (ob == null) | |
| 149 | 38 | { | |
| 150 | - if (self.unbound == null) | ||
| 151 | - { | ||
| 152 | - self.unbound = new EventBinding(self, target: null).Alloc().MoveToPyObject(); | ||
| 153 | - } | ||
| 154 | - return new NewReference(self.unbound); | ||
| 39 | + return new NewReference(ds); | ||
| 155 | 40 | } | |
| 156 | 41 | ||
| 157 | 42 | if (Runtime.PyObject_IsInstance(ob, tp) < 1) | |
| 158 | 43 | { | |
| 159 | 44 | return Exceptions.RaiseTypeError("invalid argument"); | |
| 160 | 45 | } | |
| 161 | 46 | ||
| 162 | - return new EventBinding(self, new PyObject(ob)).Alloc(); | ||
| 47 | + return new EventBinding(self.name, self.reg, new PyObject(ob)).Alloc(); | ||
| 163 | 48 | } | |
| 164 | 49 | ||
| 165 | 50 | ||
@@ -192,13 +77,6 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 192 | 77 | var self = (EventObject)GetManagedObject(ob)!; | |
| 193 | 78 | return Runtime.PyString_FromString($"<event '{self.name}'>"); | |
| 194 | 79 | } | |
| 195 | - | ||
| 196 | - | ||
| 197 | - protected override void Clear(BorrowedReference ob) | ||
| 198 | - { | ||
| 199 | - this.unbound = null!; | ||
| 200 | - base.Clear(ob); | ||
| 201 | - } | ||
| 202 | 80 | } | |
| 203 | 81 | ||
| 204 | 82 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -310,7 +310,7 @@ public class Cls | |||
| 310 | 310 | public static event Action Before; | |
| 311 | 311 | public static void Call() | |
| 312 | 312 | { | |
| 313 | - Before(); | ||
| 313 | + if (Before != null) Before(); | ||
| 314 | 314 | } | |
| 315 | 315 | } | |
| 316 | 316 | }", | |
@@ -324,7 +324,7 @@ public class Cls | |||
| 324 | 324 | public static event Action After; | |
| 325 | 325 | public static void Call() | |
| 326 | 326 | { | |
| 327 | - After(); | ||
| 327 | + if (After != null) After(); | ||
| 328 | 328 | } | |
| 329 | 329 | } | |
| 330 | 330 | }", | |
@@ -335,21 +335,29 @@ import sys | |||
| 335 | 335 | from TestNamespace import Cls | |
| 336 | 336 | ||
| 337 | 337 | called = False | |
| 338 | + before_reload_called = False | ||
| 339 | + after_reload_called = False | ||
| 338 | 340 | ||
| 339 | 341 | def callback_function(): | |
| 340 | 342 | global called | |
| 341 | 343 | called = True | |
| 342 | 344 | ||
| 343 | 345 | def before_reload(): | |
| 344 | - global called | ||
| 346 | + global called, before_reload_called | ||
| 345 | 347 | called = False | |
| 346 | 348 | Cls.Before += callback_function | |
| 347 | 349 | Cls.Call() | |
| 348 | 350 | assert called is True | |
| 351 | + before_reload_called = True | ||
| 349 | 352 | ||
| 350 | 353 | def after_reload(): | |
| 351 | - global called | ||
| 354 | + global called, after_reload_called, before_reload_called | ||
| 355 | + | ||
| 356 | + assert before_reload_called is True | ||
| 357 | + if not after_reload_called: | ||
| 352 | 358 | assert called is True | |
| 359 | + after_reload_called = True | ||
| 360 | + | ||
| 353 | 361 | called = False | |
| 354 | 362 | Cls.Call() | |
| 355 | 363 | assert called is False | |
| Back | FazBrowse Home | New Git URL |
0 commit comments