[Python-checkins] closes bpo-34868: Improve error message with '_' is combined with an invalid type specifier. (GH-9666)

Miss Islington (bot) webhook-mailer at python.org
Tue Oct 2 01:12:06 EDT 2018


https://github.com/python/cpython/commit/cd4dd9374d0fc65b070a61871801d306090f8375
commit: cd4dd9374d0fc65b070a61871801d306090f8375
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-10-01T22:12:02-07:00
summary:

closes bpo-34868: Improve error message with '_' is combined with an invalid type specifier. (GH-9666)

(cherry picked from commit cbda8fc5d76b10bcbb92d927537576c229143836)

Co-authored-by: Benjamin Peterson <benjamin at python.org>

files:
M Lib/test/test_long.py
M Python/formatter_unicode.c

diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 8472889d48ba..b4a99e353c0b 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -699,6 +699,9 @@ def test__format__(self):
         self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, '_,d')
         self.assertRaisesRegex(ValueError, 'Cannot specify both', format, 3, ',_d')
 
+        self.assertRaisesRegex(ValueError, "Cannot specify ',' with 's'", format, 3, ',s')
+        self.assertRaisesRegex(ValueError, "Cannot specify '_' with 's'", format, 3, '_s')
+
         # ensure that only int and float type specifiers work
         for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
                             [chr(x) for x in range(ord('A'), ord('Z')+1)]):
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
index 71e673d9f832..ba09cc67becf 100644
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -28,16 +28,17 @@ unknown_presentation_type(Py_UCS4 presentation_type,
 }
 
 static void
-invalid_comma_type(Py_UCS4 presentation_type)
+invalid_thousands_separator_type(char specifier, Py_UCS4 presentation_type)
 {
+    assert(specifier == ',' || specifier == '_');
     if (presentation_type > 32 && presentation_type < 128)
         PyErr_Format(PyExc_ValueError,
-                     "Cannot specify ',' with '%c'.",
-                     (char)presentation_type);
+                     "Cannot specify '%c' with '%c'.",
+                     specifier, (char)presentation_type);
     else
         PyErr_Format(PyExc_ValueError,
-                     "Cannot specify ',' with '\\x%x'.",
-                     (unsigned int)presentation_type);
+                     "Cannot specify '%c' with '\\x%x'.",
+                     specifier, (unsigned int)presentation_type);
 }
 
 static void
@@ -117,8 +118,8 @@ is_sign_element(Py_UCS4 c)
 /* Locale type codes. LT_NO_LOCALE must be zero. */
 enum LocaleType {
     LT_NO_LOCALE = 0,
-    LT_DEFAULT_LOCALE,
-    LT_UNDERSCORE_LOCALE,
+    LT_DEFAULT_LOCALE = ',',
+    LT_UNDERSCORE_LOCALE = '_',
     LT_UNDER_FOUR_LOCALE,
     LT_CURRENT_LOCALE
 };
@@ -314,7 +315,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
             }
             /* fall through */
         default:
-            invalid_comma_type(format->type);
+            invalid_thousands_separator_type(format->thousands_separators, format->type);
             return 0;
         }
     }



More information about the Python-checkins mailing list