-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-121130: Fix f-string format specifiers with debug expressions #121150
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 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c371cc9
gh-121130: Fix f-string format specifiers with debug expressions
pablogsal 89bd65a
Fix unparsing of fstring with format specs
pablogsal f4aed72
Fixes
pablogsal 93ab4a6
Fixes
pablogsal b97f4fd
Fixes
pablogsal 3333a49
fix tests
pablogsal 899cf9a
fix tests
pablogsal 4114e0c
Update Parser/lexer/lexer.c
pablogsal 88a0296
Update Parser/lexer/lexer.c
pablogsal e6e0f26
Update action_helpers.c
pablogsal 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core and Builtins/2024-06-29-10-46-14.gh-issue-121130.Rj66Xs.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 f-strings with debug expressions in format specifiers. 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
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 |
---|---|---|
|
@@ -989,6 +989,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t | |
the_current_tok->last_expr_buffer = NULL; | ||
the_current_tok->last_expr_size = 0; | ||
the_current_tok->last_expr_end = -1; | ||
the_current_tok->in_format_spec = 0; | ||
the_current_tok->f_string_debug = 0; | ||
|
||
switch (*tok->start) { | ||
|
@@ -1137,15 +1138,20 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t | |
* by the `{` case, so for ensuring that we are on the 0th level, we need | ||
* to adjust it manually */ | ||
int cursor = current_tok->curly_bracket_depth - (c != '{'); | ||
if (cursor == 0 && !_PyLexer_update_fstring_expr(tok, c)) { | ||
int in_format_spec = current_tok->in_format_spec; | ||
int cursor_in_format_with_debug = | ||
cursor == 1 && (current_tok->f_string_debug || in_format_spec); | ||
int cursor_valid = cursor == 0 || cursor_in_format_with_debug; | ||
if (cursor_valid && !_PyLexer_update_fstring_expr(tok, c)) { | ||
return MAKE_TOKEN(ENDMARKER); | ||
} | ||
if (cursor == 0 && c != '{' && set_fstring_expr(tok, token, c)) { | ||
if (cursor_valid && c != '{' && set_fstring_expr(tok, token, c)) { | ||
return MAKE_TOKEN(ERRORTOKEN); | ||
} | ||
|
||
if (c == ':' && cursor == current_tok->curly_bracket_expr_start_depth) { | ||
current_tok->kind = TOK_FSTRING_MODE; | ||
current_tok->in_format_spec = 1; | ||
p_start = tok->start; | ||
p_end = tok->cur; | ||
return MAKE_TOKEN(_PyToken_OneChar(c)); | ||
|
@@ -1235,6 +1241,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t | |
if (c == '}' && current_tok->curly_bracket_depth == current_tok->curly_bracket_expr_start_depth) { | ||
current_tok->curly_bracket_expr_start_depth--; | ||
current_tok->kind = TOK_FSTRING_MODE; | ||
current_tok->in_format_spec = 0; | ||
current_tok->f_string_debug = 0; | ||
} | ||
} | ||
|
@@ -1337,6 +1344,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct | |
if (in_format_spec && c == '\n') { | ||
tok_backup(tok, c); | ||
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE; | ||
current_tok->in_format_spec = 0; | ||
p_start = tok->start; | ||
p_end = tok->cur; | ||
return MAKE_TOKEN(FSTRING_MIDDLE); | ||
|
@@ -1387,6 +1395,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct | |
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "f-string: expressions nested too deeply")); | ||
} | ||
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE; | ||
current_tok->in_format_spec = 0; | ||
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. Shouldn't this always be 0 here, if we've correctly reset it when the format spec is ending? |
||
p_start = tok->start; | ||
p_end = tok->cur; | ||
} else { | ||
|
@@ -1413,6 +1422,7 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct | |
tok_backup(tok, peek); | ||
tok_backup(tok, c); | ||
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE; | ||
current_tok->in_format_spec = 0; | ||
p_start = tok->start; | ||
p_end = tok->cur; | ||
} | ||
|
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
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 here is because when there are debug expressions we are wrapping the values in a node that then is wrapped in turn, but that is not what 3.11 and before does so we need to detect that and unwrap.