]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41681: Fix for `f-string/str.format` error description when using 2 `,` in format...
authorhan-solo <hanish0019@gmail.com>
Tue, 1 Sep 2020 14:34:29 +0000 (10:34 -0400)
committerGitHub <noreply@github.com>
Tue, 1 Sep 2020 14:34:29 +0000 (10:34 -0400)
* Fixed `f-string/str.format` error description when using two `,` in format specifier.

Co-authored-by: millefalcon <hanish0019@hmail.com>
Lib/test/test_format.py
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2020-08-31-17-49-02.bpo-41681.3-VJiH.rst [new file with mode: 0644]
Python/formatter_unicode.c

index e9e5bb9cf00a62db0eb1bc4ecf69586292e99c88..dff8c6903218785b1f3f1acb5f8daa0b26eb198c 100644 (file)
@@ -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()
index 35a62a0632e2e6219953c5219035a6aad2144b2c..b9bede0d9b800ea4e08dfb3c96e8257942f4f9c8 100644 (file)
@@ -9,6 +9,7 @@
 
 import ast
 import os
+import re
 import types
 import decimal
 import unittest
@@ -1198,6 +1199,25 @@ non-important content
         with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
             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 (file)
index 0000000..ed557f9
--- /dev/null
@@ -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`.
index 74638ca223772950d7f43bd95ae15fc278575dc6..ed95f267d476c7bab41e74d81a34afb5fc96b395 100644 (file)
@@ -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 */