[Python-checkins] r78352 - in python/branches/release31-maint: Lib/test/test_types.py Misc/NEWS Objects/stringlib/formatter.h

eric.smith python-checkins at python.org
Tue Feb 23 01:37:55 CET 2010


Author: eric.smith
Date: Tue Feb 23 01:37:54 2010
New Revision: 78352

Log:
Merged revisions 78350 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r78350 | eric.smith | 2010-02-22 19:22:24 -0500 (Mon, 22 Feb 2010) | 9 lines
  
  Merged revisions 78349 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/python/trunk
  
  ........
    r78349 | eric.smith | 2010-02-22 19:11:16 -0500 (Mon, 22 Feb 2010) | 1 line
    
    Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
  ........
................


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/test/test_types.py
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/Objects/stringlib/formatter.h

Modified: python/branches/release31-maint/Lib/test/test_types.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_types.py	(original)
+++ python/branches/release31-maint/Lib/test/test_types.py	Tue Feb 23 01:37:54 2010
@@ -382,6 +382,17 @@
                 self.assertEqual(value.__format__(format_spec),
                                  float(value).__format__(format_spec))
 
+        # Issue 6902
+        test(123456, "0<20", '12345600000000000000')
+        test(123456, "1<20", '12345611111111111111')
+        test(123456, "*<20", '123456**************')
+        test(123456, "0>20", '00000000000000123456')
+        test(123456, "1>20", '11111111111111123456')
+        test(123456, "*>20", '**************123456')
+        test(123456, "0=20", '00000000000000123456')
+        test(123456, "1=20", '11111111111111123456')
+        test(123456, "*=20", '**************123456')
+
     def test_long__format__(self):
         def test(i, format_spec, result):
             # make sure we're not accidentally checking ints
@@ -625,6 +636,17 @@
         self.assertRaises(ValueError, format, 0.0, '#')
         self.assertRaises(ValueError, format, 0.0, '#20f')
 
+        # Issue 6902
+        test(12345.6, "0<20", '12345.60000000000000')
+        test(12345.6, "1<20", '12345.61111111111111')
+        test(12345.6, "*<20", '12345.6*************')
+        test(12345.6, "0>20", '000000000000012345.6')
+        test(12345.6, "1>20", '111111111111112345.6')
+        test(12345.6, "*>20", '*************12345.6')
+        test(12345.6, "0=20", '000000000000012345.6')
+        test(12345.6, "1=20", '111111111111112345.6')
+        test(12345.6, "*=20", '*************12345.6')
+
     def test_format_spec_errors(self):
         # int, float, and string all share the same format spec
         # mini-language parser.

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Tue Feb 23 01:37:54 2010
@@ -12,6 +12,10 @@
 Core and Builtins
 -----------------
 
+=======
+- Issue #6902: Fix problem with built-in types format incorrectly with
+  0 padding.
+
 - Issue #7988: Fix default alignment to be right aligned for
   complex.__format__. Now it matches other numeric types.
 

Modified: python/branches/release31-maint/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/release31-maint/Objects/stringlib/formatter.h	(original)
+++ python/branches/release31-maint/Objects/stringlib/formatter.h	Tue Feb 23 01:37:54 2010
@@ -133,10 +133,10 @@
     printf("internal format spec: align %d\n", format->align);
     printf("internal format spec: alternate %d\n", format->alternate);
     printf("internal format spec: sign %d\n", format->sign);
-    printf("internal format spec: width %d\n", format->width);
+    printf("internal format spec: width %zd\n", format->width);
     printf("internal format spec: thousands_separators %d\n",
            format->thousands_separators);
-    printf("internal format spec: precision %d\n", format->precision);
+    printf("internal format spec: precision %zd\n", format->precision);
     printf("internal format spec: type %c\n", format->type);
     printf("\n");
 }
@@ -163,6 +163,7 @@
        the input string */
 
     Py_ssize_t consumed;
+    int align_specified = 0;
 
     format->fill_char = '\0';
     format->align = default_align;
@@ -178,10 +179,12 @@
     if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
         format->align = ptr[1];
         format->fill_char = ptr[0];
+        align_specified = 1;
         ptr += 2;
     }
     else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
         format->align = ptr[0];
+        align_specified = 1;
         ++ptr;
     }
 
@@ -201,7 +204,7 @@
     /* The special case for 0-padding (backwards compat) */
     if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
         format->fill_char = '0';
-        if (format->align == '\0') {
+        if (!align_specified) {
             format->align = '=';
         }
         ++ptr;
@@ -478,7 +481,7 @@
 
     /* min_width can go negative, that's okay. format->width == -1 means
        we don't care. */
-    if (format->fill_char == '0')
+    if (format->fill_char == '0' && format->align == '=')
         spec->n_min_width = format->width - n_non_digit_non_padding;
     else
         spec->n_min_width = 0;


More information about the Python-checkins mailing list