Skip to content

Commit dbe60ee

Browse files
authored
bpo-43892: Validate the first term of complex literal value patterns (GH-25735)
1 parent 87655e2 commit dbe60ee

File tree

8 files changed

+595
-441
lines changed

8 files changed

+595
-441
lines changed

Grammar/python.gram

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,22 @@ literal_expr[expr_ty]:
276276
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
277277

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

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

288+
signed_real_number[expr_ty]:
289+
| real_number
290+
| '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }
291+
292+
real_number[expr_ty]:
293+
| real=NUMBER { _PyPegen_ensure_real(p, real) }
294+
286295
imaginary_number[expr_ty]:
287296
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
288297

@@ -343,7 +352,7 @@ mapping_pattern[pattern_ty]:
343352
NULL,
344353
EXTRA) }
345354
items_pattern[asdl_seq*]:
346-
| items=','.key_value_pattern+ { items }
355+
| ','.key_value_pattern+
347356
key_value_pattern[KeyPatternPair*]:
348357
| key=(literal_expr | attr) ':' pattern=pattern {
349358
_PyPegen_key_pattern_pair(p, key, pattern) }
@@ -373,7 +382,7 @@ class_pattern[pattern_ty]:
373382
positional_patterns[asdl_pattern_seq*]:
374383
| args[asdl_pattern_seq*]=','.pattern+ { args }
375384
keyword_patterns[asdl_seq*]:
376-
| keywords[asdl_seq*]=','.keyword_pattern+ { keywords }
385+
| ','.keyword_pattern+
377386
keyword_pattern[KeyPatternPair*]:
378387
| arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
379388

@@ -954,12 +963,12 @@ invalid_finally_stmt:
954963
| a='finally' ':' NEWLINE !INDENT {
955964
RAISE_INDENTATION_ERROR("expected an indented block after 'finally' statement on line %d", a->lineno) }
956965
invalid_except_stmt_indent:
957-
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
966+
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
958967
RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
959968
| a='except' ':' NEWLINE !INDENT { RAISE_SYNTAX_ERROR("expected an indented block after except statement on line %d", a->lineno) }
960969
invalid_match_stmt:
961970
| "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
962-
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
971+
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
963972
RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
964973
invalid_case_block:
965974
| "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }

Lib/test/test_patma.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,38 @@ def test_patma_284(self):
28732873
pass
28742874
""")
28752875

2876+
@no_perf
2877+
def test_patma_285(self):
2878+
self.assert_syntax_error("""
2879+
match ...:
2880+
case 0j+0:
2881+
pass
2882+
""")
2883+
2884+
@no_perf
2885+
def test_patma_286(self):
2886+
self.assert_syntax_error("""
2887+
match ...:
2888+
case 0j+0j:
2889+
pass
2890+
""")
2891+
2892+
@no_perf
2893+
def test_patma_287(self):
2894+
self.assert_syntax_error("""
2895+
match ...:
2896+
case {0j+0: _}:
2897+
pass
2898+
""")
2899+
2900+
@no_perf
2901+
def test_patma_288(self):
2902+
self.assert_syntax_error("""
2903+
match ...:
2904+
case {0j+0j: _}:
2905+
pass
2906+
""")
2907+
28762908

28772909
class PerfPatma(TestPatma):
28782910

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore proper validation of complex literal value patterns when parsing
2+
:keyword:`!match` blocks.

0 commit comments

Comments
 (0)