Skip to content

Commit f3aec60

Browse files
skirpichevvstinner
andauthored
gh-128051: Fix tests if sys.float_repr_style is 'legacy' (#135908)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent e23518f commit f3aec60

16 files changed

+50
-44
lines changed

Lib/difflib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class SequenceMatcher:
7878
sequences. As a rule of thumb, a .ratio() value over 0.6 means the
7979
sequences are close matches:
8080
81-
>>> print(round(s.ratio(), 3))
82-
0.866
81+
>>> print(round(s.ratio(), 2))
82+
0.87
8383
>>>
8484
8585
If you're only interested in where the sequences match,

Lib/test/test_builtin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,8 @@ def test_namespace_order(self):
29912991

29922992
def load_tests(loader, tests, pattern):
29932993
from doctest import DocTestSuite
2994-
tests.addTest(DocTestSuite(builtins))
2994+
if sys.float_repr_style == 'short':
2995+
tests.addTest(DocTestSuite(builtins))
29952996
return tests
29962997

29972998
if __name__ == "__main__":

Lib/test/test_configparser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -986,12 +986,12 @@ def test_add_section_default(self):
986986

987987
def test_defaults_keyword(self):
988988
"""bpo-23835 fix for ConfigParser"""
989-
cf = self.newconfig(defaults={1: 2.4})
990-
self.assertEqual(cf[self.default_section]['1'], '2.4')
991-
self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.4)
992-
cf = self.newconfig(defaults={"A": 5.2})
993-
self.assertEqual(cf[self.default_section]['a'], '5.2')
994-
self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.2)
989+
cf = self.newconfig(defaults={1: 2.5})
990+
self.assertEqual(cf[self.default_section]['1'], '2.5')
991+
self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.5)
992+
cf = self.newconfig(defaults={"A": 5.25})
993+
self.assertEqual(cf[self.default_section]['a'], '5.25')
994+
self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.25)
995995

996996

997997
class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase):

Lib/test/test_ctypes/test_parameters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import unittest
23
import test.support
34
from ctypes import (CDLL, PyDLL, ArgumentError,
@@ -240,7 +241,8 @@ def test_parameter_repr(self):
240241
self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
241242
self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
242243
self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
243-
self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
244+
if sys.float_repr_style == 'short':
245+
self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
244246
self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
245247
self.assertRegex(repr(c_char_p.from_param(b'hihi')), r"^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
246248
self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")

Lib/test/test_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def load_tests(loader, tests, ignore):
3636
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
3737
))
3838
howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst')
39-
if os.path.exists(howto_tests):
39+
if os.path.exists(howto_tests) and sys.float_repr_style == 'short':
4040
tests.addTests(doctest.DocFileSuite(
4141
howto_tests,
4242
module_relative=False,

Lib/test/test_float.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ def test_format(self):
795795
self.assertRaises(ValueError, format, x, '.6,n')
796796

797797
@support.requires_IEEE_754
798+
@unittest.skipUnless(sys.float_repr_style == 'short',
799+
"applies only when using short float repr style")
798800
def test_format_testfile(self):
799801
with open(format_testfile, encoding="utf-8") as testfile:
800802
for line in testfile:

Lib/test/test_format.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ def __bytes__(self):
346346
testcommon(b"%s", memoryview(b"abc"), b"abc")
347347
# %a will give the equivalent of
348348
# repr(some_obj).encode('ascii', 'backslashreplace')
349-
testcommon(b"%a", 3.14, b"3.14")
349+
testcommon(b"%a", 3.25, b"3.25")
350350
testcommon(b"%a", b"ghi", b"b'ghi'")
351351
testcommon(b"%a", "jkl", b"'jkl'")
352352
testcommon(b"%a", "\u0544", b"'\\u0544'")
353353
# %r is an alias for %a
354-
testcommon(b"%r", 3.14, b"3.14")
354+
testcommon(b"%r", 3.25, b"3.25")
355355
testcommon(b"%r", b"ghi", b"b'ghi'")
356356
testcommon(b"%r", "jkl", b"'jkl'")
357357
testcommon(b"%r", "\u0544", b"'\\u0544'")
@@ -407,19 +407,19 @@ def test_non_ascii(self):
407407

408408
self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007")
409409
self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007")
410-
self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007")
410+
self.assertEqual(format(12.5, "\u2007<6"), "12.5\u2007\u2007")
411411
self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007")
412412
self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007")
413413

414414
self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc")
415415
self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123")
416-
self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3")
416+
self.assertEqual(format(12.5, "\u2007>6"), "\u2007\u200712.5")
417417
self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)")
418418
self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j")
419419

420420
self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007")
421421
self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007")
422-
self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007")
422+
self.assertEqual(format(12.5, "\u2007^6"), "\u200712.5\u2007")
423423
self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007")
424424
self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007")
425425

Lib/test/test_fstring.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,9 +1336,9 @@ def test_equal_equal(self):
13361336

13371337
def test_conversions(self):
13381338
self.assertEqual(f'{3.14:10.10}', ' 3.14')
1339-
self.assertEqual(f'{3.14!s:10.10}', '3.14 ')
1340-
self.assertEqual(f'{3.14!r:10.10}', '3.14 ')
1341-
self.assertEqual(f'{3.14!a:10.10}', '3.14 ')
1339+
self.assertEqual(f'{1.25!s:10.10}', '1.25 ')
1340+
self.assertEqual(f'{1.25!r:10.10}', '1.25 ')
1341+
self.assertEqual(f'{1.25!a:10.10}', '1.25 ')
13421342

13431343
self.assertEqual(f'{"a"}', 'a')
13441344
self.assertEqual(f'{"a"!r}', "'a'")
@@ -1347,7 +1347,7 @@ def test_conversions(self):
13471347
# Conversions can have trailing whitespace after them since it
13481348
# does not provide any significance
13491349
self.assertEqual(f"{3!s }", "3")
1350-
self.assertEqual(f'{3.14!s :10.10}', '3.14 ')
1350+
self.assertEqual(f'{1.25!s :10.10}', '1.25 ')
13511351

13521352
# Not a conversion.
13531353
self.assertEqual(f'{"a!r"}', "a!r")

Lib/test/test_json/test_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_colors(self):
270270
(r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'),
271271
('"α"', f'{t.string}"\\u03b1"{t.reset}'),
272272
('123', f'{t.number}123{t.reset}'),
273-
('-1.2345e+23', f'{t.number}-1.2345e+23{t.reset}'),
273+
('-1.25e+23', f'{t.number}-1.25e+23{t.reset}'),
274274
(r'{"\\": ""}',
275275
f'''\
276276
{ob}

Lib/test/test_optparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ def test_float_default(self):
615615
self.parser.add_option(
616616
"-p", "--prob",
617617
help="blow up with probability PROB [default: %default]")
618-
self.parser.set_defaults(prob=0.43)
618+
self.parser.set_defaults(prob=0.25)
619619
expected_help = self.help_prefix + \
620-
" -p PROB, --prob=PROB blow up with probability PROB [default: 0.43]\n"
620+
" -p PROB, --prob=PROB blow up with probability PROB [default: 0.25]\n"
621621
self.assertHelp(self.parser, expected_help)
622622

623623
def test_alt_expand(self):

0 commit comments

Comments
 (0)