Skip to content

Commit aad57bd

Browse files
committed
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. ........
1 parent 1f05e2e commit aad57bd

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Lib/test/test_complex.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,16 @@ def test_format(self):
558558
self.assertEqual(format(z, '-'), str(z))
559559
self.assertEqual(format(z, '<'), str(z))
560560
self.assertEqual(format(z, '10'), str(z))
561+
z = complex(0.0, 3.0)
562+
self.assertEqual(format(z, ''), str(z))
563+
self.assertEqual(format(z, '-'), str(z))
564+
self.assertEqual(format(z, '<'), str(z))
565+
self.assertEqual(format(z, '2'), str(z))
566+
z = complex(-0.0, 2.0)
567+
self.assertEqual(format(z, ''), str(z))
568+
self.assertEqual(format(z, '-'), str(z))
569+
self.assertEqual(format(z, '<'), str(z))
570+
self.assertEqual(format(z, '3'), str(z))
561571

562572
self.assertEqual(format(1+3j, 'g'), '1+3j')
563573
self.assertEqual(format(3j, 'g'), '0+3j')

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ What's New in Python 2.7.1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #9416: Fix some issues with complex formatting where the
16+
output with no type specifier failed to match the str output:
17+
18+
- format(complex(-0.0, 2.0), '-') omitted the real part from the output,
19+
- format(complex(0.0, 2.0), '-') included a sign and parentheses.
20+
1521
- Issue #7616: Fix copying of overlapping memoryview slices with the Intel
1622
compiler.
1723

Objects/stringlib/formatter.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,10 @@ format_complex_internal(PyObject *value,
11441144
/* Omitted type specifier. Should be like str(self). */
11451145
type = 'g';
11461146
default_precision = PyFloat_STR_PRECISION;
1147-
add_parens = 1;
1148-
if (re == 0.0)
1147+
if (re == 0.0 && copysign(1.0, re) == 1.0)
11491148
skip_re = 1;
1149+
else
1150+
add_parens = 1;
11501151
}
11511152

11521153
if (type == 'n')
@@ -1231,8 +1232,11 @@ format_complex_internal(PyObject *value,
12311232
n_re_digits, n_re_remainder,
12321233
re_has_decimal, &locale, &tmp_format);
12331234

1234-
/* Same formatting, but always include a sign. */
1235-
tmp_format.sign = '+';
1235+
/* Same formatting, but always include a sign, unless the real part is
1236+
* going to be omitted, in which case we use whatever sign convention was
1237+
* requested by the original format. */
1238+
if (!skip_re)
1239+
tmp_format.sign = '+';
12361240
n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
12371241
n_im_digits, n_im_remainder,
12381242
im_has_decimal, &locale, &tmp_format);

0 commit comments

Comments
 (0)