Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5deb210

Browse files
committed
Explicitly convert err->text to Unicode. Fixes #1069.
1 parent 90d1fcd commit 5deb210

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Python/pythonrun.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ PyParser_SetError(perrdetail *err)
14591459
static void
14601460
err_input(perrdetail *err)
14611461
{
1462-
PyObject *v, *w, *errtype;
1462+
PyObject *v, *w, *errtype, *errtext;
14631463
PyObject* u = NULL;
14641464
char *msg = NULL;
14651465
errtype = PyExc_SyntaxError;
@@ -1539,8 +1539,17 @@ err_input(perrdetail *err)
15391539
msg = "unknown parsing error";
15401540
break;
15411541
}
1542-
v = Py_BuildValue("(ziiz)", err->filename,
1543-
err->lineno, err->offset, err->text);
1542+
/* err->text may not be UTF-8 in case of decoding errors.
1543+
Explicitly convert to an object. */
1544+
if (!err->text) {
1545+
errtext = Py_None;
1546+
Py_INCREF(Py_None);
1547+
} else {
1548+
errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1549+
"replace");
1550+
}
1551+
v = Py_BuildValue("(ziiN)", err->filename,
1552+
err->lineno, err->offset, errtext);
15441553
if (err->text != NULL) {
15451554
PyObject_FREE(err->text);
15461555
err->text = NULL;

0 commit comments

Comments
 (0)