Skip to content

gh-104658: Fix location of unclosed quote error for multiline f-strings #104660

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 2 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,21 @@ def test_not_closing_quotes(self):
self.assertAllRaise(SyntaxError, "unterminated f-string literal", ['f"', "f'"])
self.assertAllRaise(SyntaxError, "unterminated triple-quoted f-string literal",
['f"""', "f'''"])

# Ensure that the errors are reported at the correct line number.
data = '''\
x = 1 + 1
y = 2 + 2
z = f"""
sdfjnsdfjsdf
sdfsdfs{1+
2} dfigdf {3+
4}sdufsd""
'''
try:
compile(data, "?", "exec")
except SyntaxError as e:
self.assertEqual(e.text, 'z = f"""')
self.assertEqual(e.lineno, 3)
def test_syntax_error_after_debug(self):
self.assertAllRaise(SyntaxError, "f-string: expecting a valid expression after '{'",
[
Expand Down
7 changes: 5 additions & 2 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ tok_underflow_interactive(struct tok_state *tok) {

static int
tok_underflow_file(struct tok_state *tok) {
if (tok->start == NULL) {
if (tok->start == NULL && !INSIDE_FSTRING(tok)) {
tok->cur = tok->inp = tok->buf;
}
if (tok->decoding_state == STATE_INIT) {
Expand Down Expand Up @@ -2250,6 +2250,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
the_current_tok->f_string_quote_size = quote_size;
the_current_tok->f_string_start = tok->start;
the_current_tok->f_string_multi_line_start = tok->line_start;
the_current_tok->f_string_line_start = tok->lineno;
the_current_tok->f_string_start_offset = -1;
the_current_tok->f_string_multi_line_start_offset = -1;
the_current_tok->last_expr_buffer = NULL;
Expand Down Expand Up @@ -2580,7 +2581,9 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
tok->cur++;
tok->line_start = current_tok->f_string_multi_line_start;
int start = tok->lineno;
tok->lineno = tok->first_lineno;

tokenizer_mode *the_current_tok = TOK_GET_MODE(tok);
tok->lineno = the_current_tok->f_string_line_start;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like tok->lineno is only an int.


if (current_tok->f_string_quote_size == 3) {
return MAKE_TOKEN(syntaxerror(tok,
Expand Down
1 change: 1 addition & 0 deletions Parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct _tokenizer_mode {
int f_string_raw;
const char* f_string_start;
const char* f_string_multi_line_start;
int f_string_line_start;

Py_ssize_t f_string_start_offset;
Py_ssize_t f_string_multi_line_start_offset;
Expand Down