Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cddfc87

Browse files
committed
Added proper error checking in event callback handler
1 parent ff3a69c commit cddfc87

2 files changed

Lines changed: 50 additions & 24 deletions

File tree

Mac/Modules/carbonevt/CarbonEvtsupport.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
6565
/* Macro to test whether a weak-loaded CFM function exists */
6666
#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
67-
PyErr_SetString(PyExc_NotImplementedError, \
68-
"Not available in this shared library/OS version"); \
69-
return; \
70-
}} while(0)
67+
PyErr_SetString(PyExc_NotImplementedError, \
68+
"Not available in this shared library/OS version"); \
69+
return; \
70+
}} while(0)
7171
7272
7373
#define USE_MAC_MP_MULTITHREADING 0
@@ -145,24 +145,37 @@
145145
146146
static EventHandlerUPP myEventHandlerUPP;
147147
148-
pascal OSStatus myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) {
148+
static pascal OSStatus
149+
myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) {
149150
PyObject *retValue;
150151
int status;
151152
152153
#if USE_MAC_MP_MULTITHREADING
153-
MPEnterCriticalRegion(reentrantLock, kDurationForever);
154-
PyEval_RestoreThread(_save);
154+
MPEnterCriticalRegion(reentrantLock, kDurationForever);
155+
PyEval_RestoreThread(_save);
155156
#endif /* USE_MAC_MP_MULTITHREADING */
156157
157-
retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event);
158-
status = PyInt_AsLong(retValue);
158+
retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event);
159+
if (retValue == NULL) {
160+
PySys_WriteStderr("Error in event handler callback:\n");
161+
PyErr_Print(); /* this also clears the error */
162+
status = noErr; /* complain? how? */
163+
} else {
164+
if (retValue == Py_None)
165+
status = noErr;
166+
else if (PyInt_Check(retValue)) {
167+
status = PyInt_AsLong(retValue);
168+
} else
169+
status = noErr; /* wrong object type, complain? */
170+
Py_DECREF(retValue);
171+
}
159172
160173
#if USE_MAC_MP_MULTITHREADING
161-
_save = PyEval_SaveThread();
162-
MPExitCriticalRegion(reentrantLock);
174+
_save = PyEval_SaveThread();
175+
MPExitCriticalRegion(reentrantLock);
163176
#endif /* USE_MAC_MP_MULTITHREADING */
164177
165-
return status;
178+
return status;
166179
}
167180
168181
/******** end myEventHandler ***********/

Mac/Modules/carbonevt/_CarbonEvtmodule.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
/* Macro to test whether a weak-loaded CFM function exists */
1717
#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
18-
PyErr_SetString(PyExc_NotImplementedError, \
19-
"Not available in this shared library/OS version"); \
20-
return; \
21-
}} while(0)
18+
PyErr_SetString(PyExc_NotImplementedError, \
19+
"Not available in this shared library/OS version"); \
20+
return; \
21+
}} while(0)
2222

2323

2424
#define USE_MAC_MP_MULTITHREADING 0
@@ -96,24 +96,37 @@ EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out)
9696

9797
static EventHandlerUPP myEventHandlerUPP;
9898

99-
pascal OSStatus myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) {
99+
static pascal OSStatus
100+
myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) {
100101
PyObject *retValue;
101102
int status;
102103

103104
#if USE_MAC_MP_MULTITHREADING
104-
MPEnterCriticalRegion(reentrantLock, kDurationForever);
105-
PyEval_RestoreThread(_save);
105+
MPEnterCriticalRegion(reentrantLock, kDurationForever);
106+
PyEval_RestoreThread(_save);
106107
#endif /* USE_MAC_MP_MULTITHREADING */
107108

108-
retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event);
109-
status = PyInt_AsLong(retValue);
109+
retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event);
110+
if (retValue == NULL) {
111+
PySys_WriteStderr("Error in event handler callback:\n");
112+
PyErr_Print(); /* this also clears the error */
113+
status = noErr; /* complain? how? */
114+
} else {
115+
if (retValue == Py_None)
116+
status = noErr;
117+
else if (PyInt_Check(retValue)) {
118+
status = PyInt_AsLong(retValue);
119+
} else
120+
status = noErr; /* wrong object type, complain? */
121+
Py_DECREF(retValue);
122+
}
110123

111124
#if USE_MAC_MP_MULTITHREADING
112-
_save = PyEval_SaveThread();
113-
MPExitCriticalRegion(reentrantLock);
125+
_save = PyEval_SaveThread();
126+
MPExitCriticalRegion(reentrantLock);
114127
#endif /* USE_MAC_MP_MULTITHREADING */
115128

116-
return status;
129+
return status;
117130
}
118131

119132
/******** end myEventHandler ***********/

0 commit comments

Comments
 (0)