diff options
author | Ćukasz Langa <lukasz@langa.pl> | 2021-11-20 15:34:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-20 15:34:56 (GMT) |
commit | 904af3de2bef6d971463a564541cb6dadf22d7f8 (patch) | |
tree | dd053ac62a918edcfc1aadfaab13113533a88291 /Python | |
parent | bbe3c57c865439f2194eb760a4362b5506d221a7 (diff) | |
download | cpython-904af3de2bef6d971463a564541cb6dadf22d7f8.zip cpython-904af3de2bef6d971463a564541cb6dadf22d7f8.tar.gz cpython-904af3de2bef6d971463a564541cb6dadf22d7f8.tar.bz2 |
[3.10] bpo-45848: Allow the parser to get error lines from encoded files (GH-29646) (GH-29661)
(cherry picked from commit fdcc46d9554094994f78bedf6dc9220e5d5ee668)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/errors.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Python/errors.c b/Python/errors.c index 600300e..bc1b55e 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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 |