From 9d602d38455dc0253305694d7a379138c1d98dc8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 20 Dec 2018 23:59:45 +0200 Subject: [PATCH 1/2] bpo-27772: Make preciding width with 0 valid in string format. Previously it was an error with confusing error message. --- Doc/library/string.rst | 8 ++++++-- Lib/test/test_unicode.py | 6 ++++++ .../2018-12-20-23-59-23.bpo-27772.idHEcj.rst | 2 ++ Python/formatter_unicode.c | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-12-20-23-59-23.bpo-27772.idHEcj.rst diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 46b2bfc82b738b..588c664f2aae67 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -348,8 +348,8 @@ The meaning of the various alignment options is as follows: | ``'='`` | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form '+000000120'. This alignment option is only | - | | valid for numeric types. It becomes the default when '0'| - | | immediately precedes the field width. | + | | valid for numeric types. It becomes the default for | + | | numbers when '0' immediately precedes the field width. | +---------+----------------------------------------------------------+ | ``'^'`` | Forces the field to be centered within the available | | | space. | @@ -424,6 +424,10 @@ When no explicit alignment is given, preceding the *width* field by a zero sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of ``'0'`` with an *alignment* type of ``'='``. +.. versionchanged:: 3.8 + Preceding the *width* field by ``'0'`` no longer affects the default + alignment for strings. + The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with ``'f'`` and ``'F'``, or before and after the decimal point for a floating point diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index fb7bb2d523fe6e..d8c2e184001c5d 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -1070,6 +1070,12 @@ def __repr__(self): self.assertEqual('{0:^8s}'.format('result'), ' result ') self.assertEqual('{0:^9s}'.format('result'), ' result ') self.assertEqual('{0:^10s}'.format('result'), ' result ') + self.assertEqual('{0:8s}'.format('result'), 'result ') + self.assertEqual('{0:0s}'.format('result'), 'result') + self.assertEqual('{0:08s}'.format('result'), 'result00') + self.assertEqual('{0:<08s}'.format('result'), 'result00') + self.assertEqual('{0:>08s}'.format('result'), '00result') + self.assertEqual('{0:^08s}'.format('result'), '0result0') self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) self.assertEqual('{0:10000}'.format(''), ' ' * 10000) self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-12-20-23-59-23.bpo-27772.idHEcj.rst b/Misc/NEWS.d/next/Core and Builtins/2018-12-20-23-59-23.bpo-27772.idHEcj.rst new file mode 100644 index 00000000000000..7345152fee3568 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-12-20-23-59-23.bpo-27772.idHEcj.rst @@ -0,0 +1,2 @@ +In string formatting, preceding the *width* field by ``'0'`` no longer +affects the default alignment for strings. diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index 3e9e9ba08602cd..6f28ddb307b3a7 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -219,7 +219,7 @@ parse_internal_render_format_spec(PyObject *format_spec, /* The special case for 0-padding (backwards compat) */ if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') { format->fill_char = '0'; - if (!align_specified) { + if (!align_specified && default_align == '>') { format->align = '='; } ++pos; From d1d048edb7334d0fcc0eecf219e10975543ce4ce Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 24 Jan 2021 21:15:31 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Irit Katriel --- Doc/library/string.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 588c664f2aae67..67a27d0a15ad53 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -424,7 +424,7 @@ When no explicit alignment is given, preceding the *width* field by a zero sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of ``'0'`` with an *alignment* type of ``'='``. -.. versionchanged:: 3.8 +.. versionchanged:: 3.10 Preceding the *width* field by ``'0'`` no longer affects the default alignment for strings.