mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-45848: Allow the parser to get error lines from encoded files (GH-29646)
This commit is contained in:
parent
6d430ef5ab
commit
fdcc46d955
5 changed files with 42 additions and 11 deletions
|
@ -1692,7 +1692,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];
|
||||
|
@ -1720,7 +1720,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;
|
||||
|
@ -1746,7 +1750,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;
|
||||
|
@ -1758,7 +1762,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