gh-109611: Add convenient C API function _PyFile_Flush() (GH-109612)

This commit is contained in:
Serhiy Storchaka 2023-09-23 09:35:30 +03:00 committed by GitHub
parent 92af0cc580
commit b8d1744e7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 54 additions and 97 deletions

View file

@ -2083,11 +2083,9 @@ builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep,
}
if (flush) {
PyObject *tmp = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
if (tmp == NULL) {
if (_PyFile_Flush(file) < 0) {
return NULL;
}
Py_DECREF(tmp);
}
Py_RETURN_NONE;
@ -2146,11 +2144,9 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
}
/* First of all, flush stderr */
tmp = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
if (tmp == NULL)
if (_PyFile_Flush(ferr) < 0) {
PyErr_Clear();
else
Py_DECREF(tmp);
}
/* We should only use (GNU) readline if Python's sys.stdin and
sys.stdout are the same as C's stdin and stdout, because we
@ -2218,11 +2214,9 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
if (stdin_errors_str == NULL) {
goto _readline_errors;
}
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
if (tmp == NULL)
if (_PyFile_Flush(fout) < 0) {
PyErr_Clear();
else
Py_DECREF(tmp);
}
if (prompt != NULL) {
/* We have a prompt, encode it as stdout would */
const char *stdout_encoding_str, *stdout_errors_str;
@ -2325,11 +2319,9 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
return NULL;
}
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
if (tmp == NULL)
if (_PyFile_Flush(fout) < 0) {
PyErr_Clear();
else
Py_DECREF(tmp);
}
return PyFile_GetLine(fin, -1);
}

View file

@ -1513,11 +1513,9 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
}
/* Explicitly call file.flush() */
PyObject *res = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
if (!res) {
if (_PyFile_Flush(file) < 0) {
return -1;
}
Py_DECREF(res);
return 0;
}

View file

@ -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;
}

View file

@ -1562,14 +1562,10 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
Py_XDECREF(ctx.seen);
/* Call file.flush() */
PyObject *res = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
if (!res) {
if (_PyFile_Flush(file) < 0) {
/* Silently ignore file.flush() error */
PyErr_Clear();
}
else {
Py_DECREF(res);
}
}
void
@ -1674,11 +1670,7 @@ flush_io_stream(PyThreadState *tstate, PyObject *name)
{
PyObject *f = _PySys_GetAttr(tstate, name);
if (f != NULL) {
PyObject *r = PyObject_CallMethodNoArgs(f, &_Py_ID(flush));
if (r) {
Py_DECREF(r);
}
else {
if (_PyFile_Flush(f) < 0) {
PyErr_Clear();
}
}