https://github.com/python/cpython/commit/b5fafc3ab9e970be07fb3a69a9025aea7b26d0d6
commit: b5fafc3ab9e970be07fb3a69a9025aea7b26d0d6
branch: 3.13
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-06-30T11:40:06+02:00
summary:

[3.13] gh-128051: Fix tests if sys.float_repr_style is 'legacy' (#135908) 
(#136026)

gh-128051: Fix tests if sys.float_repr_style is 'legacy' (#135908)


(cherry picked from commit f3aec60d7a01c5f085a3ef2d6670d46b42b8ddd3)

Co-authored-by: Sergey B Kirpichev <[email protected]>

files:
M Lib/difflib.py
M Lib/test/test_builtin.py
M Lib/test/test_configparser.py
M Lib/test/test_ctypes/test_parameters.py
M Lib/test/test_enum.py
M Lib/test/test_float.py
M Lib/test/test_format.py
M Lib/test/test_fstring.py
M Lib/test/test_optparse.py
M Lib/test/test_peepholer.py
M Lib/test/test_pprint.py
M Lib/test/test_reprlib.py
M Lib/test/test_statistics.py
M Lib/test/test_str.py
M Lib/test/test_types.py

diff --git a/Lib/difflib.py b/Lib/difflib.py
index 33e7e6c165aa0c..db550ceff2e075 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -78,8 +78,8 @@ class SequenceMatcher:
     sequences.  As a rule of thumb, a .ratio() value over 0.6 means the
     sequences are close matches:
 
-    >>> print(round(s.ratio(), 3))
-    0.866
+    >>> print(round(s.ratio(), 2))
+    0.87
     >>>
 
     If you're only interested in where the sequences match,
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 12a1c48732fab1..70987dd4881e95 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -2774,7 +2774,8 @@ def test_namespace_order(self):
 
 def load_tests(loader, tests, pattern):
     from doctest import DocTestSuite
-    tests.addTest(DocTestSuite(builtins))
+    if sys.float_repr_style == 'short':
+        tests.addTest(DocTestSuite(builtins))
     return tests
 
 if __name__ == "__main__":
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index 89f9f1247b55ea..d4fdd92bdc6b6a 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -986,12 +986,12 @@ def test_add_section_default(self):
 
     def test_defaults_keyword(self):
         """bpo-23835 fix for ConfigParser"""
-        cf = self.newconfig(defaults={1: 2.4})
-        self.assertEqual(cf[self.default_section]['1'], '2.4')
-        self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.4)
-        cf = self.newconfig(defaults={"A": 5.2})
-        self.assertEqual(cf[self.default_section]['a'], '5.2')
-        self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.2)
+        cf = self.newconfig(defaults={1: 2.5})
+        self.assertEqual(cf[self.default_section]['1'], '2.5')
+        self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.5)
+        cf = self.newconfig(defaults={"A": 5.25})
+        self.assertEqual(cf[self.default_section]['a'], '5.25')
+        self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.25)
 
 
 class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase):
