summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-23 06:35:30 (GMT)
committerGitHub <noreply@github.com>2023-09-23 06:35:30 (GMT)
commitb8d1744e7ba87a4057350fdfd788b5621095fc59 (patch)
tree367ba89c2a83a1115f74b1e0c83384e047ef4b84 /Modules
parent92af0cc580051fd1129c7a86af2cbadeb2aa36dc (diff)
downloadcpython-b8d1744e7ba87a4057350fdfd788b5621095fc59.zip
cpython-b8d1744e7ba87a4057350fdfd788b5621095fc59.tar.gz
cpython-b8d1744e7ba87a4057350fdfd788b5621095fc59.tar.bz2
gh-109611: Add convenient C API function _PyFile_Flush() (GH-109612)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_io/bufferedio.c14
-rw-r--r--Modules/_io/iobase.c15
-rw-r--r--Modules/_io/textio.c44
-rw-r--r--Modules/_threadmodule.c4
-rw-r--r--Modules/faulthandler.c5
5 files changed, 27 insertions, 55 deletions
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 0983a7b..e8caf9f 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -553,17 +553,14 @@ _io__Buffered_close_impl(buffered *self)
}
/* flush() will most probably re-take the lock, so drop it first */
LEAVE_BUFFERED(self)
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ r = _PyFile_Flush((PyObject *)self);
if (!ENTER_BUFFERED(self)) {
return NULL;
}
PyObject *exc = NULL;
- if (res == NULL) {
+ if (r < 0) {
exc = PyErr_GetRaisedException();
}
- else {
- Py_DECREF(res);
- }
res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
@@ -593,12 +590,11 @@ static PyObject *
_io__Buffered_detach_impl(buffered *self)
/*[clinic end generated code: output=dd0fc057b8b779f7 input=482762a345cc9f44]*/
{
- PyObject *raw, *res;
+ PyObject *raw;
CHECK_INITIALIZED(self)
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
return NULL;
- Py_DECREF(res);
+ }
raw = self->raw;
self->raw = NULL;
self->detached = 1;
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 78f0f94..4da8e5b 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -265,7 +265,7 @@ static PyObject *
_io__IOBase_close_impl(PyObject *self)
/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
{
- int rc, closed = iobase_is_closed(self);
+ int rc1, rc2, closed = iobase_is_closed(self);
if (closed < 0) {
return NULL;
@@ -274,19 +274,14 @@ _io__IOBase_close_impl(PyObject *self)
Py_RETURN_NONE;
}
- PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush));
-
+ rc1 = _PyFile_Flush(self);
PyObject *exc = PyErr_GetRaisedException();
- rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True);
+ rc2 = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True);
_PyErr_ChainExceptions1(exc);
- if (rc < 0) {
- Py_CLEAR(res);
- }
-
- if (res == NULL)
+ if (rc1 < 0 || rc2 < 0) {
return NULL;
+ }
- Py_DECREF(res);
Py_RETURN_NONE;
}
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 91b677b..10ef8a8 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1368,11 +1368,9 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
return NULL;
}
- PyObject *res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL) {
+ if (_PyFile_Flush((PyObject *)self) < 0) {
return NULL;
}
- Py_DECREF(res);
self->b2cratio = 0;
if (newline_obj != NULL && set_newline(self, newline) < 0) {
@@ -1508,12 +1506,11 @@ static PyObject *
_io_TextIOWrapper_detach_impl(textio *self)
/*[clinic end generated code: output=7ba3715cd032d5f2 input=e5a71fbda9e1d9f9]*/
{
- PyObject *buffer, *res;
+ PyObject *buffer;
CHECK_ATTACHED(self);
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
return NULL;
- Py_DECREF(res);
+ }
buffer = self->buffer;
self->buffer = NULL;
self->detached = 1;
@@ -1713,10 +1710,9 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
}
if (needflush) {
- ret = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush));
- if (ret == NULL)
+ if (_PyFile_Flush(self->buffer) < 0) {
return NULL;
- Py_DECREF(ret);
+ }
}
textiowrapper_set_decoded_chars(self, NULL);
@@ -2502,10 +2498,9 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
goto fail;
- Py_DECREF(res);
+ }
textiowrapper_set_decoded_chars(self, NULL);
Py_CLEAR(self->snapshot);
@@ -2550,10 +2545,9 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
goto fail;
- Py_DECREF(res);
+ }
/* The strategy of seek() is to go back to the safe start point
* and replay the effect of read(chars_to_skip) from there.
@@ -2677,10 +2671,9 @@ _io_TextIOWrapper_tell_impl(textio *self)
if (_textiowrapper_writeflush(self) < 0)
return NULL;
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
goto fail;
- Py_DECREF(res);
+ }
posobj = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(tell));
if (posobj == NULL)
@@ -2885,14 +2878,11 @@ static PyObject *
_io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos)
/*[clinic end generated code: output=90ec2afb9bb7745f input=56ec8baa65aea377]*/
{
- PyObject *res;
-
CHECK_ATTACHED(self)
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
+ if (_PyFile_Flush((PyObject *)self) < 0) {
return NULL;
- Py_DECREF(res);
+ }
return PyObject_CallMethodOneArg(self->buffer, &_Py_ID(truncate), pos);
}
@@ -3076,13 +3066,9 @@ _io_TextIOWrapper_close_impl(textio *self)
PyErr_Clear();
}
}
- res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL) {
+ if (_PyFile_Flush((PyObject *)self) < 0) {
exc = PyErr_GetRaisedException();
}
- else {
- Py_DECREF(res);
- }
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));
if (exc != NULL) {
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 7692bac..9c91548 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1497,11 +1497,9 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
_PyErr_Display(file, exc_type, exc_value, exc_traceback);
/* 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;
}
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index f05cdd9..b051c71 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -146,10 +146,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
return -1;
}
- result = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
- if (result != NULL)
- Py_DECREF(result);
- else {
+ if (_PyFile_Flush(file) < 0) {
/* ignore flush() error */
PyErr_Clear();
}