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

Skip to content

Commit 27a60b1

Browse files
committed
PyFile_WriteString now returns an error indicator instead of calling
PyErr_Clear().
1 parent 745b8cf commit 27a60b1

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

Objects/fileobject.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -997,28 +997,34 @@ PyFile_WriteObject(v, f, flags)
997997
return 0;
998998
}
999999

1000-
void
1000+
int
10011001
PyFile_WriteString(s, f)
10021002
char *s;
10031003
PyObject *f;
10041004
{
10051005
if (f == NULL) {
1006-
/* Do nothing */
1006+
/* Should be caused by a pre-existing error */
1007+
if(!PyErr_Occurred())
1008+
PyErr_SetString(PyExc_SystemError,
1009+
"null file for PyFile_WriteString");
1010+
return -1;
10071011
}
10081012
else if (PyFile_Check(f)) {
10091013
FILE *fp = PyFile_AsFile(f);
1010-
if (fp != NULL)
1011-
fputs(s, fp);
1014+
if (fp == NULL) {
1015+
err_closed();
1016+
return -1;
1017+
}
1018+
fputs(s, fp);
1019+
return 0;
10121020
}
10131021
else if (!PyErr_Occurred()) {
10141022
PyObject *v = PyString_FromString(s);
1015-
if (v == NULL) {
1016-
PyErr_Clear();
1017-
}
1018-
else {
1019-
if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
1020-
PyErr_Clear();
1021-
Py_DECREF(v);
1022-
}
1023+
int err;
1024+
if (v == NULL)
1025+
return -1;
1026+
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
1027+
Py_DECREF(v);
1028+
return err;
10231029
}
10241030
}

0 commit comments

Comments
 (0)