diff --git a/Lib/test/test_ctypes/test_parameters.py 
b/Lib/test/test_ctypes/test_parameters.py
index effb8db418f790..1a6ddb91a7d063 100644
--- a/Lib/test/test_ctypes/test_parameters.py
+++ b/Lib/test/test_ctypes/test_parameters.py
@@ -1,3 +1,4 @@
+import sys
 import unittest
 import test.support
 from ctypes import (CDLL, PyDLL, ArgumentError,
@@ -226,7 +227,8 @@ def test_parameter_repr(self):
         self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam 
'[LIQ]' \(20000\)>$")
         self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
         self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
-        self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' 
(1e+300)>")
+        if sys.float_repr_style == 'short':
+            self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' 
(1e+300)>")
         self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' 
\(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
         self.assertRegex(repr(c_char_p.from_param(b'hihi')), r"^<cparam 'z' 
\(0x[A-Fa-f0-9]+\)>$")
         self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' 
\(0x[A-Fa-f0-9]+\)>$")
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 73ae53a3694dca..c5ec56bf6092f5 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -36,7 +36,7 @@ def load_tests(loader, tests, ignore):
                 optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
                 ))
     howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst')
-    if os.path.exists(howto_tests):
+    if os.path.exists(howto_tests) and sys.float_repr_style == 'short':
         tests.addTests(doctest.DocFileSuite(
                 howto_tests,
                 module_relative=False,
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 97f951f12998a3..87af79eb446083 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -724,6 +724,8 @@ def test_format(self):
         self.assertEqual(format(INF, 'F'), 'INF')
 
     @support.requires_IEEE_754
+    @unittest.skipUnless(sys.float_repr_style == 'short',
+                         "applies only when using short float repr style")
     def test_format_testfile(self):
         with open(format_testfile, encoding="utf-8") as testfile:
             for line in testfile:
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 8d83880a8c040b..c18dffe61a7370 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -342,12 +342,12 @@ def __bytes__(self):
         testcommon(b"%s", memoryview(b"abc"), b"abc")
         # %a will give the equivalent of
         # repr(some_obj).encode('ascii', 'backslashreplace')
-        testcommon(b"%a", 3.14, b"3.14")
+        testcommon(b"%a", 3.25, b"3.25")
         testcommon(b"%a", b"ghi", b"b'ghi'")
         testcommon(b"%a", "jkl", b"'jkl'")
         testcommon(b"%a", "\u0544", b"'\\u0544'")
         # %r is an alias for %a
-        testcommon(b"%r", 3.14, b"3.14")
+        testcommon(b"%r", 3.25, b"3.25")
         testcommon(b"%r", b"ghi", b"b'ghi'")
         testcommon(b"%r", "jkl", b"'jkl'")
         testcommon(b"%r", "\u0544", b"'\\u0544'")
@@ -403,19 +403,19 @@ def test_non_ascii(self):
 
         self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007")
         self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007")
-        self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007")
+        self.assertEqual(format(12.5, "\u2007<6"), "12.5\u2007\u2007")
         self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007")
         self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007")
 
         self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc")
         self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123")
-        self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3")
+        self.assertEqual(format(12.5, "\u2007>6"), "\u2007\u200712.5")
         self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)")
         self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j")
 
         self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007")
         self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007")
-        self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007")
+        self.assertEqual(format(12.5, "\u2007^6"), "\u200712.5\u2007")
         self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007")
         self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007")
 
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index aad2a5fab4da09..f1fa26c20ab5ae 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -1325,9 +1325,9 @@ def test_equal_equal(self):
 
     def test_conversions(self):
         self.assertEqual(f'{3.14:10.10}', '      3.14')
-        self.assertEqual(f'{3.14!s:10.10}', '3.14      ')
-        self.assertEqual(f'{3.14!r:10.10}', '3.14      ')
-        self.assertEqual(f'{3.14!a:10.10}', '3.14      ')
+        self.assertEqual(f'{1.25!s:10.10}', '1.25      ')
+        self.assertEqual(f'{1.25!r:10.10}', '1.25      ')
+        self.assertEqual(f'{1.25!a:10.10}', '1.25      ')
 
         self.assertEqual(f'{"a"}', 'a')
         self.assertEqual(f'{"a"!r}', "'a'")
@@ -1336,7 +1336,7 @@ def test_conversions(self):
         # Conversions can have trailing whitespace after them since it
         # does not provide any significance
         self.assertEqual(f"{3!s  }", "3")
-        self.assertEqual(f'{3.14!s  :10.10}', '3.14      ')
+        self.assertEqual(f'{1.25!s  :10.10}', '1.25      ')
 
         # Not a conversion.
         self.assertEqual(f'{"a!r"}', "a!r")
diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py
index 8655a0537a5e56..c68214961e3a87 100644
--- a/Lib/test/test_optparse.py
+++ b/Lib/test/test_optparse.py
@@ -614,9 +614,9 @@ def test_float_default(self):
         self.parser.add_option(
             "-p", "--prob",
             help="blow up with probability PROB [default: %default]")
-        self.parser.set_defaults(prob=0.43)
+        self.parser.set_defaults(prob=0.25)
         expected_help = self.help_prefix + \
-            "  -p PROB, --prob=PROB  blow up with probability PROB [default: 
0.43]\n"
+            "  -p PROB, --prob=PROB  blow up with probability PROB [default: 
0.25]\n"
         self.assertHelp(self.parser, expected_help)
 
     def test_alt_expand(self):
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index 6c27ee4db97af3..8c9f5703ca61ff 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -562,9 +562,9 @@ def format(fmt, *values):
         self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
         self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
         self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
-        self.assertEqual(format('x = %s!', 1234.5678901), 'x = 1234.5678901!')
-        self.assertEqual(format('x = %f!', 1234.5678901), 'x = 1234.567890!')
-        self.assertEqual(format('x = %d!', 1234.5678901), 'x = 1234!')
+        self.assertEqual(format('x = %s!', 1234.0000625), 'x = 1234.0000625!')
+        self.assertEqual(format('x = %f!', 1234.0000625), 'x = 1234.000063!')
+        self.assertEqual(format('x = %d!', 1234.0000625), 'x = 1234!')
         self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
         self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
         self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34')
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 4e6fed1ab969ac..ace75561f25103 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -374,7 +374,7 @@ def __new__(cls, celsius_degrees):
                 return super().__new__(Temperature, celsius_degrees)
             def __repr__(self):
                 kelvin_degrees = self + 273.15
