Skip to content

Commit 7b2ac6c

Browse files
[3.11] gh-102310: Change error range for invalid bytes literals (GH-103663) (#103703)
1 parent 8642fdc commit 7b2ac6c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/test/test_syntax.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,30 @@ def f(x: *b)
18031803
Traceback (most recent call last):
18041804
...
18051805
SyntaxError: invalid syntax
1806+
1807+
Invalid bytes literals:
1808+
1809+
>>> b"Ā"
1810+
Traceback (most recent call last):
1811+
...
1812+
b"Ā"
1813+
^^^
1814+
SyntaxError: bytes can only contain ASCII literal characters
1815+
1816+
>>> b"абвгде"
1817+
Traceback (most recent call last):
1818+
...
1819+
b"абвгде"
1820+
^^^^^^^^
1821+
SyntaxError: bytes can only contain ASCII literal characters
1822+
1823+
>>> b"abc ъющый" # first 3 letters are ascii
1824+
Traceback (most recent call last):
1825+
...
1826+
b"abc ъющый"
1827+
^^^^^^^^^^^
1828+
SyntaxError: bytes can only contain ASCII literal characters
1829+
18061830
"""
18071831

18081832
import re
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the error range for invalid bytes literals.

Parser/string_parser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
259259
const char *ch;
260260
for (ch = s; *ch; ch++) {
261261
if (Py_CHARMASK(*ch) >= 0x80) {
262-
RAISE_SYNTAX_ERROR(
262+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
263+
t,
263264
"bytes can only contain ASCII "
264265
"literal characters");
265266
return -1;

0 commit comments

Comments
 (0)