-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-135148: Correctly handle f/t strings with comments and debug expressions #135198
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
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core_and_Builtins/2025-06-06-02-24-42.gh-issue-135148.r-t2sC.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,3 @@ | ||
| Fixed a bug where f-string debug expressions (using =) would incorrectly | ||
| strip out parts of strings containing escaped quotes and # characters. Patch | ||
| by Pablo Galindo. |
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 |
|---|---|---|
|
|
@@ -123,35 +123,96 @@ set_ftstring_expr(struct tok_state* tok, struct token *token, char c) { | |
|
|
||
| // Check if there is a # character in the expression | ||
| int hash_detected = 0; | ||
| int in_string = 0; | ||
| char string_quote = 0; | ||
| for (Py_ssize_t i = 0; i < tok_mode->last_expr_size - tok_mode->last_expr_end; i++) { | ||
| if (tok_mode->last_expr_buffer[i] == '#') { | ||
| char ch = tok_mode->last_expr_buffer[i]; | ||
| if (ch == '\\' && i + 1 < tok_mode->last_expr_size - tok_mode->last_expr_end) { | ||
| // Skip the next character if it's an escape sequence | ||
| i++; | ||
| continue; | ||
| } | ||
| if (ch == '"' || ch == '\'') { | ||
| if (!in_string) { | ||
| in_string = 1; | ||
| string_quote = ch; | ||
| } else if (ch == string_quote) { | ||
| // Check for triple quotes | ||
| if (i > 0 && tok_mode->last_expr_buffer[i-1] == ch && | ||
| i > 1 && tok_mode->last_expr_buffer[i-2] == ch) { | ||
| // Skip the rest of the triple quote | ||
| i += 2; | ||
| } | ||
| in_string = 0; | ||
| } | ||
| } else if (ch == '#' && !in_string) { | ||
| hash_detected = 1; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // If we found a # character in the expression, we need to handle comments | ||
| if (hash_detected) { | ||
| // Calculate length of input we need to process | ||
| Py_ssize_t input_length = tok_mode->last_expr_size - tok_mode->last_expr_end; | ||
|
|
||
| // Allocate buffer for processed result, with room for null terminator | ||
| char *result = (char *)PyMem_Malloc((input_length + 1) * sizeof(char)); | ||
| if (!result) { | ||
| return -1; | ||
| } | ||
|
|
||
| Py_ssize_t i = 0; | ||
| Py_ssize_t j = 0; | ||
| // Initialize counters and state | ||
| Py_ssize_t i = 0; // Input position | ||
| Py_ssize_t j = 0; // Output position | ||
| in_string = 0; // Whether we're currently inside a string | ||
| string_quote = 0; // The quote character for current string (' or ") | ||
|
|
||
| // Process each character of input | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lysnikolaou please read this carefully. I am mostly sure its correct but it really needs some extra eyes |
||
| for (i = 0, j = 0; i < input_length; i++) { | ||
| if (tok_mode->last_expr_buffer[i] == '#') { | ||
| // Skip characters until newline or end of string | ||
| char ch = tok_mode->last_expr_buffer[i]; | ||
|
|
||
| // Handle escape sequences - copy both backslash and next char | ||
| if (ch == '\\' && i + 1 < input_length) { | ||
| result[j++] = ch; // Copy backslash | ||
| result[j++] = tok_mode->last_expr_buffer[++i]; // Copy escaped char | ||
| continue; | ||
| } | ||
|
|
||
| // Handle string quotes | ||
| if (ch == '"' || ch == '\'') { | ||
| if (!in_string) { | ||
| // Start of new string | ||
| in_string = 1; | ||
| string_quote = ch; | ||
| } else if (ch == string_quote) { | ||
| // Potential end of string - check for triple quotes | ||
| if (i > 0 && tok_mode->last_expr_buffer[i-1] == ch && | ||
| i > 1 && tok_mode->last_expr_buffer[i-2] == ch) { | ||
| // Found triple quote - copy all three quotes | ||
| result[j++] = ch; | ||
| result[j++] = ch; | ||
| result[j++] = ch; | ||
| i += 2; // Skip the other two quotes | ||
| continue; | ||
| } | ||
|
lysnikolaou marked this conversation as resolved.
Outdated
|
||
| // End of regular string | ||
| in_string = 0; | ||
| } | ||
| result[j++] = ch; // Copy the quote character | ||
| } | ||
| // Handle comments - skip everything until newline | ||
| else if (ch == '#' && !in_string) { | ||
| while (i < input_length && tok_mode->last_expr_buffer[i] != '\0') { | ||
| if (tok_mode->last_expr_buffer[i] == '\n') { | ||
| result[j++] = tok_mode->last_expr_buffer[i]; | ||
| result[j++] = tok_mode->last_expr_buffer[i]; // Keep newline | ||
| break; | ||
| } | ||
| i++; | ||
| i++; // Skip comment character | ||
| } | ||
| } else { | ||
| result[j++] = tok_mode->last_expr_buffer[i]; | ||
| } | ||
| // Copy any other character unchanged | ||
| else { | ||
| result[j++] = ch; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -164,11 +225,9 @@ set_ftstring_expr(struct tok_state* tok, struct token *token, char c) { | |
| tok_mode->last_expr_size - tok_mode->last_expr_end, | ||
| NULL | ||
| ); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| if (!res) { | ||
| if (!res) { | ||
| return -1; | ||
| } | ||
| token->metadata = res; | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only works because there's always an odd number of quotes in a
STRINGtoken and we're sure that allSTRINGtokens will be valid at this point. Maybe add a comment to explain that?