Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (revision 66542) +++ Python/pythonrun.c (working copy) @@ -1150,6 +1150,41 @@ PyErr_PrintEx(1); } +static int +adjust_offset_to_file_encoding(PyObject *f, int offset, const char *text) +{ + PyObject *encoding = NULL; + PyObject *errors = NULL; + PyObject *unicode = NULL; + PyObject *encoded = NULL; + + encoding = PyObject_GetAttrString(f, "encoding"); + if (encoding == NULL) + goto final; + + errors = PyObject_GetAttrString(f, "errors"); + if (errors == NULL) + goto final; + + unicode = PyUnicode_FromStringAndSize(text, offset); + if (unicode == NULL) + goto final; + + encoded = PyUnicode_AsEncodedString(unicode, + _PyUnicode_AsString(encoding), + _PyUnicode_AsString(errors)); + if (encoded == NULL) + goto final; + + offset = PyBytes_GET_SIZE(encoded); +final: + Py_XDECREF(encoding); + Py_XDECREF(errors); + Py_XDECREF(unicode); + Py_XDECREF(encoded); + return offset; +} + static void print_error_text(PyObject *f, int offset, const char *text) { @@ -1176,6 +1211,7 @@ if (offset == -1) return; PyFile_WriteString(" ", f); + offset = adjust_offset_to_file_encoding(f, offset, text); offset--; while (offset > 0) { PyFile_WriteString(" ", f);