Skip to content

Commit dcdb23f

Browse files
authored
gh-143429: Use compile-time NaN encoding detection for test_struct (#143432)
1 parent 234a15d commit dcdb23f

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

Lib/test/test_capi/test_float.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import math
22
import random
3-
import platform
43
import sys
54
import unittest
65
import warnings
@@ -215,8 +214,8 @@ def test_pack_unpack_roundtrip_for_nans(self):
215214
# PyFloat_Pack/Unpack*() API. See also gh-130317 and
216215
# e.g. https://developercommunity.visualstudio.com/t/155064
217216
signaling = 0
218-
if platform.machine().startswith('parisc'):
219-
# HP PA RISC uses 0 for quiet, see:
217+
if _testcapi.nan_msb_is_signaling:
218+
# HP PA RISC and some MIPS CPUs use 0 for quiet, see:
220219
# https://en.wikipedia.org/wiki/NaN#Encoding
221220
signaling = 1
222221
i = make_nan(size, sign, not signaling)

Lib/test/test_struct.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import math
66
import operator
77
import unittest
8-
import platform
98
import struct
109
import sys
1110
import weakref
@@ -891,6 +890,7 @@ def test_module_func(self):
891890
self.assertRaises(StopIteration, next, it)
892891

893892
def test_half_float(self):
893+
_testcapi = import_helper.import_module('_testcapi')
894894
# Little-endian examples from:
895895
# http://en.wikipedia.org/wiki/Half_precision_floating-point_format
896896
format_bits_float__cleanRoundtrip_list = [
@@ -935,8 +935,8 @@ def test_half_float(self):
935935

936936
# Check that packing produces a bit pattern representing a quiet NaN:
937937
# 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:
938+
if _testcapi.nan_msb_is_signaling:
939+
# HP PA RISC and some MIPS CPUs use 0 for quiet, see:
940940
# https://en.wikipedia.org/wiki/NaN#Encoding
941941
expected = 0x7c
942942
else:

Modules/_testcapi/float.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,9 @@ _PyTestCapi_Init_Float(PyObject *mod)
171171
return -1;
172172
}
173173

174-
return 0;
174+
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
175+
return PyModule_Add(mod, "nan_msb_is_signaling", PyBool_FromLong(1));
176+
#else
177+
return PyModule_Add(mod, "nan_msb_is_signaling", PyBool_FromLong(0));
178+
#endif
175179
}

0 commit comments

Comments
 (0)