bpo-41681: Fix for `f-string/str.format` error description when using 2 `,` in format specifier (GH-22036)
* Fixed `f-string/str.format` error description when using two `,` in format specifier. Co-authored-by: millefalcon <hanish0019@hmail.com>
This commit is contained in:
parent
f5a16b4dbf
commit
0d6aa7f0ee
|
@ -1,6 +1,7 @@
|
||||||
from test.support import verbose, TestFailed
|
from test.support import verbose, TestFailed
|
||||||
import locale
|
import locale
|
||||||
import sys
|
import sys
|
||||||
|
import re
|
||||||
import test.support as support
|
import test.support as support
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
@ -495,6 +496,25 @@ class FormatTest(unittest.TestCase):
|
||||||
self.assertEqual(format(12300050.0, ".6g"), "1.23e+07")
|
self.assertEqual(format(12300050.0, ".6g"), "1.23e+07")
|
||||||
self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07")
|
self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07")
|
||||||
|
|
||||||
|
def test_with_two_commas_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify ',' with ','.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
'{:,,}'.format(1)
|
||||||
|
|
||||||
|
def test_with_two_underscore_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify '_' with '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
'{:__}'.format(1)
|
||||||
|
|
||||||
|
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify both ',' and '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
'{:,_}'.format(1)
|
||||||
|
|
||||||
|
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify both ',' and '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
'{:,_}'.format(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import types
|
import types
|
||||||
import decimal
|
import decimal
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -1198,6 +1199,25 @@ non-important content
|
||||||
with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
|
with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
|
||||||
compile("f'{a $ b}'", "?", "exec")
|
compile("f'{a $ b}'", "?", "exec")
|
||||||
|
|
||||||
|
def test_with_two_commas_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify ',' with ','.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
f'{1:,,}'
|
||||||
|
|
||||||
|
def test_with_two_underscore_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify '_' with '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
f'{1:__}'
|
||||||
|
|
||||||
|
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify both ',' and '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
f'{1:,_}'
|
||||||
|
|
||||||
|
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
|
||||||
|
error_msg = re.escape("Cannot specify both ',' and '_'.")
|
||||||
|
with self.assertRaisesRegex(ValueError, error_msg):
|
||||||
|
f'{1:,_}'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fixes the wrong error description in the error raised by using 2 `,` in
|
||||||
|
format string in f-string and :meth:`str.format`.
|
|
@ -252,8 +252,10 @@ parse_internal_render_format_spec(PyObject *format_spec,
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
if (end-pos && READ_spec(pos) == ',') {
|
if (end-pos && READ_spec(pos) == ',') {
|
||||||
invalid_comma_and_underscore();
|
if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
|
||||||
return 0;
|
invalid_comma_and_underscore();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse field precision */
|
/* Parse field precision */
|
||||||
|
|
Loading…
Reference in New Issue