8000 bpo-46339: Fix crash in the parser when computing error text for multi-line f-strings by pablogsal · Pull Request #30529 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46339: Fix crash in the pars 8000 er when computing error text for multi-line f-strings #30529

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
Jan 11, 2022
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
6 changes: 6 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ def baz():
}
\"\"\"
}'''""", 5, 17)
check('''f"""


{
6
0="""''', 5, 13)

# Errors thrown by symtable.c
check('x = [(yield i) for i in range(3)]', 1, 7)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in the parser when retrieving the error text for multi-line
f-strings expressions that do not start in the first line of the string.
Patch by Pablo Galindo
11 changes: 9 additions & 2 deletions Parser/pegen_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,15 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
assert(cur_line != NULL);

for (int i = 0; i < lineno - 1; i++) {
cur_line = strchr(cur_line, '\n') + 1;
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;

for (int i = 0; i < relative_lineno - 1; i++) {
char *new_line = strchr(cur_line, '\n') + 1;
assert(new_line != NULL && new_line < p->tok->inp);
if (new_line == NULL || new_line >= p->tok->inp) {
break;
}
cur_line = new_line;
}

char *next_newline;
Expand Down
0