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.
This commit is contained in:
Mark Dickinson 2010-08-01 10:41:49 +00:00
parent 8708e38597
commit 5b65df7ce2
3 changed files with 24 additions and 4 deletions

View File

@ -506,6 +506,16 @@ class ComplexTest(unittest.TestCase):
self.assertEqual(format(z, '-'), str(z)) self.assertEqual(format(z, '-'), str(z))
self.assertEqual(format(z, '<'), str(z)) self.assertEqual(format(z, '<'), str(z))
self.assertEqual(format(z, '10'), 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(1+3j, 'g'), '1+3j')
self.assertEqual(format(3j, 'g'), '0+3j') self.assertEqual(format(3j, 'g'), '0+3j')

View File

@ -12,6 +12,12 @@ What's New in Python 3.2 Alpha 2?
Core and Builtins 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.
Library Library
------- -------

View File

@ -1136,9 +1136,10 @@ format_complex_internal(PyObject *value,
/* Omitted type specifier. Should be like str(self). */ /* Omitted type specifier. Should be like str(self). */
type = 'g'; type = 'g';
default_precision = PyFloat_STR_PRECISION; default_precision = PyFloat_STR_PRECISION;
add_parens = 1; if (re == 0.0 && copysign(1.0, re) == 1.0)
if (re == 0.0)
skip_re = 1; skip_re = 1;
else
add_parens = 1;
} }
if (type == 'n') if (type == 'n')
@ -1223,8 +1224,11 @@ format_complex_internal(PyObject *value,
n_re_digits, n_re_remainder, n_re_digits, n_re_remainder,
re_has_decimal, &locale, &tmp_format); re_has_decimal, &locale, &tmp_format);
/* Same formatting, but always include a sign. */ /* Same formatting, but always include a sign, unless the real part is
tmp_format.sign = '+'; * 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_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
n_im_digits, n_im_remainder, n_im_digits, n_im_remainder,
im_has_decimal, &locale, &tmp_format); im_has_decimal, &locale, &tmp_format);