[pypy-commit] pypy stdlib-2.7.8: allow \x00 as fill char in __format__

bdkearns noreply at buildbot.pypy.org
Wed Aug 27 21:32:59 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.8
Changeset: r73101:c4dbc5c1e8d9
Date: 2014-08-27 15:32 -0400
http://bitbucket.org/pypy/pypy/changeset/c4dbc5c1e8d9/

Log:	allow \x00 as fill char in __format__

diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py
--- a/pypy/objspace/std/newformat.py
+++ b/pypy/objspace/std/newformat.py
@@ -428,7 +428,7 @@
 
         def _parse_spec(self, default_type, default_align):
             space = self.space
-            self._fill_char = self._lit("\0")[0]
+            self._fill_char = self._lit(" ")[0]
             self._align = default_align
             self._alternate = False
             self._sign = "\0"
@@ -441,9 +441,11 @@
             length = len(spec)
             i = 0
             got_align = True
+            got_fill_char = False
             if length - i >= 2 and self._is_alignment(spec[i + 1]):
                 self._align = spec[i + 1]
                 self._fill_char = spec[i]
+                got_fill_char = True
                 i += 2
             elif length - i >= 1 and self._is_alignment(spec[i]):
                 self._align = spec[i]
@@ -456,7 +458,7 @@
             if length - i >= 1 and spec[i] == "#":
                 self._alternate = True
                 i += 1
-            if self._fill_char == "\0" and length - i >= 1 and spec[i] == "0":
+            if not got_fill_char and length - i >= 1 and spec[i] == "0":
                 self._fill_char = self._lit("0")[0]
                 if not got_align:
                     self._align = "="
@@ -569,8 +571,6 @@
                 assert precision >= 0
                 length = precision
                 string = string[:precision]
-            if self._fill_char == "\0":
-                self._fill_char = self._lit(" ")[0]
             self._calc_padding(string, length)
             return space.wrap(self._pad(string))
 
@@ -811,7 +811,7 @@
             self._get_locale(tp)
             spec = self._calc_num_width(n_prefix, sign_char, to_numeric, n_digits,
                                         n_remainder, False, result)
-            fill = self._lit(" ") if self._fill_char == "\0" else self._fill_char
+            fill = self._fill_char
             upper = self._type == "X"
             return self.space.wrap(self._fill_number(spec, result, to_numeric,
                                      to_prefix, fill, to_remainder, upper))
@@ -957,7 +957,7 @@
                 digits = result
             spec = self._calc_num_width(0, sign, to_number, n_digits,
                                         n_remainder, have_dec_point, digits)
-            fill = self._lit(" ") if self._fill_char == "\0" else self._fill_char
+            fill = self._fill_char
             return self.space.wrap(self._fill_number(spec, digits, to_number, 0,
                                       fill, to_remainder, False))
 
@@ -1092,8 +1092,6 @@
 
             out = self._builder()
             fill = self._fill_char
-            if fill == "\0":
-                fill = self._lit(" ")[0]
 
             #compose the string
             #add left padding
diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -184,6 +184,20 @@
         format_string = self.s("{{{}:.6f}}").format(sys.maxsize + 1)
         raises(ValueError, "format(2.34, format_string)")
 
+    def test_format_null_fill_char(self):
+        assert self.s('{0:\x00<6s}').format('foo') == 'foo' + '\x00' * 3
+        assert self.s('{0:\x01<6s}').format('foo') == 'foo' + '\x01' * 3
+        assert self.s('{0:\x00^6s}').format('foo') == '\x00foo\x00\x00'
+
+        assert self.s('{0:\x00<6}').format(3) == '3' + '\x00' * 5
+        assert self.s('{0:\x01<6}').format(3) == '3' + '\x01' * 5
+
+        assert self.s('{0:\x00<6}').format(3.14) == '3.14' + '\x00' * 2
+        assert self.s('{0:\x01<6}').format(3.14) == '3.14' + '\x01' * 2
+
+        assert self.s('{0:\x00<12}').format(3+2.0j) == '(3+2j)' + '\x00' * 6
+        assert self.s('{0:\x01<12}').format(3+2.0j) == '(3+2j)' + '\x01' * 6
+
 
 class AppTestUnicodeFormat(BaseStringFormatTests):
     def setup_class(cls):


More information about the pypy-commit mailing list