summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2021-11-20 14:36:07 (GMT)
committerGitHub <noreply@github.com>2021-11-20 14:36:07 (GMT)
commitfdcc46d9554094994f78bedf6dc9220e5d5ee668 (patch)
tree4f8dbf3fc78bf76311d63c84a5c2384c9fd089d9 /Python/errors.c
parent6d430ef5ab62158a200b94dff31b89524a9576bb (diff)
downloadcpython-fdcc46d9554094994f78bedf6dc9220e5d5ee668.zip
cpython-fdcc46d9554094994f78bedf6dc9220e5d5ee668.tar.gz
cpython-fdcc46d9554094994f78bedf6dc9220e5d5ee668.tar.bz2
bpo-45848: Allow the parser to get error lines from encoded files (GH-29646)
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 519f2d4..cb3938d 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -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