Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (révision 70384) +++ Python/pythonrun.c (copie de travail) @@ -1890,6 +1890,25 @@ err_input(err); } +/* Convert the byte index in the UTF-8 string into the character index + in the unicode string */ + +static int +adjust_offset(PyObject *textobj, int byte_offset) +{ + Py_UNICODE *text = PyUnicode_AS_UNICODE(textobj); + Py_ssize_t textlen = PyUnicode_GET_SIZE(textobj); + int character_offset = 0; + Py_ssize_t i; + for (i=0; i < textlen; i++, text++) { + byte_offset -= _PyUnicode_UTF8Size(*text); + if (byte_offset <= 0) + break; + character_offset++; + } + return character_offset; +} + /* Set the error appropriate to the given input error code (see errcode.h) */ static void @@ -1983,6 +2002,8 @@ } else { errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text), "replace"); + if (errtext) + err->offset = adjust_offset(errtext, err->offset); } v = Py_BuildValue("(ziiN)", err->filename, err->lineno, err->offset, errtext);