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

bpo-43892: Validate the first term of complex literal value patterns #25735

Merged
merged 7 commits into from
Apr 30, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,22 @@ literal_expr[expr_ty]:
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }

complex_number[expr_ty]:
| real=signed_number '+' imag=imaginary_number { _PyAST_BinOp(real, Add, imag, EXTRA) }
| real=signed_number '-' imag=imaginary_number { _PyAST_BinOp(real, Sub, imag, EXTRA) }
| real=signed_real_number '+' imag=imaginary_number {
_PyAST_BinOp(real, Add, imag, EXTRA) }
| real=signed_real_number '-' imag=imaginary_number {
_PyAST_BinOp(real, Sub, imag, EXTRA) }

signed_number[expr_ty]:
| NUMBER
| '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }

signed_real_number[expr_ty]:
| real_number
| '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }

real_number[expr_ty]:
| real=NUMBER { _PyPegen_ensure_real(p, real) }

imaginary_number[expr_ty]:
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }

Expand Down Expand Up @@ -343,7 +352,7 @@ mapping_pattern[pattern_ty]:
NULL,
EXTRA) }
items_pattern[asdl_seq*]:
| items=','.key_value_pattern+ { items }
| ','.key_value_pattern+
key_value_pattern[KeyPatternPair*]:
| key=(literal_expr | attr) ':' pattern=pattern {
_PyPegen_key_pattern_pair(p, key, pattern) }
Expand Down Expand Up @@ -373,7 +382,7 @@ class_pattern[pattern_ty]:
positional_patterns[asdl_pattern_seq*]:
| args[asdl_pattern_seq*]=','.pattern+ { args }
keyword_patterns[asdl_seq*]:
| keywords[asdl_seq*]=','.keyword_pattern+ { keywords }
| ','.keyword_pattern+
keyword_pattern[KeyPatternPair*]:
| arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }

Expand Down Expand Up @@ -954,12 +963,12 @@ invalid_finally_stmt:
| a='finally' ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
invalid_except_stmt_indent:
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
| a='except' ':' NEWLINE !INDENT { RAISE_SYNTAX_ERROR("expected an indented block after except statement on line %d", a->lineno) }
invalid_match_stmt:
| "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
invalid_case_block:
| "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,38 @@ def test_patma_284(self):
pass
""")

@no_perf
def test_patma_285(self):
self.assert_syntax_error("""
match ...:
case 0j+0:
pass
""")

@no_perf
def test_patma_286(self):
self.assert_syntax_error("""
match ...:
case 0j+0j:
pass
""")

@no_perf
def test_patma_287(self):
self.assert_syntax_error("""
match ...:
case {0j+0: _}:
pass
""")

@no_perf
def test_patma_288(self):
self.assert_syntax_error("""
match ...:
case {0j+0j: _}:
pass
""")


class PerfPatma(TestPatma):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Restore proper validation of complex literal value patterns when parsing
:keyword:`!match` blocks.
Loading