summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorAN Long <aisk@users.noreply.github.com>2023-11-18 23:21:04 (GMT)
committerGitHub <noreply@github.com>2023-11-18 23:21:04 (GMT)
commit1a969b4f55f92a17bec82ce0366021a53afdb2c3 (patch)
tree38f65b15bbd9e0d262fb6ba1351ac2cf975b4b23 /Modules/_io
parente52cc80f7fc3a560bf3d0053e0821a2db070cdd1 (diff)
downloadcpython-1a969b4f55f92a17bec82ce0366021a53afdb2c3.zip
cpython-1a969b4f55f92a17bec82ce0366021a53afdb2c3.tar.gz
cpython-1a969b4f55f92a17bec82ce0366021a53afdb2c3.tar.bz2
gh-111965: Use critical sections to make io.TextIOWrapper thread safe (gh-112193)
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/clinic/textio.c.h86
-rw-r--r--Modules/_io/textio.c134
2 files changed, 181 insertions, 39 deletions
diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h
index b24a166..76bf484 100644
--- a/Modules/_io/clinic/textio.c.h
+++ b/Modules/_io/clinic/textio.c.h
@@ -630,7 +630,9 @@ _io_TextIOWrapper_reconfigure(textio *self, PyObject *const *args, Py_ssize_t na
}
write_through_obj = args[4];
skip_optional_kwonly:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_reconfigure_impl(self, encoding, errors, newline_obj, line_buffering_obj, write_through_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -650,7 +652,13 @@ _io_TextIOWrapper_detach_impl(textio *self);
static PyObject *
_io_TextIOWrapper_detach(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_detach_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_detach_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_write__doc__,
@@ -675,7 +683,9 @@ _io_TextIOWrapper_write(textio *self, PyObject *arg)
goto exit;
}
text = arg;
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_write_impl(self, text);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -708,7 +718,9 @@ _io_TextIOWrapper_read(textio *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_read_impl(self, n);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -750,7 +762,9 @@ _io_TextIOWrapper_readline(textio *self, PyObject *const *args, Py_ssize_t nargs
size = ival;
}
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_readline_impl(self, size);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -804,7 +818,9 @@ _io_TextIOWrapper_seek(textio *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_seek_impl(self, cookieObj, whence);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -828,7 +844,13 @@ _io_TextIOWrapper_tell_impl(textio *self);
static PyObject *
_io_TextIOWrapper_tell(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_tell_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_tell_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_truncate__doc__,
@@ -856,7 +878,9 @@ _io_TextIOWrapper_truncate(textio *self, PyObject *const *args, Py_ssize_t nargs
}
pos = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io_TextIOWrapper_truncate_impl(self, pos);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -876,7 +900,13 @@ _io_TextIOWrapper_fileno_impl(textio *self);
static PyObject *
_io_TextIOWrapper_fileno(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_fileno_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_fileno_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_seekable__doc__,
@@ -893,7 +923,13 @@ _io_TextIOWrapper_seekable_impl(textio *self);
static PyObject *
_io_TextIOWrapper_seekable(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_seekable_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_seekable_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_readable__doc__,
@@ -910,7 +946,13 @@ _io_TextIOWrapper_readable_impl(textio *self);
static PyObject *
_io_TextIOWrapper_readable(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_readable_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_readable_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_writable__doc__,
@@ -927,7 +969,13 @@ _io_TextIOWrapper_writable_impl(textio *self);
static PyObject *
_io_TextIOWrapper_writable(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_writable_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_writable_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_isatty__doc__,
@@ -944,7 +992,13 @@ _io_TextIOWrapper_isatty_impl(textio *self);
static PyObject *
_io_TextIOWrapper_isatty(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_isatty_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_isatty_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_flush__doc__,
@@ -961,7 +1015,13 @@ _io_TextIOWrapper_flush_impl(textio *self);
static PyObject *
_io_TextIOWrapper_flush(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_flush_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_flush_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(_io_TextIOWrapper_close__doc__,
@@ -978,6 +1038,12 @@ _io_TextIOWrapper_close_impl(textio *self);
static PyObject *
_io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
{
- return _io_TextIOWrapper_close_impl(self);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _io_TextIOWrapper_close_impl(self);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
-/*[clinic end generated code: output=e58ce89b7354e77a input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ec8ccae78ec3b379 input=a9049054013a1b77]*/
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 8d19198..9f4155a 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -7,14 +7,15 @@
*/
#include "Python.h"
-#include "pycore_call.h" // _PyObject_CallMethod()
-#include "pycore_codecs.h" // _PyCodecInfo_GetIncrementalDecoder()
-#include "pycore_fileutils.h" // _Py_GetLocaleEncoding()
-#include "pycore_interp.h" // PyInterpreterState.fs_codec
-#include "pycore_long.h" // _PyLong_GetZero()
-#include "pycore_object.h" // _PyObject_GC_UNTRACK()
-#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
-#include "pycore_pystate.h" // _PyInterpreterState_GET()
+#include "pycore_call.h" // _PyObject_CallMethod()
+#include "pycore_codecs.h" // _PyCodecInfo_GetIncrementalDecoder()
+#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
+#include "pycore_fileutils.h" // _Py_GetLocaleEncoding()
+#include "pycore_interp.h" // PyInterpreterState.fs_codec
+#include "pycore_long.h" // _PyLong_GetZero()
+#include "pycore_object.h" // _PyObject_GC_UNTRACK()
+#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
+#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "_iomodule.h"
@@ -1327,6 +1328,7 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.reconfigure
*
encoding: object = None
@@ -1346,7 +1348,7 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
PyObject *errors, PyObject *newline_obj,
PyObject *line_buffering_obj,
PyObject *write_through_obj)
-/*[clinic end generated code: output=52b812ff4b3d4b0f input=671e82136e0f5822]*/
+/*[clinic end generated code: output=52b812ff4b3d4b0f input=dc3bd35ebda702a7]*/
{
int line_buffering;
int write_through;
@@ -1531,12 +1533,13 @@ textiowrapper_closed_get(textio *self, void *context);
/*[clinic input]
+@critical_section
_io.TextIOWrapper.detach
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_detach_impl(textio *self)
-/*[clinic end generated code: output=7ba3715cd032d5f2 input=e5a71fbda9e1d9f9]*/
+/*[clinic end generated code: output=7ba3715cd032d5f2 input=c908a3b4ef203b0f]*/
{
PyObject *buffer;
CHECK_ATTACHED(self);
@@ -1622,6 +1625,7 @@ _textiowrapper_writeflush(textio *self)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.write
text: unicode
/
@@ -1629,7 +1633,7 @@ _io.TextIOWrapper.write
static PyObject *
_io_TextIOWrapper_write_impl(textio *self, PyObject *text)
-/*[clinic end generated code: output=d2deb0d50771fcec input=fdf19153584a0e44]*/
+/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/
{
PyObject *ret;
PyObject *b;
@@ -1933,6 +1937,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.read
size as n: Py_ssize_t(accept={int, NoneType}) = -1
/
@@ -1940,7 +1945,7 @@ _io.TextIOWrapper.read
static PyObject *
_io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
-/*[clinic end generated code: output=7e651ce6cc6a25a6 input=123eecbfe214aeb8]*/
+/*[clinic end generated code: output=7e651ce6cc6a25a6 input=67d14c5661121377]*/
{
PyObject *result = NULL, *chunks = NULL;
@@ -2308,6 +2313,7 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.readline
size: Py_ssize_t = -1
/
@@ -2315,7 +2321,7 @@ _io.TextIOWrapper.readline
static PyObject *
_io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size)
-/*[clinic end generated code: output=344afa98804e8b25 input=56c7172483b36db6]*/
+/*[clinic end generated code: output=344afa98804e8b25 input=b65bab871dc3ddba]*/
{
CHECK_ATTACHED(self);
return _textiowrapper_readline(self, size);
@@ -2454,6 +2460,7 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.seek
cookie as cookieObj: object
Zero or an opaque number returned by tell().
@@ -2478,7 +2485,7 @@ and may raise exceptions.
static PyObject *
_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
-/*[clinic end generated code: output=0a15679764e2d04d input=0f68adcb02cf2823]*/
+/*[clinic end generated code: output=0a15679764e2d04d input=4bea78698be23d7e]*/
{
PyObject *posobj;
cookie_type cookie;
@@ -2665,6 +2672,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.tell
Return the stream position as an opaque number.
@@ -2675,7 +2683,7 @@ previous stream position.
static PyObject *
_io_TextIOWrapper_tell_impl(textio *self)
-/*[clinic end generated code: output=4f168c08bf34ad5f input=0852d627d76fb520]*/
+/*[clinic end generated code: output=4f168c08bf34ad5f input=415d6b4e4f8e6e8c]*/
{
PyObject *res;
PyObject *posobj = NULL;
@@ -2901,6 +2909,7 @@ fail:
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.truncate
pos: object = None
/
@@ -2908,7 +2917,7 @@ _io.TextIOWrapper.truncate
static PyObject *
_io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos)
-/*[clinic end generated code: output=90ec2afb9bb7745f input=56ec8baa65aea377]*/
+/*[clinic end generated code: output=90ec2afb9bb7745f input=8bddb320834c93ee]*/
{
CHECK_ATTACHED(self)
@@ -2988,72 +2997,78 @@ textiowrapper_repr(textio *self)
/* Inquiries */
/*[clinic input]
+@critical_section
_io.TextIOWrapper.fileno
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_fileno_impl(textio *self)
-/*[clinic end generated code: output=21490a4c3da13e6c input=c488ca83d0069f9b]*/
+/*[clinic end generated code: output=21490a4c3da13e6c input=515e1196aceb97ab]*/
{
CHECK_ATTACHED(self);
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(fileno));
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.seekable
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_seekable_impl(textio *self)
-/*[clinic end generated code: output=ab223dbbcffc0f00 input=8b005ca06e1fca13]*/
+/*[clinic end generated code: output=ab223dbbcffc0f00 input=71c4c092736c549b]*/
{
CHECK_ATTACHED(self);
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(seekable));
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.readable
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_readable_impl(textio *self)
-/*[clinic end generated code: output=72ff7ba289a8a91b input=0704ea7e01b0d3eb]*/
+/*[clinic end generated code: output=72ff7ba289a8a91b input=80438d1f01b0a89b]*/
{
CHECK_ATTACHED(self);
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable));
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.writable
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_writable_impl(textio *self)
-/*[clinic end generated code: output=a728c71790d03200 input=c41740bc9d8636e8]*/
+/*[clinic end generated code: output=a728c71790d03200 input=9d6c22befb0c340a]*/
{
CHECK_ATTACHED(self);
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable));
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.isatty
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_isatty_impl(textio *self)
-/*[clinic end generated code: output=12be1a35bace882e input=fb68d9f2c99bbfff]*/
+/*[clinic end generated code: output=12be1a35bace882e input=7f83ff04d4d1733d]*/
{
CHECK_ATTACHED(self);
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(isatty));
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.flush
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_flush_impl(textio *self)
-/*[clinic end generated code: output=59de9165f9c2e4d2 input=928c60590694ab85]*/
+/*[clinic end generated code: output=59de9165f9c2e4d2 input=3ac3bf521bfed59d]*/
{
CHECK_ATTACHED(self);
CHECK_CLOSED(self);
@@ -3064,12 +3079,13 @@ _io_TextIOWrapper_flush_impl(textio *self)
}
/*[clinic input]
+@critical_section
_io.TextIOWrapper.close
[clinic start generated code]*/
static PyObject *
_io_TextIOWrapper_close_impl(textio *self)
-/*[clinic end generated code: output=056ccf8b4876e4f4 input=9c2114315eae1948]*/
+/*[clinic end generated code: output=056ccf8b4876e4f4 input=8e12d7079d5ac5c1]*/
{
PyObject *res;
int r;
@@ -3150,21 +3166,41 @@ textiowrapper_iternext(textio *self)
}
static PyObject *
-textiowrapper_name_get(textio *self, void *context)
+textiowrapper_name_get_impl(textio *self, void *context)
{
CHECK_ATTACHED(self);
return PyObject_GetAttr(self->buffer, &_Py_ID(name));
}
static PyObject *
-textiowrapper_closed_get(textio *self, void *context)
+textiowrapper_name_get(textio *self, void *context)
+{
+ PyObject *result = NULL;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_name_get_impl(self, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
+static PyObject *
+textiowrapper_closed_get_impl(textio *self, void *context)
{
CHECK_ATTACHED(self);
return PyObject_GetAttr(self->buffer, &_Py_ID(closed));
}
static PyObject *
-textiowrapper_newlines_get(textio *self, void *context)
+textiowrapper_closed_get(textio *self, void *context)
+{
+ PyObject *result = NULL;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_closed_get_impl(self, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
+static PyObject *
+textiowrapper_newlines_get_impl(textio *self, void *context)
{
PyObject *res;
CHECK_ATTACHED(self);
@@ -3177,21 +3213,51 @@ textiowrapper_newlines_get(textio *self, void *context)
}
static PyObject *
-textiowrapper_errors_get(textio *self, void *context)
+textiowrapper_newlines_get(textio *self, void *context)
+{
+ PyObject *result = NULL;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_newlines_get_impl(self, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
+static PyObject *
+textiowrapper_errors_get_impl(textio *self, void *context)
{
CHECK_INITIALIZED(self);
return Py_NewRef(self->errors);
}
static PyObject *
-textiowrapper_chunk_size_get(textio *self, void *context)
+textiowrapper_errors_get(textio *self, void *context)
+{
+ PyObject *result = NULL;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_errors_get_impl(self, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
+static PyObject *
+textiowrapper_chunk_size_get_impl(textio *self, void *context)
{
CHECK_ATTACHED(self);
return PyLong_FromSsize_t(self->chunk_size);
}
+static PyObject *
+textiowrapper_chunk_size_get(textio *self, void *context)
+{
+ PyObject *result = NULL;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_chunk_size_get_impl(self, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
static int
-textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
+textiowrapper_chunk_size_set_impl(textio *self, PyObject *arg, void *context)
{
Py_ssize_t n;
CHECK_ATTACHED_INT(self);
@@ -3211,6 +3277,16 @@ textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
return 0;
}
+static int
+textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
+{
+ int result = 0;
+ Py_BEGIN_CRITICAL_SECTION(self);
+ result = textiowrapper_chunk_size_set_impl(self, arg, context);
+ Py_END_CRITICAL_SECTION();
+ return result;
+}
+
static PyMethodDef incrementalnewlinedecoder_methods[] = {
_IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
_IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF