Skip to content

[3.11] gh-102310: Change error range for invalid bytes literals (GH-103663) #103703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the error range for invalid bytes literals.
3 changes: 2 additions & 1 deletion Parser/string_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
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 -1;
Expand Down