Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- #922 Classify autoimport project sources by path containment (@marlon-costa-dc)
- #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar)
- #933 Add type annotations to project.py (@lieryan)
- #844 Fix patchedast end-quote detection for `'''` inside a double-quoted f-string (@marlon-costa-dc)
- #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl)
- #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc)
- #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li)
Expand Down
19 changes: 12 additions & 7 deletions rope/refactor/patchedast.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,19 @@ def start_quote_char():
return self.source[start : quote_pos + len(quote_char)]

def end_quote_char():
possible_quotes = [
(self.source.source.rfind(q, start, end), q)
for q in reversed(QUOTE_CHARS)
]
_, quote_pos, quote_char = max(
(len(q), pos, q) for pos, q in possible_quotes if pos != -1
# The closing delimiter is the quote char the f-string literally
# ends with -- NOT the longest quote char anywhere in the body.
# A ''' appearing inside a "..." f-string body must not be picked
# as the delimiter (issue: end_quote_char used max(len(q)) and
# mis-matched, producing a token that consume_joined_string could
# not locate).
for quote_char in QUOTE_CHARS:
if self.source.source[end - len(quote_char) : end] == quote_char:
return self.source[end - len(quote_char) : end]
raise MismatchedTokenError(
f"f-string at line {self.lines.get_line_number(start)} does not"
" end with a quote delimiter"
)
return self.source[end - len(quote_char) : end]

QUOTE_CHARS = ['"""', "'''", '"', "'"]
offset = self.source.offset
Expand Down
22 changes: 22 additions & 0 deletions ropetest/refactor/patchedasttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,28 @@ def test_handling_match_or_and_singleton(self):
"MatchOr", ["MatchValue", " ", "|", " ", "MatchSingleton"]
)

@testutils.only_for_versions_higher("3.6")
def test_handling_format_strings_with_triple_quote_in_body(self):
# A double-quoted f-string whose body contains a ''' sequence must not
# confuse the end-quote detection (end_quote_char picked the longest
# quote in the body instead of the matching delimiter).
source = 'f"abc = \'\'\'{a}"\n'
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
checker.check_children(
"JoinedStr", ['f"', "abc = '''", "FormattedValue", "", '"']
)
checker.check_children("FormattedValue", ["{", "", "Name", "", "}"])

def test_format_string_region_without_closing_quote_is_rejected(self):
source = 'f"abc{a}"\n'
node = ast.parse(source)
node.body[0].value.end_col_offset -= 1
with self.assertRaisesRegex(
patchedast.MismatchedTokenError, "does not end with a quote delimiter"
):
patchedast.patch_ast(node, source)

@testutils.only_for_versions_higher("3.6")
def test_handling_format_strings_with_implicit_join(self):
source = '''"1" + rf'abc{a}' f"""xxx{b} """\n'''
Expand Down
Loading