Skip to content

Commit b9348e7

Browse files
committed
fix parse_syntax_error to clean up its resources
1 parent b6e21a0 commit b9348e7

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

Python/pythonrun.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -989,55 +989,67 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
989989
return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
990990
lineno, offset, text);
991991

992-
/* new style errors. `err' is an instance */
992+
*message = NULL;
993993

994-
if (! (v = PyObject_GetAttrString(err, "msg")))
994+
/* new style errors. `err' is an instance */
995+
*message = PyObject_GetAttrString(err, "msg");
996+
if (!*message)
995997
goto finally;
996-
*message = v;
997998

998-
if (!(v = PyObject_GetAttrString(err, "filename")))
999+
v = PyObject_GetAttrString(err, "filename");
1000+
if (!v)
9991001
goto finally;
1000-
if (v == Py_None)
1002+
if (v == Py_None) {
1003+
Py_DECREF(v);
10011004
*filename = NULL;
1002-
else if (! (*filename = PyString_AsString(v)))
1003-
goto finally;
1005+
}
1006+
else {
1007+
*filename = PyString_AsString(v);
1008+
Py_DECREF(v);
1009+
if (!*filename)
1010+
goto finally;
1011+
}
10041012

1005-
Py_DECREF(v);
1006-
if (!(v = PyObject_GetAttrString(err, "lineno")))
1013+
v = PyObject_GetAttrString(err, "lineno");
1014+
if (!v)
10071015
goto finally;
10081016
hold = PyInt_AsLong(v);
10091017
Py_DECREF(v);
1010-
v = NULL;
10111018
if (hold < 0 && PyErr_Occurred())
10121019
goto finally;
10131020
*lineno = (int)hold;
10141021

1015-
if (!(v = PyObject_GetAttrString(err, "offset")))
1022+
v = PyObject_GetAttrString(err, "offset");
1023+
if (!v)
10161024
goto finally;
10171025
if (v == Py_None) {
10181026
*offset = -1;
10191027
Py_DECREF(v);
1020-
v = NULL;
10211028
} else {
10221029
hold = PyInt_AsLong(v);
10231030
Py_DECREF(v);
1024-
v = NULL;
10251031
if (hold < 0 && PyErr_Occurred())
10261032
goto finally;
10271033
*offset = (int)hold;
10281034
}
10291035

1030-
if (!(v = PyObject_GetAttrString(err, "text")))
1036+
v = PyObject_GetAttrString(err, "text");
1037+
if (!v)
10311038
goto finally;
1032-
if (v == Py_None)
1039+
if (v == Py_None) {
1040+
Py_DECREF(v);
10331041
*text = NULL;
1034-
else if (! (*text = PyString_AsString(v)))
1035-
goto finally;
1036-
Py_DECREF(v);
1042+
}
1043+
else {
1044+
*text = PyString_AsString(v);
1045+
Py_DECREF(v);
1046+
if (!*text)
1047+
goto finally;
1048+
}
10371049
return 1;
10381050

10391051
finally:
1040-
Py_XDECREF(v);
1052+
Py_XDECREF(*message);
10411053
return 0;
10421054
}
10431055

0 commit comments

Comments
 (0)