Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-125553: Fix backslash continuation in untokenize#126010
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
3bc07b8 Fix backslash continuation in untokenize
tomasr8 cc2fb5e Add news entry
tomasr8 ca62935 Fix Windows
tomasr8 a595dde Be more lenient with test_traceback
tomasr8 6f6a688 Check if a file can be compared exactly
tomasr8 497067a Simplify regex
tomasr8 4b32c8e Use a list for ambiguous files
tomasr8 e2c9bb7 Revert "Use a list for ambiguous files"
tomasr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -169,21 +169,36 @@ def __init__(self): | ||
| self.prev_row = 1 | ||
| self.prev_col = 0 | ||
| self.prev_type = None | ||
| self.prev_line = "" | ||
| self.encoding = None | ||
| def add_whitespace(self, start): | ||
| row, col = start | ||
| 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 @@ def untokenize(self, iterable): | ||
| 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): | ||
pablogsal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.tokens.append(" ") | ||
| self.add_whitespace(start) | ||
| self.tokens.append(token) | ||
| @@ -253,6 +266,7 @@ def untokenize(self, iterable): | ||
| 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): | ||
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2024-10-26-16-59-02.gh-issue-125553.4pDLzt.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix round-trip invariance for backslash continuations in | ||
| :func:`tokenize.untokenize`. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.