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
4 changes: 2 additions & 2 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cdef extern from "pack.h":
int msgpack_pack_false(msgpack_packer* pk) except -1
int msgpack_pack_long_long(msgpack_packer* pk, long long d) except -1
int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d) except -1
int msgpack_pack_float(msgpack_packer* pk, float d) except -1
int msgpack_pack_float(msgpack_packer* pk, double d) except -1
int msgpack_pack_double(msgpack_packer* pk, double d) except -1
int msgpack_pack_array(msgpack_packer* pk, size_t l) except -1
int msgpack_pack_map(msgpack_packer* pk, size_t l) except -1
Expand Down Expand Up @@ -179,7 +179,7 @@ cdef class Packer:
raise OverflowError("Integer value out of range")
elif PyFloat_CheckExact(o) if strict else PyFloat_Check(o):
if self.use_float:
msgpack_pack_float(&self.pk, <float>o)
msgpack_pack_float(&self.pk, <double>o)
else:
msgpack_pack_double(&self.pk, <double>o)
elif PyBytesLike_CheckExact(o) if strict else PyBytesLike_Check(o):
Expand Down
6 changes: 3 additions & 3 deletions msgpack/pack_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,15 @@ static inline int msgpack_pack_unsigned_long_long(msgpack_packer* x, unsigned lo
* Float
*/

static inline int msgpack_pack_float(msgpack_packer* x, float d)
static inline int msgpack_pack_float(msgpack_packer* x, double d)
{
unsigned char buf[5];
buf[0] = 0xca;

#if PY_VERSION_HEX >= 0x030B00A7
PyFloat_Pack4(d, (char *)&buf[1], 0);
if (PyFloat_Pack4(d, (char *)&buf[1], 0) < 0) { return -1; }
#else
_PyFloat_Pack4(d, &buf[1], 0);
if (_PyFloat_Pack4(d, &buf[1], 0) < 0) { return -1; }
#endif
msgpack_pack_append_buffer(x, buf, 5);
}
Expand Down
11 changes: 11 additions & 0 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def test_integer():
packb(x + 1)


def test_single_float():
x = 3.4028234663852886e38 # FLT_MAX
assert unpackb(packb(x, use_single_float=True)) == x
with pytest.raises(PackOverflowError):
packb(x * 2, use_single_float=True)

# Infinities are representable in single precision, so they must not raise.
for x in (float("inf"), float("-inf")):
assert unpackb(packb(x, use_single_float=True)) == x


def test_array_header():
packer = Packer()
packer.pack_array_header(2**32 - 1)
Expand Down
Loading