From f33a58b106437910b5400097a1ebcc4cb49ba6cb Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 15 Jan 2026 15:23:18 +0000 Subject: [PATCH 1/3] Optimize BytesWriter.append Python wrapper --- mypyc/lib-rt/librt_strings.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index 788d27070114..0e16e5aad7b9 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -199,12 +199,12 @@ static PySequenceMethods BytesWriter_as_sequence = { .sq_ass_item = (ssizeobjargproc)BytesWriter_ass_item, }; -static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames); +static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs); static PyObject* BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames); static PyObject* BytesWriter_truncate(PyObject *self, PyObject *const *args, size_t nargs); static PyMethodDef BytesWriter_methods[] = { - {"append", (PyCFunction) BytesWriter_append, METH_FASTCALL | METH_KEYWORDS, + {"append", (PyCFunction) BytesWriter_append, METH_FASTCALL, PyDoc_STR("Append a single byte to the buffer") }, {"write", (PyCFunction) BytesWriter_write, METH_FASTCALL | METH_KEYWORDS, @@ -295,16 +295,16 @@ BytesWriter_append_internal(BytesWriterObject *self, uint8_t value) { } static PyObject* -BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) { - static const char * const kwlist[] = {"value", 0}; - static CPyArg_Parser parser = {"O:append", kwlist, 0}; - PyObject *value; - if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) { +BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs) { + if (unlikely(nargs != 1)) { + PyErr_Format(PyExc_TypeError, + "append() takes exactly 1 argument (%zu given)", nargs); return NULL; } if (!check_bytes_writer(self)) { return NULL; } + PyObject *value = args[0]; uint8_t unboxed = CPyLong_AsUInt8(value); if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) { CPy_TypeError("u8", value); From 9ffcf81eeebe0a76aed5f772fa41ca9ef1e43524 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 15 Jan 2026 15:26:37 +0000 Subject: [PATCH 2/3] Optimize additional methods --- mypyc/lib-rt/librt_strings.c | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index 0e16e5aad7b9..502aacdc9563 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -200,14 +200,14 @@ static PySequenceMethods BytesWriter_as_sequence = { }; static PyObject* BytesWriter_append(PyObject *self, PyObject *const *args, size_t nargs); -static PyObject* BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames); +static PyObject* BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs); static PyObject* BytesWriter_truncate(PyObject *self, PyObject *const *args, size_t nargs); static PyMethodDef BytesWriter_methods[] = { {"append", (PyCFunction) BytesWriter_append, METH_FASTCALL, PyDoc_STR("Append a single byte to the buffer") }, - {"write", (PyCFunction) BytesWriter_write, METH_FASTCALL | METH_KEYWORDS, + {"write", (PyCFunction) BytesWriter_write, METH_FASTCALL, PyDoc_STR("Append bytes to the buffer") }, {"getvalue", (PyCFunction) BytesWriter_getvalue, METH_NOARGS, @@ -265,16 +265,16 @@ BytesWriter_write_internal(BytesWriterObject *self, PyObject *value) { } static PyObject* -BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) { - static const char * const kwlist[] = {"value", 0}; - static CPyArg_Parser parser = {"O:write", kwlist, 0}; - PyObject *value; - if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) { +BytesWriter_write(PyObject *self, PyObject *const *args, size_t nargs) { + if (unlikely(nargs != 1)) { + PyErr_Format(PyExc_TypeError, + "write() takes exactly 1 argument (%zu given)", nargs); return NULL; } if (!check_bytes_writer(self)) { return NULL; } + PyObject *value = args[0]; if (unlikely(!PyBytes_Check(value) && !PyByteArray_Check(value))) { PyErr_SetString(PyExc_TypeError, "value must be a bytes or bytearray object"); return NULL; @@ -574,14 +574,14 @@ static PySequenceMethods StringWriter_as_sequence = { .sq_item = (ssizeargfunc)StringWriter_item, }; -static PyObject* StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames); -static PyObject* StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames); +static PyObject* StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs); +static PyObject* StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs); static PyMethodDef StringWriter_methods[] = { - {"append", (PyCFunction) StringWriter_append, METH_FASTCALL | METH_KEYWORDS, + {"append", (PyCFunction) StringWriter_append, METH_FASTCALL, PyDoc_STR("Append a single character (as int codepoint) to the buffer") }, - {"write", (PyCFunction) StringWriter_write, METH_FASTCALL | METH_KEYWORDS, + {"write", (PyCFunction) StringWriter_write, METH_FASTCALL, PyDoc_STR("Append a string to the buffer") }, {"getvalue", (PyCFunction) StringWriter_getvalue, METH_NOARGS, @@ -661,16 +661,16 @@ StringWriter_write_internal(StringWriterObject *self, PyObject *value) { } static PyObject* -StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) { - static const char * const kwlist[] = {"value", 0}; - static CPyArg_Parser parser = {"O:write", kwlist, 0}; - PyObject *value; - if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) { +StringWriter_write(PyObject *self, PyObject *const *args, size_t nargs) { + if (unlikely(nargs != 1)) { + PyErr_Format(PyExc_TypeError, + "write() takes exactly 1 argument (%zu given)", nargs); return NULL; } if (!check_string_writer(self)) { return NULL; } + PyObject *value = args[0]; if (unlikely(!PyUnicode_Check(value))) { PyErr_SetString(PyExc_TypeError, "value must be a str object"); return NULL; @@ -795,16 +795,16 @@ StringWriter_append_internal(StringWriterObject *self, int32_t value) { } static PyObject* -StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames) { - static const char * const kwlist[] = {"value", 0}; - static CPyArg_Parser parser = {"O:append", kwlist, 0}; - PyObject *value; - if (unlikely(!CPyArg_ParseStackAndKeywordsSimple(args, nargs, kwnames, &parser, &value))) { +StringWriter_append(PyObject *self, PyObject *const *args, size_t nargs) { + if (unlikely(nargs != 1)) { + PyErr_Format(PyExc_TypeError, + "append() takes exactly 1 argument (%zu given)", nargs); return NULL; } if (!check_string_writer(self)) { return NULL; } + PyObject *value = args[0]; int32_t unboxed = CPyLong_AsInt32(value); if (unlikely(unboxed == CPY_LL_INT_ERROR && PyErr_Occurred())) { CPy_TypeError("i32", value); From d966cd31e18265d68aec03c560ea763bf91c3f3e Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 15 Jan 2026 15:41:33 +0000 Subject: [PATCH 3/3] Update librt.strings stub --- mypy/typeshed/stubs/librt/librt/strings.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/typeshed/stubs/librt/librt/strings.pyi b/mypy/typeshed/stubs/librt/librt/strings.pyi index 448b06bdebcf..92aa09c07ae9 100644 --- a/mypy/typeshed/stubs/librt/librt/strings.pyi +++ b/mypy/typeshed/stubs/librt/librt/strings.pyi @@ -4,7 +4,7 @@ from mypy_extensions import i64, i32, u8 @final class BytesWriter: - def append(self, /, x: int) -> None: ... + def append(self, x: int, /) -> None: ... def write(self, b: bytes | bytearray, /) -> None: ... def getvalue(self) -> bytes: ... def truncate(self, size: i64, /) -> None: ...