[Python-checkins] r78329 - in python/trunk: Lib/test/test_complex.py Misc/NEWS Objects/stringlib/formatter.h

eric.smith python-checkins at python.org
Mon Feb 22 19:33:47 CET 2010


Author: eric.smith
Date: Mon Feb 22 19:33:47 2010
New Revision: 78329

Log:
Issue #7988: Fix default alignment to be right aligned for complex.__format__. Now it matches other numeric types.

Modified:
   python/trunk/Lib/test/test_complex.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/stringlib/formatter.h

Modified: python/trunk/Lib/test/test_complex.py
==============================================================================
--- python/trunk/Lib/test/test_complex.py	(original)
+++ python/trunk/Lib/test/test_complex.py	Mon Feb 22 19:33:47 2010
@@ -568,6 +568,8 @@
         self.assertEqual(format(1.5+3j, '^20'),   '      (1.5+3j)      ')
         self.assertEqual(format(1.123-3.123j, '^20.2'), '     (1.1-3.1j)     ')
 
+        self.assertEqual(format(1.5+3j, '20.2f'), '          1.50+3.00j')
+        self.assertEqual(format(1.5+3j, '>20.2f'), '          1.50+3.00j')
         self.assertEqual(format(1.5+3j, '<20.2f'), '1.50+3.00j          ')
         self.assertEqual(format(1.5e20+3j, '<20.2f'), '150000000000000000000.00+3.00j')
         self.assertEqual(format(1.5e20+3j, '>40.2f'), '          150000000000000000000.00+3.00j')

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Feb 22 19:33:47 2010
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #7988: Fix default alignment to be right aligned for
+  complex.__format__. Now it matches other numeric types.
+
 - Issue #5211: the complex type no longer uses implicit coercion in
   mixed-type binary arithmetic operations.
 

Modified: python/trunk/Objects/stringlib/formatter.h
==============================================================================
--- python/trunk/Objects/stringlib/formatter.h	(original)
+++ python/trunk/Objects/stringlib/formatter.h	Mon Feb 22 19:33:47 2010
@@ -141,6 +141,26 @@
     STRINGLIB_CHAR type;
 } InternalFormatSpec;
 
+
+#if 0
+/* Occassionally useful for debugging. Should normally be commented out. */
+static void
+DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
+{
+    printf("internal format spec: fill_char %d\n", format->fill_char);
+    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: thousands_separators %d\n",
+           format->thousands_separators);
+    printf("internal format spec: precision %d\n", format->precision);
+    printf("internal format spec: type %c\n", format->type);
+    printf("\n");
+}
+#endif
+
+
 /*
   ptr points to the start of the format_spec, end points just past its end.
   fills in format with the parsed information.
@@ -151,7 +171,8 @@
 parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
                                   Py_ssize_t format_spec_len,
                                   InternalFormatSpec *format,
-                                  char default_type)
+                                  char default_type,
+                                  char default_align)
 {
     STRINGLIB_CHAR *ptr = format_spec;
     STRINGLIB_CHAR *end = format_spec + format_spec_len;
@@ -162,7 +183,7 @@
     Py_ssize_t consumed;
 
     format->fill_char = '\0';
-    format->align = '\0';
+    format->align = default_align;
     format->alternate = 0;
     format->sign = '\0';
     format->width = -1;
@@ -296,14 +317,19 @@
         *n_total = nchars;
     }
 
-    /* figure out how much leading space we need, based on the
+    /* Figure out how much leading space we need, based on the
        aligning */
     if (align == '>')
         *n_lpadding = *n_total - nchars;
     else if (align == '^')
         *n_lpadding = (*n_total - nchars) / 2;
-    else
+    else if (align == '<' || align == '=')
+        *n_lpadding = 0;
+    else {
+        /* We should never have an unspecified alignment. */
         *n_lpadding = 0;
+        assert(0);
+    }
 
     *n_rpadding = *n_total - nchars - *n_lpadding;
 }
@@ -505,9 +531,13 @@
         case '=':
             spec->n_spadding = n_padding;
             break;
+        case '>':
+            spec->n_lpadding = n_padding;
+            break;
         default:
-            /* Handles '>', plus catch-all just in case. */
+            /* Shouldn't get here, but treat it as '>' */
             spec->n_lpadding = n_padding;
+            assert(0);
             break;
         }
     }
@@ -1190,7 +1220,7 @@
     /* Turn off any padding. We'll do it later after we've composed
        the numbers without padding. */
     tmp_format.fill_char = '\0';
-    tmp_format.align = '\0';
+    tmp_format.align = '<';
     tmp_format.width = -1;
 
     /* Calculate how much memory we'll need. */
@@ -1266,7 +1296,7 @@
 
     /* parse the format_spec */
     if (!parse_internal_render_format_spec(format_spec, format_spec_len,
-                                           &format, 's'))
+                                           &format, 's', '<'))
         goto done;
 
     /* type conversion? */
@@ -1306,7 +1336,7 @@
     /* parse the format_spec */
     if (!parse_internal_render_format_spec(format_spec,
                                            format_spec_len,
-                                           &format, 'd'))
+                                           &format, 'd', '>'))
         goto done;
 
     /* type conversion? */
@@ -1417,7 +1447,7 @@
     /* parse the format_spec */
     if (!parse_internal_render_format_spec(format_spec,
                                            format_spec_len,
-                                           &format, '\0'))
+                                           &format, '\0', '>'))
         goto done;
 
     /* type conversion? */
@@ -1465,7 +1495,7 @@
     /* parse the format_spec */
     if (!parse_internal_render_format_spec(format_spec,
                                            format_spec_len,
-                                           &format, '\0'))
+                                           &format, '\0', '>'))
         goto done;
 
     /* type conversion? */


More information about the Python-checkins mailing list