[Python-checkins] cpython (3.4): Issue #12546: Allow \x00 as a fill character for builtin type __format__

eric.smith python-checkins at python.org
Mon Apr 14 18:08:29 CEST 2014


http://hg.python.org/cpython/rev/7c484551bce1
changeset:   90262:7c484551bce1
branch:      3.4
parent:      90259:ef52ae167555
user:        Eric V. Smith <eric at trueblade.com>
date:        Mon Apr 14 11:55:10 2014 -0400
summary:
  Issue #12546: Allow \x00 as a fill character for builtin type __format__ methods.

files:
  Lib/test/test_unicode.py   |  21 +++++++++++++++++++++
  Misc/NEWS                  |   3 +++
  Python/formatter_unicode.c |  19 ++++++++-----------
  3 files changed, 32 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -845,6 +845,27 @@
         self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
         self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
 
+        # issue 12546: use \x00 as a fill character
+        self.assertEqual('{0:\x00<6s}'.format('foo'), 'foo\x00\x00\x00')
+        self.assertEqual('{0:\x01<6s}'.format('foo'), 'foo\x01\x01\x01')
+        self.assertEqual('{0:\x00^6s}'.format('foo'), '\x00foo\x00\x00')
+        self.assertEqual('{0:^6s}'.format('foo'), ' foo  ')
+
+        self.assertEqual('{0:\x00<6}'.format(3), '3\x00\x00\x00\x00\x00')
+        self.assertEqual('{0:\x01<6}'.format(3), '3\x01\x01\x01\x01\x01')
+        self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00')
+        self.assertEqual('{0:<6}'.format(3), '3     ')
+
+        self.assertEqual('{0:\x00<6}'.format(3.14), '3.14\x00\x00')
+        self.assertEqual('{0:\x01<6}'.format(3.14), '3.14\x01\x01')
+        self.assertEqual('{0:\x00^6}'.format(3.14), '\x003.14\x00')
+        self.assertEqual('{0:^6}'.format(3.14), ' 3.14 ')
+
+        self.assertEqual('{0:\x00<12}'.format(3+2.0j), '(3+2j)\x00\x00\x00\x00\x00\x00')
+        self.assertEqual('{0:\x01<12}'.format(3+2.0j), '(3+2j)\x01\x01\x01\x01\x01\x01')
+        self.assertEqual('{0:\x00^12}'.format(3+2.0j), '\x00\x00\x00(3+2j)\x00\x00\x00')
+        self.assertEqual('{0:^12}'.format(3+2.0j), '   (3+2j)   ')
+
         # format specifiers for user defined type
         self.assertEqual('{0:abc}'.format(C()), 'abc')
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 - Issue #20637: Key-sharing now also works for instance dictionaries of
   subclasses.  Patch by Peter Ingebretson.
 
+- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
+  float, and complex __format__ methods.
+
 Library
 -------
 
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -156,8 +156,9 @@
 
     Py_ssize_t consumed;
     int align_specified = 0;
+    int fill_char_specified = 0;
 
-    format->fill_char = '\0';
+    format->fill_char = ' ';
     format->align = default_align;
     format->alternate = 0;
     format->sign = '\0';
@@ -171,6 +172,7 @@
     if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
         format->align = READ_spec(pos+1);
         format->fill_char = READ_spec(pos);
+        fill_char_specified = 1;
         align_specified = 1;
         pos += 2;
     }
@@ -194,7 +196,7 @@
     }
 
     /* The special case for 0-padding (backwards compat) */
-    if (format->fill_char == '\0' && end-pos >= 1 && READ_spec(pos) == '0') {
+    if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
         format->fill_char = '0';
         if (!align_specified) {
             format->align = '=';
@@ -784,9 +786,7 @@
         goto done;
 
     /* Write into that space. First the padding. */
-    result = fill_padding(writer, len,
-                          format->fill_char=='\0'?' ':format->fill_char,
-                          lpad, rpad);
+    result = fill_padding(writer, len, format->fill_char, lpad, rpad);
     if (result == -1)
         goto done;
 
@@ -956,8 +956,7 @@
     /* Populate the memory. */
     result = fill_number(writer, &spec,
                          tmp, inumeric_chars, inumeric_chars + n_digits,
-                         tmp, prefix,
-                         format->fill_char == '\0' ? ' ' : format->fill_char,
+                         tmp, prefix, format->fill_char,
                          &locale, format->type == 'X');
 
 done:
@@ -1104,8 +1103,7 @@
     /* Populate the memory. */
     result = fill_number(writer, &spec,
                          unicode_tmp, index, index + n_digits,
-                         NULL, 0,
-                         format->fill_char == '\0' ? ' ' : format->fill_char,
+                         NULL, 0, format->fill_char,
                          &locale, 0);
 
 done:
@@ -1311,8 +1309,7 @@
     /* Populate the memory. First, the padding. */
     result = fill_padding(writer,
                           n_re_total + n_im_total + 1 + add_parens * 2,
-                          format->fill_char=='\0' ? ' ' : format->fill_char,
-                          lpad, rpad);
+                          format->fill_char, lpad, rpad);
     if (result == -1)
         goto done;
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list