Skip to content

Commit cbda8fc

Browse files
authored
closes bpo-34868: Improve error message with '_' is combined with an invalid type specifier. (GH-9666)
1 parent 30534cc commit cbda8fc

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Lib/test/test_long.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ def test__format__(self):
700700
self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,d')
701701
self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_d')
702702

703+
self.assertRaisesRegex(ValueError, "Cannot specify ',' with 's'", format, 3, ',s')
704+
self.assertRaisesRegex(ValueError, "Cannot specify '_' with 's'", format, 3, '_s')
705+
703706
# ensure that only int and float type specifiers work
704707
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
705708
[chr(x) for x in range(ord('A'), ord('Z')+1)]):

Python/formatter_unicode.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ unknown_presentation_type(Py_UCS4 presentation_type,
2828
}
2929

3030
static void
31-
invalid_comma_type(Py_UCS4 presentation_type)
31+
invalid_thousands_separator_type(char specifier, Py_UCS4 presentation_type)
3232
{
33+
assert(specifier == ',' || specifier == '_');
3334
if (presentation_type > 32 && presentation_type < 128)
3435
PyErr_Format(PyExc_ValueError,
35-
"Cannot specify ',' with '%c'.",
36-
(char)presentation_type);
36+
"Cannot specify '%c' with '%c'.",
37+
specifier, (char)presentation_type);
3738
else
3839
PyErr_Format(PyExc_ValueError,
39-
"Cannot specify ',' with '\\x%x'.",
40-
(unsigned int)presentation_type);
40+
"Cannot specify '%c' with '\\x%x'.",
41+
specifier, (unsigned int)presentation_type);
4142
}
4243

4344
static void
@@ -117,8 +118,8 @@ is_sign_element(Py_UCS4 c)
117118
/* Locale type codes. LT_NO_LOCALE must be zero. */
118119
enum LocaleType {
119120
LT_NO_LOCALE = 0,
120-
LT_DEFAULT_LOCALE,
121-
LT_UNDERSCORE_LOCALE,
121+
LT_DEFAULT_LOCALE = ',',
122+
LT_UNDERSCORE_LOCALE = '_',
122123
LT_UNDER_FOUR_LOCALE,
123124
LT_CURRENT_LOCALE
124125
};
@@ -314,7 +315,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
314315
}
315316
/* fall through */
316317
default:
317-
invalid_comma_type(format->type);
318+
invalid_thousands_separator_type(format->thousands_separators, format->type);
318319
return 0;
319320
}
320321
}

0 commit comments

Comments
 (0)