diff --git a/kitty/data-types.c b/kitty/data-types.c index aab0334d8..5f9460ce1 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -70,11 +70,6 @@ static inline double monotonic_() { double monotonic() { return monotonic_(); } -static PyObject* -wcwidth_wrap(PyObject UNUSED *self, PyObject *chr) { - return PyLong_FromUnsignedLong(safe_wcwidth(PyLong_AsLong(chr))); -} - static PyObject* redirect_std_streams(PyObject UNUSED *self, PyObject *args) { char *devnull = NULL; @@ -140,7 +135,6 @@ static PyMethodDef module_methods[] = { {"parse_bytes", (PyCFunction)parse_bytes, METH_VARARGS, ""}, {"parse_bytes_dump", (PyCFunction)parse_bytes_dump, METH_VARARGS, ""}, {"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""}, - {"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""}, {"install_sigchld_handler", (PyCFunction)install_sigchld_handler, METH_NOARGS, ""}, #ifdef __APPLE__ METHODB(user_cache_dir, METH_NOARGS), diff --git a/kitty/screen.c b/kitty/screen.c index d6c4c6264..ae0debac9 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -5,7 +5,10 @@ * Distributed under terms of the GPL3 license. */ -#define EXTRA_INIT PyModule_AddIntMacro(module, SCROLL_LINE); PyModule_AddIntMacro(module, SCROLL_PAGE); PyModule_AddIntMacro(module, SCROLL_FULL); +#define EXTRA_INIT { \ + PyModule_AddIntMacro(module, SCROLL_LINE); PyModule_AddIntMacro(module, SCROLL_PAGE); PyModule_AddIntMacro(module, SCROLL_FULL); \ + if (PyModule_AddFunctions(module, module_methods) != 0) return false; \ +} #include "state.h" #include "fonts.h" @@ -1756,8 +1759,6 @@ static PyMethodDef methods[] = { MND(cursor_down, METH_VARARGS) MND(cursor_down1, METH_VARARGS) MND(cursor_forward, METH_VARARGS) - {"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""}, - {"wcswidth", (PyCFunction)screen_wcswidth, METH_O, ""}, {"index", (PyCFunction)xxx_index, METH_VARARGS, ""}, MND(refresh_sprite_positions, METH_NOARGS) MND(tab, METH_NOARGS) @@ -1831,5 +1832,11 @@ PyTypeObject Screen_Type = { .tp_getset = getsetters, }; +static PyMethodDef module_methods[] = { + {"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""}, + {"wcswidth", (PyCFunction)screen_wcswidth, METH_O, ""}, + {NULL} /* Sentinel */ +}; + INIT_TYPE(Screen) // }}} diff --git a/kitty/utils.py b/kitty/utils.py index 4b50dd74e..131b3f9fc 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -15,12 +15,11 @@ import subprocess import sys import tempfile from contextlib import contextmanager -from functools import lru_cache from time import monotonic from .constants import appname, is_macos, is_wayland from .fast_data_types import ( - GLSL_VERSION, redirect_std_streams, wcwidth as wcwidth_impl, x11_display, + GLSL_VERSION, redirect_std_streams, x11_display, x11_window_id ) from .rgb import Color, to_color @@ -45,14 +44,6 @@ def ceil_int(x): return int(math.ceil(x)) -@lru_cache(maxsize=2**13) -def wcwidth(c: str) -> int: - try: - return wcwidth_impl(ord(c)) - except TypeError: - return wcwidth_impl(ord(c[0])) - - @contextmanager def timeit(name, do_timing=False): if do_timing: diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index 1308b8321..2fdb90fee 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -4,10 +4,10 @@ from kitty.config import build_ansi_color_table, defaults from kitty.fast_data_types import ( - REVERSE, ColorProfile, Cursor as C, HistoryBuf, LineBuf + REVERSE, ColorProfile, Cursor as C, HistoryBuf, LineBuf, wcwidth ) -from kitty.utils import sanitize_title, wcwidth from kitty.rgb import to_color +from kitty.utils import sanitize_title from . import BaseTest, filled_cursor, filled_history_buf, filled_line_buf @@ -332,7 +332,9 @@ class TestDataTypes(BaseTest): self.assertContinued(lb2, False, True, True, True) def test_utils(self): - self.ae(tuple(map(wcwidth, 'a1\0コニチ ')), (1, 1, 0, 2, 2, 2, 1)) + def w(x): + return wcwidth(ord(x)) + self.ae(tuple(map(w, 'a1\0コニチ ')), (1, 1, 0, 2, 2, 2, 1)) self.assertEqual(sanitize_title('a\0\01 \t\n\f\rb'), 'a b') def test_color_profile(self):