Skip to content

Commit 0d6aa7f

Browse files
millefalconmillefalcon
and
millefalcon
authored
bpo-41681: Fix for f-string/str.format error description when using 2 , in format specifier (pythonGH-22036)
* Fixed `f-string/str.format` error description when using two `,` in format specifier. Co-authored-by: millefalcon <hanish0019@hmail.com>
1 parent f5a16b4 commit 0d6aa7f

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Lib/test/test_format.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from test.support import verbose, TestFailed
22
import locale
33
import sys
4+
import re
45
import test.support as support
56
import unittest
67

@@ -495,6 +496,25 @@ def test_g_format_has_no_trailing_zeros(self):
495496
self.assertEqual(format(12300050.0, ".6g"), "1.23e+07")
496497
self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07")
497498

499+
def test_with_two_commas_in_format_specifier(self):
500+
error_msg = re.escape("Cannot specify ',' with ','.")
501+
with self.assertRaisesRegex(ValueError, error_msg):
502+
'{:,,}'.format(1)
503+
504+
def test_with_two_underscore_in_format_specifier(self):
505+
error_msg = re.escape("Cannot specify '_' with '_'.")
506+
with self.assertRaisesRegex(ValueError, error_msg):
507+
'{:__}'.format(1)
508+
509+
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
510+
error_msg = re.escape("Cannot specify both ',' and '_'.")
511+
with self.assertRaisesRegex(ValueError, error_msg):
512+
'{:,_}'.format(1)
513+
514+
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
515+
error_msg = re.escape("Cannot specify both ',' and '_'.")
516+
with self.assertRaisesRegex(ValueError, error_msg):
517+
'{:,_}'.format(1)
498518

499519
if __name__ == "__main__":
500520
unittest.main()

Lib/test/test_fstring.py

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import ast
1111
import os
12+
import re
1213
import types
1314
import decimal
1415
import unittest
@@ -1198,6 +1199,25 @@ def test_invalid_syntax_error_message(self):
11981199
with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
11991200
compile("f'{a $ b}'", "?", "exec")
12001201

1202+
def test_with_two_commas_in_format_specifier(self):
1203+
error_msg = re.escape("Cannot specify ',' with ','.")
1204+
with self.assertRaisesRegex(ValueError, error_msg):
1205+
f'{1:,,}'
1206+
1207+
def test_with_two_underscore_in_format_specifier(self):
1208+
error_msg = re.escape("Cannot specify '_' with '_'.")
1209+
with self.assertRaisesRegex(ValueError, error_msg):
1210+
f'{1:__}'
1211+
1212+
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
1213+
error_msg = re.escape("Cannot specify both ',' and '_'.")
1214+
with self.assertRaisesRegex(ValueError, error_msg):
1215+
f'{1:,_}'
1216+
1217+
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
1218+
error_msg = re.escape("Cannot specify both ',' and '_'.")
1219+
with self.assertRaisesRegex(ValueError, error_msg):
1220+
f'{1:,_}'
12011221

12021222
if __name__ == '__main__':
12031223
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes the wrong error description in the error raised by using 2 `,` in
2+
format string in f-string and :meth:`str.format`.

Python/formatter_unicode.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ parse_internal_render_format_spec(PyObject *format_spec,
252252
++pos;
253253
}
254254
if (end-pos && READ_spec(pos) == ',') {
255-
invalid_comma_and_underscore();
256-
return 0;
255+
if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
256+
invalid_comma_and_underscore();
257+
return 0;
258+
}
257259
}
258260

259261
/* Parse field precision */

0 commit comments

Comments
 (0)