FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.10] bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords (GH-26630) by miss-islington · Pull Request #26631 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions Lib/test/test_exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def testSyntaxErrorOffset(self):
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
check('[file for\n str(file) in []]', 2, 2)
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)

# Errors thrown by compile.c
check('class foo:return 1', 1, 11)
Expand Down
20 changes: 19 additions & 1 deletion Lib/test/test_syntax.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
SyntaxError: invalid syntax. Perhaps you forgot a comma?

# Make sure soft keywords constructs don't raise specialized
# errors regarding missing commas
# errors regarding missing commas or other spezialiced errors

>>> match x:
... y = 3
Expand All @@ -280,6 +280,24 @@
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> match x:
... case $:
... ...
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> match ...:
... case {**rest, "key": value}:
... ...
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> match ...:
... case {**_}:
... ...
Traceback (most recent call last):
SyntaxError: invalid syntax

From compiler_complex_args():

>>> def f(None=1):
Expand Down
15 changes: 11 additions & 4 deletions Parser/pegen.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,9 @@ _PyPegen_get_last_nonnwhitespace_token(Parser *p)
return token;
}

expr_ty
_PyPegen_name_token(Parser *p)
static expr_ty
_PyPegen_name_from_token(Parser *p, Token* t)
{
Token *t = _PyPegen_expect_token(p, NAME);
if (t == NULL) {
return NULL;
}
Expand All @@ -957,6 +956,14 @@ _PyPegen_name_token(Parser *p)
t->end_col_offset, p->arena);
}


expr_ty
_PyPegen_name_token(Parser *p)
{
Token *t = _PyPegen_expect_token(p, NAME);
return _PyPegen_name_from_token(p, t);
}

void *
_PyPegen_string_token(Parser *p)
{
Expand All @@ -974,7 +981,7 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
if (strncmp(*keyword, the_token, size) == 0) {
return _PyPegen_name_token(p);
return _PyPegen_name_from_token(p, t);
}
}
return NULL;
Expand Down

Back | FazBrowse Home | New Git URL