mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-109611: Add convenient C API function _PyFile_Flush() (GH-109612)
This commit is contained in:
parent
92af0cc580
commit
b8d1744e7b
11 changed files with 54 additions and 97 deletions
|
@ -1639,27 +1639,20 @@ flush_std_files(void)
|
|||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *fout = _PySys_GetAttr(tstate, &_Py_ID(stdout));
|
||||
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
PyObject *tmp;
|
||||
int status = 0;
|
||||
|
||||
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
|
||||
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
|
||||
if (tmp == NULL) {
|
||||
if (_PyFile_Flush(fout) < 0) {
|
||||
PyErr_WriteUnraisable(fout);
|
||||
status = -1;
|
||||
}
|
||||
else
|
||||
Py_DECREF(tmp);
|
||||
}
|
||||
|
||||
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
|
||||
tmp = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
|
||||
if (tmp == NULL) {
|
||||
if (_PyFile_Flush(ferr) < 0) {
|
||||
PyErr_Clear();
|
||||
status = -1;
|
||||
}
|
||||
else
|
||||
Py_DECREF(tmp);
|
||||
}
|
||||
|
||||
return status;
|
||||
|
@ -2632,13 +2625,9 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
|
|||
Py_DECREF(exc);
|
||||
|
||||
/* sys.stderr may be buffered: call sys.stderr.flush() */
|
||||
PyObject *res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
|
||||
if (res == NULL) {
|
||||
if (_PyFile_Flush(ferr) < 0) {
|
||||
_PyErr_Clear(tstate);
|
||||
}
|
||||
else {
|
||||
Py_DECREF(res);
|
||||
}
|
||||
|
||||
return has_tb;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue