diff --git a/mypyc/lib-rt/librt_internal.c b/mypyc/lib-rt/librt_internal.c index 4f6e138c96f9..eb864619d398 100644 --- a/mypyc/lib-rt/librt_internal.c +++ b/mypyc/lib-rt/librt_internal.c @@ -509,14 +509,18 @@ write_bytes(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnam /* float format: - stored as a C double + stored using PyFloat helpers in little-endian format. */ static double read_float_internal(PyObject *data) { _CHECK_BUFFER(data, CPY_FLOAT_ERROR) - _CHECK_READ(data, sizeof(double), CPY_FLOAT_ERROR) - double res = _READ(data, double) + _CHECK_READ(data, 8, CPY_FLOAT_ERROR) + char *buf = ((BufferObject *)data)->buf; + double res = PyFloat_Unpack8(buf + ((BufferObject *)data)->pos, 1); + if (unlikely((res == -1.0) && PyErr_Occurred())) + return CPY_FLOAT_ERROR; + ((BufferObject *)data)->pos += 8; return res; } @@ -538,9 +542,13 @@ read_float(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwname static char write_float_internal(PyObject *data, double value) { _CHECK_BUFFER(data, CPY_NONE_ERROR) - _CHECK_SIZE(data, sizeof(double)) - _WRITE(data, double, value) - ((BufferObject *)data)->end += sizeof(double); + _CHECK_SIZE(data, 8) + char *buf = ((BufferObject *)data)->buf; + int res = PyFloat_Pack8(value, buf + ((BufferObject *)data)->pos, 1); + if (unlikely(res == -1)) + return CPY_NONE_ERROR; + ((BufferObject *)data)->pos += 8; + ((BufferObject *)data)->end += 8; return CPY_NONE; } diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index 04bbed78b318..9c2ba14c0873 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -2741,6 +2741,7 @@ def test_buffer_roundtrip() -> None: write_bytes(b, b"a" * 117) write_bytes(b, b"a" * 118) write_float(b, 0.1) + write_float(b, -1.0) write_float(b, -113.0) write_int(b, 0) write_int(b, 1) @@ -2767,6 +2768,7 @@ def test_buffer_roundtrip() -> None: assert read_bytes(b) == b"a" * 117 assert read_bytes(b) == b"a" * 118 assert read_float(b) == 0.1 + assert read_float(b) == -1.0 assert read_float(b) == -113.0 assert read_int(b) == 0 assert read_int(b) == 1