Skip to content
Merged
Prev Previous commit
Next Next commit
Refactor: replace query with parameter
Pass module state to _unsupported() in textio.c
  • Loading branch information
erlend-aasland committed May 10, 2023
commit 7f34d0afcb3a65772367cfefe243ecf1122d6fa8
41 changes: 23 additions & 18 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ PyDoc_STRVAR(textiobase_doc,
);

static PyObject *
_unsupported(const char *message)
_unsupported(_PyIO_State *state, const char *message)
{
_PyIO_State *state = IO_STATE();
if (state != NULL)
PyErr_SetString(state->unsupported_operation, message);
PyErr_SetString(state->unsupported_operation, message);
return NULL;
}

Expand All @@ -52,7 +50,8 @@ PyDoc_STRVAR(textiobase_detach_doc,
static PyObject *
textiobase_detach(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return _unsupported("detach");
_PyIO_State *state = IO_STATE();
return _unsupported(state, "detach");
}

PyDoc_STRVAR(textiobase_read_doc,
Expand All @@ -65,7 +64,8 @@ PyDoc_STRVAR(textiobase_read_doc,
static PyObject *
textiobase_read(PyObject *self, PyObject *args)
{
return _unsupported("read");
_PyIO_State *state = IO_STATE();
return _unsupported(state, "read");
}

PyDoc_STRVAR(textiobase_readline_doc,
Expand All @@ -77,7 +77,8 @@ PyDoc_STRVAR(textiobase_readline_doc,
static PyObject *
textiobase_readline(PyObject *self, PyObject *args)
{
return _unsupported("readline");
_PyIO_State *state = IO_STATE();
return _unsupported(state, "readline");
}

PyDoc_STRVAR(textiobase_write_doc,
Expand All @@ -89,7 +90,8 @@ PyDoc_STRVAR(textiobase_write_doc,
static PyObject *
textiobase_write(PyObject *self, PyObject *args)
{
return _unsupported("write");
_PyIO_State *state = IO_STATE();
return _unsupported(state, "write");
}

PyDoc_STRVAR(textiobase_encoding_doc,
Expand Down Expand Up @@ -1349,7 +1351,8 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
/* Check if something is in the read buffer */
if (self->decoded_chars != NULL) {
if (encoding != Py_None || errors != Py_None || newline_obj != NULL) {
_unsupported("It is not possible to set the encoding or newline "
_unsupported(self->state,
"It is not possible to set the encoding or newline "
"of stream after the first read");
return NULL;
}
Expand Down Expand Up @@ -1616,8 +1619,9 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
CHECK_ATTACHED(self);
CHECK_CLOSED(self);

if (self->encoder == NULL)
return _unsupported("not writable");
if (self->encoder == NULL) {
return _unsupported(self->state, "not writable");
}

Py_INCREF(text);

Expand Down Expand Up @@ -1798,7 +1802,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
*/

if (self->decoder == NULL) {
_unsupported("not readable");
_unsupported(self->state, "not readable");
return -1;
}

Expand Down Expand Up @@ -1923,8 +1927,9 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
CHECK_ATTACHED(self);
CHECK_CLOSED(self);

if (self->decoder == NULL)
return _unsupported("not readable");
if (self->decoder == NULL) {
return _unsupported(self->state, "not readable");
}

if (_textiowrapper_writeflush(self) < 0)
return NULL;
Expand Down Expand Up @@ -2455,7 +2460,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
Py_INCREF(cookieObj);

if (!self->seekable) {
_unsupported("underlying stream is not seekable");
_unsupported(self->state, "underlying stream is not seekable");
goto fail;
}

Expand All @@ -2469,7 +2474,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;

if (cmp == 0) {
_unsupported("can't do nonzero cur-relative seeks");
_unsupported(self->state, "can't do nonzero cur-relative seeks");
goto fail;
}

Expand All @@ -2489,7 +2494,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;

if (cmp == 0) {
_unsupported("can't do nonzero end-relative seeks");
_unsupported(self->state, "can't do nonzero end-relative seeks");
goto fail;
}

Expand Down Expand Up @@ -2652,7 +2657,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
CHECK_CLOSED(self);

if (!self->seekable) {
_unsupported("underlying stream is not seekable");
_unsupported(self->state, "underlying stream is not seekable");
goto fail;
}
if (!self->telling) {
Expand Down