bpo-34683: Make SyntaxError column offsets consistently 1-indexed (gh-9338)

Also point to start of tokens in parsing errors.

Fixes bpo-34683
This commit is contained in:
Ammar Askar 2018-09-24 17:12:49 -04:00 • committed by Guido van Rossum
parent 223e501fb9
commit 025eb98dc0
11 changed files with 65 additions and 21 deletions

View file

@ -187,6 +187,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
parser_state *ps;
node *n;
int started = 0;
int col_offset;
if ((ps = PyParser_New(g, start)) == NULL) {
err_ret->error = E_NOMEM;
@ -203,7 +204,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
int type;
size_t len;
char *str;
int col_offset;
col_offset = -1;
type = PyTokenizer_Get(tok, &a, &b);
if (type == ERRORTOKEN) {
@ -321,7 +322,10 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
if (tok->buf != NULL) {
size_t len;
assert(tok->cur - tok->buf < INT_MAX);
err_ret->offset = (int)(tok->cur - tok->buf);
/* if we've managed to parse a token, point the offset to its start,
* else use the current reading position of the tokenizer
*/
err_ret->offset = col_offset != -1 ? col_offset + 1 : ((int)(tok->cur - tok->buf));
len = tok->inp - tok->buf;
err_ret->text = (char *) PyObject_MALLOC(len + 1);
if (err_ret->text != NULL) {