mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-125553: Fix backslash continuation in untokenize
(#126010)
This commit is contained in:
parent
a4760ef8e5
commit
7ad793e5db
3 changed files with 49 additions and 6 deletions
|
@ -169,6 +169,7 @@ class Untokenizer:
|
|||
self.prev_row = 1
|
||||
self.prev_col = 0
|
||||
self.prev_type = None
|
||||
self.prev_line = ""
|
||||
self.encoding = None
|
||||
|
||||
def add_whitespace(self, start):
|
||||
|
@ -176,14 +177,28 @@ class Untokenizer:
|
|||
if row < self.prev_row or row == self.prev_row and col < self.prev_col:
|
||||
raise ValueError("start ({},{}) precedes previous end ({},{})"
|
||||
.format(row, col, self.prev_row, self.prev_col))
|
||||
row_offset = row - self.prev_row
|
||||
if row_offset:
|
||||
self.tokens.append("\\\n" * row_offset)
|
||||
self.prev_col = 0
|
||||
self.add_backslash_continuation(start)
|
||||
col_offset = col - self.prev_col
|
||||
if col_offset:
|
||||
self.tokens.append(" " * col_offset)
|
||||
|
||||
def add_backslash_continuation(self, start):
|
||||
"""Add backslash continuation characters if the row has increased
|
||||
without encountering a newline token.
|
||||
|
||||
This also inserts the correct amount of whitespace before the backslash.
|
||||
"""
|
||||
row = start[0]
|
||||
row_offset = row - self.prev_row
|
||||
if row_offset == 0:
|
||||
return
|
||||
|
||||
newline = '\r\n' if self.prev_line.endswith('\r\n') else '\n'
|
||||
line = self.prev_line.rstrip('\\\r\n')
|
||||
ws = ''.join(_itertools.takewhile(str.isspace, reversed(line)))
|
||||
self.tokens.append(ws + f"\\{newline}" * row_offset)
|
||||
self.prev_col = 0
|
||||
|
||||
def escape_brackets(self, token):
|
||||
characters = []
|
||||
consume_until_next_bracket = False
|
||||
|
@ -243,8 +258,6 @@ class Untokenizer:
|
|||
end_line, end_col = end
|
||||
extra_chars = last_line.count("{{") + last_line.count("}}")
|
||||
end = (end_line, end_col + extra_chars)
|
||||
elif tok_type in (STRING, FSTRING_START) and self.prev_type in (STRING, FSTRING_END):
|
||||
self.tokens.append(" ")
|
||||
|
||||
self.add_whitespace(start)
|
||||
self.tokens.append(token)
|
||||
|
@ -253,6 +266,7 @@ class Untokenizer:
|
|||
self.prev_row += 1
|
||||
self.prev_col = 0
|
||||
self.prev_type = tok_type
|
||||
self.prev_line = line
|
||||
return "".join(self.tokens)
|
||||
|
||||
def compat(self, token, iterable):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue