From 051b0ff30d0645f6f5512cb0653a7049a3f17db9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 30 Dec 2025 22:11:05 +0530 Subject: [PATCH] Free C locale on exit --- kitty/colors.c | 6 ++---- kitty/data-types.c | 6 ++++++ kitty/data-types.h | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/kitty/colors.c b/kitty/colors.c index 37ffc6b9a..fa83e6473 100644 --- a/kitty/colors.c +++ b/kitty/colors.c @@ -8,11 +8,10 @@ #include "state.h" #include #include "colors.h" -#include #ifdef __APPLE__ +// Needed for strod_l #include #endif -locale_t c_locale; static uint32_t FG_BG_256[256] = { @@ -806,7 +805,7 @@ static bool parse_double(const char *src, double *out) { char *endptr; errno = 0; - *out = strtod_l(src, &endptr, c_locale); + *out = strtod_l(src, &endptr, get_c_locale()); return endptr != src && *endptr == 0 && errno == 0; } @@ -1128,7 +1127,6 @@ static PyMethodDef module_methods[] = { }; int init_ColorProfile(PyObject *module) {\ - c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0); if (PyType_Ready(&ColorProfile_Type) < 0) return 0; if (PyModule_AddObject(module, "ColorProfile", (PyObject *)&ColorProfile_Type) != 0) return 0; Py_INCREF(&ColorProfile_Type); diff --git a/kitty/data-types.c b/kitty/data-types.c index 687dccd87..c7d6ec0e3 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -617,6 +617,9 @@ py_timed_debug_print(PyObject *self UNUSED, PyObject *args) { Py_RETURN_NONE; } +static locale_t c_locale = 0; +locale_t get_c_locale(void) { return c_locale; } + static PyObject* py_run_atexit_cleanup_functions(PyObject *self UNUSED, PyObject *args UNUSED) { run_at_exit_cleanup_functions(); @@ -726,6 +729,7 @@ static PyMethodDef module_methods[] = { static void free_fast_data_types_module(void *m UNUSED) { + freelocale(c_locale); run_at_exit_cleanup_functions(); } @@ -877,6 +881,8 @@ PyInit_fast_data_types(void) { if (PyModule_AddObject(m, "StreamingBase64Decoder", (PyObject *) &StreamingBase64Decoder_Type) < 0) return NULL; if (PyType_Ready(&StreamingBase64Encoder_Type) < 0) return NULL; if (PyModule_AddObject(m, "StreamingBase64Encoder", (PyObject *) &StreamingBase64Encoder_Type) < 0) return NULL; + c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0); + if (!c_locale) { PyErr_NoMemory(); return NULL; } return m; } diff --git a/kitty/data-types.h b/kitty/data-types.h index fec7ee97e..8111a2cb0 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "glfw-wrapper.h" #include "banned.h" // Required minimum OpenGL version @@ -331,6 +332,7 @@ void focus_in_event(void); void scroll_event(double, double, int, int); void on_key_input(const GLFWkeyevent *ev); void request_window_attention(id_type, bool); +locale_t get_c_locale(void); #ifndef __APPLE__ void play_canberra_sound(const char *which_sound, const char *event_id, bool is_path, const char *role, const char *theme_name); #endif