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

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Sergey B Kirpichev 2025-06-27 13:00:25 +03:00 committed by GitHub
parent e23518fa96
commit f3aec60d7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 50 additions and 44 deletions

View file

@ -78,8 +78,8 @@ class SequenceMatcher:
sequences. As a rule of thumb, a .ratio() value over 0.6 means the sequences. As a rule of thumb, a .ratio() value over 0.6 means the
sequences are close matches: sequences are close matches:
>>> print(round(s.ratio(), 3)) >>> print(round(s.ratio(), 2))
0.866 0.87
>>> >>>
If you're only interested in where the sequences match, If you're only interested in where the sequences match,

View file

@ -2991,7 +2991,8 @@ class TestType(unittest.TestCase):
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
from doctest import DocTestSuite from doctest import DocTestSuite
tests.addTest(DocTestSuite(builtins)) if sys.float_repr_style == 'short':
tests.addTest(DocTestSuite(builtins))
return tests return tests
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -986,12 +986,12 @@ class ConfigParserTestCase(BasicTestCase, unittest.TestCase):
def test_defaults_keyword(self): def test_defaults_keyword(self):
"""bpo-23835 fix for ConfigParser""" """bpo-23835 fix for ConfigParser"""
cf = self.newconfig(defaults={1: 2.4}) cf = self.newconfig(defaults={1: 2.5})
self.assertEqual(cf[self.default_section]['1'], '2.4') self.assertEqual(cf[self.default_section]['1'], '2.5')
self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.4) self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.5)
cf = self.newconfig(defaults={"A": 5.2}) cf = self.newconfig(defaults={"A": 5.25})
self.assertEqual(cf[self.default_section]['a'], '5.2') self.assertEqual(cf[self.default_section]['a'], '5.25')
self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.2) self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.25)
class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase): class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase):

View file

@ -1,3 +1,4 @@
import sys
import unittest import unittest
import test.support import test.support
from ctypes import (CDLL, PyDLL, ArgumentError, from ctypes import (CDLL, PyDLL, ArgumentError,
@ -240,7 +241,8 @@ class SimpleTypesTestCase(unittest.TestCase):
self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$") 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_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(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_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_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]+\)>$") self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")

View file

@ -36,7 +36,7 @@ def load_tests(loader, tests, ignore):
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE, optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
)) ))
howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst') 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( tests.addTests(doctest.DocFileSuite(
howto_tests, howto_tests,
module_relative=False, module_relative=False,

View file

@ -795,6 +795,8 @@ class FormatTestCase(unittest.TestCase):
self.assertRaises(ValueError, format, x, '.6,n') self.assertRaises(ValueError, format, x, '.6,n')
@support.requires_IEEE_754 @support.requires_IEEE_754
@unittest.skipUnless(sys.float_repr_style == 'short',
"applies only when using short float repr style")
def test_format_testfile(self): def test_format_testfile(self):
with open(format_testfile, encoding="utf-8") as testfile: with open(format_testfile, encoding="utf-8") as testfile:
for line in testfile: for line in testfile:

View file

@ -346,12 +346,12 @@ class FormatTest(unittest.TestCase):
testcommon(b"%s", memoryview(b"abc"), b"abc") testcommon(b"%s", memoryview(b"abc"), b"abc")
# %a will give the equivalent of # %a will give the equivalent of
# repr(some_obj).encode('ascii', 'backslashreplace') # 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", b"ghi", b"b'ghi'")
testcommon(b"%a", "jkl", b"'jkl'") testcommon(b"%a", "jkl", b"'jkl'")
testcommon(b"%a", "\u0544", b"'\\u0544'") testcommon(b"%a", "\u0544", b"'\\u0544'")
# %r is an alias for %a # %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", b"ghi", b"b'ghi'")
testcommon(b"%r", "jkl", b"'jkl'") testcommon(b"%r", "jkl", b"'jkl'")
testcommon(b"%r", "\u0544", b"'\\u0544'") testcommon(b"%r", "\u0544", b"'\\u0544'")
@ -407,19 +407,19 @@ class FormatTest(unittest.TestCase):
self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007") self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007")
self.assertEqual(format(123, "\u2007<5"), "123\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(0j, "\u2007<4"), "0j\u2007\u2007")
self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007") self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007")
self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc") self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc")
self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123") 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(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)")
self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j") self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j")
self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007") self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007")
self.assertEqual(format(123, "\u2007^5"), "\u2007123\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(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007")
self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007") self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007")

