[Python-checkins] bpo-27772: Make preceding width with 0 valid in string format. (GH-11270)

serhiy-storchaka webhook-mailer at python.org
Mon Jan 25 04:56:56 EST 2021


https://github.com/python/cpython/commit/cf19cc3b920ca5995e1c202d2c3dd7a59ac8eac8
commit: cf19cc3b920ca5995e1c202d2c3dd7a59ac8eac8
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-01-25T11:56:33+02:00
summary:

bpo-27772: Make preceding width with 0 valid in string format. (GH-11270)

Previously it was an error with confusing error message.

files:
A Misc/NEWS.d/next/Core and Builtins/2018-12-20-23-59-23.bpo-27772.idHEcj.rst
M Doc/library/string.rst
M Lib/test/test_unicode.py
M Python/formatter_unicode.c

diff --git a/Doc/library/string.rst b/Doc/library/string.rst
index 54786d0c2ab0d..1bfd518349b38 100644
--- a/Doc/library/string.rst
+++ b/Doc/library/string.rst
@@ -347,8 +347,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.10
+   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 4f5636e1426f4..df8f2c92b38f8 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1098,6 +1098,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 0000000000000..7345152fee356
--- /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 ed95f267d476c..5ccf9d303e34e 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;



More information about the Python-checkins mailing list