Only set the glfw error callback immediately before initializing glfw

Avoids spurious glfw not initialized error messages in case fo
exceptions that occur before the call to init
This commit is contained in:
Kovid Goyal 2017-09-18 08:46:25 +05:30
parent 1e01c2a07c
commit 93ca469d85
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 5 additions and 26 deletions

View File

@ -158,24 +158,15 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
} }
// Global functions {{{ // Global functions {{{
static PyObject *error_callback = NULL;
static void static void
cb_error_callback(int error, const char* description) { error_callback(int error, const char* description) {
CALLBACK(error_callback, "is", error, description) else fprintf(stderr, "[glfw error]: %s\n", description); fprintf(stderr, "[glfw error %d]: %s\n", error, description);
}
PyObject*
glfw_set_error_callback(PyObject UNUSED *self, PyObject *callback) {
Py_CLEAR(error_callback);
error_callback = callback;
Py_INCREF(callback);
Py_RETURN_NONE;
} }
PyObject* PyObject*
glfw_init(PyObject UNUSED *self) { glfw_init(PyObject UNUSED *self) {
glfwSetErrorCallback(error_callback);
PyObject *ans = glfwInit() ? Py_True: Py_False; PyObject *ans = glfwInit() ? Py_True: Py_False;
Py_INCREF(ans); Py_INCREF(ans);
return ans; return ans;
@ -466,7 +457,6 @@ PyTypeObject WindowWrapper_Type = {
}; };
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
{"glfw_set_error_callback", (PyCFunction)glfw_set_error_callback, METH_O, ""}, \
{"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""}, \ {"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""}, \
{"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, \ {"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, \
{"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""}, \ {"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""}, \
@ -486,7 +476,6 @@ init_glfw(PyObject *m) {
if (PyType_Ready(&WindowWrapper_Type) < 0) return false; if (PyType_Ready(&WindowWrapper_Type) < 0) return false;
if (PyModule_AddObject(m, "GLFWWindow", (PyObject *)&WindowWrapper_Type) != 0) return 0; if (PyModule_AddObject(m, "GLFWWindow", (PyObject *)&WindowWrapper_Type) != 0) return 0;
Py_INCREF(&WindowWrapper_Type); Py_INCREF(&WindowWrapper_Type);
glfwSetErrorCallback(cb_error_callback);
#define ADDC(n) if(PyModule_AddIntConstant(m, #n, n) != 0) return false; #define ADDC(n) if(PyModule_AddIntConstant(m, #n, n) != 0) return false;
#ifdef GLFW_X11_WM_CLASS_NAME #ifdef GLFW_X11_WM_CLASS_NAME
ADDC(GLFW_X11_WM_CLASS_NAME) ADDC(GLFW_X11_WM_CLASS_NAME)

View File

@ -22,8 +22,8 @@ from .fast_data_types import (
GLFW_OPENGL_FORWARD_COMPAT, GLFW_OPENGL_PROFILE, GLFW_SAMPLES, GLFW_OPENGL_FORWARD_COMPAT, GLFW_OPENGL_PROFILE, GLFW_SAMPLES,
GLFW_STENCIL_BITS, GLFWWindow, change_wcwidth, check_for_extensions, GLFW_STENCIL_BITS, GLFWWindow, change_wcwidth, check_for_extensions,
clear_buffers, glewInit, glfw_init, glfw_init_hint_string, clear_buffers, glewInit, glfw_init, glfw_init_hint_string,
glfw_set_error_callback, glfw_swap_interval, glfw_terminate, glfw_swap_interval, glfw_terminate, glfw_window_hint, set_logical_dpi,
glfw_window_hint, set_logical_dpi, set_options set_options
) )
from .layout import all_layouts from .layout import all_layouts
from .utils import color_as_int, detach, get_logical_dpi, safe_print from .utils import color_as_int, detach, get_logical_dpi, safe_print
@ -207,15 +207,6 @@ def run_app(opts, args):
save_cached_values() save_cached_values()
def on_glfw_error(code, msg):
if isinstance(msg, bytes):
try:
msg = msg.decode('utf-8')
except Exception:
msg = repr(msg)
safe_print('[glfw error] ', msg, file=sys.stderr)
def ensure_osx_locale(): def ensure_osx_locale():
# Ensure the LANG env var is set. See # Ensure the LANG env var is set. See
# https://github.com/kovidgoyal/kitty/issues/90 # https://github.com/kovidgoyal/kitty/issues/90
@ -286,7 +277,6 @@ def main():
overrides = (a.replace('=', ' ', 1) for a in args.override or ()) overrides = (a.replace('=', ' ', 1) for a in args.override or ())
opts = load_config(*config, overrides=overrides) opts = load_config(*config, overrides=overrides)
change_wcwidth(not opts.use_system_wcwidth) change_wcwidth(not opts.use_system_wcwidth)
glfw_set_error_callback(on_glfw_error)
if GLFW_X11_WM_CLASS_CLASS is not None: if GLFW_X11_WM_CLASS_CLASS is not None:
glfw_init_hint_string(GLFW_X11_WM_CLASS_CLASS, opts.cls) glfw_init_hint_string(GLFW_X11_WM_CLASS_CLASS, opts.cls)
if not glfw_init(): if not glfw_init():