Skip to content

Commit abb28c6

Browse files
committed
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. ........
1 parent b2ceb3a commit abb28c6

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lib/test/test_types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,17 @@ def test(i, format_spec, result):
357357
self.assertEqual(value.__format__(format_spec),
358358
float(value).__format__(format_spec))
359359

360+
# Issue 6902
361+
test(123456, "0<20", '12345600000000000000')
362+
test(123456, "1<20", '12345611111111111111')
363+
test(123456, "*<20", '123456**************')
364+
test(123456, "0>20", '00000000000000123456')
365+
test(123456, "1>20", '11111111111111123456')
366+
test(123456, "*>20", '**************123456')
367+
test(123456, "0=20", '00000000000000123456')
368+
test(123456, "1=20", '11111111111111123456')
369+
test(123456, "*=20", '**************123456')
370+
360371
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
361372
def test_float__format__locale(self):
362373
# test locale support for __format__ code 'n'
@@ -510,6 +521,17 @@ def test(f, format_spec, result):
510521
self.assertRaises(ValueError, format, 0.0, '#')
511522
self.assertRaises(ValueError, format, 0.0, '#20f')
512523

524+
# Issue 6902
525+
test(12345.6, "0<20", '12345.60000000000000')
526+
test(12345.6, "1<20", '12345.61111111111111')
527+
test(12345.6, "*<20", '12345.6*************')
528+
test(12345.6, "0>20", '000000000000012345.6')
529+
test(12345.6, "1>20", '111111111111112345.6')
530+
test(12345.6, "*>20", '*************12345.6')
531+
test(12345.6, "0=20", '000000000000012345.6')
532+
test(12345.6, "1=20", '111111111111112345.6')
533+
test(12345.6, "*=20", '*************12345.6')
534+
513535
def test_format_spec_errors(self):
514536
# int, float, and string all share the same format spec
515537
# mini-language parser.

Misc/NEWS

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

15+
=======
16+
- Issue #6902: Fix problem with built-in types format incorrectly with
17+
0 padding.
18+
1519
- Issue #7988: Fix default alignment to be right aligned for
1620
complex.__format__. Now it matches other numeric types.
1721

Objects/stringlib/formatter.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
151151
printf("internal format spec: align %d\n", format->align);
152152
printf("internal format spec: alternate %d\n", format->alternate);
153153
printf("internal format spec: sign %d\n", format->sign);
154-
printf("internal format spec: width %d\n", format->width);
154+
printf("internal format spec: width %zd\n", format->width);
155155
printf("internal format spec: thousands_separators %d\n",
156156
format->thousands_separators);
157-
printf("internal format spec: precision %d\n", format->precision);
157+
printf("internal format spec: precision %zd\n", format->precision);
158158
printf("internal format spec: type %c\n", format->type);
159159
printf("\n");
160160
}
@@ -181,6 +181,7 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
181181
the input string */
182182

183183
Py_ssize_t consumed;
184+
int align_specified = 0;
184185

185186
format->fill_char = '\0';
186187
format->align = default_align;
@@ -196,10 +197,12 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
196197
if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
197198
format->align = ptr[1];
198199
format->fill_char = ptr[0];
200+
align_specified = 1;
199201
ptr += 2;
200202
}
201203
else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
202204
format->align = ptr[0];
205+
align_specified = 1;
203206
++ptr;
204207
}
205208

@@ -219,7 +222,7 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
219222
/* The special case for 0-padding (backwards compat) */
220223
if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
221224
format->fill_char = '0';
222-
if (format->align == '\0') {
225+
if (!align_specified) {
223226
format->align = '=';
224227
}
225228
++ptr;
@@ -495,7 +498,7 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
495498

496499
/* min_width can go negative, that's okay. format->width == -1 means
497500
we don't care. */
498-
if (format->fill_char == '0')
501+
if (format->fill_char == '0' && format->align == '=')
499502
spec->n_min_width = format->width - n_non_digit_non_padding;
500503
else
501504
spec->n_min_width = 0;

0 commit comments

Comments
 (0)