//==================================================================== // Descriptor __get__ implementation. A getattr on an event returns // a "bound" event that keeps a reference to the object instance. //==================================================================== public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp) { EventObject self = GetManagedObject(ds) as EventObject; EventBinding binding; if (self == null) { return Exceptions.RaiseTypeError("invalid argument"); } // If the event is accessed through its type (rather than via // an instance) we return an 'unbound' EventBinding that will // be cached for future accesses through the type. if (ob == IntPtr.Zero) { if (self.unbound == null) { self.unbound = new EventBinding(self, IntPtr.Zero); } binding = self.unbound; Runtime.XIncref(binding.pyHandle); return binding.pyHandle; } if (Runtime.PyObject_IsInstance(ob, tp) < 1) { return Exceptions.RaiseTypeError("invalid argument"); } binding = new EventBinding(self, ob); return binding.pyHandle; }
//==================================================================== // EventBinding -= operator implementation. //==================================================================== public static IntPtr nb_inplace_subtract(IntPtr ob, IntPtr arg) { EventBinding self = (EventBinding)GetManagedObject(ob); if (Runtime.PyCallable_Check(arg) < 1) { Exceptions.SetError(Exceptions.TypeError, "invalid event handler" ); return(IntPtr.Zero); } if (!self.e.RemoveEventHandler(self.target, arg)) { return(IntPtr.Zero); } Runtime.Incref(self.pyHandle); return(self.pyHandle); }
//==================================================================== // EventBinding += operator implementation. //==================================================================== public static IntPtr nb_inplace_add(IntPtr ob, IntPtr arg) { EventBinding self = (EventBinding)GetManagedObject(ob); if (Runtime.PyCallable_Check(arg) < 1) { Exceptions.SetError(Exceptions.TypeError, "event handlers must be callable" ); return(IntPtr.Zero); } if (!self.e.AddEventHandler(self.target, arg)) { return(IntPtr.Zero); } Runtime.XIncref(self.pyHandle); return(self.pyHandle); }