Skip to content

Commit b5fafc3

Browse files
vstinnerskirpichev
andauthored
[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 f3aec60) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 008c8ca commit b5fafc3

15 files changed

+49
-43
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
@@ -2774,7 +2774,8 @@ def test_namespace_order(self):
27742774

27752775
def load_tests(loader, tests, pattern):
27762776
from doctest import DocTestSuite
2777-
tests.addTest(DocTestSuite(builtins))
2777+
if sys.float_repr_style == 'short':
2778+
tests.addTest(DocTestSuite(builtins))
27782779
return tests
27792780

27802781
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,
@@ -226,7 +227,8 @@ def test_parameter_repr(self):
226227
self.assertRegex(repr(c_ulonglong.from_param(20000)), r"^<cparam '[LIQ]' \(20000\)>$")
227228
self.assertEqual(repr(c_float.from_param(1.5)), "<cparam 'f' (1.5)>")
228229
self.assertEqual(repr(c_double.from_param(1.5)), "<cparam 'd' (1.5)>")
229-
self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
230+
if sys.float_repr_style == 'short':
231+
self.assertEqual(repr(c_double.from_param(1e300)), "<cparam 'd' (1e+300)>")
230232
self.assertRegex(repr(c_longdouble.from_param(1.5)), r"^<cparam ('d' \(1.5\)|'g' at 0x[A-Fa-f0-9]+)>$")
231233
self.assertRegex(repr(c_char_p.from_param(b'hihi')), r"^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
232234
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
@@ -724,6 +724,8 @@ def test_format(self):
724724
self.assertEqual(format(INF, 'F'), 'INF')
725725

726726
@support.requires_IEEE_754
727+
@unittest.skipUnless(sys.float_repr_style == 'short',
728+
"applies only when using short float repr style")
727729
def test_format_testfile(self):
728730
with open(format_testfile, encoding="utf-8") as testfile:
729731
for line in testfile:

Lib/test/test_format.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ def __bytes__(self):
342342
testcommon(b"%s", memoryview(b"abc"), b"abc")
343343
# %a will give the equivalent of
344344
# repr(some_obj).encode('ascii', 'backslashreplace')
345-
testcommon(b"%a", 3.14, b"3.14")
345+
testcommon(b"%a", 3.25, b"3.25")
346346
testcommon(b"%a", b"ghi", b"b'ghi'")
347347
testcommon(b"%a", "jkl", b"'jkl'")
348348
testcommon(b"%a", "\u0544", b"'\\u0544'")
349349
# %r is an alias for %a
350-
testcommon(b"%r", 3.14, b"3.14")
350+
testcommon(b"%r", 3.25, b"3.25")
351351
testcommon(b"%r", b"ghi", b"b'ghi'")
352352
testcommon(b"%r", "jkl", b"'jkl'")
353353
testcommon(b"%r", "\u0544", b"'\\u0544'")
@@ -403,19 +403,19 @@ def test_non_ascii(self):
403403

404404
self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007")
405405
self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007")
406-
self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007")
406+
self.assertEqual(format(12.5, "\u2007<6"), "12.5\u2007\u2007")
407407
self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007")
408408
self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007")
409409

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

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

Lib/test/test_fstring.py

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

13261326
def test_conversions(self):
13271327
self.assertEqual(f'{3.14:10.10}', ' 3.14')
1328-
self.assertEqual(f'{3.14!s:10.10}', '3.14 ')
1329-
self.assertEqual(f'{3.14!r:10.10}', '3.14 ')
1330-
self.assertEqual(f'{3.14!a:10.10}', '3.14 ')
1328+
self.assertEqual(f'{1.25!s:10.10}', '1.25 ')
1329+
self.assertEqual(f'{1.25!r:10.10}', '1.25 ')
1330+
self.assertEqual(f'{1.25!a:10.10}', '1.25 ')
13311331

13321332
self.assertEqual(f'{"a"}', 'a')
13331333
self.assertEqual(f'{"a"!r}', "'a'")
@@ -1336,7 +1336,7 @@ def test_conversions(self):
13361336
# Conversions can have trailing whitespace after them since it
13371337
# does not provide any significance
13381338
self.assertEqual(f"{3!s }", "3")
1339-
self.assertEqual(f'{3.14!s :10.10}', '3.14 ')
1339+
self.assertEqual(f'{1.25!s :10.10}', '1.25 ')
13401340

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

Lib/test/test_optparse.py

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

622622
def test_alt_expand(self):

Lib/test/test_peepholer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ def format(fmt, *values):
562562
self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
563563
self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
564564
self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
565-
self.assertEqual(format('x = %s!', 1234.5678901), 'x = 1234.5678901!')
566-
self.assertEqual(format('x = %f!', 1234.5678901), 'x = 1234.567890!')
567-
self.assertEqual(format('x = %d!', 1234.5678901), 'x = 1234!')
565+
self.assertEqual(format('x = %s!', 1234.0000625), 'x = 1234.0000625!')
566+
self.assertEqual(format('x = %f!', 1234.0000625), 'x = 1234.000063!')
567+
self.assertEqual(format('x = %d!', 1234.0000625), 'x = 1234!')
568568
self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
569569
self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
570570
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
@@ -374,7 +374,7 @@ def __new__(cls, celsius_degrees):
374374
return super().__new__(Temperature, celsius_degrees)
375375
def __repr__(self):
376376
kelvin_degrees = self + 273.15
377-
return f"{kelvin_degrees}°K"
377+
return f"{kelvin_degrees:.2f}°K"
378378
self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')
379379

380380
def test_sorted_dict(self):

Lib/test/test_reprlib.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,20 @@ def test_valid_indent(self):
396396
'object': {
397397
1: 'two',
398398
b'three': [
399-
(4.5, 6.7),
399+
(4.5, 6.25),
400400
[set((8, 9)), frozenset((10, 11))],
401401
],
402402
},
403403
'tests': (
404404
(dict(indent=None), '''\
405-
{1: 'two', b'three': [(4.5, 6.7), [{8, 9}, frozenset({10, 11})]]}'''),
405+
{1: 'two', b'three': [(4.5, 6.25), [{8, 9}, frozenset({10, 11})]]}'''),
406406
(dict(indent=False), '''\
407407
{
408408
1: 'two',
409409
b'three': [
410410
(
411411
4.5,
412-
6.7,
412+
6.25,
413413
),
414414
[
415415
{
@@ -429,7 +429,7 @@ def test_valid_indent(self):
429429
b'three': [
430430
(
431431
4.5,
432-
6.7,
432+
6.25,
433433
),
434434
[
435435
{
@@ -449,7 +449,7 @@ def test_valid_indent(self):
449449
b'three': [
450450
(
451451
4.5,
452-
6.7,
452+
6.25,
453453
),
454454
[
455455
{
@@ -469,7 +469,7 @@ def test_valid_indent(self):
469469
b'three': [
470470
(
471471
4.5,
472-
6.7,
472+
6.25,
473473
),
474474
[
475475
{
@@ -489,7 +489,7 @@ def test_valid_indent(self):
489489
b'three': [
490490
(
491491
4.5,
492-
6.7,
492+
6.25,
493493
),
494494
[
495495
{
@@ -517,7 +517,7 @@ def test_valid_indent(self):
517517
b'three': [
518518
(
519519
4.5,
520-
6.7,
520+
6.25,
521521
),
522522
[
523523
{
@@ -537,7 +537,7 @@ def test_valid_indent(self):
537537
-->b'three': [
538538
-->-->(
539539
-->-->-->4.5,
540-
-->-->-->6.7,
540+
-->-->-->6.25,
541541
-->-->),
542542
-->-->[
543543
-->-->-->{
@@ -557,7 +557,7 @@ def test_valid_indent(self):
557557
....b'three': [
558558
........(
559559
............4.5,
560-
............6.7,
560+
............6.25,
561561
........),
562562
........[
563563
............{

Lib/test/test_statistics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,8 @@ def tearDown(self):
33233323
def load_tests(loader, tests, ignore):
33243324
"""Used for doctest/unittest integration."""
33253325
tests.addTests(doctest.DocTestSuite())
3326-
tests.addTests(doctest.DocTestSuite(statistics))
3326+
if sys.float_repr_style == 'short':
3327+
tests.addTests(doctest.DocTestSuite(statistics))
33273328
return tests
33283329

33293330

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
@@ -495,8 +495,8 @@ def test(f, format_spec, result):
495495
# and a number after the decimal. This is tricky, because
496496
# a totally empty format specifier means something else.
497497
# So, just use a sign flag
498-
test(1e200, '+g', '+1e+200')
499-
test(1e200, '+', '+1e+200')
498+
test(1.25e200, '+g', '+1.25e+200')
499+
test(1.25e200, '+', '+1.25e+200')
500500

501501
test(1.1e200, '+g', '+1.1e+200')
502502
test(1.1e200, '+', '+1.1e+200')

0 commit comments

Comments
 (0)