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

Skip to content

Commit 2fe4bb1

Browse files
committed
merge
2 parents 4b03b68 + 90f50d4 commit 2fe4bb1

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

Lib/test/test_format.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,18 @@ def test_locale(self):
289289
except locale.Error as err:
290290
self.skipTest("Cannot set locale: {}".format(err))
291291
try:
292-
sep = locale.localeconv()['thousands_sep']
292+
localeconv = locale.localeconv()
293+
sep = localeconv['thousands_sep']
294+
point = localeconv['decimal_point']
295+
293296
text = format(123456789, "n")
294297
self.assertIn(sep, text)
295298
self.assertEqual(text.replace(sep, ''), '123456789')
299+
300+
text = format(1234.5, "n")
301+
self.assertIn(sep, text)
302+
self.assertIn(point, text)
303+
self.assertEqual(text.replace(sep, ''), '1234' + point + '5')
296304
finally:
297305
locale.setlocale(locale.LC_ALL, oldloc)
298306

Objects/unicodeobject.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9176,9 +9176,16 @@ _PyUnicode_InsertThousandsGrouping(
91769176
thousands_sep_data = PyUnicode_DATA(thousands_sep);
91779177
thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep);
91789178
if (unicode != NULL && thousands_sep_kind != kind) {
9179-
thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind);
9180-
if (!thousands_sep_data)
9181-
return -1;
9179+
if (thousands_sep_kind < kind) {
9180+
thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind);
9181+
if (!thousands_sep_data)
9182+
return -1;
9183+
}
9184+
else {
9185+
data = _PyUnicode_AsKind(unicode, thousands_sep_kind);
9186+
if (!data)
9187+
return -1;
9188+
}
91829189
}
91839190

91849191
switch (kind) {
@@ -9210,8 +9217,12 @@ _PyUnicode_InsertThousandsGrouping(
92109217
assert(0);
92119218
return -1;
92129219
}
9213-
if (unicode != NULL && thousands_sep_kind != kind)
9214-
PyMem_Free(thousands_sep_data);
9220+
if (unicode != NULL && thousands_sep_kind != kind) {
9221+
if (thousands_sep_kind < kind)
9222+
PyMem_Free(thousands_sep_data);
9223+
else
9224+
PyMem_Free(data);
9225+
}
92159226
if (unicode == NULL) {
92169227
*maxchar = 127;
92179228
if (len != n_digits) {

Python/formatter_unicode.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
529529
if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
530530
*maxchar = Py_MAX(*maxchar, format->fill_char);
531531

532+
if (spec->n_decimal)
533+
*maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
534+
532535
return spec->n_lpadding + spec->n_sign + spec->n_prefix +
533536
spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
534537
spec->n_remainder + spec->n_rpadding;
@@ -548,10 +551,7 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
548551
Py_ssize_t d_pos = d_start;
549552
unsigned int kind = PyUnicode_KIND(out);
550553
void *data = PyUnicode_DATA(out);
551-
552-
#ifndef NDEBUG
553554
Py_ssize_t r;
554-
#endif
555555

556556
if (spec->n_lpadding) {
557557
PyUnicode_Fill(out, pos, pos + spec->n_lpadding, fill_char);
@@ -593,18 +593,15 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
593593
if (pdigits == NULL)
594594
return -1;
595595
}
596-
#ifndef NDEBUG
597-
r =
598-
#endif
599-
_PyUnicode_InsertThousandsGrouping(
596+
r = _PyUnicode_InsertThousandsGrouping(
600597
out, pos,
601598
spec->n_grouped_digits,
602599
pdigits + kind * d_pos,
603600
spec->n_digits, spec->n_min_width,
604601
locale->grouping, locale->thousands_sep, NULL);
605-
#ifndef NDEBUG
602+
if (r == -1)
603+
return -1;
606604
assert(r == spec->n_grouped_digits);
607-
#endif
608605
if (PyUnicode_KIND(digits) < kind)
609606
PyMem_Free(pdigits);
610607
d_pos += spec->n_digits;

0 commit comments

Comments
 (0)