From 9ab97b7fdf7903a86bf6653498e2a616a1d653c1 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Mon, 10 Jan 2022 22:49:50 +0300 Subject: [PATCH 1/2] Fix handling of standalone match/case with newlines/comments --- CHANGES.md | 2 + src/blib2to3/pgen2/parse.py | 4 ++ tests/data/pattern_matching_style.py | 89 ++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a1c8ccb0b7d..64f14792ef4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,8 @@ be formatted. (#2526) - Speed-up the new backtracking parser about 4X in general (enabled when `--target-version` is set to 3.10 and higher). (#2728) +- Fix handling of standalone `match()` or `case()` when used there is a trailing newline + or a comment inside of the parentheses. (#2760) ### Packaging diff --git a/src/blib2to3/pgen2/parse.py b/src/blib2to3/pgen2/parse.py index 8fe96672897..4a23d538b49 100644 --- a/src/blib2to3/pgen2/parse.py +++ b/src/blib2to3/pgen2/parse.py @@ -269,6 +269,10 @@ def addtoken(self, type: int, value: Text, context: Context) -> bool: break next_token_type, next_token_value, *_ = proxy.eat(counter) + if next_token_type in (tokenize.COMMENT, tokenize.NL): + counter += 1 + continue + if next_token_type == tokenize.OP: next_token_type = grammar.opmap[next_token_value] diff --git a/tests/data/pattern_matching_style.py b/tests/data/pattern_matching_style.py index c1c0aeedb70..0c779fb7795 100644 --- a/tests/data/pattern_matching_style.py +++ b/tests/data/pattern_matching_style.py @@ -10,6 +10,60 @@ ): print(2) case a: pass +match( + arg # comment +) + +match( +) + +match( + + +) + +case( + arg # comment +) + +case( +) + +case( + + +) + + +match something: + case b(): print(1+1) + case c( + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=- 1 + ): print(1) + case c( + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + ): print(2) + case a: pass + +re.match( + something # fast +) +re.match( + + + +) +match match( + + +): + case case( + arg, # comment + ): + pass + # output match something: @@ -25,3 +79,38 @@ print(2) case a: pass + +match(arg) # comment + +match() + +match() + +case(arg) # comment + +case() + +case() + + +match something: + case b(): + print(1 + 1) + case c( + very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + ): + print(1) + case c( + very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + ): + print(2) + case a: + pass + +re.match(something) # fast +re.match() +match match(): + case case( + arg, # comment + ): + pass From 8a24d857401b86405674d0fb6eea6ebc98bd0510 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Mon, 10 Jan 2022 22:59:54 +0300 Subject: [PATCH 2/2] Fix the test case --- CHANGES.md | 4 ++-- tests/data/pattern_matching_style.py | 31 +++------------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 64f14792ef4..748dbca7019 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,8 +26,8 @@ be formatted. (#2526) - Speed-up the new backtracking parser about 4X in general (enabled when `--target-version` is set to 3.10 and higher). (#2728) -- Fix handling of standalone `match()` or `case()` when used there is a trailing newline - or a comment inside of the parentheses. (#2760) +- Fix handling of standalone `match()` or `case()` when there is a trailing newline or a + comment inside of the parentheses. (#2760) ### Packaging diff --git a/tests/data/pattern_matching_style.py b/tests/data/pattern_matching_style.py index 0c779fb7795..8e18ce2ada6 100644 --- a/tests/data/pattern_matching_style.py +++ b/tests/data/pattern_matching_style.py @@ -6,7 +6,7 @@ ): print(1) case c( very_complex=True, - perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, ): print(2) case a: pass @@ -35,18 +35,6 @@ ) -match something: - case b(): print(1+1) - case c( - very_complex=True, - perhaps_even_loooooooooooooooooooooooooooooooooooooong=- 1 - ): print(1) - case c( - very_complex=True, - perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 - ): print(2) - case a: pass - re.match( something # fast ) @@ -74,7 +62,8 @@ ): print(1) case c( - very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, ): print(2) case a: @@ -93,20 +82,6 @@ case() -match something: - case b(): - print(1 + 1) - case c( - very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 - ): - print(1) - case c( - very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 - ): - print(2) - case a: - pass - re.match(something) # fast re.match() match match():