-                return f"{kelvin_degrees}°K"
+                return f"{kelvin_degrees:.2f}°K"
         self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')
 
     def test_sorted_dict(self):
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py
index b96c75dfdc9341..9e556943516be8 100644
--- a/Lib/test/test_reprlib.py
+++ b/Lib/test/test_reprlib.py
@@ -396,20 +396,20 @@ def test_valid_indent(self):
                 'object': {
                     1: 'two',
                     b'three': [
-                        (4.5, 6.7),
+                        (4.5, 6.25),
                         [set((8, 9)), frozenset((10, 11))],
                     ],
                 },
                 'tests': (
                     (dict(indent=None), '''\
-                        {1: 'two', b'three': [(4.5, 6.7), [{8, 9}, 
frozenset({10, 11})]]}'''),
+                        {1: 'two', b'three': [(4.5, 6.25), [{8, 9}, 
frozenset({10, 11})]]}'''),
                     (dict(indent=False), '''\
                         {
                         1: 'two',
                         b'three': [
                         (
                         4.5,
-                        6.7,
+                        6.25,
                         ),
                         [
                         {
@@ -429,7 +429,7 @@ def test_valid_indent(self):
                          b'three': [
                           (
                            4.5,
-                           6.7,
+                           6.25,
                           ),
                           [
                            {
@@ -449,7 +449,7 @@ def test_valid_indent(self):
                         b'three': [
                         (
                         4.5,
-                        6.7,
+                        6.25,
                         ),
                         [
                         {
@@ -469,7 +469,7 @@ def test_valid_indent(self):
                          b'three': [
                           (
                            4.5,
-                           6.7,
+                           6.25,
                           ),
                           [
                            {
@@ -489,7 +489,7 @@ def test_valid_indent(self):
                             b'three': [
                                 (
                                     4.5,
-                                    6.7,
+                                    6.25,
                                 ),
                                 [
                                     {
@@ -517,7 +517,7 @@ def test_valid_indent(self):
                         b'three': [
                         (
                         4.5,
-                        6.7,
+                        6.25,
                         ),
                         [
                         {
@@ -537,7 +537,7 @@ def test_valid_indent(self):
                         -->b'three': [
                         -->-->(
                         -->-->-->4.5,
-                        -->-->-->6.7,
+                        -->-->-->6.25,
                         -->-->),
                         -->-->[
                         -->-->-->{
@@ -557,7 +557,7 @@ def test_valid_indent(self):
                         ....b'three': [
                         ........(
                         ............4.5,
-                        ............6.7,
+                        ............6.25,
                         ........),
                         ........[
                         ............{
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index 6fed4fc7570fd7..40599c776a6a80 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -3323,7 +3323,8 @@ def tearDown(self):
 def load_tests(loader, tests, ignore):
     """Used for doctest/unittest integration."""
     tests.addTests(doctest.DocTestSuite())
-    tests.addTests(doctest.DocTestSuite(statistics))
+    if sys.float_repr_style == 'short':
+        tests.addTests(doctest.DocTestSuite(statistics))
     return tests
 
 
diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index 46673cc56adb35..cdd54cc19a2de7 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -1231,10 +1231,10 @@ def __repr__(self):
         self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00')
         self.assertEqual('{0:<6}'.format(3), '3     ')
 
-        self.assertEqual('{0:\x00<6}'.format(3.14), '3.14\x00\x00')
-        self.assertEqual('{0:\x01<6}'.format(3.14), '3.14\x01\x01')
-        self.assertEqual('{0:\x00^6}'.format(3.14), '\x003.14\x00')
-        self.assertEqual('{0:^6}'.format(3.14), ' 3.14 ')
+        self.assertEqual('{0:\x00<6}'.format(3.25), '3.25\x00\x00')
+        self.assertEqual('{0:\x01<6}'.format(3.25), '3.25\x01\x01')
+        self.assertEqual('{0:\x00^6}'.format(3.25), '\x003.25\x00')
+        self.assertEqual('{0:^6}'.format(3.25), ' 3.25 ')
 
         self.assertEqual('{0:\x00<12}'.format(3+2.0j), 
'(3+2j)\x00\x00\x00\x00\x00\x00')
         self.assertEqual('{0:\x01<12}'.format(3+2.0j), 
'(3+2j)\x01\x01\x01\x01\x01\x01')
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index d5c613b75ec74a..cfb92816d735fc 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -495,8 +495,8 @@ def test(f, format_spec, result):
         # and a number after the decimal.  This is tricky, because
         # a totally empty format specifier means something else.
         # So, just use a sign flag
-        test(1e200, '+g', '+1e+200')
-        test(1e200, '+', '+1e+200')
+        test(1.25e200, '+g', '+1.25e+200')
+        test(1.25e200, '+', '+1.25e+200')
 
         test(1.1e200, '+g', '+1.1e+200')
         test(1.1e200, '+', '+1.1e+200')

_______________________________________________
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