Skip to content

Commit 0fd3891

Browse files
authored
gh-102310: Change error range for invalid bytes literals (#103663)
1 parent 7bf9456 commit 0fd3891

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
@@ -1853,6 +1853,30 @@ def f(x: *b)
18531853
Traceback (most recent call last):
18541854
...
18551855
SyntaxError: invalid syntax
1856+
1857+
Invalid bytes literals:
1858+
1859+
>>> b"Ā"
1860+
Traceback (most recent call last):
1861+
...
1862+
b"Ā"
1863+
^^^
1864+
SyntaxError: bytes can only contain ASCII literal characters
1865+
1866+
>>> b"абвгде"
1867+
Traceback (most recent call last):
1868+
...
1869+
b"абвгде"
1870+
^^^^^^^^
1871+
SyntaxError: bytes can only contain ASCII literal characters
1872+
1873+
>>> b"abc ъющый" # first 3 letters are ascii
1874+
Traceback (most recent call last):
1875+
...
1876+
b"abc ъющый"
1877+
^^^^^^^^^^^
1878+
SyntaxError: bytes can only contain ASCII literal characters
1879+
18561880
"""
18571881

18581882
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
@@ -248,7 +248,8 @@ _PyPegen_parse_string(Parser *p, Token *t)
248248
const char *ch;
249249
for (ch = s; *ch; ch++) {
250250
if (Py_CHARMASK(*ch) >= 0x80) {
251-
RAISE_SYNTAX_ERROR(
251+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
252+
t,
252253
"bytes can only contain ASCII "
253254
"literal characters");
254255
return NULL;

0 commit comments

Comments
 (0)