Skip to content

Commit

Permalink
[3.9] bpo-41659: Disallow curly brace directly after primary (GH-22996)…
Browse files Browse the repository at this point in the history
… (#23006)

(cherry picked from commit 15acc4e)
  • Loading branch information
lysnikolaou authored Oct 27, 2020
1 parent 8aee019 commit c4b58ce
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 167 deletions.
3 changes: 3 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.
Loading

0 comments on commit c4b58ce

Please sign in to comment.