Skip to content

Commit 80e6fa8

Browse files
miss-islingtonencukoujohnslavik
authored
[3.15] gh-154775: Don't accept duplicate plus sign when matching complex number (GH-154776) (GH-154802)
(cherry picked from commit a85830c) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent c441fa2 commit 80e6fa8

4 files changed

Lines changed: 31 additions & 37 deletions

File tree

Grammar/python.gram

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ real_number[expr_ty]:
568568

569569
imaginary_number[expr_ty]:
570570
| imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
571-
| '+' imag=NUMBER { _PyPegen_ensure_imaginary(p, imag) }
572571

573572
capture_pattern[pattern_ty]:
574573
| target=pattern_capture_target { _PyAST_MatchAs(NULL, target->v.Name.id, EXTRA) }

Lib/test/test_patma.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,14 +2835,6 @@ def test_patma_264(self):
28352835
self.assertEqual(y, 0)
28362836

28372837
def test_patma_265(self):
2838-
x = 0.25 - 1.75j
2839-
match x:
2840-
case 0.25 - +1.75j:
2841-
y = 0
2842-
self.assertEqual(x, 0.25 - 1.75j)
2843-
self.assertEqual(y, 0)
2844-
2845-
def test_patma_266(self):
28462838
x = 0
28472839
match x:
28482840
case +1e1000:
@@ -3329,6 +3321,34 @@ def test_mapping_pattern_duplicate_key_edge_case3(self):
33293321
pass
33303322
""")
33313323

3324+
def test_duplicate_sign_in_complex_1(self):
3325+
self.assert_syntax_error("""
3326+
match ...:
3327+
case 0 ++ 0j:
3328+
pass
3329+
""")
3330+
3331+
def test_duplicate_sign_in_complex_2(self):
3332+
self.assert_syntax_error("""
3333+
match ...:
3334+
case 0 -+ 0j:
3335+
pass
3336+
""")
3337+
3338+
def test_duplicate_sign_in_complex_3(self):
3339+
self.assert_syntax_error("""
3340+
match ...:
3341+
case 0 +- 0j:
3342+
pass
3343+
""")
3344+
3345+
def test_duplicate_sign_in_complex_4(self):
3346+
self.assert_syntax_error("""
3347+
match ...:
3348+
case 0 -- 0j:
3349+
pass
3350+
""")
3351+
33323352
class TestTypeErrors(unittest.TestCase):
33333353

33343354
def test_accepts_positional_subpatterns_0(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When matching a complex literal in :keyword:`case` statements, an extraneous
2+
``+`` sign (for example, ``1++1j`` or ``1-+1j``) is no longer allowed.

Parser/parser.c

Lines changed: 1 addition & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)