From ac12e54b4f7a3a79c0cc7a8837bd49129d12eb51 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 4 Nov 2026 16:35:44 +0000 Subject: [PATCH 1/3] [mypyc] Add librt.strings read/write functions for i8, u16, u32 and u64 Add write_u16/u32/u64_le/be and read_u16/u32/u64_le/be for packed unsigned binary data, following the existing signed variants. These can be used as faster alternatives to the struct "H", "I" and "Q" format codes in compiled code. Also add write_i8 and read_i8. Reading a signed byte previously required i8(u8(b[n])), since i8(b[n]) raises OverflowError for values above 127. --- mypy/typeshed/stubs/librt/librt/strings.pyi | 16 +- mypyc/doc/librt_strings.rst | 68 ++++- mypyc/lib-rt/byteswriter_extra_ops.h | 152 +++++++++++ mypyc/lib-rt/strings/librt_strings.c | 204 +++++++++++++++ mypyc/primitives/librt_strings_ops.py | 138 ++++++++++ mypyc/test-data/irbuild-librt-strings.test | 118 +++++++++ mypyc/test-data/run-librt-strings.test | 264 ++++++++++++++++++++ 7 files changed, 956 insertions(+), 4 deletions(-) diff --git a/mypy/typeshed/stubs/librt/librt/strings.pyi b/mypy/typeshed/stubs/librt/librt/strings.pyi index af9ae60936d8e..b06ae319be428 100644 --- a/mypy/typeshed/stubs/librt/librt/strings.pyi +++ b/mypy/typeshed/stubs/librt/librt/strings.pyi @@ -1,6 +1,6 @@ from typing import final -from mypy_extensions import i64, i32, i16, u8 +from mypy_extensions import i8, i16, i32, i64, u8, u16, u32, u64 @final class BytesWriter: @@ -20,6 +20,8 @@ class StringWriter: def __len__(self) -> i64: ... def __getitem__(self, i: i64, /) -> i32: ... +def write_i8(b: BytesWriter, n: i8, /) -> None: ... +def read_i8(b: bytes, index: i64, /) -> i8: ... def write_i16_le(b: BytesWriter, n: i16, /) -> None: ... def write_i16_be(b: BytesWriter, n: i16, /) -> None: ... def read_i16_le(b: bytes, index: i64, /) -> i16: ... @@ -32,6 +34,18 @@ def write_i64_le(b: BytesWriter, n: i64, /) -> None: ... def write_i64_be(b: BytesWriter, n: i64, /) -> None: ... def read_i64_le(b: bytes, index: i64, /) -> i64: ... def read_i64_be(b: bytes, index: i64, /) -> i64: ... +def write_u16_le(b: BytesWriter, n: u16, /) -> None: ... +def write_u16_be(b: BytesWriter, n: u16, /) -> None: ... +def read_u16_le(b: bytes, index: i64, /) -> u16: ... +def read_u16_be(b: bytes, index: i64, /) -> u16: ... +def write_u32_le(b: BytesWriter, n: u32, /) -> None: ... +def write_u32_be(b: BytesWriter, n: u32, /) -> None: ... +def read_u32_le(b: bytes, index: i64, /) -> u32: ... +def read_u32_be(b: bytes, index: i64, /) -> u32: ... +def write_u64_le(b: BytesWriter, n: u64, /) -> None: ... +def write_u64_be(b: BytesWriter, n: u64, /) -> None: ... +def read_u64_le(b: bytes, index: i64, /) -> u64: ... +def read_u64_be(b: bytes, index: i64, /) -> u64: ... def write_f32_le(b: BytesWriter, n: float, /) -> None: ... def write_f32_be(b: BytesWriter, n: float, /) -> None: ... def read_f32_le(b: bytes, index: i64, /) -> float: ... diff --git a/mypyc/doc/librt_strings.rst b/mypyc/doc/librt_strings.rst index 6bb4beef6f25e..62fee85534a84 100644 --- a/mypyc/doc/librt_strings.rst +++ b/mypyc/doc/librt_strings.rst @@ -108,9 +108,9 @@ The ``write_*`` and ``read_*`` functions allow interpreting bytes as packed bina data. They can be used as (much) more efficient but lower-level alternatives to the stdlib :mod:`struct` module in compiled code. -There are no functions for reading or writing individual bytes. ``BytesWriter.append`` can -be used to insert a byte value, and ``b[n]`` can be used to read a byte value. Both -are fast operations in compiled code. +Individual unsigned byte values can be read using ``b[n]`` and written using +``BytesWriter.append``. Both are fast operations in compiled code. Use ``write_i8`` and +``read_i8`` to write and read a byte as a signed value. This example writes two binary values and reads them afterwards:: @@ -124,6 +124,14 @@ This example writes two binary values and reads them afterwards:: y = read_f64_le(data, 4) ... +.. function:: write_i8(b: BytesWriter, n: i8, /) -> None + + Append an 8-bit signed integer as a single byte. + +.. function:: read_i8(b: bytes, index: i64, /) -> i8 + + Read a byte at the given index as an 8-bit signed integer (-128 to 127). + .. function:: write_i16_le(b: BytesWriter, n: i16, /) -> None Append a 16-bit integer as a little-endian binary value. @@ -178,6 +186,60 @@ This example writes two binary values and reads them afterwards:: Read a 64-bit integer value starting at the given index as a big-endian binary value (8 bytes). +.. function:: write_u16_le(b: BytesWriter, n: u16, /) -> None + + Append a 16-bit unsigned integer as a little-endian binary value. + +.. function:: write_u16_be(b: BytesWriter, n: u16, /) -> None + + Append a 16-bit unsigned integer as a big-endian binary value. + +.. function:: read_u16_le(b: bytes, index: i64, /) -> u16 + + Read a 16-bit unsigned integer value starting at the given index as a little-endian + binary value (2 bytes). + +.. function:: read_u16_be(b: bytes, index: i64, /) -> u16 + + Read a 16-bit unsigned integer value starting at the given index as a big-endian + binary value (2 bytes). + +.. function:: write_u32_le(b: BytesWriter, n: u32, /) -> None + + Append a 32-bit unsigned integer as a little-endian binary value. + +.. function:: write_u32_be(b: BytesWriter, n: u32, /) -> None + + Append a 32-bit unsigned integer as a big-endian binary value. + +.. function:: read_u32_le(b: bytes, index: i64, /) -> u32 + + Read a 32-bit unsigned integer value starting at the given index as a little-endian + binary value (4 bytes). + +.. function:: read_u32_be(b: bytes, index: i64, /) -> u32 + + Read a 32-bit unsigned integer value starting at the given index as a big-endian + binary value (4 bytes). + +.. function:: write_u64_le(b: BytesWriter, n: u64, /) -> None + + Append a 64-bit unsigned integer as a little-endian binary value. + +.. function:: write_u64_be(b: BytesWriter, n: u64, /) -> None + + Append a 64-bit unsigned integer as a big-endian binary value. + +.. function:: read_u64_le(b: bytes, index: i64, /) -> u64 + + Read a 64-bit unsigned integer value starting at the given index as a little-endian + binary value (8 bytes). + +.. function:: read_u64_be(b: bytes, index: i64, /) -> u64 + + Read a 64-bit unsigned integer value starting at the given index as a big-endian + binary value (8 bytes). + .. function:: write_f32_le(b: BytesWriter, n: float, /) -> None Append a 32-bit floating-point value as a little-endian binary value. diff --git a/mypyc/lib-rt/byteswriter_extra_ops.h b/mypyc/lib-rt/byteswriter_extra_ops.h index 4aec322a730cd..2831297e5e190 100644 --- a/mypyc/lib-rt/byteswriter_extra_ops.h +++ b/mypyc/lib-rt/byteswriter_extra_ops.h @@ -65,6 +65,11 @@ static inline void CPyBytesWriter_SetItem(PyObject *obj, int64_t index, uint8_t // BytesWriter: Write integer operations (little-endian) +static inline char +CPyBytesWriter_WriteI8(PyObject *obj, int8_t value) { + return CPyBytesWriter_Append(obj, (uint8_t)value); +} + static inline char CPyBytesWriter_WriteI16LE(PyObject *obj, int16_t value) { BytesWriterObject *self = (BytesWriterObject *)obj; @@ -119,6 +124,68 @@ CPyBytesWriter_WriteI64BE(PyObject *obj, int64_t value) { return CPY_NONE; } +// BytesWriter: Write unsigned integer operations + +static inline char +CPyBytesWriter_WriteU16LE(PyObject *obj, uint16_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 2)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI16LEUnsafe(self, (int16_t)value); + return CPY_NONE; +} + +static inline char +CPyBytesWriter_WriteU16BE(PyObject *obj, uint16_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 2)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI16BEUnsafe(self, (int16_t)value); + return CPY_NONE; +} + +static inline char +CPyBytesWriter_WriteU32LE(PyObject *obj, uint32_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 4)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI32LEUnsafe(self, (int32_t)value); + return CPY_NONE; +} + +static inline char +CPyBytesWriter_WriteU32BE(PyObject *obj, uint32_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 4)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI32BEUnsafe(self, (int32_t)value); + return CPY_NONE; +} + +static inline char +CPyBytesWriter_WriteU64LE(PyObject *obj, uint64_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 8)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI64LEUnsafe(self, (int64_t)value); + return CPY_NONE; +} + +static inline char +CPyBytesWriter_WriteU64BE(PyObject *obj, uint64_t value) { + BytesWriterObject *self = (BytesWriterObject *)obj; + if (!CPyBytesWriter_EnsureSize(self, 8)) + return CPY_NONE_ERROR; + // Only the bit pattern matters, so reuse the signed implementation + BytesWriter_WriteI64BEUnsafe(self, (int64_t)value); + return CPY_NONE; +} + // BytesWriter: Write float operations static inline char @@ -162,6 +229,17 @@ CPyBytesWriter_WriteF64BE(PyObject *obj, double value) { // Helper function for bytes read error handling (negative index or out of range) void CPyBytes_ReadError(int64_t index, Py_ssize_t size); +static inline int8_t +CPyBytes_ReadI8(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index >= size)) { + CPyBytes_ReadError(index, size); + return CPY_LL_INT_ERROR; + } + return (int8_t)((const unsigned char *)PyBytes_AS_STRING(bytes_obj))[index]; +} + static inline int16_t CPyBytes_ReadI16LE(PyObject *bytes_obj, int64_t index) { // bytes_obj type is enforced by mypyc @@ -234,6 +312,80 @@ CPyBytes_ReadI64BE(PyObject *bytes_obj, int64_t index) { return CPyBytes_ReadI64BEUnsafe(data + index); } +// Bytes: Read unsigned integer operations + +static inline uint16_t +CPyBytes_ReadU16LE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 2)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint16_t)CPyBytes_ReadI16LEUnsafe(data + index); +} + +static inline uint16_t +CPyBytes_ReadU16BE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 2)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint16_t)CPyBytes_ReadI16BEUnsafe(data + index); +} + +static inline uint32_t +CPyBytes_ReadU32LE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 4)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint32_t)CPyBytes_ReadI32LEUnsafe(data + index); +} + +static inline uint32_t +CPyBytes_ReadU32BE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 4)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint32_t)CPyBytes_ReadI32BEUnsafe(data + index); +} + +static inline uint64_t +CPyBytes_ReadU64LE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 8)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint64_t)CPyBytes_ReadI64LEUnsafe(data + index); +} + +static inline uint64_t +CPyBytes_ReadU64BE(PyObject *bytes_obj, int64_t index) { + // bytes_obj type is enforced by mypyc + Py_ssize_t size = PyBytes_GET_SIZE(bytes_obj); + if (unlikely(index < 0 || index > size - 8)) { + CPyBytes_ReadError(index, size); + return CPY_LL_UINT_ERROR; + } + const unsigned char *data = (const unsigned char *)PyBytes_AS_STRING(bytes_obj); + return (uint64_t)CPyBytes_ReadI64BEUnsafe(data + index); +} + // Bytes: Read float operations static inline double diff --git a/mypyc/lib-rt/strings/librt_strings.c b/mypyc/lib-rt/strings/librt_strings.c index 153ef54454f36..ef0df125faa11 100644 --- a/mypyc/lib-rt/strings/librt_strings.c +++ b/mypyc/lib-rt/strings/librt_strings.c @@ -923,6 +923,30 @@ parse_read_args(PyObject *const *args, size_t nargs, const char *func_name, return (const unsigned char *)PyBytes_AS_STRING(bytes_obj); } +static PyObject* +write_i8(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_i8"); + if (bw == NULL) + return NULL; + int8_t unboxed = CPyLong_AsInt8(args[1]); + if (unlikely(unboxed == CPY_LL_INT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 1))) + return NULL; + bw->buf[bw->len] = (char)(uint8_t)unboxed; + bw->len += 1; + Py_RETURN_NONE; +} + +static PyObject* +read_i8(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_i8", 1, &index); + if (data == NULL) + return NULL; + return PyLong_FromLong((int8_t)data[index]); +} + static PyObject* write_i16_le(PyObject *module, PyObject *const *args, size_t nargs) { BytesWriterObject *bw = parse_write_args(args, nargs, "write_i16_le"); @@ -1061,6 +1085,144 @@ read_i64_be(PyObject *module, PyObject *const *args, size_t nargs) { return PyLong_FromLongLong(CPyBytes_ReadI64BEUnsafe(data + index)); } +static PyObject* +write_u16_le(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u16_le"); + if (bw == NULL) + return NULL; + uint16_t unboxed = CPyLong_AsUInt16(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 2))) + return NULL; + BytesWriter_WriteI16LEUnsafe(bw, (int16_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +write_u16_be(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u16_be"); + if (bw == NULL) + return NULL; + uint16_t unboxed = CPyLong_AsUInt16(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 2))) + return NULL; + BytesWriter_WriteI16BEUnsafe(bw, (int16_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +read_u16_le(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u16_le", 2, &index); + if (data == NULL) + return NULL; + return PyLong_FromLong((long)(uint16_t)CPyBytes_ReadI16LEUnsafe(data + index)); +} + +static PyObject* +read_u16_be(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u16_be", 2, &index); + if (data == NULL) + return NULL; + return PyLong_FromLong((long)(uint16_t)CPyBytes_ReadI16BEUnsafe(data + index)); +} + +static PyObject* +write_u32_le(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u32_le"); + if (bw == NULL) + return NULL; + uint32_t unboxed = CPyLong_AsUInt32(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 4))) + return NULL; + BytesWriter_WriteI32LEUnsafe(bw, (int32_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +write_u32_be(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u32_be"); + if (bw == NULL) + return NULL; + uint32_t unboxed = CPyLong_AsUInt32(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 4))) + return NULL; + BytesWriter_WriteI32BEUnsafe(bw, (int32_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +read_u32_le(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u32_le", 4, &index); + if (data == NULL) + return NULL; + return PyLong_FromUnsignedLong((unsigned long)(uint32_t)CPyBytes_ReadI32LEUnsafe(data + index)); +} + +static PyObject* +read_u32_be(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u32_be", 4, &index); + if (data == NULL) + return NULL; + return PyLong_FromUnsignedLong((unsigned long)(uint32_t)CPyBytes_ReadI32BEUnsafe(data + index)); +} + +static PyObject* +write_u64_le(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u64_le"); + if (bw == NULL) + return NULL; + uint64_t unboxed = CPyLong_AsUInt64(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 8))) + return NULL; + BytesWriter_WriteI64LEUnsafe(bw, (int64_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +write_u64_be(PyObject *module, PyObject *const *args, size_t nargs) { + BytesWriterObject *bw = parse_write_args(args, nargs, "write_u64_be"); + if (bw == NULL) + return NULL; + uint64_t unboxed = CPyLong_AsUInt64(args[1]); + if (unlikely(unboxed == CPY_LL_UINT_ERROR && PyErr_Occurred())) + return NULL; + if (unlikely(!ensure_bytes_writer_size(bw, 8))) + return NULL; + BytesWriter_WriteI64BEUnsafe(bw, (int64_t)unboxed); + Py_RETURN_NONE; +} + +static PyObject* +read_u64_le(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u64_le", 8, &index); + if (data == NULL) + return NULL; + return PyLong_FromUnsignedLongLong((unsigned long long)(uint64_t)CPyBytes_ReadI64LEUnsafe(data + index)); +} + +static PyObject* +read_u64_be(PyObject *module, PyObject *const *args, size_t nargs) { + int64_t index; + const unsigned char *data = parse_read_args(args, nargs, "read_u64_be", 8, &index); + if (data == NULL) + return NULL; + return PyLong_FromUnsignedLongLong((unsigned long long)(uint64_t)CPyBytes_ReadI64BEUnsafe(data + index)); +} + static PyObject* write_f32_le(PyObject *module, PyObject *const *args, size_t nargs) { BytesWriterObject *bw = parse_write_args(args, nargs, "write_f32_le"); @@ -1204,6 +1366,12 @@ DEFINE_CP_I32_WRAPPER(toupper, LibRTStrings_ToUpper) DEFINE_CP_I32_WRAPPER(tolower, LibRTStrings_ToLower) static PyMethodDef librt_strings_module_methods[] = { + {"write_i8", (PyCFunction) write_i8, METH_FASTCALL, + PyDoc_STR("Write an 8-bit signed integer to BytesWriter as a single byte") + }, + {"read_i8", (PyCFunction) read_i8, METH_FASTCALL, + PyDoc_STR("Read a byte from bytes as an 8-bit signed integer") + }, {"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL, PyDoc_STR("Write a 16-bit signed integer to BytesWriter in little-endian format") }, @@ -1240,6 +1408,42 @@ static PyMethodDef librt_strings_module_methods[] = { {"read_i64_be", (PyCFunction) read_i64_be, METH_FASTCALL, PyDoc_STR("Read a 64-bit signed integer from bytes in big-endian format") }, + {"write_u16_le", (PyCFunction) write_u16_le, METH_FASTCALL, + PyDoc_STR("Write a 16-bit unsigned integer to BytesWriter in little-endian format") + }, + {"write_u16_be", (PyCFunction) write_u16_be, METH_FASTCALL, + PyDoc_STR("Write a 16-bit unsigned integer to BytesWriter in big-endian format") + }, + {"read_u16_le", (PyCFunction) read_u16_le, METH_FASTCALL, + PyDoc_STR("Read a 16-bit unsigned integer from bytes in little-endian format") + }, + {"read_u16_be", (PyCFunction) read_u16_be, METH_FASTCALL, + PyDoc_STR("Read a 16-bit unsigned integer from bytes in big-endian format") + }, + {"write_u32_le", (PyCFunction) write_u32_le, METH_FASTCALL, + PyDoc_STR("Write a 32-bit unsigned integer to BytesWriter in little-endian format") + }, + {"write_u32_be", (PyCFunction) write_u32_be, METH_FASTCALL, + PyDoc_STR("Write a 32-bit unsigned integer to BytesWriter in big-endian format") + }, + {"read_u32_le", (PyCFunction) read_u32_le, METH_FASTCALL, + PyDoc_STR("Read a 32-bit unsigned integer from bytes in little-endian format") + }, + {"read_u32_be", (PyCFunction) read_u32_be, METH_FASTCALL, + PyDoc_STR("Read a 32-bit unsigned integer from bytes in big-endian format") + }, + {"write_u64_le", (PyCFunction) write_u64_le, METH_FASTCALL, + PyDoc_STR("Write a 64-bit unsigned integer to BytesWriter in little-endian format") + }, + {"write_u64_be", (PyCFunction) write_u64_be, METH_FASTCALL, + PyDoc_STR("Write a 64-bit unsigned integer to BytesWriter in big-endian format") + }, + {"read_u64_le", (PyCFunction) read_u64_le, METH_FASTCALL, + PyDoc_STR("Read a 64-bit unsigned integer from bytes in little-endian format") + }, + {"read_u64_be", (PyCFunction) read_u64_be, METH_FASTCALL, + PyDoc_STR("Read a 64-bit unsigned integer from bytes in big-endian format") + }, {"write_f32_le", (PyCFunction) write_f32_le, METH_FASTCALL, PyDoc_STR("Write a 32-bit float to BytesWriter in little-endian format") }, diff --git a/mypyc/primitives/librt_strings_ops.py b/mypyc/primitives/librt_strings_ops.py index f3fceb483f968..47dd769dd7540 100644 --- a/mypyc/primitives/librt_strings_ops.py +++ b/mypyc/primitives/librt_strings_ops.py @@ -6,6 +6,7 @@ bytes_rprimitive, bytes_writer_rprimitive, float_rprimitive, + int8_rprimitive, int16_rprimitive, int32_rprimitive, int64_rprimitive, @@ -14,6 +15,9 @@ str_rprimitive, string_writer_rprimitive, uint8_rprimitive, + uint16_rprimitive, + uint32_rprimitive, + uint64_rprimitive, void_rtype, ) from mypyc.primitives.registry import custom_primitive_op, function_op, method_op @@ -121,6 +125,26 @@ dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], ) +# i8 write/read functions + +function_op( + name="librt.strings.write_i8", + arg_types=[bytes_writer_rprimitive, int8_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteI8", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_i8", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=int8_rprimitive, + c_function_name="CPyBytes_ReadI8", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + # i16 write/read functions function_op( @@ -235,6 +259,120 @@ dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], ) +# u16 write/read functions + +function_op( + name="librt.strings.write_u16_le", + arg_types=[bytes_writer_rprimitive, uint16_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU16LE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.write_u16_be", + arg_types=[bytes_writer_rprimitive, uint16_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU16BE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u16_le", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint16_rprimitive, + c_function_name="CPyBytes_ReadU16LE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u16_be", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint16_rprimitive, + c_function_name="CPyBytes_ReadU16BE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +# u32 write/read functions + +function_op( + name="librt.strings.write_u32_le", + arg_types=[bytes_writer_rprimitive, uint32_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU32LE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.write_u32_be", + arg_types=[bytes_writer_rprimitive, uint32_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU32BE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u32_le", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint32_rprimitive, + c_function_name="CPyBytes_ReadU32LE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u32_be", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint32_rprimitive, + c_function_name="CPyBytes_ReadU32BE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +# u64 write/read functions + +function_op( + name="librt.strings.write_u64_le", + arg_types=[bytes_writer_rprimitive, uint64_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU64LE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.write_u64_be", + arg_types=[bytes_writer_rprimitive, uint64_rprimitive], + return_type=none_rprimitive, + c_function_name="CPyBytesWriter_WriteU64BE", + error_kind=ERR_MAGIC, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u64_le", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint64_rprimitive, + c_function_name="CPyBytes_ReadU64LE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + +function_op( + name="librt.strings.read_u64_be", + arg_types=[bytes_rprimitive, int64_rprimitive], + return_type=uint64_rprimitive, + c_function_name="CPyBytes_ReadU64BE", + error_kind=ERR_MAGIC_OVERLAPPING, + dependencies=[LIBRT_STRINGS, BYTES_WRITER_EXTRA_OPS], +) + # f32 write/read functions function_op( diff --git a/mypyc/test-data/irbuild-librt-strings.test b/mypyc/test-data/irbuild-librt-strings.test index 83523cb2468e0..748cea0cefb62 100644 --- a/mypyc/test-data/irbuild-librt-strings.test +++ b/mypyc/test-data/irbuild-librt-strings.test @@ -94,6 +94,38 @@ L2: CPyBytesWriter_SetItem(b, r0, x) return 1 +[case testLibrtStrings_i8_64bit] +from librt.strings import BytesWriter, write_i8, read_i8 +from mypy_extensions import i8, i64 + +def test_write_i8(b: BytesWriter, n: i8) -> None: + write_i8(b, n) +def test_write_i8_literal(b: BytesWriter) -> None: + write_i8(b, -5) +def test_read_i8(b: bytes, i: i64) -> i8: + return read_i8(b, i) +[out] +def test_write_i8(b, n): + b :: librt.strings.BytesWriter + n :: i8 + r0 :: None +L0: + r0 = CPyBytesWriter_WriteI8(b, n) + return 1 +def test_write_i8_literal(b): + b :: librt.strings.BytesWriter + r0 :: None +L0: + r0 = CPyBytesWriter_WriteI8(b, -5) + return 1 +def test_read_i8(b, i): + b :: bytes + i :: i64 + r0 :: i8 +L0: + r0 = CPyBytes_ReadI8(b, i) + return r0 + [case testLibrtStrings_write_i16_le_64bit] from librt.strings import BytesWriter, write_i16_le from mypy_extensions import i16 @@ -219,6 +251,92 @@ L0: r0 = CPyBytes_ReadI64LE(b, 0) return r0 +[case testLibrtStrings_unsigned_64bit] +from librt.strings import ( + BytesWriter, write_u16_le, read_u16_be, write_u32_be, read_u32_le, write_u64_le, read_u64_be, +) +from mypy_extensions import u16, u32, u64, i64 + +def test_u16(b: BytesWriter, n: u16, x: bytes, i: i64) -> u16: + write_u16_le(b, n) + write_u16_le(b, 0xFFFF) + return read_u16_be(x, i) +def test_u32(b: BytesWriter, n: u32, x: bytes, i: i64) -> u32: + write_u32_be(b, n) + write_u32_be(b, 0xFFFFFFFF) + return read_u32_le(x, i) +def test_u64(b: BytesWriter, n: u64, x: bytes, i: i64) -> u64: + write_u64_le(b, n) + write_u64_le(b, 0xFFFFFFFFFFFFFFFF) + return read_u64_be(x, i) +[out] +def test_u16(b, n, x, i): + b :: librt.strings.BytesWriter + n :: u16 + x :: bytes + i :: i64 + r0, r1 :: None + r2 :: u16 +L0: + r0 = CPyBytesWriter_WriteU16LE(b, n) + r1 = CPyBytesWriter_WriteU16LE(b, 65535) + r2 = CPyBytes_ReadU16BE(x, i) + return r2 +def test_u32(b, n, x, i): + b :: librt.strings.BytesWriter + n :: u32 + x :: bytes + i :: i64 + r0, r1 :: None + r2 :: u32 +L0: + r0 = CPyBytesWriter_WriteU32BE(b, n) + r1 = CPyBytesWriter_WriteU32BE(b, 4294967295) + r2 = CPyBytes_ReadU32LE(x, i) + return r2 +def test_u64(b, n, x, i): + b :: librt.strings.BytesWriter + n :: u64 + x :: bytes + i :: i64 + r0 :: None + r1 :: int + r2 :: native_int + r3, r4 :: bit + r5, r6 :: u64 + r7 :: ptr + r8 :: c_ptr + r9 :: u64 + r10 :: None + r11 :: u64 +L0: + r0 = CPyBytesWriter_WriteU64LE(b, n) + r1 = object 18446744073709551615 + r2 = r1 & 1 + r3 = r2 == 0 + if r3 goto L1 else goto L3 :: bool +L1: + r4 = r1 >= 0 :: signed + if r4 goto L2 else goto L4 :: bool +L2: + r5 = r1 >> 1 + r6 = r5 + goto L5 +L3: + r7 = r1 ^ 1 + r8 = r7 + r9 = CPyLong_AsUInt64(r8) + r6 = r9 + keep_alive r1 + goto L5 +L4: + CPyUInt64_Overflow() + unreachable +L5: + r10 = CPyBytesWriter_WriteU64LE(b, r6) + r11 = CPyBytes_ReadU64BE(x, i) + return r11 + [case testLibrtStrings_StringWriter_64bit] from librt.strings import StringWriter from mypy_extensions import i32, i64 diff --git a/mypyc/test-data/run-librt-strings.test b/mypyc/test-data/run-librt-strings.test index 333b5962c4c99..e48f506011409 100644 --- a/mypyc/test-data/run-librt-strings.test +++ b/mypyc/test-data/run-librt-strings.test @@ -1440,6 +1440,270 @@ def test_new_without_init_is_usable() -> None: sw.write("hello") assert sw.getvalue() == "hello" +[case testLibrtStringsI8AndUnsigned_librt] +from typing import Any +import struct + +from librt.strings import ( + BytesWriter, + write_i8, + read_i8, + write_u16_le, write_u16_be, read_u16_le, read_u16_be, write_u32_le, write_u32_be, + read_u32_le, read_u32_be, write_u64_le, write_u64_be, read_u64_le, read_u64_be, +) +from mypy_extensions import i8, u8, u16, u32, u64, i64 + +from testutil import assertRaises + +# 239 is the mypyc overlapping error value for unsigned native ints +U16_TEST_VALUES: list[int] = [0, 1, 127, 128, 239, 255, 256, 0x1234, 0x7FFF, 0x8000, 0xFFFE, 0xFFFF] +U32_TEST_VALUES: list[int] = U16_TEST_VALUES + [ + 0x10000, 0x123456, 0x12345678, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFE, 0xFFFFFFFF, +] +U64_TEST_VALUES: list[int] = U32_TEST_VALUES + [ + 0x100000000, 0x123456789ABCDEF0, 0x7FFFFFFFFFFFFFFF, 0x8000000000000000, + 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, +] + +def test_write_read_i8() -> None: + data = bytes(range(256)) + for i in range(256): + expected = i - 256 if i >= 128 else i + assert read_i8(data, i) == expected + # -113 is the mypyc overlapping error value for signed native ints + assert read_i8(b"\x8f", 0) == -113 + n: i64 = 3 + x: i8 = read_i8(b"\x00\x01\xff", n - 1) + assert x == -1 + + # Round trip through BytesWriter + w = BytesWriter() + for v in (-128, -113, -1, 0, 1, 127): + write_i8(w, i8(v)) + result = w.getvalue() + assert result == b"\x80\x8f\xff\x00\x01\x7f" + assert [read_i8(result, j) for j in range(6)] == [-128, -113, -1, 0, 1, 127] + + with assertRaises(IndexError, "index 3 out of range for bytes of length 3"): + read_i8(b"abc", n) + with assertRaises(IndexError): + read_i8(b"", 0) + with assertRaises(ValueError, "index must be non-negative"): + read_i8(b"abc", -1) + + # Buffer growth, mixed with other writes + w = BytesWriter() + for j in range(600): + write_i8(w, i8(j % 256 - 128)) + w.append(u8(j % 256)) + result = w.getvalue() + assert len(result) == 1200 + for j in range(600): + assert read_i8(result, 2 * j) == j % 256 - 128 + assert result[2 * j + 1] == j % 256 + +def test_write_i8_via_any() -> None: + f: Any = write_i8 + w: Any = BytesWriter() + for v in (-128, -113, -1, 0, 127): + f(w, v) + assert w.getvalue() == b"\x80\x8f\xff\x00\x7f" + with assertRaises(ValueError, "int too large to convert to i8"): + f(w, 128 + int()) + with assertRaises(ValueError, "int too large to convert to i8"): + f(w, -129 + int()) + with assertRaises(TypeError): + f(w, "x") + with assertRaises(TypeError): + f(bytearray(), 1) + with assertRaises(TypeError): + f(w) + assert w.getvalue() == b"\x80\x8f\xff\x00\x7f" + +def test_read_i8_via_any() -> None: + f: Any = read_i8 + assert f(b"\x7f\x80\xff", 0) == 127 + assert f(b"\x7f\x80\xff", 1) == -128 + assert f(b"\x7f\x80\xff", 2) == -1 + with assertRaises(IndexError, "index 3 out of range for bytes of length 3"): + f(b"abc", 3) + with assertRaises(ValueError, "index must be non-negative"): + f(b"abc", -1) + with assertRaises(TypeError): + f(bytearray(b"abc"), 0) + with assertRaises(TypeError): + f(b"abc") + +def test_u16() -> None: + w = BytesWriter() + for v in U16_TEST_VALUES: + write_u16_le(w, u16(v)) + data = w.getvalue() + assert data == b"".join(struct.pack("H", v) for v in U16_TEST_VALUES) + for i, v in enumerate(U16_TEST_VALUES): + assert read_u16_be(data, i * 2) == v + + x: u16 = 0xFFFF + w = BytesWriter() + write_u16_le(w, x) + write_u16_be(w, x - 1) + data = w.getvalue() + assert data == b"\xff\xff\xff\xfe" + assert read_u16_le(data, 0) == x + assert read_u16_be(data, 2) == x - 1 + # Unaligned read + assert read_u16_le(b"\x00\x34\x12", 1) == 0x1234 + assert read_u16_be(b"\x00\x12\x34", 1) == 0x1234 + +def test_u32() -> None: + w = BytesWriter() + for v in U32_TEST_VALUES: + write_u32_le(w, u32(v)) + data = w.getvalue() + assert data == b"".join(struct.pack("I", v) for v in U32_TEST_VALUES) + for i, v in enumerate(U32_TEST_VALUES): + assert read_u32_be(data, i * 4) == v + + x: u32 = 0xFFFFFFFF + w = BytesWriter() + write_u32_le(w, x) + write_u32_be(w, x - 1) + data = w.getvalue() + assert data == b"\xff\xff\xff\xff\xff\xff\xff\xfe" + assert read_u32_le(data, 0) == x + assert read_u32_be(data, 4) == x - 1 + assert read_u32_le(b"\x00\x78\x56\x34\x12", 1) == 0x12345678 + assert read_u32_be(b"\x00\x12\x34\x56\x78", 1) == 0x12345678 + +def test_u64() -> None: + w = BytesWriter() + for v in U64_TEST_VALUES: + write_u64_le(w, u64(v)) + data = w.getvalue() + assert data == b"".join(struct.pack("Q", v) for v in U64_TEST_VALUES) + for i, v in enumerate(U64_TEST_VALUES): + assert read_u64_be(data, i * 8) == v + + x: u64 = 0xFFFFFFFFFFFFFFFF + w = BytesWriter() + write_u64_le(w, x) + write_u64_be(w, x - 1) + data = w.getvalue() + assert data == b"\xff" * 15 + b"\xfe" + assert read_u64_le(data, 0) == x + assert read_u64_be(data, 8) == x - 1 + assert read_u64_le(b"\x00" + struct.pack("Q", 0x123456789ABCDEF0), 1) == 0x123456789ABCDEF0 + +def test_buffer_growth() -> None: + w = BytesWriter() + w.append(0xAA) + for i in range(200): + write_u16_le(w, u16(i)) + write_u32_be(w, u32(i)) + write_u64_le(w, u64(i)) + w.append(0xBB) + data = w.getvalue() + assert len(data) == 2 + 200 * 14 + assert data[0] == 0xAA + assert data[-1] == 0xBB + for i in range(200): + base = 1 + i * 14 + assert read_u16_le(data, base) == i + assert read_u32_be(data, base + 2) == i + assert read_u64_le(data, base + 6) == i + +def test_read_out_of_range() -> None: + data = b"\x01\x02\x03\x04\x05\x06\x07" + n: i64 = len(data) + with assertRaises(IndexError): + read_u16_le(data, n - 1) + with assertRaises(ValueError, "index must be non-negative"): + read_u16_be(data, -1) + with assertRaises(IndexError): + read_u32_le(data, n - 3) + with assertRaises(ValueError, "index must be non-negative"): + read_u32_be(data, -1) + with assertRaises(IndexError): + read_u64_le(data, 0) + with assertRaises(IndexError): + read_u64_be(b"", 0) + assert read_u16_le(data, n - 2) == 0x0706 + assert read_u32_be(data, n - 4) == 0x04050607 + +WRITE_FUNCS: list[tuple[Any, str, int]] = [ + (write_u16_le, "H", 16), + (write_u32_le, "I", 32), + (write_u64_le, "Q", 64), +] +READ_FUNCS: list[tuple[Any, str, int]] = [ + (read_u16_le, "H", 16), + (read_u32_le, "I", 32), + (read_u64_le, "Q", 64), +] + +def test_write_via_any() -> None: + for write_func, fmt, bits in WRITE_FUNCS: + values = [0, 1, 239, (1 << bits) - 1, 1 << (bits - 1), 0x1234] + w: Any = BytesWriter() + for v in values: + write_func(w, v) + assert w.getvalue() == b"".join(struct.pack(fmt, v) for v in values) + + msg = f"int too large or small to convert to u{bits}" + with assertRaises(ValueError, msg): + write_func(w, (1 << bits) + int()) + with assertRaises(ValueError, msg): + write_func(w, -1 + int()) + with assertRaises(TypeError): + write_func(w, "x") + with assertRaises(TypeError): + write_func(bytearray(), 1) + with assertRaises(TypeError): + write_func(w) + +def test_read_via_any() -> None: + for read_func, fmt, bits in READ_FUNCS: + values = [0, 239, (1 << bits) - 1, 1 << (bits - 1)] + data: Any = b"".join(struct.pack(fmt, v) for v in values) + size = bits // 8 + for i, v in enumerate(values): + assert read_func(data, i * size) == v + with assertRaises(IndexError, f"index {len(data)} out of range for bytes of length {len(data)}"): + read_func(data, len(data) + int()) + with assertRaises(IndexError): + read_func(data, len(data) - size + 1 + int()) + with assertRaises(ValueError, "index must be non-negative"): + read_func(data, -1 + int()) + with assertRaises(TypeError): + read_func(bytearray(data), 0) + with assertRaises(TypeError): + read_func(data) + [case testLibrtStringsCodepointClassifiers_librt] from typing import Any from mypy_extensions import i32 From 524b35c0c45d855beef70364348f72f3373c4615 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 12 Nov 2026 18:01:00 +0000 Subject: [PATCH 2/3] Improve docs --- mypyc/doc/librt_strings.rst | 139 +++++++++++++++++++++++++++++------- 1 file changed, 113 insertions(+), 26 deletions(-) diff --git a/mypyc/doc/librt_strings.rst b/mypyc/doc/librt_strings.rst index 62fee85534a84..7333c8681f9a6 100644 --- a/mypyc/doc/librt_strings.rst +++ b/mypyc/doc/librt_strings.rst @@ -112,6 +112,10 @@ Individual unsigned byte values can be read using ``b[n]`` and written using ``BytesWriter.append``. Both are fast operations in compiled code. Use ``write_i8`` and ``read_i8`` to write and read a byte as a signed value. +The ``read_*`` functions raise ``ValueError`` if the index is negative, and ``IndexError`` +if the value doesn't fit within the data starting at the index. Unlike ``b[n]``, they don't +support negative indices that count from the end. + This example writes two binary values and reads them afterwards:: def example() -> None: @@ -124,66 +128,149 @@ This example writes two binary values and reads them afterwards:: y = read_f64_le(data, 4) ... +This table shows the :mod:`struct` format codes that correspond to these functions (``<`` +is little-endian and ``>`` is big-endian): + +.. list-table:: + :header-rows: 1 + + * - Format + - Type + - Write function + - Read function + * - ``b`` + - ``i8`` + - :func:`write_i8` + - :func:`read_i8` + * - ``B`` + - ``u8`` + - :meth:`BytesWriter.append` + - ``b[n]`` + * - ``h`` + - ``i16`` + - :func:`write_i16_be` + - :func:`read_i16_be` + * - ``i`` + - ``i32`` + - :func:`write_i32_be` + - :func:`read_i32_be` + * - ``q`` + - ``i64`` + - :func:`write_i64_be` + - :func:`read_i64_be` + * - ``H`` + - ``u16`` + - :func:`write_u16_be` + - :func:`read_u16_be` + * - ``I`` + - ``u32`` + - :func:`write_u32_be` + - :func:`read_u32_be` + * - ``Q`` + - ``u64`` + - :func:`write_u64_be` + - :func:`read_u64_be` + * - ``f`` + - ``float`` + - :func:`write_f32_be` + - :func:`read_f32_be` + * - ``d`` + - ``float`` + - :func:`write_f64_be` + - :func:`read_f64_be` + .. function:: write_i8(b: BytesWriter, n: i8, /) -> None - Append an 8-bit signed integer as a single byte. + Append an 8-bit signed integer as a binary value (1 byte). .. function:: read_i8(b: bytes, index: i64, /) -> i8 - Read a byte at the given index as an 8-bit signed integer (-128 to 127). + Read an 8-bit signed integer value at the given index (1 byte). .. function:: write_i16_le(b: BytesWriter, n: i16, /) -> None - Append a 16-bit integer as a little-endian binary value. + Append a 16-bit signed integer as a little-endian binary value. .. function:: write_i16_be(b: BytesWriter, n: i16, /) -> None - Append a 16-bit integer as a big-endian binary value. + Append a 16-bit signed integer as a big-endian binary value. .. function:: read_i16_le(b: bytes, index: i64, /) -> i16 - Read a 16-bit integer value starting at the given index as a little-endian binary value + Read a 16-bit signed integer value starting at the given index as a little-endian binary value (2 bytes). .. function:: read_i16_be(b: bytes, index: i64, /) -> i16 - Read a 16-bit integer value starting at the given index as a big-endian binary value + Read a 16-bit signed integer value starting at the given index as a big-endian binary value (2 bytes). .. function:: write_i32_le(b: BytesWriter, n: i32, /) -> None - Append a 32-bit integer as a little-endian binary value. + Append a 32-bit signed integer as a little-endian binary value. .. function:: write_i32_be(b: BytesWriter, n: i32, /) -> None - Append a 32-bit integer as a big-endian binary value. + Append a 32-bit signed integer as a big-endian binary value. .. function:: read_i32_le(b: bytes, index: i64, /) -> i32 - Read a 32-bit integer value starting at the given index as a little-endian binary value + Read a 32-bit signed integer value starting at the given index as a little-endian binary value (4 bytes). .. function:: read_i32_be(b: bytes, index: i64, /) -> i32 - Read a 32-bit integer value starting at the given index as a big-endian binary value + Read a 32-bit signed integer value starting at the given index as a big-endian binary value (4 bytes). .. function:: write_i64_le(b: BytesWriter, n: i64, /) -> None - Append a 64-bit integer as a little-endian binary value. + Append a 64-bit signed integer as a little-endian binary value. .. function:: write_i64_be(b: BytesWriter, n: i64, /) -> None - Append a 64-bit integer as a big-endian binary value. + Append a 64-bit signed integer as a big-endian binary value. .. function:: read_i64_le(b: bytes, index: i64, /) -> i64 - Read a 64-bit integer value starting at the given index as a little-endian binary value + Read a 64-bit signed integer value starting at the given index as a little-endian binary value (8 bytes). .. function:: read_i64_be(b: bytes, index: i64, /) -> i64 - Read a 64-bit integer value starting at the given index as a big-endian binary value + Read a 64-bit signed integer value starting at the given index as a big-endian binary value (8 bytes). .. function:: write_u16_le(b: BytesWriter, n: u16, /) -> None @@ -196,13 +283,13 @@ This example writes two binary values and reads them afterwards:: .. function:: read_u16_le(b: bytes, index: i64, /) -> u16 - Read a 16-bit unsigned integer value starting at the given index as a little-endian - binary value (2 bytes). + Read a 16-bit unsigned integer value starting at the given index as a little-endian binary value + (2 bytes). .. function:: read_u16_be(b: bytes, index: i64, /) -> u16 - Read a 16-bit unsigned integer value starting at the given index as a big-endian - binary value (2 bytes). + Read a 16-bit unsigned integer value starting at the given index as a big-endian binary value + (2 bytes). .. function:: write_u32_le(b: BytesWriter, n: u32, /) -> None @@ -214,13 +301,13 @@ This example writes two binary values and reads them afterwards:: .. function:: read_u32_le(b: bytes, index: i64, /) -> u32 - Read a 32-bit unsigned integer value starting at the given index as a little-endian - binary value (4 bytes). + Read a 32-bit unsigned integer value starting at the given index as a little-endian binary value + (4 bytes). .. function:: read_u32_be(b: bytes, index: i64, /) -> u32 - Read a 32-bit unsigned integer value starting at the given index as a big-endian - binary value (4 bytes). + Read a 32-bit unsigned integer value starting at the given index as a big-endian binary value + (4 bytes). .. function:: write_u64_le(b: BytesWriter, n: u64, /) -> None @@ -232,13 +319,13 @@ This example writes two binary values and reads them afterwards:: .. function:: read_u64_le(b: bytes, index: i64, /) -> u64 - Read a 64-bit unsigned integer value starting at the given index as a little-endian - binary value (8 bytes). + Read a 64-bit unsigned integer value starting at the given index as a little-endian binary value + (8 bytes). .. function:: read_u64_be(b: bytes, index: i64, /) -> u64 - Read a 64-bit unsigned integer value starting at the given index as a big-endian - binary value (8 bytes). + Read a 64-bit unsigned integer value starting at the given index as a big-endian binary value + (8 bytes). .. function:: write_f32_le(b: BytesWriter, n: float, /) -> None From 2176542d45bd59eaf56927bc8eccd8faacf74721 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 12 Nov 2026 18:06:49 +0000 Subject: [PATCH 3/3] Update test --- mypyc/test-data/irbuild-librt-strings.test | 37 +++------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/mypyc/test-data/irbuild-librt-strings.test b/mypyc/test-data/irbuild-librt-strings.test index 748cea0cefb62..85f6295abde06 100644 --- a/mypyc/test-data/irbuild-librt-strings.test +++ b/mypyc/test-data/irbuild-librt-strings.test @@ -301,41 +301,14 @@ def test_u64(b, n, x, i): i :: i64 r0 :: None r1 :: int - r2 :: native_int - r3, r4 :: bit - r5, r6 :: u64 - r7 :: ptr - r8 :: c_ptr - r9 :: u64 - r10 :: None - r11 :: u64 + r2 :: None + r3 :: u64 L0: r0 = CPyBytesWriter_WriteU64LE(b, n) r1 = object 18446744073709551615 - r2 = r1 & 1 - r3 = r2 == 0 - if r3 goto L1 else goto L3 :: bool -L1: - r4 = r1 >= 0 :: signed - if r4 goto L2 else goto L4 :: bool -L2: - r5 = r1 >> 1 - r6 = r5 - goto L5 -L3: - r7 = r1 ^ 1 - r8 = r7 - r9 = CPyLong_AsUInt64(r8) - r6 = r9 - keep_alive r1 - goto L5 -L4: - CPyUInt64_Overflow() - unreachable -L5: - r10 = CPyBytesWriter_WriteU64LE(b, r6) - r11 = CPyBytes_ReadU64BE(x, i) - return r11 + r2 = CPyBytesWriter_WriteU64LE(b, 18446744073709551615) + r3 = CPyBytes_ReadU64BE(x, i) + return r3 [case testLibrtStrings_StringWriter_64bit] from librt.strings import StringWriter