Skip to content

Commit 78b7c3e

Browse files
committed
test struct nan packing
1 parent 3fe8383 commit 78b7c3e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

graalpython/com.oracle.graal.python.cext/src/floatobject.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ typedef enum {
1616
static float_format_type double_format, float_format;
1717
static float_format_type detected_double_format, detected_float_format;
1818

19+
__attribute__((constructor))
20+
static void init_formats(void) {
21+
#if SIZEOF_DOUBLE == 8
22+
{
23+
double x = 9006104071832581.0;
24+
if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
25+
detected_double_format = ieee_big_endian_format;
26+
else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
27+
detected_double_format = ieee_little_endian_format;
28+
else
29+
detected_double_format = unknown_format;
30+
}
31+
#else
32+
detected_double_format = unknown_format;
33+
#endif
34+
35+
#if SIZEOF_FLOAT == 4
36+
{
37+
float y = 16711938.0;
38+
if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
39+
detected_float_format = ieee_big_endian_format;
40+
else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
41+
detected_float_format = ieee_little_endian_format;
42+
else
43+
detected_float_format = unknown_format;
44+
}
45+
#else
46+
detected_float_format = unknown_format;
47+
#endif
48+
49+
double_format = detected_double_format;
50+
float_format = detected_float_format;
51+
}
52+
1953
UPCALL_ID(PyFloat_AsDouble);
2054
double PyFloat_AsDouble(PyObject *op) {
2155
return ((double (*)(void*))_jls_PyFloat_AsDouble)(native_to_java(op));

graalpython/com.oracle.graal.python.test/src/tests/test_struct.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,8 @@ def test_pack_unpack():
140140
_bytes = struct.pack(_fmt, val)
141141
_res = struct.unpack(_fmt, _bytes)[0]
142142
assert _res == res, "({}), {} != {}".format(_fmt, _res, res)
143+
144+
145+
def test_pack_nan():
146+
import math
147+
assert struct.pack('<d', math.nan) == b'\x00\x00\x00\x00\x00\x00\xf8\x7f'

0 commit comments

Comments
 (0)