View file

@ -1336,9 +1336,9 @@ x = (
def test_conversions(self): def test_conversions(self):
self.assertEqual(f'{3.14:10.10}', ' 3.14') self.assertEqual(f'{3.14:10.10}', ' 3.14')
self.assertEqual(f'{3.14!s:10.10}', '3.14 ') self.assertEqual(f'{1.25!s:10.10}', '1.25 ')
self.assertEqual(f'{3.14!r:10.10}', '3.14 ') self.assertEqual(f'{1.25!r:10.10}', '1.25 ')
self.assertEqual(f'{3.14!a:10.10}', '3.14 ') self.assertEqual(f'{1.25!a:10.10}', '1.25 ')
self.assertEqual(f'{"a"}', 'a') self.assertEqual(f'{"a"}', 'a')
self.assertEqual(f'{"a"!r}', "'a'") self.assertEqual(f'{"a"!r}', "'a'")
@ -1347,7 +1347,7 @@ x = (
# Conversions can have trailing whitespace after them since it # Conversions can have trailing whitespace after them since it
# does not provide any significance # does not provide any significance
self.assertEqual(f"{3!s }", "3") 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. # Not a conversion.
self.assertEqual(f'{"a!r"}', "a!r") self.assertEqual(f'{"a!r"}', "a!r")

View file

@ -270,7 +270,7 @@ class TestMain(unittest.TestCase):
(r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'), (r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'),
('"α"', f'{t.string}"\\u03b1"{t.reset}'), ('"α"', f'{t.string}"\\u03b1"{t.reset}'),
('123', f'{t.number}123{t.reset}'), ('123', f'{t.number}123{t.reset}'),
('-1.2345e+23', f'{t.number}-1.2345e+23{t.reset}'), ('-1.25e+23', f'{t.number}-1.25e+23{t.reset}'),
(r'{"\\": ""}', (r'{"\\": ""}',
f'''\ f'''\
{ob} {ob}

View file

@ -615,9 +615,9 @@ Options:
self.parser.add_option( self.parser.add_option(
"-p", "--prob", "-p", "--prob",
help="blow up with probability PROB [default: %default]") 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 + \ 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) self.assertHelp(self.parser, expected_help)
def test_alt_expand(self): def test_alt_expand(self):

View file

@ -718,9 +718,9 @@ class TestTranforms(BytecodeTestCase):
self.assertEqual(format('x = %d!', 1234), 'x = 1234!') self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
self.assertEqual(format('x = %x!', 1234), 'x = 4d2!') self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!') self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
self.assertEqual(format('x = %s!', 1234.5678901), 'x = 1234.5678901!') self.assertEqual(format('x = %s!', 1234.0000625), 'x = 1234.0000625!')
self.assertEqual(format('x = %f!', 1234.5678901), 'x = 1234.567890!') self.assertEqual(format('x = %f!', 1234.0000625), 'x = 1234.000063!')
self.assertEqual(format('x = %d!', 1234.5678901), 'x = 1234!') self.assertEqual(format('x = %d!', 1234.0000625), 'x = 1234!')
self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%') self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!') self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34') self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34')

View file

@ -458,7 +458,7 @@ class QueryTestCase(unittest.TestCase):
return super().__new__(Temperature, celsius_degrees) return super().__new__(Temperature, celsius_degrees)
def __repr__(self): def __repr__(self):
kelvin_degrees = self + 273.15 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') self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')
def test_sorted_dict(self): def test_sorted_dict(self):

View file

@ -397,20 +397,20 @@ class ReprTests(unittest.TestCase):
'object': { 'object': {
1: 'two', 1: 'two',
b'three': [ b'three': [
(4.5, 6.7), (4.5, 6.25),
[set((8, 9)), frozenset((10, 11))], [set((8, 9)), frozenset((10, 11))],
], ],
}, },
'tests': ( 'tests': (
(dict(indent=None), '''\ (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), '''\ (dict(indent=False), '''\
{ {
1: 'two', 1: 'two',
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -430,7 +430,7 @@ class ReprTests(unittest.TestCase):
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -450,7 +450,7 @@ class ReprTests(unittest.TestCase):
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -470,7 +470,7 @@ class ReprTests(unittest.TestCase):
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -490,7 +490,7 @@ class ReprTests(unittest.TestCase):
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -518,7 +518,7 @@ class ReprTests(unittest.TestCase):
b'three': [ b'three': [
( (
4.5, 4.5,
6.7, 6.25,
), ),
[ [
{ {
@ -538,7 +538,7 @@ class ReprTests(unittest.TestCase):
-->b'three': [ -->b'three': [
-->-->( -->-->(
-->-->-->4.5, -->-->-->4.5,
-->-->-->6.7, -->-->-->6.25,
-->-->), -->-->),
-->-->[ -->-->[
-->-->-->{ -->-->-->{
@ -558,7 +558,7 @@ class ReprTests(unittest.TestCase):
....b'three': [ ....b'three': [
........( ........(
............4.5, ............4.5,
............6.7, ............6.25,
........), ........),
........[ ........[
............{ ............{

View file

@ -3319,7 +3319,8 @@ class TestNormalDistC(unittest.TestCase, TestNormalDist):
def load_tests(loader, tests, ignore): def load_tests(loader, tests, ignore):
"""Used for doctest/unittest integration.""" """Used for doctest/unittest integration."""
tests.addTests(doctest.DocTestSuite()) tests.addTests(doctest.DocTestSuite())
tests.addTests(doctest.DocTestSuite(statistics)) if sys.float_repr_style == 'short':
tests.addTests(doctest.DocTestSuite(statistics))
return tests return tests

View file

@ -1231,10 +1231,10 @@ class StrTest(string_tests.StringLikeTest,
self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00') self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00')
self.assertEqual('{0:<6}'.format(3), '3 ') self.assertEqual('{0:<6}'.format(3), '3 ')
self.assertEqual('{0:\x00<6}'.format(3.14), '3.14\x00\x00') self.assertEqual('{0:\x00<6}'.format(3.25), '3.25\x00\x00')
self.assertEqual('{0:\x01<6}'.format(3.14), '3.14\x01\x01') self.assertEqual('{0:\x01<6}'.format(3.25), '3.25\x01\x01')
self.assertEqual('{0:\x00^6}'.format(3.14), '\x003.14\x00') self.assertEqual('{0:\x00^6}'.format(3.25), '\x003.25\x00')
self.assertEqual('{0:^6}'.format(3.14), ' 3.14 ') 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:\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') self.assertEqual('{0:\x01<12}'.format(3+2.0j), '(3+2j)\x01\x01\x01\x01\x01\x01')

View file

@ -517,8 +517,8 @@ class TypesTests(unittest.TestCase):
# and a number after the decimal. This is tricky, because # and a number after the decimal. This is tricky, because
# a totally empty format specifier means something else. # a totally empty format specifier means something else.
# So, just use a sign flag # So, just use a sign flag
test(1e200, '+g', '+1e+200') test(1.25e200, '+g', '+1.25e+200')
test(1e200, '+', '+1e+200') test(1.25e200, '+', '+1.25e+200')
test(1.1e200, '+g', '+1.1e+200') test(1.1e200, '+g', '+1.1e+200')
test(1.1e200, '+', '+1.1e+200') test(1.1e200, '+', '+1.1e+200')