8000 [3.14] gh-128051: Fix tests if sys.float_repr_style is 'legacy' (GH-1… · python/cpython@33c83ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 33c83ce

Browse files
miss-islingtonskirpichevvstinner
authored
[3.14] gh-128051: Fix tests if sys.float_repr_style is 'legacy' (GH-135908) (#136025)
gh-128051: Fix tests if sys.float_repr_style is 'legacy' (GH-135908) (cherry picked from commit f3aec60) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent a168c77 commit 33c83ce

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):

Lib/test/test_peepholer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,9 @@ def format(fmt, *values):
718718
self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
719719
self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
720720
self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
721-
self.assertEqual(format('x = %s!', 1234.5678901), 'x = 1234.5678901!')
722-
self.assertEqual(format('x = %f!', 1234.5678901), 'x = 1234.567890!')
723-
self.assertEqual(format('x = %d!', 1234.5678901), 'x = 1234!')
721+
self.assertEqual(format('x = %s!', 1234.0000625), 'x = 1234.0000625!')
722+
self.assertEqual(format('x = %f!', 1234.0000625), 'x = 1234.000063!')
723+
self.assertEqual(format('x = %d!', 1234.0000625), 'x = 1234!')
724724
self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
725725
self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
726726
self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34')

Lib/test/test_pprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def __new__(cls, celsius_degrees):
380380
return super().__new__(Temperature, celsius_degrees)
381381
def __repr__(self):
382382
kelvin_degrees = self + 273.15
383-
return f"{kelvin_degrees}°K"
383+
return f"{kelvin_degrees:.2f}°K"
384384
self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')
385385

386386
def test_sorted_dict(self):

Lib/test/test_reprlib.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,20 +397,20 @@ def test_valid_indent(self):
397397
'object': {
398398
1: 'two',
399399
b'three': [
400-
(4.5, 6.7),
400+
(4.5, 6.25),
401401
[set((8, 9)), frozenset((10, 11))],
402402
],
403403
},
404404
'tests': (
405405
(dict(indent=None), '''\
406-
{1: 'two', b'three': [(4.5, 6.7), [{8, 9}, frozenset({10, 11})]]}'''),
406+
{1: 'two', b'three': [(4.5, 6.25), [{8, 9}, frozenset({10, 11})]]}'''),
407407
(dict(indent=False), '''\
408408
{
409409
1: 'two',
410410
b'three': [
411411
(
412412
4.5,
413-
6.7,
413+
6.25,
414414
),
415415
[
416416
{
@@ -430,7 +430,7 @@ def test_valid_indent(self):
430430
b'three': [
431431
(
432432
4.5,
433-
6.7,
433+
6.25,
434434
),
435435
[
436436
{
@@ -450,7 +450,7 @@ def test_valid_indent(self):
450450
b'three': [
451451
(
452452
4.5,
453-
6.7,
453+
6.25,
454454
),
455455
[
456456
{
@@ -470,7 +470,7 @@ def test_valid_indent(self):
470470
b'three': [
471471
(
472472
4.5,
473-
6.7,
473+
6.25,
474474
),
475475
[
476476
{
@@ -490,7 +490,7 @@ def test_valid_indent(self):
490490
b'three': [
491491
(
492492
4.5,
493-
6.7,
493+
6.25,
494494
),
495495
[
496496
{
@@ -518,7 +518,7 @@ def test_valid_indent(self):
518518
b'three': [
519519
(
520520
4.5,
521-
6.7,
521+
6.25,
522522
),
523523
[
524524
{
@@ -538,7 +538,7 @@ def test_valid_indent(self):
538538
-->b'three': [
539539
-->-->(
540540
-->-->-->4.5,
541-
-->-->-->6.7,
541+
-->-->-->6.25,
542542
-->-->),
543543
-->-->[
544544
-->-->-->{
@@ -558,7 +558,7 @@ def test_valid_indent(self):
558558
....b'three': [
559559
........(
560560
............4.5,
561-
............6.7,
561+
............6.25,
562562
........),
563563
........[
564564
............{

Lib/test/test_statistics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3319,7 +3319,8 @@ def tearDown(self):
33193319
def load_tests(loader, tests, ignore):
33203320
"""Used for doctest/unittest integration."""
33213321
tests.addTests(doctest.DocTestSuite())
3322-
tests.addTests(doctest.DocTestSuite(statistics))
3322+
if sys.float_repr_style == 'short':
3323+
tests.addTests(doctest.DocTestSuite(statistics))
33233324
return tests
33243325

33253326

Lib/test/test_str.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,10 @@ def __repr__(self):
12311231
self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00')
12321232
self.assertEqual('{0:<6}'.format(3), '3 ')
12331233

1234-
self.assertEqual('{0:\x00<6}'.format(3.14), '3.14\x00\x00')
1235-
self.assertEqual('{0:\x01<6}'.format(3.14), '3.14\x01\x01')
1236-
self.assertEqual('{0:\x00^6}'.format(3.14), '\x003.14\x00')
1237-
self.assertEqual('{0:^6}'.format(3.14), ' 3.14 ')
1234+
self.assertEqual('{0:\x00<6}'.format(3.25), '3.25\x00\x00')
1235+
self.assertEqual('{0:\x01<6}'.format(3.25), '3.25\x01\x01')
1236+
self.assertEqual('{0:\x00^6}'.format(3.25), '\x003.25\x00')
1237+
self.assertEqual('{0:^6}'.format(3.25), ' 3.25 ')
12381238

12391239
self.assertEqual('{0:\x00<12}'.format(3+2.0j), '(3+2j)\x00\x00\x00\x00\x00\x00')
12401240
self.assertEqual('{0:\x01<12}'.format(3+2.0j), '(3+2j)\x01\x01\x01\x01\x01\x01')

Lib/test/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ def test(f, format_spec, result):
517517
# and a number after the decimal. This is tricky, because
518518
# a totally empty format specifier means something else.
519519
# So, just use a sign flag
520-
test(1e200, '+g', '+1e+200')
521-
test(1e200, '+', '+1e+200')
520+
test(1.25e200, '+g', '+1.25e+200')
521+
test(1.25e200, '+', '+1.25e+200')
522522

523523
test(1.1e200, '+g', '+1.1e+200')
524524
test(1.1e200, '+', '+1.1e+200')

0 commit comments

Comments
 (0)
0