-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
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) { | ||
|
Member
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. Just a question: after fixing this, when
Contributor
Author
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. I would think it's not possible now, though I don't know of all the cases for every other token. I think tqs are the only multiline token (since iirc escaped newlines are not a token) |
||
| 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++; | ||
| } | ||
| } | ||
|
Member
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. I don't understand this function well. These value are get from Is this OK?
Contributor
Author
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. yeah I think so, what's happening here (and the variable names aren't super great):
From reading #1800 (where this function was introduced) it was discussed briefly but I didn't pick up the rationale for that loop there. When I removed / adjusted it during testing it broke all of the f-string column / offset tests so I think it's necessary. |
||
| } | ||
| } | ||
| 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
-1due to the multiline string).For instance, here's the ast dump of (using astpretty)
You'll notice that the
BinOpand itsleft(Num) have the samecol_offset