From 89860920f7dd17fca5fd8c3962683018f363614c Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Tue, 17 Dec 2024 20:11:51 +1100 Subject: [PATCH 1/8] Improved invalid escape sequence warning message --- Lib/test/test_cmd_line_script.py | 4 +++- Lib/test/test_codeop.py | 2 +- Lib/test/test_string_literals.py | 33 +++++++++++++++++++++++--------- Lib/test/test_unparse.py | 4 +++- Objects/bytesobject.c | 13 ++++++++----- Objects/unicodeobject.c | 12 ++++++++---- Parser/string_parser.c | 26 +++++++++++++++++++------ Parser/tokenizer/helpers.c | 13 +++++++++++-- 8 files changed, 78 insertions(+), 29 deletions(-) diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index f30107225ff612..5d36ada9e00ece 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -659,7 +659,9 @@ def test_syntaxerror_invalid_escape_sequence_multi_line(self): stderr.splitlines()[-3:], [ b' foo = """\\q"""', b' ^^^^^^^^', - b'SyntaxError: invalid escape sequence \'\\q\'' + b'SyntaxError: "\\q" is an invalid escape sequence. ' + b'Such sequences will not work in the future. ' + b'Did you mean "\\\\q"? A raw string is also an option.' ], ) diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 787bd1b6a79e20..0eefc22d11bce0 100644 --- a/Lib/test/test_codeop.py +++ b/Lib/test/test_codeop.py @@ -282,7 +282,7 @@ def test_warning(self): # Test that the warning is only returned once. with warnings_helper.check_warnings( ('"is" with \'str\' literal', SyntaxWarning), - ("invalid escape sequence", SyntaxWarning), + ('"\\\\e" is an invalid escape sequence', SyntaxWarning), ) as w: compile_command(r"'\e' is 0") self.assertEqual(len(w.warnings), 2) diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index c7c6f684cd33f0..b24d7375b27d35 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -116,7 +116,9 @@ def test_eval_str_invalid_escape(self): warnings.simplefilter('always', category=SyntaxWarning) eval("'''\n\\z'''") self.assertEqual(len(w), 1) - self.assertEqual(str(w[0].message), r"invalid escape sequence '\z'") + self.assertEqual(str(w[0].message), r'"\z" is an invalid escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(w[0].filename, '') self.assertEqual(w[0].lineno, 1) @@ -126,7 +128,9 @@ def test_eval_str_invalid_escape(self): eval("'''\n\\z'''") exc = cm.exception self.assertEqual(w, []) - self.assertEqual(exc.msg, r"invalid escape sequence '\z'") + self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) self.assertEqual(exc.offset, 1) @@ -153,7 +157,9 @@ def test_eval_str_invalid_octal_escape(self): eval("'''\n\\407'''") self.assertEqual(len(w), 1) self.assertEqual(str(w[0].message), - r"invalid octal escape sequence '\407'") + r'"\407" is an invalid octal escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(w[0].filename, '') self.assertEqual(w[0].lineno, 1) @@ -163,7 +169,9 @@ def test_eval_str_invalid_octal_escape(self): eval("'''\n\\407'''") exc = cm.exception self.assertEqual(w, []) - self.assertEqual(exc.msg, r"invalid octal escape sequence '\407'") + self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) self.assertEqual(exc.offset, 1) @@ -205,7 +213,9 @@ def test_eval_bytes_invalid_escape(self): warnings.simplefilter('always', category=SyntaxWarning) eval("b'''\n\\z'''") self.assertEqual(len(w), 1) - self.assertEqual(str(w[0].message), r"invalid escape sequence '\z'") + self.assertEqual(str(w[0].message), r'"\z" is an invalid escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(w[0].filename, '') self.assertEqual(w[0].lineno, 1) @@ -215,7 +225,9 @@ def test_eval_bytes_invalid_escape(self): eval("b'''\n\\z'''") exc = cm.exception self.assertEqual(w, []) - self.assertEqual(exc.msg, r"invalid escape sequence '\z'") + self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) @@ -228,8 +240,9 @@ def test_eval_bytes_invalid_octal_escape(self): warnings.simplefilter('always', category=SyntaxWarning) eval("b'''\n\\407'''") self.assertEqual(len(w), 1) - self.assertEqual(str(w[0].message), - r"invalid octal escape sequence '\407'") + self.assertEqual(str(w[0].message), r'"\407" is an invalid octal escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(w[0].filename, '') self.assertEqual(w[0].lineno, 1) @@ -239,7 +252,9 @@ def test_eval_bytes_invalid_octal_escape(self): eval("b'''\n\\407'''") exc = cm.exception self.assertEqual(w, []) - self.assertEqual(exc.msg, r"invalid octal escape sequence '\407'") + self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. ' + r'Such sequences will not work in the future. ' + r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py index 35394f29fbe49d..332919540da4d6 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -651,7 +651,9 @@ def test_multiquote_joined_string(self): def test_backslash_in_format_spec(self): import re - msg = re.escape("invalid escape sequence '\\ '") + msg = re.escape('"\\ " is an invalid escape sequence. ' + 'Such sequences will not work in the future. ' + 'Did you mean "\\\\ "? A raw string is also an option.') with self.assertWarnsRegex(SyntaxWarning, msg): self.check_ast_roundtrip("""f"{x:\\ }" """) self.check_ast_roundtrip("""f"{x:\\n}" """) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 533089d25cd73a..51cf3b40f585ae 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1184,8 +1184,10 @@ PyObject *PyBytes_DecodeEscape(const char *s, unsigned char c = *first_invalid_escape; if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid octal escape sequence '\\%.3s'", - first_invalid_escape) < 0) + "'\\%.3s' is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%.3s\"? A raw string is also an option.", + first_invalid_escape, first_invalid_escape) < 0) { Py_DECREF(result); return NULL; @@ -1193,8 +1195,10 @@ PyObject *PyBytes_DecodeEscape(const char *s, } else { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid escape sequence '\\%c'", - c) < 0) + "'\\%c' is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + c, c) < 0) { Py_DECREF(result); return NULL; @@ -1202,7 +1206,6 @@ PyObject *PyBytes_DecodeEscape(const char *s, } } return result; - } /* -------------------------------------------------------------------- */ /* object api */ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b7aeb06d32bcec..bf5faf0f76bb51 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6802,8 +6802,10 @@ _PyUnicode_DecodeUnicodeEscapeStateful(const char *s, unsigned char c = *first_invalid_escape; if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid octal escape sequence '\\%.3s'", - first_invalid_escape) < 0) + "\"\\%.3s\" is an invalid octal escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%.3s\"? A raw string is also an option.", + first_invalid_escape, first_invalid_escape) < 0) { Py_DECREF(result); return NULL; @@ -6811,8 +6813,10 @@ _PyUnicode_DecodeUnicodeEscapeStateful(const char *s, } else { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "invalid escape sequence '\\%c'", - c) < 0) + "\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + c, c) < 0) { Py_DECREF(result); return NULL; diff --git a/Parser/string_parser.c b/Parser/string_parser.c index 9537c543b0eb93..ea85b21164697a 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -28,9 +28,16 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token int octal = ('4' <= c && c <= '7'); PyObject *msg = octal - ? PyUnicode_FromFormat("invalid octal escape sequence '\\%.3s'", - first_invalid_escape) - : PyUnicode_FromFormat("invalid escape sequence '\\%c'", c); + ? PyUnicode_FromFormat( + "\"\\%.3s\" is an invalid octal escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%.3s\"? A raw string is also an option.", + first_invalid_escape, first_invalid_escape) + : PyUnicode_FromFormat( + "\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + c, c); if (msg == NULL) { return -1; } @@ -53,11 +60,18 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token error location, if p->known_err_token is not set. */ p->known_err_token = t; if (octal) { - RAISE_SYNTAX_ERROR("invalid octal escape sequence '\\%.3s'", - first_invalid_escape); + RAISE_SYNTAX_ERROR( + "\"\\%.3s\" is an invalid octal escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%.3s\"? A raw string is also an option.", + first_invalid_escape, first_invalid_escape); } else { - RAISE_SYNTAX_ERROR("invalid escape sequence '\\%c'", c); + RAISE_SYNTAX_ERROR( + "\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + c, c); } } Py_DECREF(msg); diff --git a/Parser/tokenizer/helpers.c b/Parser/tokenizer/helpers.c index 9c9d05bbef0f1a..7429acd08d71ef 100644 --- a/Parser/tokenizer/helpers.c +++ b/Parser/tokenizer/helpers.c @@ -113,7 +113,10 @@ _PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_inval } PyObject *msg = PyUnicode_FromFormat( - "invalid escape sequence '\\%c'", + "\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + (char) first_invalid_escape_char, (char) first_invalid_escape_char ); @@ -129,7 +132,13 @@ _PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_inval /* Replace the SyntaxWarning exception with a SyntaxError to get a more accurate error report */ PyErr_Clear(); - return _PyTokenizer_syntaxerror(tok, "invalid escape sequence '\\%c'", (char) first_invalid_escape_char); + + return _PyTokenizer_syntaxerror(tok, + "\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. " + "Did you mean \"\\\\%c\"? A raw string is also an option.", + (char) first_invalid_escape_char, + (char) first_invalid_escape_char); } return -1; From b91b470d973663156031da8b75732b9ab792d056 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:28:23 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst new file mode 100644 index 00000000000000..51c8a200cdd91a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst @@ -0,0 +1 @@ +Improved the `SyntaxWarning` message for invalid escape sequences to clarify that such sequences will raise a `SyntaxError` in future Python releases. The new message also suggests a potential fix, i.e., `Did you mean "\\e"?`. From 6375e71a45491518d0da43a6d1ced5904d5b8485 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Tue, 17 Dec 2024 20:31:32 +1100 Subject: [PATCH 3/8] Fixed typos in blurb. --- .../2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst index 51c8a200cdd91a..0832d777bc3251 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-17-09-28-17.gh-issue-128016.DPqhah.rst @@ -1 +1 @@ -Improved the `SyntaxWarning` message for invalid escape sequences to clarify that such sequences will raise a `SyntaxError` in future Python releases. The new message also suggests a potential fix, i.e., `Did you mean "\\e"?`. +Improved the ``SyntaxWarning`` message for invalid escape sequences to clarify that such sequences will raise a ``SyntaxError`` in future Python releases. The new message also suggests a potential fix, i.e., ``Did you mean "\\e"?``. From 9a134270f9984b207d0472023b2a01e1db604479 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Tue, 17 Dec 2024 20:36:41 +1100 Subject: [PATCH 4/8] Remove extra whitespace. --- Parser/tokenizer/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/tokenizer/helpers.c b/Parser/tokenizer/helpers.c index 7429acd08d71ef..88d1666e502c41 100644 --- a/Parser/tokenizer/helpers.c +++ b/Parser/tokenizer/helpers.c @@ -133,7 +133,7 @@ _PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_inval to get a more accurate error report */ PyErr_Clear(); - return _PyTokenizer_syntaxerror(tok, + return _PyTokenizer_syntaxerror(tok, "\"\\%c\" is an invalid escape sequence. " "Such sequences will not work in the future. " "Did you mean \"\\\\%c\"? A raw string is also an option.", From a8f6a89d17f284be7820cda3065bdfc035afc961 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Wed, 18 Dec 2024 14:11:13 +1100 Subject: [PATCH 5/8] Revised wording of `SyntaxError`. --- Lib/test/test_cmd_line_script.py | 1 - Lib/test/test_string_literals.py | 4 ---- Parser/string_parser.c | 2 -- Parser/tokenizer/helpers.c | 1 - 4 files changed, 8 deletions(-) diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index 5d36ada9e00ece..527d51857fc904 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -660,7 +660,6 @@ def test_syntaxerror_invalid_escape_sequence_multi_line(self): [ b' foo = """\\q"""', b' ^^^^^^^^', b'SyntaxError: "\\q" is an invalid escape sequence. ' - b'Such sequences will not work in the future. ' b'Did you mean "\\\\q"? A raw string is also an option.' ], ) diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index b24d7375b27d35..f56195ca27672c 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -129,7 +129,6 @@ def test_eval_str_invalid_escape(self): exc = cm.exception self.assertEqual(w, []) self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. ' - r'Such sequences will not work in the future. ' r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) @@ -170,7 +169,6 @@ def test_eval_str_invalid_octal_escape(self): exc = cm.exception self.assertEqual(w, []) self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. ' - r'Such sequences will not work in the future. ' r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) @@ -226,7 +224,6 @@ def test_eval_bytes_invalid_escape(self): exc = cm.exception self.assertEqual(w, []) self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. ' - r'Such sequences will not work in the future. ' r'Did you mean "\\z"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) @@ -253,7 +250,6 @@ def test_eval_bytes_invalid_octal_escape(self): exc = cm.exception self.assertEqual(w, []) self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. ' - r'Such sequences will not work in the future. ' r'Did you mean "\\407"? A raw string is also an option.') self.assertEqual(exc.filename, '') self.assertEqual(exc.lineno, 1) diff --git a/Parser/string_parser.c b/Parser/string_parser.c index ea85b21164697a..9dd8f9ef28bd4f 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -62,14 +62,12 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token if (octal) { RAISE_SYNTAX_ERROR( "\"\\%.3s\" is an invalid octal escape sequence. " - "Such sequences will not work in the future. " "Did you mean \"\\\\%.3s\"? A raw string is also an option.", first_invalid_escape, first_invalid_escape); } else { RAISE_SYNTAX_ERROR( "\"\\%c\" is an invalid escape sequence. " - "Such sequences will not work in the future. " "Did you mean \"\\\\%c\"? A raw string is also an option.", c, c); } diff --git a/Parser/tokenizer/helpers.c b/Parser/tokenizer/helpers.c index 88d1666e502c41..5a416adb875aa1 100644 --- a/Parser/tokenizer/helpers.c +++ b/Parser/tokenizer/helpers.c @@ -135,7 +135,6 @@ _PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_inval return _PyTokenizer_syntaxerror(tok, "\"\\%c\" is an invalid escape sequence. " - "Such sequences will not work in the future. " "Did you mean \"\\\\%c\"? A raw string is also an option.", (char) first_invalid_escape_char, (char) first_invalid_escape_char); From aca1eb71a953470a2b1433f726f8f63ed9c1f3b4 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Wed, 18 Dec 2024 14:20:18 +1100 Subject: [PATCH 6/8] Fixed typo. --- Objects/bytesobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 51cf3b40f585ae..baf6d713a9b90b 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1184,7 +1184,7 @@ PyObject *PyBytes_DecodeEscape(const char *s, unsigned char c = *first_invalid_escape; if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "'\\%.3s' is an invalid escape sequence. " + "'\\%.3s' is an invalid octal escape sequence. " "Such sequences will not work in the future. " "Did you mean \"\\\\%.3s\"? A raw string is also an option.", first_invalid_escape, first_invalid_escape) < 0) From b0242188356a0f50a3956aa3b53113f1e0ca7071 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Wed, 18 Dec 2024 14:33:41 +1100 Subject: [PATCH 7/8] Fixed typo. --- Objects/bytesobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index baf6d713a9b90b..8a1df9e6302496 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1184,7 +1184,7 @@ PyObject *PyBytes_DecodeEscape(const char *s, unsigned char c = *first_invalid_escape; if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "'\\%.3s' is an invalid octal escape sequence. " + "\"\\%.3s\" is an invalid octal escape sequence. " "Such sequences will not work in the future. " "Did you mean \"\\\\%.3s\"? A raw string is also an option.", first_invalid_escape, first_invalid_escape) < 0) @@ -1195,7 +1195,7 @@ PyObject *PyBytes_DecodeEscape(const char *s, } else { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "'\\%c' is an invalid escape sequence. " + "\"\\%c\" is an invalid escape sequence. " "Such sequences will not work in the future. " "Did you mean \"\\\\%c\"? A raw string is also an option.", c, c) < 0) From 8a573154dd9989858b3f4921168c64d8609da5a9 Mon Sep 17 00:00:00 2001 From: Umar Butler Date: Wed, 18 Dec 2024 20:36:58 +1100 Subject: [PATCH 8/8] Removed unbefitting 'Did you mean?' suggestion for certain cases. --- Objects/bytesobject.c | 14 ++++++-------- Objects/unicodeobject.c | 10 ++++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8a1df9e6302496..7b835c97266491 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1184,10 +1184,9 @@ PyObject *PyBytes_DecodeEscape(const char *s, unsigned char c = *first_invalid_escape; if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "\"\\%.3s\" is an invalid octal escape sequence. " - "Such sequences will not work in the future. " - "Did you mean \"\\\\%.3s\"? A raw string is also an option.", - first_invalid_escape, first_invalid_escape) < 0) + "b\"\\%.3s\" is an invalid octal escape sequence. " + "Such sequences will not work in the future. ", + first_invalid_escape) < 0) { Py_DECREF(result); return NULL; @@ -1195,10 +1194,9 @@ PyObject *PyBytes_DecodeEscape(const char *s, } else { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "\"\\%c\" is an invalid escape sequence. " - "Such sequences will not work in the future. " - "Did you mean \"\\\\%c\"? A raw string is also an option.", - c, c) < 0) + "b\"\\%c\" is an invalid escape sequence. " + "Such sequences will not work in the future. ", + c) < 0) { Py_DECREF(result); return NULL; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index bf5faf0f76bb51..5499c3afc23c9e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6803,9 +6803,8 @@ _PyUnicode_DecodeUnicodeEscapeStateful(const char *s, if ('4' <= c && c <= '7') { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "\"\\%.3s\" is an invalid octal escape sequence. " - "Such sequences will not work in the future. " - "Did you mean \"\\\\%.3s\"? A raw string is also an option.", - first_invalid_escape, first_invalid_escape) < 0) + "Such sequences will not work in the future. ", + first_invalid_escape) < 0) { Py_DECREF(result); return NULL; @@ -6814,9 +6813,8 @@ _PyUnicode_DecodeUnicodeEscapeStateful(const char *s, else { if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "\"\\%c\" is an invalid escape sequence. " - "Such sequences will not work in the future. " - "Did you mean \"\\\\%c\"? A raw string is also an option.", - c, c) < 0) + "Such sequences will not work in the future. ", + c) < 0) { Py_DECREF(result); return NULL;