Fixed formatting with thousands separator and padding. Resolves issue 3140.
This commit is contained in:
parent
c72b787992
commit
5dce7e9a83
|
@ -497,6 +497,14 @@ class TypesTests(unittest.TestCase):
|
||||||
# move to the next integer to test
|
# move to the next integer to test
|
||||||
x = x // 10
|
x = x // 10
|
||||||
|
|
||||||
|
rfmt = ">20n"
|
||||||
|
lfmt = "<20n"
|
||||||
|
cfmt = "^20n"
|
||||||
|
for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
|
||||||
|
self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
|
||||||
|
self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
|
||||||
|
self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
|
||||||
|
|
||||||
def test_float__format__(self):
|
def test_float__format__(self):
|
||||||
# these should be rewritten to use both format(x, spec) and
|
# these should be rewritten to use both format(x, spec) and
|
||||||
# x.__format__(spec)
|
# x.__format__(spec)
|
||||||
|
|
|
@ -313,8 +313,8 @@ calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign,
|
||||||
as determined in _calc_integer_widths(). returns the pointer to
|
as determined in _calc_integer_widths(). returns the pointer to
|
||||||
where the digits go. */
|
where the digits go. */
|
||||||
static STRINGLIB_CHAR *
|
static STRINGLIB_CHAR *
|
||||||
fill_number(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
|
fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
|
||||||
Py_ssize_t n_digits, STRINGLIB_CHAR fill_char)
|
Py_ssize_t n_digits, STRINGLIB_CHAR fill_char)
|
||||||
{
|
{
|
||||||
STRINGLIB_CHAR* p_digits;
|
STRINGLIB_CHAR* p_digits;
|
||||||
|
|
||||||
|
@ -557,17 +557,17 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
|
||||||
pnumeric_chars += leading_chars_to_skip;
|
pnumeric_chars += leading_chars_to_skip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the widths of the various leading and trailing parts */
|
|
||||||
calc_number_widths(&spec, sign, n_digits, format);
|
|
||||||
|
|
||||||
if (format->type == 'n')
|
if (format->type == 'n')
|
||||||
/* Compute how many additional chars we need to allocate
|
/* Compute how many additional chars we need to allocate
|
||||||
to hold the thousands grouping. */
|
to hold the thousands grouping. */
|
||||||
STRINGLIB_GROUPING(NULL, n_digits, n_digits,
|
STRINGLIB_GROUPING(NULL, n_digits, n_digits,
|
||||||
0, &n_grouping_chars, 0);
|
0, &n_grouping_chars, 0);
|
||||||
|
|
||||||
|
/* Calculate the widths of the various leading and trailing parts */
|
||||||
|
calc_number_widths(&spec, sign, n_digits + n_grouping_chars, format);
|
||||||
|
|
||||||
/* Allocate a new string to hold the result */
|
/* Allocate a new string to hold the result */
|
||||||
result = STRINGLIB_NEW(NULL, spec.n_total + n_grouping_chars);
|
result = STRINGLIB_NEW(NULL, spec.n_total);
|
||||||
if (!result)
|
if (!result)
|
||||||
goto done;
|
goto done;
|
||||||
p = STRINGLIB_STR(result);
|
p = STRINGLIB_STR(result);
|
||||||
|
@ -587,7 +587,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
|
||||||
|
|
||||||
/* Insert the grouping, if any, after the uppercasing of 'X', so we can
|
/* Insert the grouping, if any, after the uppercasing of 'X', so we can
|
||||||
ensure that grouping chars won't be affected. */
|
ensure that grouping chars won't be affected. */
|
||||||
if (n_grouping_chars && format->type == 'n') {
|
if (n_grouping_chars) {
|
||||||
/* We know this can't fail, since we've already
|
/* We know this can't fail, since we've already
|
||||||
reserved enough space. */
|
reserved enough space. */
|
||||||
STRINGLIB_CHAR *pstart = p + n_leading_chars;
|
STRINGLIB_CHAR *pstart = p + n_leading_chars;
|
||||||
|
@ -597,9 +597,9 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
|
||||||
assert(r);
|
assert(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill in the non-digit parts */
|
/* Fill in the non-digit parts (padding, sign, etc.) */
|
||||||
fill_number(p, &spec, n_digits,
|
fill_non_digits(p, &spec, n_digits + n_grouping_chars,
|
||||||
format->fill_char == '\0' ? ' ' : format->fill_char);
|
format->fill_char == '\0' ? ' ' : format->fill_char);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
Py_XDECREF(tmp);
|
Py_XDECREF(tmp);
|
||||||
|
@ -737,9 +737,9 @@ format_float_internal(PyObject *value,
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* fill in the non-digit parts */
|
/* Fill in the non-digit parts (padding, sign, etc.) */
|
||||||
fill_number(STRINGLIB_STR(result), &spec, n_digits,
|
fill_non_digits(STRINGLIB_STR(result), &spec, n_digits,
|
||||||
format->fill_char == '\0' ? ' ' : format->fill_char);
|
format->fill_char == '\0' ? ' ' : format->fill_char);
|
||||||
|
|
||||||
/* fill in the digit parts */
|
/* fill in the digit parts */
|
||||||
memmove(STRINGLIB_STR(result) +
|
memmove(STRINGLIB_STR(result) +
|
||||||
|
|
Loading…
Reference in New Issue