mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
[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:
parent
bbe3c57c86
commit
904af3de2b
6 changed files with 49 additions and 11 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue