[Python-checkins] r83402 - in python/branches/release27-maint: Lib/test/test_complex.py Misc/NEWS Objects/stringlib/formatter.h

mark.dickinson python-checkins at python.org
Sun Aug 1 12:45:15 CEST 2010


Author: mark.dickinson
Date: Sun Aug  1 12:45:15 2010
New Revision: 83402

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

........
  r83400 | mark.dickinson | 2010-08-01 11:41:49 +0100 (Sun, 01 Aug 2010) | 7 lines
  
  Issue #9416: Fix some issues with complex formatting where the
  output with no type specifier failed to match the str output:
  
    - format(complex(-0.0, 2.0), '-') omitted the real part from the output,
    - format(complex(0.0, 2.0), '-') included a sign and parentheses.
........


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

Modified: python/branches/release27-maint/Lib/test/test_complex.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_complex.py	(original)
+++ python/branches/release27-maint/Lib/test/test_complex.py	Sun Aug  1 12:45:15 2010
@@ -558,6 +558,16 @@
         self.assertEqual(format(z, '-'), str(z))
         self.assertEqual(format(z, '<'), str(z))
         self.assertEqual(format(z, '10'), str(z))
+        z = complex(0.0, 3.0)
+        self.assertEqual(format(z, ''), str(z))
+        self.assertEqual(format(z, '-'), str(z))
+        self.assertEqual(format(z, '<'), str(z))
+        self.assertEqual(format(z, '2'), str(z))
+        z = complex(-0.0, 2.0)
+        self.assertEqual(format(z, ''), str(z))
+        self.assertEqual(format(z, '-'), str(z))
+        self.assertEqual(format(z, '<'), str(z))
+        self.assertEqual(format(z, '3'), str(z))
 
         self.assertEqual(format(1+3j, 'g'), '1+3j')
         self.assertEqual(format(3j, 'g'), '0+3j')

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sun Aug  1 12:45:15 2010
@@ -12,6 +12,12 @@
 Core and Builtins
 -----------------
 
+- Issue #9416: Fix some issues with complex formatting where the
+  output with no type specifier failed to match the str output:
+
+    - format(complex(-0.0, 2.0), '-') omitted the real part from the output,
+    - format(complex(0.0, 2.0), '-') included a sign and parentheses.
+
 - Issue #7616: Fix copying of overlapping memoryview slices with the Intel
   compiler.
 

Modified: python/branches/release27-maint/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/release27-maint/Objects/stringlib/formatter.h	(original)
+++ python/branches/release27-maint/Objects/stringlib/formatter.h	Sun Aug  1 12:45:15 2010
@@ -1144,9 +1144,10 @@
         /* Omitted type specifier. Should be like str(self). */
         type = 'g';
         default_precision = PyFloat_STR_PRECISION;
-        add_parens = 1;
-        if (re == 0.0)
+        if (re == 0.0 && copysign(1.0, re) == 1.0)
             skip_re = 1;
+        else
+            add_parens = 1;
     }
 
     if (type == 'n')
@@ -1231,8 +1232,11 @@
                                     n_re_digits, n_re_remainder,
                                     re_has_decimal, &locale, &tmp_format);
 
-    /* Same formatting, but always include a sign. */
-    tmp_format.sign = '+';
+    /* Same formatting, but always include a sign, unless the real part is
+     * going to be omitted, in which case we use whatever sign convention was
+     * requested by the original format. */
+    if (!skip_re)
+        tmp_format.sign = '+';
     n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
                                     n_im_digits, n_im_remainder,
                                     im_has_decimal, &locale, &tmp_format);


More information about the Python-checkins mailing list