[3.10] bpo-45848: Allow the parser to get error lines from encoded files (GH-29646) (GH-29661)

(cherry picked from commit fdcc46d955)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Łukasz Langa 2021-11-20 16:34:56 +01:00 committed by GitHub
parent bbe3c57c86
commit 904af3de2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 11 deletions

View file

@ -1724,7 +1724,7 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
functionality in tb_displayline() in traceback.c. */
static PyObject *
err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
err_programtext(PyThreadState *tstate, FILE *fp, int lineno, const char* encoding)
{
int i;
char linebuf[1000];
@ -1752,7 +1752,11 @@ after_loop:
fclose(fp);
if (i == lineno) {
PyObject *res;
res = PyUnicode_FromString(linebuf);
if (encoding != NULL) {
res = PyUnicode_Decode(linebuf, strlen(linebuf), encoding, "replace");
} else {
res = PyUnicode_FromString(linebuf);
}
if (res == NULL)
_PyErr_Clear(tstate);
return res;
@ -1778,7 +1782,7 @@ PyErr_ProgramText(const char *filename, int lineno)
}
PyObject *
PyErr_ProgramTextObject(PyObject *filename, int lineno)
_PyErr_ProgramDecodedTextObject(PyObject *filename, int lineno, const char* encoding)
{
if (filename == NULL || lineno <= 0) {
return NULL;
@ -1790,7 +1794,13 @@ PyErr_ProgramTextObject(PyObject *filename, int lineno)
_PyErr_Clear(tstate);
return NULL;
}
return err_programtext(tstate, fp, lineno);
return err_programtext(tstate, fp, lineno, encoding);
}
PyObject *
PyErr_ProgramTextObject(PyObject *filename, int lineno)
{
return _PyErr_ProgramDecodedTextObject(filename, lineno, NULL);
}
#ifdef __cplusplus