-
- Notifications
You must be signed in to change notification settings - Fork 33.1k
bpo-16806: Fix lineno
and col_offset
for multi-line string tokens. #10021
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -1844,3 +1844,4 @@ Gennadiy Zlobin | |
Doug Zongker | ||
Peter Åstrand | ||
Zheao Li | ||
Carsten Klein |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix ``lineno`` and ``col_offset`` for multi-line string tokens. |
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -205,6 +205,8 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, | |
size_t len; | ||
char *str; | ||
col_offset = -1; | ||
int lineno; | ||
const char *line_start; | ||
| ||
type = PyTokenizer_Get(tok, &a, &b); | ||
if (type == ERRORTOKEN) { | ||
| @@ -253,8 +255,15 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, | |
} | ||
} | ||
#endif | ||
if (a != NULL && a >= tok->line_start) { | ||
col_offset = Py_SAFE_DOWNCAST(a - tok->line_start, | ||
| ||
/* Nodes of type STRING, especially multi line strings | ||
must be handled differently in order to get both | ||
the starting line number and the column offset right. | ||
(cf. issue 16806) */ | ||
lineno = type == STRING ? tok->first_lineno : tok->lineno; | ||
line_start = type == STRING ? tok->multi_line_start : tok->line_start; | ||
if (a != NULL && a >= line_start) { | ||
| ||
col_offset = Py_SAFE_DOWNCAST(a - line_start, | ||
intptr_t, int); | ||
} | ||
else { | ||
| @@ -263,7 +272,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, | |
| ||
if ((err_ret->error = | ||
PyParser_AddToken(ps, (int)type, str, | ||
tok->lineno, col_offset, | ||
lineno, col_offset, | ||
&(err_ret->expected))) != E_OK) { | ||
if (err_ret->error != E_DONE) { | ||
PyObject_FREE(str); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -4282,9 +4282,13 @@ fstring_fix_node_location(const node *parent, node *n, char *expr_str) | |
start--; | ||
} | ||
cols += (int)(substr - start); | ||
/* Fix lineno in mulitline strings. */ | ||
while ((substr = strchr(substr + 1, '\n'))) | ||
lines--; | ||
/* adjust the start based on the number of newlines encountered | ||
before the f-string expression */ | ||
for (char* p = parent->n_str; p < substr; p++) { | ||
if (*p == '\n') { | ||
lines++; | ||
} | ||
} | ||
| ||
} | ||
} | ||
fstring_shift_node_locations(n, lines, cols); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I suspect this was incorrect before (being adjusted by
-1
due to the multiline string).For instance, here's the ast dump of (using astpretty)
You'll notice that the
BinOp
and itsleft
(Num
) have the samecol_offset