-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Open
pablogsal
wants to merge
3
commits into
python:main
Choose a base branch
from
pablogsal:gh-135148
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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 |
---|---|---|
|
@@ -121,38 +121,81 @@ set_ftstring_expr(struct tok_state* tok, struct token *token, char c) { | |
} | ||
PyObject *res = NULL; | ||
|
||
// Check if there is a # character in the expression | ||
// Look for a # character outside of string literals | ||
int hash_detected = 0; | ||
int in_string = 0; | ||
char quote_char = 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]; | ||
|
||
// Skip escaped characters | ||
if (ch == '\\') { | ||
i++; | ||
continue; | ||
} | ||
|
||
// Handle quotes | ||
if (ch == '"' || ch == '\'') { | ||
if (!in_string) { | ||
in_string = 1; | ||
quote_char = ch; | ||
} | ||
else if (ch == quote_char) { | ||
in_string = 0; | ||
} | ||
Comment on lines
+141
to
+147
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. This only works because there's always an odd number of quotes in a |
||
continue; | ||
} | ||
|
||
// Check for # outside strings | ||
if (ch == '#' && !in_string) { | ||
hash_detected = 1; | ||
break; | ||
} | ||
} | ||
|
||
// If we found a # character in the expression, we need to handle comments | ||
if (hash_detected) { | ||
Py_ssize_t input_length = tok_mode->last_expr_size - tok_mode->last_expr_end; | ||
char *result = (char *)PyMem_Malloc((input_length + 1) * sizeof(char)); | ||
// Allocate buffer for processed result | ||
char *result = (char *)PyMem_Malloc((tok_mode->last_expr_size - tok_mode->last_expr_end + 1) * sizeof(char)); | ||
if (!result) { | ||
return -1; | ||
} | ||
|
||
Py_ssize_t i = 0; | ||
Py_ssize_t j = 0; | ||
Py_ssize_t i = 0; // Input position | ||
Py_ssize_t j = 0; // Output position | ||
in_string = 0; // Whether we're in a string | ||
string_quote = 0; // Current string quote char | ||
|
||
for (i = 0, j = 0; i < input_length; i++) { | ||
if (tok_mode->last_expr_buffer[i] == '#') { | ||
// Skip characters until newline or end of 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]; | ||
break; | ||
} | ||
// Process each character | ||
while (i < tok_mode->last_expr_size - tok_mode->last_expr_end) { | ||
char ch = tok_mode->last_expr_buffer[i]; | ||
|
||
// Handle string quotes | ||
if (ch == '"' || ch == '\'') { | ||
if (!in_string) { | ||
in_string = 1; | ||
string_quote = ch; | ||
} else if (ch == string_quote) { | ||
in_string = 0; | ||
} | ||
result[j++] = ch; | ||
} | ||
// Skip comments | ||
else if (ch == '#' && !in_string) { | ||
while (i < tok_mode->last_expr_size - tok_mode->last_expr_end && | ||
tok_mode->last_expr_buffer[i] != '\n') { | ||
i++; | ||
} | ||
} else { | ||
result[j++] = tok_mode->last_expr_buffer[i]; | ||
if (i < tok_mode->last_expr_size - tok_mode->last_expr_end) { | ||
result[j++] = '\n'; | ||
} | ||
} | ||
// Copy other chars | ||
else { | ||
result[j++] = ch; | ||
} | ||
i++; | ||
} | ||
|
||
result[j] = '\0'; // Null-terminate the result string | ||
|
@@ -164,11 +207,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.
Do we need both of these?