diff -r 829117ae2e55 Include/unicodeobject.h --- a/Include/unicodeobject.h Tue Jul 19 16:46:09 2016 -0500 +++ b/Include/unicodeobject.h Fri Jul 22 02:33:46 2016 +1000 @@ -2073,6 +2073,10 @@ PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s); +/* Returns -1 if the string is a valid identifier, otherwise the index of +the first character that is illegal. 0 is returned for empty strings. */ +PyAPI_FUNC(int) _PyUnicode_FindNonIdentifier(PyObject *self); + #ifndef Py_LIMITED_API /* Externally visible for str.strip(unicode) */ PyAPI_FUNC(PyObject *) _PyUnicode_XStrip( diff -r 829117ae2e55 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Tue Jul 19 16:46:09 2016 -0500 +++ b/Objects/unicodeobject.c Fri Jul 22 02:33:46 2016 +1000 @@ -11923,6 +11923,12 @@ int PyUnicode_IsIdentifier(PyObject *self) { + return _PyUnicode_FindNonIdentifier(self) == -1; +} + +int +_PyUnicode_FindNonIdentifier(PyObject *self) +{ int kind; void *data; Py_ssize_t i; @@ -11953,8 +11959,8 @@ for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) - return 0; - return 1; + return i; + return -1; } PyDoc_STRVAR(isidentifier__doc__, diff -r 829117ae2e55 Parser/parsetok.c --- a/Parser/parsetok.c Tue Jul 19 16:46:09 2016 -0500 +++ b/Parser/parsetok.c Fri Jul 22 02:33:46 2016 +1000 @@ -320,7 +320,10 @@ if (tok->buf != NULL) { size_t len; assert(tok->cur - tok->buf < INT_MAX); - err_ret->offset = (int)(tok->cur - tok->buf); + if (tok->start) + err_ret->offset = (int)(tok->start - tok->buf + 1); + else + err_ret->offset = (int)(tok->cur - tok->buf); len = tok->inp - tok->buf; err_ret->text = (char *) PyObject_MALLOC(len + 1); if (err_ret->text != NULL) { diff -r 829117ae2e55 Parser/tokenizer.c --- a/Parser/tokenizer.c Tue Jul 19 16:46:09 2016 -0500 +++ b/Parser/tokenizer.c Fri Jul 22 02:33:46 2016 +1000 @@ -1325,11 +1325,13 @@ } return 0; } - result = PyUnicode_IsIdentifier(s); + result = _PyUnicode_FindNonIdentifier(s); Py_DECREF(s); - if (result == 0) - tok->done = E_IDENTIFIER; - return result; + if (result == -1) + return 1; + tok->done = E_IDENTIFIER; + tok->start += result + 1; + return 0; } #endif