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

Fix --experiemental-string-processing crash when matching parens not found #2283

Merged
merged 2 commits into from May 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,7 @@
`.gitignore` rules like `git` does) (#2225)
- Restored compatibility with Click 8.0 on Python 3.6 when LANG=C used (#2227)
- Add extra uvloop install + import support if in python env (#2258)
- Fix --experimental-string-processing crash when matching parens are not found (#2283)

### _Blackd_

Expand Down
14 changes: 10 additions & 4 deletions src/black/trans.py
Expand Up @@ -25,7 +25,7 @@
from black.mode import Feature
from black.nodes import syms, replace_child, parent_type
from black.nodes import is_empty_par, is_empty_lpar, is_empty_rpar
from black.nodes import CLOSING_BRACKETS, STANDALONE_COMMENT
from black.nodes import OPENING_BRACKETS, CLOSING_BRACKETS, STANDALONE_COMMENT
from black.lines import Line, append_leaves
from black.brackets import BracketMatchError
from black.comments import contains_pragma_comment
Expand Down Expand Up @@ -1398,6 +1398,11 @@ class StringParenWrapper(CustomSplitMapMixin, BaseStringSplitter):
def do_splitter_match(self, line: Line) -> TMatchResult:
LL = line.leaves

if line.leaves[-1].type in OPENING_BRACKETS:
return TErr(
"Cannot wrap parens around a line that ends in an opening bracket."
)

string_idx = (
self._return_match(LL)
or self._else_match(LL)
Expand Down Expand Up @@ -1665,9 +1670,10 @@ def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
right_leaves.pop()

if old_parens_exist:
assert (
right_leaves and right_leaves[-1].type == token.RPAR
), "Apparently, old parentheses do NOT exist?!"
assert right_leaves and right_leaves[-1].type == token.RPAR, (
"Apparently, old parentheses do NOT exist?!"
f" (left_leaves={left_leaves}, right_leaves={right_leaves})"
)
old_rpar_leaf = right_leaves.pop()

append_leaves(string_line, line, right_leaves)
Expand Down
20 changes: 20 additions & 0 deletions tests/data/long_strings__regression.py
Expand Up @@ -396,6 +396,16 @@ def xxxxxxx_xxxxxx(xxxx):
" it has now"
)


def _legacy_listen_examples():
text += (
" \"listen for the '%(event_name)s' event\"\n"
"\n # ... (event logic logic logic) ...\n"
% {
"since": since,
}
)

# output


Expand Down Expand Up @@ -886,3 +896,13 @@ def xxxxxxx_xxxxxx(xxxx):
" it goes over 88 characters which"
" it has now"
)


def _legacy_listen_examples():
text += (
" \"listen for the '%(event_name)s' event\"\n"
"\n # ... (event logic logic logic) ...\n"
% {
"since": since,
}
)