]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102310: Change error range for invalid bytes literals (#103663)
authorNikita Sobolev <mail@sobolevn.me>
Sun, 23 Apr 2023 00:08:27 +0000 (03:08 +0300)
committerGitHub <noreply@github.com>
Sun, 23 Apr 2023 00:08:27 +0000 (18:08 -0600)
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2023-04-21-17-03-14.gh-issue-102310.anLjDx.rst [new file with mode: 0644]
Parser/string_parser.c

index f23653558a9119f5a824654c8042e10ae86ebf05..f959bbb4400702627f81105f22660fb9e73344fa 100644 (file)
@@ -1853,6 +1853,30 @@ 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
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 (file)
index 0000000..15cb6c6
--- /dev/null
@@ -0,0 +1 @@
+Change the error range for invalid bytes literals.
index be5f0c4a60a663a63f61e0920c2763c2b2b49f15..d4ce33850f7c5809686ac738cefb62eb36d703a8 100644 (file)
@@ -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;