From b5467b8e26a00cfb269cf72c8b5fccf3817b7bdc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 14 Aug 2022 19:33:32 +0530 Subject: [PATCH] Set the cocoa uncaught exception handler during glfw init not at module import time --- kitty/cocoa_window.m | 18 +++++++++++------- kitty/glfw.c | 4 ++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index a9cb69f65..477a3154d 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -906,6 +906,17 @@ - (BOOL)openFileURLs:(NSPasteboard*)pasteboard else NSBeep(); } +static void +uncaughtExceptionHandler(NSException *exception) { + log_error("Unhandled exception in Cocoa: %s", [[exception description] UTF8String]); + log_error("Stack trace:\n%s", [[exception.callStackSymbols description] UTF8String]); +} + +void +cocoa_set_uncaught_exception_handler(void) { + NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); +} + static PyMethodDef module_methods[] = { {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, {"cocoa_set_global_shortcut", (PyCFunction)cocoa_set_global_shortcut, METH_VARARGS, ""}, @@ -915,17 +926,10 @@ - (BOOL)openFileURLs:(NSPasteboard*)pasteboard {NULL, NULL, 0, NULL} /* Sentinel */ }; -static void -uncaughtExceptionHandler(NSException *exception) { - log_error("Unhandled exception in Cocoa: %s", [[exception description] UTF8String]); - log_error("Stack trace:\n%s", [[exception.callStackSymbols description] UTF8String]); -} - bool init_cocoa(PyObject *module) { memset(&global_shortcuts, 0, sizeof(global_shortcuts)); if (PyModule_AddFunctions(module, module_methods) != 0) return false; register_at_exit_cleanup_func(COCOA_CLEANUP_FUNC, cleanup); - NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); return true; } diff --git a/kitty/glfw.c b/kitty/glfw.c index 604e4461f..becc283ea 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -25,6 +25,7 @@ extern void cocoa_set_titlebar_appearance(void *w, unsigned int theme); extern void cocoa_set_titlebar_color(void *w, color_type color); extern bool cocoa_alt_option_key_pressed(unsigned long); extern void cocoa_toggle_secure_keyboard_entry(void); +extern void cocoa_set_uncaught_exception_handler(void); extern void cocoa_update_menu_bar_title(PyObject*); extern size_t cocoa_get_workspace_ids(void *w, size_t *workspace_ids, size_t array_sz); extern monotonic_t cocoa_cursor_blink_interval(void); @@ -1037,6 +1038,9 @@ glfw_init(PyObject UNUSED *self, PyObject *args) { const char* path; int debug_keyboard = 0, debug_rendering = 0; if (!PyArg_ParseTuple(args, "s|pp", &path, &debug_keyboard, &debug_rendering)) return NULL; +#ifdef __APPLE__ + cocoa_set_uncaught_exception_handler(); +#endif const char* err = load_glfw(path); if (err) { PyErr_SetString(PyExc_RuntimeError, err); return NULL; } glfwSetErrorCallback(error_callback);