Skip to content

Commit 6674a76

Browse files
committed
Use compile-time NaN encoding detection for test_struct
1 parent f5bddb5 commit 6674a76

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Lib/test/test_struct.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
INF = float('inf')
2424
NAN = float('nan')
2525

26+
_testcapi = import_helper.import_module('_testcapi')
27+
2628
def iter_integer_formats(byteorders=byteorders):
2729
for code in integer_codes:
2830
for byteorder in byteorders:
@@ -935,21 +937,17 @@ def test_half_float(self):
935937

936938
# Check that packing produces a bit pattern representing a quiet NaN:
937939
# all exponent bits and the msb of the fraction should all be 1.
938-
if platform.machine().startswith('parisc'):
939-
# HP PA RISC uses 0 for quiet, see:
940+
if _testcapi.nan_encoding == 'parisc':
941+
# HP PA RISC and some MIPS CPUs use 0 for quiet, see:
940942
# https://en.wikipedia.org/wiki/NaN#Encoding
941943
expected = 0x7c
942944
else:
943945
expected = 0x7e
944946

945-
# Skip NaN encoding checks for MIPS because `math.nan` changes its value
946-
# depending on toolchain settings. See:
947-
# https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html#index-mnan_003d2008
948-
if not platform.machine().startswith('mips'):
949-
packed = struct.pack('<e', math.nan)
950-
self.assertEqual(packed[1] & 0x7e, expected)
951-
packed = struct.pack('<e', -math.nan)
952-
self.assertEqual(packed[1] & 0x7e, expected)
947+
packed = struct.pack('<e', math.nan)
948+
self.assertEqual(packed[1] & 0x7e, expected)
949+
packed = struct.pack('<e', -math.nan)
950+
self.assertEqual(packed[1] & 0x7e, expected)
953951

954952
# Checks for round-to-even behavior
955953
format_bits_float__rounding_list = [

Modules/_testcapimodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,6 +3359,12 @@ _testcapi_exec(PyObject *m)
33593359
PyModule_AddObject(m, "INT64_MAX", PyLong_FromInt64(INT64_MAX));
33603360
PyModule_AddObject(m, "UINT64_MAX", PyLong_FromUInt64(UINT64_MAX));
33613361

3362+
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
3363+
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc"));
3364+
#else
3365+
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular"));
3366+
#endif
3367+
33623368
if (PyModule_AddIntMacro(m, _Py_STACK_GROWS_DOWN)) {
33633369
return -1;
33643370
}

0 commit comments

Comments
 (0)