aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-23 09:35:30 +0300
committerGitHub <noreply@github.com>2023-09-23 09:35:30 +0300
commitb8d1744e7ba87a4057350fdfd788b5621095fc59 (patch)
tree367ba89c2a83a1115f74b1e0c83384e047ef4b84 /Python/pylifecycle.c
parent92af0cc580051fd1129c7a86af2cbadeb2aa36dc (diff)
downloadcpython-b8d1744e7ba87a4057350fdfd788b5621095fc59.tar.gz
cpython-b8d1744e7ba87a4057350fdfd788b5621095fc59.zip
gh-109611: Add convenient C API function _PyFile_Flush() (GH-109612)
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c17
1 files changed, 3 insertions, 14 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 48000153854..aec8da10249 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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;
}