summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-09-01 15:45:59 (GMT)
committerGitHub <noreply@github.com>2020-09-01 15:45:59 (GMT)
commitc16a2a1b643d3e04f86780e2c9e66c3f9f322560 (patch)
tree8d707bcefac61faf87fd7fd23a998c86421dba20
parentca55ecbf9aab305fa301ec69410ca3d3d18ec848 (diff)
downloadcpython-c16a2a1b643d3e04f86780e2c9e66c3f9f322560.zip
cpython-c16a2a1b643d3e04f86780e2c9e66c3f9f322560.tar.gz
cpython-c16a2a1b643d3e04f86780e2c9e66c3f9f322560.tar.bz2
bpo-41681: Fix for `f-string/str.format` error description when using 2 `,` in format specifier (GH-22036) (GH-22041)
* Fixed `f-string/str.format` error description when using two `,` in format specifier. Co-authored-by: millefalcon <hanish0019@hmail.com> (cherry picked from commit 0d6aa7f0ee38eb453bc8f73bf4830e6172be2f35) Co-authored-by: han-solo <hanish0019@gmail.com> Co-authored-by: han-solo <hanish0019@gmail.com>
-rw-r--r--Lib/test/test_format.py20
-rw-r--r--Lib/test/test_fstring.py20
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-08-31-17-49-02.bpo-41681.3-VJiH.rst2
-rw-r--r--Python/formatter_unicode.c6
4 files changed, 46 insertions, 2 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index e9e5bb9..dff8c69 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -1,6 +1,7 @@
from test.support import verbose, TestFailed
import locale
import sys
+import re
import test.support as support
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.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__":
unittest.main()
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index e0812d1..7432ae9 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -9,6 +9,7 @@
import ast
import os
+import re
import types
import decimal
import unittest
@@ -1208,6 +1209,25 @@ non-important content
with self.assertRaisesRegex(SyntaxError, err_msg):
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__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-08-31-17-49-02.bpo-41681.3-VJiH.rst b/Misc/NEWS.d/next/Core and Builtins/2020-08-31-17-49-02.bpo-41681.3-VJiH.rst
new file mode 100644
index 0000000..ed557f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-08-31-17-49-02.bpo-41681.3-VJiH.rst
@@ -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`.
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
index 74638ca..ed95f26 100644
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -252,8 +252,10 @@ parse_internal_render_format_spec(PyObject *format_spec,
++pos;
}
if (end-pos && READ_spec(pos) == ',') {
- invalid_comma_and_underscore();
- return 0;
+ if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
+ invalid_comma_and_underscore();
+ return 0;
+ }
}
/* Parse field precision */