8000 bpo-33471: Fix invalid numeric formatting with UCS2 separator by Licht-T · Pull Request #6861 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-33471: Fix invalid numeric formatting with UCS2 separator #6861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,34 @@ def test_locale(self):
self.assertIn(sep, text)
self.assertIn(point, text)
self.assertEqual(text.replace(sep, ''), '1234' + point + '5')

text = format(12, "n")
self.assertEqual(text, '12')
finally:
locale.setlocale(locale.LC_ALL, oldloc)

def test_locale_ucs2_sep(self):
try:
oldloc = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
except locale.Error as err:
self.skipTest("Cannot set locale: {}".format(err))
try:
localeconv = locale.localeconv()
sep = localeconv['thousands_sep']
point = localeconv['decimal_point']

text = format(123456789, "n")
self.assertIn(sep, text)
self.assertEqual(text.replace(sep, ''), '123456789')

text = format(1234.5, "n")
self.assertIn(sep, text)
self.assertIn(point, text)
self.assertEqual(text.replace(sep, ''), '1234' + point + '5')

text = format(12, "n")
self.assertEqual(text, '12')
finally:
locale.setlocale(locale.LC_ALL, oldloc)

Expand Down
10 changes: 3 additions & 7 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9316,11 +9316,9 @@ _PyUnicode_InsertThousandsGrouping(
if (!thousands_sep_data)
return -1;
}
else {
data = _PyUnicode_AsKind(unicode, thousands_sep_kind);
if (!data)
return -1;
}
/* when thousands_sep_kind > kind, trust
* the writer setting by _PyUnicodeWriter_Prepare.
*/
}

switch (kind) {
Expand Down Expand Up @@ -9354,8 +9352,6 @@ _PyUnicode_InsertThousandsGrouping(
if (unicode != NULL && thousands_sep_kind != kind) {
if (thousands_sep_kind < kind)
PyMem_Free(thousands_sep_data);
else
PyMem_Free(data);
}
if (unicode == NULL) {
*maxchar = 127;
Expand Down
0