https://github.com/python/cpython/commit/b6bae536efbec937d6f46f9c98029efa70da3bdb
commit: b6bae536efbec937d6f46f9c98029efa70da3bdb
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: vstinner <[email protected]>
date: 2026-05-28T13:42:39+02:00
summary:

gh-85989: Add skip_if_double_rounding to test.support (#150219)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Lib/test/support/__init__.py
M Lib/test/test_builtin.py
M Lib/test/test_math.py
M Lib/test/test_statistics.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 1b4ad5a22ee271..5b0ae098b636ed 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -73,6 +73,7 @@
     "run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
     "reset_code", "on_github_actions",
     "requires_root_user", "requires_non_root_user",
+    "skip_if_double_rounding",
     ]
 
 
@@ -514,6 +515,15 @@ def dec(*args, **kwargs):
     float.__getformat__("double").startswith("IEEE"),
     "test requires IEEE 754 doubles")
 
+# detect evidence of double-rounding:
+x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
+HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
+skip_if_double_rounding = unittest.skipIf(HAVE_DOUBLE_ROUNDING,
+                                          "accuracy not guaranteed on "
+                                          "machines with double rounding")
+del x, y, HAVE_DOUBLE_ROUNDING
+
+
 def requires_zlib(reason='requires zlib'):
     try:
         import zlib
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 1f52b16948c703..1d2c105ac047e1 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -39,7 +39,7 @@
 from test.support.script_helper import assert_python_ok
 from test.support.testcase import ComplexesAreIdenticalMixin
 from test.support.warnings_helper import check_warnings
-from test.support import requires_IEEE_754
+from test.support import requires_IEEE_754, skip_if_double_rounding
 from unittest.mock import MagicMock, patch
 try:
     import pty, signal
@@ -47,11 +47,6 @@
     pty = signal = None
 
 
-# Detect evidence of double-rounding: sum() does not always
-# get improved accuracy on machines that suffer from double rounding.
-x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
-HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
-
 # used as proof of globals being used
 A_GLOBAL_VALUE = 123
 A_SENTINEL = sentinel("A_SENTINEL")
@@ -2235,8 +2230,7 @@ def __getitem__(self, index):
                                          complex(2, -0.0))
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                         "sum accuracy not guaranteed on machines with double 
rounding")
+    @skip_if_double_rounding
     @support.cpython_only    # Other implementations may choose a different 
algorithm
     def test_sum_accuracy(self):
         self.assertEqual(sum([0.1] * 10), 1.0)
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 7c40f9f94c37ad..85ddf68b3f36ef 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -1,7 +1,8 @@
 # Python test set -- math module
 # XXXX Should not do tests around zero only
 
-from test.support import verbose, requires_IEEE_754
+from test.support import (verbose, requires_IEEE_754,
+                          skip_if_double_rounding)
 from test import support
 import unittest
 import fractions
@@ -23,11 +24,6 @@
 FLOAT_MAX = sys.float_info.max
 FLOAT_MIN = sys.float_info.min
 
-# detect evidence of double-rounding: fsum is not always correctly
-# rounded on machines that suffer from double rounding.
-x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
-HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
-
 # locate file with test values
 if __name__ == '__main__':
     file = sys.argv[0]
@@ -683,8 +679,7 @@ def testfrexp(name, result, expected):
         self.assertTrue(math.isnan(math.frexp(NAN)[0]))
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                         "fsum is not exact on machines with double rounding")
+    @skip_if_double_rounding
     def testFsum(self):
         # math.fsum relies on exact rounding for correct operation.
         # There's a known problem with IA32 floating-point that causes
@@ -920,8 +915,7 @@ def testHypot(self):
         self.assertRaises(TypeError, math.hypot, *([1.0]*18), 'spam')
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                     "hypot() loses accuracy on machines with double rounding")
+    @skip_if_double_rounding
     @support.skip_on_newlib
     def testHypotAccuracy(self):
         # Verify improved accuracy in cases that were known to be inaccurate.
@@ -1412,8 +1406,7 @@ def __rmul__(self, other):
         self.assertEqual(sumprod(*args), 0.0)
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                         "sumprod() accuracy not guaranteed on machines with 
double rounding")
+    @skip_if_double_rounding
     @support.cpython_only    # Other implementations may choose a different 
algorithm
     def test_sumprod_accuracy(self):
         sumprod = math.sumprod
@@ -1498,8 +1491,7 @@ def run(func, *args):
                         )
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                         "sumprod() accuracy not guaranteed on machines with 
double rounding")
+    @skip_if_double_rounding
     @support.cpython_only    # Other implementations may choose a different 
algorithm
     @support.requires_resource('cpu')
     def test_sumprod_extended_precision_accuracy(self):
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index de7d13651cfea6..700c5ac304f717 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -16,7 +16,8 @@
 import sys
 import unittest
 from test import support
-from test.support import import_helper, requires_IEEE_754, skip_on_newlib
+from test.support import (import_helper, requires_IEEE_754,
+                          skip_if_double_rounding, skip_on_newlib)
 
 from decimal import Decimal
 from fractions import Fraction
@@ -28,12 +29,6 @@
 
 # === Helper functions and class ===
 
-# Test copied from Lib/test/test_math.py
-# detect evidence of double-rounding: fsum is not always correctly
-# rounded on machines that suffer from double rounding.
-x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
-HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
-
 def sign(x):
     """Return -1.0 for negatives, including -0.0, otherwise +1.0."""
     return math.copysign(1, x)
@@ -2796,8 +2791,7 @@ def test_sqrtprod_helper_function_fundamentals(self):
                 self.assertEqual(sign(actual), sign(expected))
 
     @requires_IEEE_754
-    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
-                     "accuracy not guaranteed on machines with double 
rounding")
+    @skip_if_double_rounding
     @support.cpython_only    # Allow for a weaker sumprod() implementation
     @skip_on_newlib
     def test_sqrtprod_helper_function_improved_accuracy(self):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to