summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/carbonevt/_CarbonEvtmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Mac/Modules/carbonevt/_CarbonEvtmodule.c')
-rwxr-xr-xMac/Modules/carbonevt/_CarbonEvtmodule.c62
1 files changed, 44 insertions, 18 deletions
diff --git a/Mac/Modules/carbonevt/_CarbonEvtmodule.c b/Mac/Modules/carbonevt/_CarbonEvtmodule.c
index 0aefcc6..f9f3886 100755
--- a/Mac/Modules/carbonevt/_CarbonEvtmodule.c
+++ b/Mac/Modules/carbonevt/_CarbonEvtmodule.c
@@ -799,6 +799,7 @@ PyTypeObject EventHandlerRef_Type;
typedef struct EventHandlerRefObject {
PyObject_HEAD
EventHandlerRef ob_itself;
+ PyObject *ob_callback;
} EventHandlerRefObject;
PyObject *EventHandlerRef_New(EventHandlerRef itself)
@@ -807,6 +808,7 @@ PyObject *EventHandlerRef_New(EventHandlerRef itself)
it = PyObject_NEW(EventHandlerRefObject, &EventHandlerRef_Type);
if (it == NULL) return NULL;
it->ob_itself = itself;
+ it->ob_callback = NULL;
return (PyObject *)it;
}
int EventHandlerRef_Convert(PyObject *v, EventHandlerRef *p_itself)
@@ -822,29 +824,23 @@ int EventHandlerRef_Convert(PyObject *v, EventHandlerRef *p_itself)
static void EventHandlerRef_dealloc(EventHandlerRefObject *self)
{
- /* Cleanup of self->ob_itself goes here */
+ if (self->ob_itself != NULL) {
+ RemoveEventHandler(self->ob_itself);
+ Py_DECREF(self->ob_callback);
+ }
PyMem_DEL(self);
}
-static PyObject *EventHandlerRef_RemoveEventHandler(EventHandlerRefObject *_self, PyObject *_args)
-{
- PyObject *_res = NULL;
- OSStatus _err;
- if (!PyArg_ParseTuple(_args, ""))
- return NULL;
- _err = RemoveEventHandler(_self->ob_itself);
- if (_err != noErr) return PyMac_Error(_err);
- Py_INCREF(Py_None);
- _res = Py_None;
- return _res;
-}
-
static PyObject *EventHandlerRef_AddEventTypesToHandler(EventHandlerRefObject *_self, PyObject *_args)
{
PyObject *_res = NULL;
OSStatus _err;
UInt32 inNumTypes;
EventTypeSpec inList;
+ if (_self->ob_itself == NULL) {
+ PyErr_SetString(CarbonEvents_Error, "Handler has been removed");
+ return NULL;
+ }
if (!PyArg_ParseTuple(_args, "lO&",
&inNumTypes,
EventTypeSpec_Convert, &inList))
@@ -864,6 +860,10 @@ static PyObject *EventHandlerRef_RemoveEventTypesFromHandler(EventHandlerRefObje
OSStatus _err;
UInt32 inNumTypes;
EventTypeSpec inList;
+ if (_self->ob_itself == NULL) {
+ PyErr_SetString(CarbonEvents_Error, "Handler has been removed");
+ return NULL;
+ }
if (!PyArg_ParseTuple(_args, "lO&",
&inNumTypes,
EventTypeSpec_Convert, &inList))
@@ -877,13 +877,34 @@ static PyObject *EventHandlerRef_RemoveEventTypesFromHandler(EventHandlerRefObje
return _res;
}
+static PyObject *EventHandlerRef_RemoveEventHandler(EventHandlerRefObject *_self, PyObject *_args)
+{
+ PyObject *_res = NULL;
+
+ OSStatus _err;
+ if (_self->ob_itself == NULL) {
+ PyErr_SetString(CarbonEvents_Error, "Handler has been removed");
+ return NULL;
+ }
+ if (!PyArg_ParseTuple(_args, ""))
+ return NULL;
+ _err = RemoveEventHandler(_self->ob_itself);
+ if (_err != noErr) return PyMac_Error(_err);
+ _self->ob_itself = NULL;
+ Py_DECREF(_self->ob_callback);
+ _self->ob_callback = NULL;
+ Py_INCREF(Py_None);
+ _res = Py_None;
+ return _res;
+}
+
static PyMethodDef EventHandlerRef_methods[] = {
- {"RemoveEventHandler", (PyCFunction)EventHandlerRef_RemoveEventHandler, 1,
- "() -> None"},
{"AddEventTypesToHandler", (PyCFunction)EventHandlerRef_AddEventTypesToHandler, 1,
"(UInt32 inNumTypes, EventTypeSpec inList) -> None"},
{"RemoveEventTypesFromHandler", (PyCFunction)EventHandlerRef_RemoveEventTypesFromHandler, 1,
"(UInt32 inNumTypes, EventTypeSpec inList) -> None"},
+ {"RemoveEventHandler", (PyCFunction)EventHandlerRef_RemoveEventHandler, 1,
+ "() -> None"},
{NULL, NULL, 0}
};
@@ -1083,14 +1104,19 @@ static PyObject *EventTargetRef_InstallEventHandler(EventTargetRefObject *_self,
_err = InstallEventHandler(_self->ob_itself, myEventHandlerUPP, 1, &inSpec, (void *)callback, &outRef);
if (_err != noErr) return PyMac_Error(_err);
- return Py_BuildValue("O&", EventHandlerRef_New, outRef);
+ _res = EventHandlerRef_New(outRef);
+ if (_res != NULL) {
+ ((EventHandlerRefObject*)_res)->ob_callback = callback;
+ Py_INCREF(callback);
+ }
+ return _res;
}
static PyMethodDef EventTargetRef_methods[] = {
{"InstallStandardEventHandler", (PyCFunction)EventTargetRef_InstallStandardEventHandler, 1,
"() -> None"},
{"InstallEventHandler", (PyCFunction)EventTargetRef_InstallEventHandler, 1,
- "(EventTargetRef inTarget, EventTypeSpec inSpec, Method callback) -> (EventHandlerRef outRef)"},
+ "(EventTypeSpec inSpec, Method callback) -> (EventHandlerRef outRef)"},
{NULL, NULL, 0}
};