From 0bbf71e637d5ea0be797b8f1125a58edbb702d32 Mon Sep 17 00:00:00 2001 From: Henry Chen Date: Mon, 5 Jan 2026 17:15:40 +0800 Subject: [PATCH 1/6] Skip NaN encoding assertions for MIPS --- Lib/test/test_struct.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index cceecdd526c006..d61f3167010af9 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -942,10 +942,15 @@ def test_half_float(self): else: expected = 0x7e - packed = struct.pack(' Date: Mon, 5 Jan 2026 17:58:06 +0800 Subject: [PATCH 2/6] Remove redundant comments --- Lib/test/test_struct.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index d61f3167010af9..cc4f54ea965242 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -944,7 +944,6 @@ def test_half_float(self): # Skip NaN encoding checks for MIPS because `math.nan` changes its value # depending on toolchain settings. See: - # https://en.wikipedia.org/wiki/NaN#Encoding # https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/MIPS-Options.html#index-mnan_003d2008 if not platform.machine().startswith('mips'): packed = struct.pack(' Date: Mon, 5 Jan 2026 18:15:06 +0800 Subject: [PATCH 3/6] Use the unversioned gcc doc link --- Lib/test/test_struct.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index cc4f54ea965242..e9f6c97a80716a 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -944,7 +944,7 @@ def test_half_float(self): # Skip NaN encoding checks for MIPS because `math.nan` changes its value # depending on toolchain settings. See: - # https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/MIPS-Options.html#index-mnan_003d2008 + # https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html#index-mnan_003d2008 if not platform.machine().startswith('mips'): packed = struct.pack(' Date: Wed, 7 Jan 2026 20:33:16 +0800 Subject: [PATCH 4/6] Use compile-time NaN encoding detection for test_struct --- Lib/test/test_struct.py | 18 ++++++++---------- Modules/_testcapimodule.c | 6 ++++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index e9f6c97a80716a..112bd1ef0c3c90 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -23,6 +23,8 @@ INF = float('inf') NAN = float('nan') +_testcapi = import_helper.import_module('_testcapi') + def iter_integer_formats(byteorders=byteorders): for code in integer_codes: for byteorder in byteorders: @@ -935,21 +937,17 @@ def test_half_float(self): # 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_encoding == 'parisc': + # HP PA RISC and some MIPS CPUs use 0 for quiet, see: # https://en.wikipedia.org/wiki/NaN#Encoding expected = 0x7c else: expected = 0x7e - # Skip NaN encoding checks for MIPS because `math.nan` changes its value - # depending on toolchain settings. See: - # https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html#index-mnan_003d2008 - if not platform.machine().startswith('mips'): - packed = struct.pack(' Date: Wed, 7 Jan 2026 20:36:54 +0800 Subject: [PATCH 5/6] Remove unused imports --- Lib/test/test_struct.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 112bd1ef0c3c90..67e5701a881b5d 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -5,7 +5,6 @@ import math import operator import unittest -import platform import struct import sys import weakref From 376c04db93fdb4b2e49fd5c2a27f11decd5e8d8b Mon Sep 17 00:00:00 2001 From: Henry Chen Date: Thu, 8 Jan 2026 22:30:47 +0800 Subject: [PATCH 6/6] Move nan flag to Modules/_testcapi/float.c and use bool ... and migrate another parisc detection to the new nan flag --- Lib/test/test_capi/test_float.py | 5 ++--- Lib/test/test_struct.py | 5 ++--- Modules/_testcapi/float.c | 6 +++++- Modules/_testcapimodule.c | 6 ------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index df7017e6436a69..8b25607b6d504f 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -1,6 +1,5 @@ import math import random -import platform import sys import unittest import warnings @@ -215,8 +214,8 @@ def test_pack_unpack_roundtrip_for_nans(self): # 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) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 67e5701a881b5d..bbfe19a4e0bab7 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -22,8 +22,6 @@ INF = float('inf') NAN = float('nan') -_testcapi = import_helper.import_module('_testcapi') - def iter_integer_formats(byteorders=byteorders): for code in integer_codes: for byteorder in byteorders: @@ -892,6 +890,7 @@ def test_module_func(self): 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 = [ @@ -936,7 +935,7 @@ def test_half_float(self): # 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 _testcapi.nan_encoding == 'parisc': + 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 diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index e3869134c84d43..63de77ca6b8651 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -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 } diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 55a21e0e80c27b..de6d3cbce54fbe 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3359,12 +3359,6 @@ _testcapi_exec(PyObject *m) PyModule_AddObject(m, "INT64_MAX", PyLong_FromInt64(INT64_MAX)); PyModule_AddObject(m, "UINT64_MAX", PyLong_FromUInt64(UINT64_MAX)); -#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) - PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc")); -#else - PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular")); -#endif - if (PyModule_AddIntMacro(m, _Py_STACK_GROWS_DOWN)) { return -1; }