From 93071607f6055d074bb17d2e21beb3e9cec9ec28 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 21 Apr 2023 17:03:45 +0300 Subject: [PATCH 1/2] gh-102310: Change error range for invalid bytes literals --- .../2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst | 1 + Parser/string_parser.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst new file mode 100644 index 000000000000000..15cb6c64adbab11 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst @@ -0,0 +1 @@ +Change the error range for invalid bytes literals. diff --git a/Parser/string_parser.c b/Parser/string_parser.c index be5f0c4a60a663a..d4ce33850f7c580 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -248,7 +248,8 @@ _PyPegen_parse_string(Parser *p, Token *t) const char *ch; for (ch = s; *ch; ch++) { if (Py_CHARMASK(*ch) >= 0x80) { - RAISE_SYNTAX_ERROR( + RAISE_SYNTAX_ERROR_KNOWN_LOCATION( + t, "bytes can only contain ASCII " "literal characters"); return NULL; From ca58ed810e23943955bb85dafed25c250eae44a6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 21 Apr 2023 22:59:19 +0300 Subject: [PATCH 2/2] Address review --- Lib/test/test_syntax.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index f23653558a9119f..f959bbb44007026 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -1853,6 +1853,30 @@ def f(x: *b) Traceback (most recent call last): ... SyntaxError: invalid syntax + +Invalid bytes literals: + + >>> b"Ā" + Traceback (most recent call last): + ... + b"Ā" + ^^^ + SyntaxError: bytes can only contain ASCII literal characters + + >>> b"абвгде" + Traceback (most recent call last): + ... + b"абвгде" + ^^^^^^^^ + SyntaxError: bytes can only contain ASCII literal characters + + >>> b"abc ъющый" # first 3 letters are ascii + Traceback (most recent call last): + ... + b"abc ъющый" + ^^^^^^^^^^^ + SyntaxError: bytes can only contain ASCII literal characters + """ import re