Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (revision 61632) +++ Python/pythonrun.c (working copy) @@ -1173,7 +1173,55 @@ PyErr_PrintEx(1); } +static char * +get_attr(PyObject *f, const char *name) +{ + char* attr = NULL; + PyObject *v; + + v = PyObject_GetAttrString(f, name); + if (v && PyUnicode_Check(v)) { + char *s = PyUnicode_AsString(v); + if (s) attr = strdup(s); + } + Py_XDECREF(v); + return attr; +} + static void +adjust_offset(PyObject *f, const char *text, int *offset) +{ + char *encoding = NULL, *errors = NULL; + PyObject *unicode = NULL, *encoded = NULL; + + encoding = get_attr(f, "encoding"); + if (encoding == NULL) + goto final; + + errors = get_attr(f, "errors"); + if (errors == NULL) + goto final; + + unicode = PyUnicode_FromStringAndSize(text, *offset); + if (unicode == NULL) + goto final; + + encoded = PyUnicode_AsEncodedString(unicode, encoding, errors); + if (encoded == NULL) + goto final; + + *offset = PyString_GET_SIZE(encoded); + +final: + if (encoding) + free(encoding); + if (errors) + free(errors); + Py_XDECREF(unicode); + Py_XDECREF(encoded); +} + +static void print_error_text(PyObject *f, int offset, const char *text) { char *nl; @@ -1198,6 +1246,7 @@ PyFile_WriteString("\n", f); if (offset == -1) return; + adjust_offset(f, text, &offset); PyFile_WriteString(" ", f); offset--; while (offset > 0) {