From 83160c10c4fc2d82b4d2bc5029b1094d5ff9742e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 16 Mar 2026 17:38:14 +0530 Subject: [PATCH] Cleanup previous PR --- kitty/colors.c | 20 ++++++++++---------- kitty_tests/datatypes.py | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/kitty/colors.c b/kitty/colors.c index 776618211..b708ee32a 100644 --- a/kitty/colors.c +++ b/kitty/colors.c @@ -696,19 +696,19 @@ color_vectorcall(PyObject *type UNUSED, PyObject *const *args, size_t nargsf, Py rgba[i] = (unsigned char)val; } if (kwnames) { - static const char *const kwnames_list[] = {"red", "green", "blue", "alpha"}; - const Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwnames); - for (Py_ssize_t i = 0; i < nkwargs; i++) { + const unsigned num = PyTuple_GET_SIZE(kwnames); + for (unsigned i = 0; i < num; i++) { const char *name = PyUnicode_AsUTF8(PyTuple_GET_ITEM(kwnames, i)); if (!name) return NULL; - int idx = -1; - for (int j = 0; j < 4; j++) { - if (strcmp(name, kwnames_list[j]) == 0) { idx = j; break; } - } - if (idx < 0) { - PyErr_Format(PyExc_TypeError, "Color() got an unexpected keyword argument '%s'", name); - return NULL; + int idx; +#define C(ch, i, expected) case ch: idx = i; if (strcmp(name, expected) != 0) { \ + PyErr_Format(PyExc_TypeError, "Color() got an unexpected keyword argument '%s'", name); return NULL; }; break; + switch(name[0]) { + C('r', 0, "red"); C('g', 1, "green"); C('b', 2, "blue"); C('a', 3, "alpha"); + default: + PyErr_Format(PyExc_TypeError, "Color() got an unexpected keyword argument '%s'", name); return NULL; } +#undef C if (idx < nargs) { PyErr_Format(PyExc_TypeError, "Color() got multiple values for argument '%s'", name); return NULL; diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index 91497ceef..ca04e4610 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -66,6 +66,7 @@ class TestDataTypes(BaseTest): def c(spec, r=0, g=0, b=0, a=0): c = to_color(spec) self.ae(Color(r, g, b, a), c, spec) + self.ae(Color(r, green=g, alpha=a, blue=b), c, spec) c('#eee # comment', 0xee, 0xee, 0xee) c('#234567', 0x23, 0x45, 0x67)