]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143429: Use compile-time NaN encoding detection for test_struct (#143432)
authorHenry Chen <chenx97@aosc.io>
Fri, 9 Jan 2026 10:46:48 +0000 (18:46 +0800)
committerGitHub <noreply@github.com>
Fri, 9 Jan 2026 10:46:48 +0000 (10:46 +0000)
Lib/test/test_capi/test_float.py
Lib/test/test_struct.py
Modules/_testcapi/float.c

index df7017e6436a694ac487250bf8c29c7a748b12a3..8b25607b6d504f7a2fad214cc6dd8e2da039c0d6 100644 (file)
@@ -1,6 +1,5 @@
 import math
 import random
-import platform
 import sys
 import unittest
 import warnings
@@ -215,8 +214,8 @@ class CAPIFloatTest(unittest.TestCase):
                     # PyFloat_Pack/Unpack*() API.  See also gh-130317 and
                     # e.g. https://developercommunity.visualstudio.com/t/155064
                     signaling = 0
-                    if platform.machine().startswith('parisc'):
-                        # HP PA RISC uses 0 for quiet, see:
+                    if _testcapi.nan_msb_is_signaling:
+                        # HP PA RISC and some MIPS CPUs use 0 for quiet, see:
                         # https://en.wikipedia.org/wiki/NaN#Encoding
                         signaling = 1
                 i = make_nan(size, sign, not signaling)
index cceecdd526c006f4eb998e0b8fd769a7cb51e3c0..bbfe19a4e0bab7e966d50d9e49db58dde81fc689 100644 (file)
@@ -5,7 +5,6 @@ import gc
 import math
 import operator
 import unittest
-import platform
 import struct
 import sys
 import weakref
@@ -891,6 +890,7 @@ class UnpackIteratorTest(unittest.TestCase):
         self.assertRaises(StopIteration, next, it)
 
     def test_half_float(self):
+        _testcapi = import_helper.import_module('_testcapi')
         # Little-endian examples from:
         # http://en.wikipedia.org/wiki/Half_precision_floating-point_format
         format_bits_float__cleanRoundtrip_list = [
@@ -935,8 +935,8 @@ class UnpackIteratorTest(unittest.TestCase):
 
         # Check that packing produces a bit pattern representing a quiet NaN:
         # all exponent bits and the msb of the fraction should all be 1.
-        if platform.machine().startswith('parisc'):
-            # HP PA RISC uses 0 for quiet, see:
+        if _testcapi.nan_msb_is_signaling:
+            # HP PA RISC and some MIPS CPUs use 0 for quiet, see:
             # https://en.wikipedia.org/wiki/NaN#Encoding
             expected = 0x7c
         else:
index e3869134c84d43c03b7951cd11867ca49ff08e94..63de77ca6b8651585de6f438eef41566f2a08d69 100644 (file)
@@ -171,5 +171,9 @@ _PyTestCapi_Init_Float(PyObject *mod)
         return -1;
     }
 
-    return 0;
+#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
+    return PyModule_Add(mod, "nan_msb_is_signaling", PyBool_FromLong(1));
+#else
+    return PyModule_Add(mod, "nan_msb_is_signaling", PyBool_FromLong(0));
+#endif
 }