Skip to content
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

[3.9] bpo-41659: Disallow curly brace directly after primary (GH-22996) #23006

Merged
merged 1 commit into from Oct 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Grammar/python.gram
Expand Up @@ -460,6 +460,7 @@ await_primary[expr_ty] (memo):
| AWAIT a=primary { CHECK_VERSION(5, "Await expressions are", _Py_Await(a, EXTRA)) }
| primary
primary[expr_ty]:
| invalid_primary # must be before 'primay genexp' because of invalid_genexp
| a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
| a=primary b=genexp { _Py_Call(a, CHECK(_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
| a=primary '(' b=[arguments] ')' {
Expand Down Expand Up @@ -664,6 +665,8 @@ invalid_del_stmt:
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_primary:
| primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
invalid_comprehension:
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_exceptions.py
Expand Up @@ -205,6 +205,7 @@ def testSyntaxErrorOffset(self):
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
check('x = "a', 1, 7)
check('lambda x: x = 2', 1, 1)
check('f{a + b + c}', 1, 2)

# Errors thrown by compile.c
check('class foo:return 1', 1, 11)
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_syntax.py
Expand Up @@ -802,6 +802,9 @@ def _check_error(self, code, errtext,
else:
self.fail("compile() did not raise SyntaxError")

def test_curly_brace_after_primary_raises_immediately(self):
self._check_error("f{", "invalid syntax", mode="single")

def test_assign_call(self):
self._check_error("f() = 1", "assign")

Expand Down
@@ -0,0 +1,3 @@
Fix a bug in the parser, where a curly brace following a `primary` didn't fail immediately.
This led to invalid expressions like `a {b}` to throw a :exc:`SyntaxError` with a wrong offset,
or invalid expressions ending with a curly brace like `a {` to not fail immediately in the REPL.