Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions mypyc/lib-rt/librt_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading