Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b151a45

Browse files
committed
Merged revisions 64499 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r64499 | eric.smith | 2008-06-24 07:11:59 -0400 (Tue, 24 Jun 2008) | 1 line Fixed formatting with thousands separator and padding. Resolves issue 3140. ........
1 parent 6ed16dc commit b151a45

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

Lib/test/test_types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,14 @@ def test_int__format__locale(self):
428428
# move to the next integer to test
429429
x = x // 10
430430

431+
rfmt = ">20n"
432+
lfmt = "<20n"
433+
cfmt = "^20n"
434+
for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
435+
self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
436+
self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
437+
self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
438+
431439
def test_float__format__(self):
432440
# these should be rewritten to use both format(x, spec) and
433441
# x.__format__(spec)

Objects/stringlib/formatter.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign,
313313
as determined in _calc_integer_widths(). returns the pointer to
314314
where the digits go. */
315315
static STRINGLIB_CHAR *
316-
fill_number(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
317-
Py_ssize_t n_digits, STRINGLIB_CHAR fill_char)
316+
fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
317+
Py_ssize_t n_digits, STRINGLIB_CHAR fill_char)
318318
{
319319
STRINGLIB_CHAR* p_digits;
320320

@@ -557,17 +557,17 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
557557
pnumeric_chars += leading_chars_to_skip;
558558
}
559559

560-
/* Calculate the widths of the various leading and trailing parts */
561-
calc_number_widths(&spec, sign, n_digits, format);
562-
563560
if (format->type == 'n')
564561
/* Compute how many additional chars we need to allocate
565562
to hold the thousands grouping. */
566563
STRINGLIB_GROUPING(NULL, n_digits, n_digits,
567564
0, &n_grouping_chars, 0);
568565

566+
/* Calculate the widths of the various leading and trailing parts */
567+
calc_number_widths(&spec, sign, n_digits + n_grouping_chars, format);
568+
569569
/* Allocate a new string to hold the result */
570-
result = STRINGLIB_NEW(NULL, spec.n_total + n_grouping_chars);
570+
result = STRINGLIB_NEW(NULL, spec.n_total);
571571
if (!result)
572572
goto done;
573573
p = STRINGLIB_STR(result);
@@ -587,7 +587,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
587587

588588
/* Insert the grouping, if any, after the uppercasing of 'X', so we can
589589
ensure that grouping chars won't be affected. */
590-
if (n_grouping_chars && format->type == 'n') {
590+
if (n_grouping_chars) {
591591
/* We know this can't fail, since we've already
592592
reserved enough space. */
593593
STRINGLIB_CHAR *pstart = p + n_leading_chars;
@@ -597,9 +597,9 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
597597
assert(r);
598598
}
599599

600-
/* Fill in the non-digit parts */
601-
fill_number(p, &spec, n_digits,
602-
format->fill_char == '\0' ? ' ' : format->fill_char);
600+
/* Fill in the non-digit parts (padding, sign, etc.) */
601+
fill_non_digits(p, &spec, n_digits + n_grouping_chars,
602+
format->fill_char == '\0' ? ' ' : format->fill_char);
603603

604604
done:
605605
Py_XDECREF(tmp);
@@ -737,9 +737,9 @@ format_float_internal(PyObject *value,
737737
if (result == NULL)
738738
goto done;
739739

740-
/* fill in the non-digit parts */
741-
fill_number(STRINGLIB_STR(result), &spec, n_digits,
742-
format->fill_char == '\0' ? ' ' : format->fill_char);
740+
/* Fill in the non-digit parts (padding, sign, etc.) */
741+
fill_non_digits(STRINGLIB_STR(result), &spec, n_digits,
742+
format->fill_char == '\0' ? ' ' : format->fill_char);
743743

744744
/* fill in the digit parts */
745745
memmove(STRINGLIB_STR(result) +

0 commit comments

Comments
 (0)