Skip to content

Commit

Permalink
[3.10] pythongh-94192: Fix error for dictionary literals with invalid…
Browse files Browse the repository at this point in the history
… expression as value. (pythonGH-94304) (python#94344)

Co-authored-by: wookie184 <[email protected]>
  • Loading branch information
wookie184 authored Jun 27, 2022
1 parent da6f859 commit 0ae7284
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,4 @@ invalid_kvpair:
| a=expression !(':') {
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
| expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
16 changes: 13 additions & 3 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,12 +970,22 @@
Traceback (most recent call last):
SyntaxError: expression expected after dictionary key and ':'
# Ensure that the error is not raise for syntax errors that happen after sets
# Ensure that the error is not raised for syntax errors that happen after sets
>>> {1} $
Traceback (most recent call last):
SyntaxError: invalid syntax
# Ensure that the error is not raised for invalid expressions
>>> {1: 2, 3: foo(,), 4: 5}
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> {1: $, 2: 3}
Traceback (most recent call last):
SyntaxError: invalid syntax
Specialized indentation errors:
>>> while condition:
Expand Down Expand Up @@ -1508,8 +1518,8 @@ def fib(n):
self.assertEqual(compile(s1, '<string>', 'exec'), compile(s2, '<string>', 'exec'))
except SyntaxError:
self.fail("Indented statement over multiple lines is valid")
def test_continuation_bad_indentation(self):

def test_continuation_bad_indentation(self):
# Check that code that breaks indentation across multiple lines raises a syntax error

code = r"""\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error for dictionary literals with invalid expression as value.
Loading

0 comments on commit 0ae7284

Please sign in to comment.