diff options
author | Just van Rossum <just@letterror.com> | 2001-12-12 21:48:00 (GMT) |
---|---|---|
committer | Just van Rossum <just@letterror.com> | 2001-12-12 21:48:00 (GMT) |
commit | cddfc8736f35a05deae8b37958d9a091f94c7c9e (patch) | |
tree | 8e273664764f4abb85c64dc78e0f2ee12ae371f7 /Mac | |
parent | ff3a69c4bcb94433d018208fc07dc9d24c999c03 (diff) | |
download | cpython-cddfc8736f35a05deae8b37958d9a091f94c7c9e.zip cpython-cddfc8736f35a05deae8b37958d9a091f94c7c9e.tar.gz cpython-cddfc8736f35a05deae8b37958d9a091f94c7c9e.tar.bz2 |
Added proper error checking in event callback handler
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Modules/carbonevt/CarbonEvtsupport.py | 37 | ||||
-rwxr-xr-x | Mac/Modules/carbonevt/_CarbonEvtmodule.c | 37 |
2 files changed, 50 insertions, 24 deletions
diff --git a/Mac/Modules/carbonevt/CarbonEvtsupport.py b/Mac/Modules/carbonevt/CarbonEvtsupport.py index f3de719..a43d47c 100644 --- a/Mac/Modules/carbonevt/CarbonEvtsupport.py +++ b/Mac/Modules/carbonevt/CarbonEvtsupport.py @@ -64,10 +64,10 @@ includestuff = r""" /* Macro to test whether a weak-loaded CFM function exists */ #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\ - PyErr_SetString(PyExc_NotImplementedError, \ - "Not available in this shared library/OS version"); \ - return; \ - }} while(0) + PyErr_SetString(PyExc_NotImplementedError, \ + "Not available in this shared library/OS version"); \ + return; \ + }} while(0) #define USE_MAC_MP_MULTITHREADING 0 @@ -145,24 +145,37 @@ EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out) static EventHandlerUPP myEventHandlerUPP; -pascal OSStatus myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { +static pascal OSStatus +myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { PyObject *retValue; int status; #if USE_MAC_MP_MULTITHREADING - MPEnterCriticalRegion(reentrantLock, kDurationForever); - PyEval_RestoreThread(_save); + MPEnterCriticalRegion(reentrantLock, kDurationForever); + PyEval_RestoreThread(_save); #endif /* USE_MAC_MP_MULTITHREADING */ - retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event); - status = PyInt_AsLong(retValue); + retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event); + if (retValue == NULL) { + PySys_WriteStderr("Error in event handler callback:\n"); + PyErr_Print(); /* this also clears the error */ + status = noErr; /* complain? how? */ + } else { + if (retValue == Py_None) + status = noErr; + else if (PyInt_Check(retValue)) { + status = PyInt_AsLong(retValue); + } else + status = noErr; /* wrong object type, complain? */ + Py_DECREF(retValue); + } #if USE_MAC_MP_MULTITHREADING - _save = PyEval_SaveThread(); - MPExitCriticalRegion(reentrantLock); + _save = PyEval_SaveThread(); + MPExitCriticalRegion(reentrantLock); #endif /* USE_MAC_MP_MULTITHREADING */ - return status; + return status; } /******** end myEventHandler ***********/ diff --git a/Mac/Modules/carbonevt/_CarbonEvtmodule.c b/Mac/Modules/carbonevt/_CarbonEvtmodule.c index 8427b96..0aefcc6 100755 --- a/Mac/Modules/carbonevt/_CarbonEvtmodule.c +++ b/Mac/Modules/carbonevt/_CarbonEvtmodule.c @@ -15,10 +15,10 @@ /* Macro to test whether a weak-loaded CFM function exists */ #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\ - PyErr_SetString(PyExc_NotImplementedError, \ - "Not available in this shared library/OS version"); \ - return; \ - }} while(0) + PyErr_SetString(PyExc_NotImplementedError, \ + "Not available in this shared library/OS version"); \ + return; \ + }} while(0) #define USE_MAC_MP_MULTITHREADING 0 @@ -96,24 +96,37 @@ EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out) static EventHandlerUPP myEventHandlerUPP; -pascal OSStatus myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { +static pascal OSStatus +myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { PyObject *retValue; int status; #if USE_MAC_MP_MULTITHREADING - MPEnterCriticalRegion(reentrantLock, kDurationForever); - PyEval_RestoreThread(_save); + MPEnterCriticalRegion(reentrantLock, kDurationForever); + PyEval_RestoreThread(_save); #endif /* USE_MAC_MP_MULTITHREADING */ - retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event); - status = PyInt_AsLong(retValue); + retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event); + if (retValue == NULL) { + PySys_WriteStderr("Error in event handler callback:\n"); + PyErr_Print(); /* this also clears the error */ + status = noErr; /* complain? how? */ + } else { + if (retValue == Py_None) + status = noErr; + else if (PyInt_Check(retValue)) { + status = PyInt_AsLong(retValue); + } else + status = noErr; /* wrong object type, complain? */ + Py_DECREF(retValue); + } #if USE_MAC_MP_MULTITHREADING - _save = PyEval_SaveThread(); - MPExitCriticalRegion(reentrantLock); + _save = PyEval_SaveThread(); + MPExitCriticalRegion(reentrantLock); #endif /* USE_MAC_MP_MULTITHREADING */ - return status; + return status; } /******** end myEventHandler ***********/ |