gh-101819: Refactor _io futher in preparation for module isolation (#104369)

This commit is contained in:
Erlend E. Aasland 2023-05-11 12:26:30 +02:00 committed by GitHub
parent 7dabb35f83
commit d0a738c6df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 268 additions and 116 deletions

View file

@ -215,27 +215,45 @@ PyDoc_STRVAR(_io_FileIO_readinto__doc__,
"Same as RawIOBase.readinto()."); "Same as RawIOBase.readinto().");
#define _IO_FILEIO_READINTO_METHODDEF \ #define _IO_FILEIO_READINTO_METHODDEF \
{"readinto", (PyCFunction)_io_FileIO_readinto, METH_O, _io_FileIO_readinto__doc__}, {"readinto", _PyCFunction_CAST(_io_FileIO_readinto), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_readinto__doc__},
static PyObject * static PyObject *
_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer); _io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer);
static PyObject * static PyObject *
_io_FileIO_readinto(fileio *self, PyObject *arg) _io_FileIO_readinto(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "readinto",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_buffer buffer = {NULL, NULL}; Py_buffer buffer = {NULL, NULL};
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear(); PyErr_Clear();
_PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg); _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]);
goto exit; goto exit;
} }
if (!PyBuffer_IsContiguous(&buffer, 'C')) { if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg); _PyArg_BadArgument("readinto", "argument 1", "contiguous buffer", args[0]);
goto exit; goto exit;
} }
return_value = _io_FileIO_readinto_impl(self, &buffer); return_value = _io_FileIO_readinto_impl(self, cls, &buffer);
exit: exit:
/* Cleanup for buffer */ /* Cleanup for buffer */
@ -278,28 +296,43 @@ PyDoc_STRVAR(_io_FileIO_read__doc__,
"Return an empty bytes object at EOF."); "Return an empty bytes object at EOF.");
#define _IO_FILEIO_READ_METHODDEF \ #define _IO_FILEIO_READ_METHODDEF \
{"read", _PyCFunction_CAST(_io_FileIO_read), METH_FASTCALL, _io_FileIO_read__doc__}, {"read", _PyCFunction_CAST(_io_FileIO_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_read__doc__},
static PyObject * static PyObject *
_io_FileIO_read_impl(fileio *self, Py_ssize_t size); _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size);
static PyObject * static PyObject *
_io_FileIO_read(fileio *self, PyObject *const *args, Py_ssize_t nargs) _io_FileIO_read(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "read",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_ssize_t size = -1; Py_ssize_t size = -1;
if (!_PyArg_CheckPositional("read", nargs, 0, 1)) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
if (!args) {
goto exit; goto exit;
} }
if (nargs < 1) { if (nargs < 1) {
goto skip_optional; goto skip_optional_posonly;
} }
if (!_Py_convert_optional_to_ssize_t(args[0], &size)) { if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
goto exit; goto exit;
} }
skip_optional: skip_optional_posonly:
return_value = _io_FileIO_read_impl(self, size); return_value = _io_FileIO_read_impl(self, cls, size);
exit: exit:
return return_value; return return_value;
@ -316,25 +349,43 @@ PyDoc_STRVAR(_io_FileIO_write__doc__,
"returns None if the write would block."); "returns None if the write would block.");
#define _IO_FILEIO_WRITE_METHODDEF \ #define _IO_FILEIO_WRITE_METHODDEF \
{"write", (PyCFunction)_io_FileIO_write, METH_O, _io_FileIO_write__doc__}, {"write", _PyCFunction_CAST(_io_FileIO_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_write__doc__},
static PyObject * static PyObject *
_io_FileIO_write_impl(fileio *self, Py_buffer *b); _io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b);
static PyObject * static PyObject *
_io_FileIO_write(fileio *self, PyObject *arg) _io_FileIO_write(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "write",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_buffer b = {NULL, NULL}; Py_buffer b = {NULL, NULL};
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &b, PyBUF_SIMPLE) != 0) {
goto exit; goto exit;
} }
if (!PyBuffer_IsContiguous(&b, 'C')) { if (!PyBuffer_IsContiguous(&b, 'C')) {
_PyArg_BadArgument("write", "argument", "contiguous buffer", arg); _PyArg_BadArgument("write", "argument 1", "contiguous buffer", args[0]);
goto exit; goto exit;
} }
return_value = _io_FileIO_write_impl(self, &b); return_value = _io_FileIO_write_impl(self, cls, &b);
exit: exit:
/* Cleanup for b */ /* Cleanup for b */
@ -422,26 +473,41 @@ PyDoc_STRVAR(_io_FileIO_truncate__doc__,
"The current file position is changed to the value of size."); "The current file position is changed to the value of size.");
#define _IO_FILEIO_TRUNCATE_METHODDEF \ #define _IO_FILEIO_TRUNCATE_METHODDEF \
{"truncate", _PyCFunction_CAST(_io_FileIO_truncate), METH_FASTCALL, _io_FileIO_truncate__doc__}, {"truncate", _PyCFunction_CAST(_io_FileIO_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_truncate__doc__},
static PyObject * static PyObject *
_io_FileIO_truncate_impl(fileio *self, PyObject *posobj); _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj);
static PyObject * static PyObject *
_io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs) _io_FileIO_truncate(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "truncate",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
PyObject *posobj = Py_None; PyObject *posobj = Py_None;
if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
if (!args) {
goto exit; goto exit;
} }
if (nargs < 1) { if (nargs < 1) {
goto skip_optional; goto skip_optional_posonly;
} }
posobj = args[0]; posobj = args[0];
skip_optional: skip_optional_posonly:
return_value = _io_FileIO_truncate_impl(self, posobj); return_value = _io_FileIO_truncate_impl(self, cls, posobj);
exit: exit:
return return_value; return return_value;
@ -470,4 +536,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF #ifndef _IO_FILEIO_TRUNCATE_METHODDEF
#define _IO_FILEIO_TRUNCATE_METHODDEF #define _IO_FILEIO_TRUNCATE_METHODDEF
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */ #endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
/*[clinic end generated code: output=29ed2ae6c451c139 input=a9049054013a1b77]*/ /*[clinic end generated code: output=bef47b31b644996a input=a9049054013a1b77]*/

View file

@ -20,15 +20,19 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_close__doc__,
"close() may be called more than once without error."); "close() may be called more than once without error.");
#define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \ #define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \
{"close", (PyCFunction)_io__WindowsConsoleIO_close, METH_NOARGS, _io__WindowsConsoleIO_close__doc__}, {"close", _PyCFunction_CAST(_io__WindowsConsoleIO_close), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_close__doc__},
static PyObject * static PyObject *
_io__WindowsConsoleIO_close_impl(winconsoleio *self); _io__WindowsConsoleIO_close_impl(winconsoleio *self, PyTypeObject *cls);
static PyObject * static PyObject *
_io__WindowsConsoleIO_close(winconsoleio *self, PyObject *Py_UNUSED(ignored)) _io__WindowsConsoleIO_close(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
return _io__WindowsConsoleIO_close_impl(self); if (nargs) {
PyErr_SetString(PyExc_TypeError, "close() takes no arguments");
return NULL;
}
return _io__WindowsConsoleIO_close_impl(self, cls);
} }
#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */ #endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
@ -208,27 +212,46 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_readinto__doc__,
"Same as RawIOBase.readinto()."); "Same as RawIOBase.readinto().");
#define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF \ #define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF \
{"readinto", (PyCFunction)_io__WindowsConsoleIO_readinto, METH_O, _io__WindowsConsoleIO_readinto__doc__}, {"readinto", _PyCFunction_CAST(_io__WindowsConsoleIO_readinto), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_readinto__doc__},
static PyObject * static PyObject *
_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer); _io__WindowsConsoleIO_readinto_impl(winconsoleio *self, PyTypeObject *cls,
Py_buffer *buffer);
static PyObject * static PyObject *
_io__WindowsConsoleIO_readinto(winconsoleio *self, PyObject *arg) _io__WindowsConsoleIO_readinto(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "readinto",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_buffer buffer = {NULL, NULL}; Py_buffer buffer = {NULL, NULL};
if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &buffer, PyBUF_WRITABLE) < 0) {
PyErr_Clear(); PyErr_Clear();
_PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg); _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]);
goto exit; goto exit;
} }
if (!PyBuffer_IsContiguous(&buffer, 'C')) { if (!PyBuffer_IsContiguous(&buffer, 'C')) {
_PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg); _PyArg_BadArgument("readinto", "argument 1", "contiguous buffer", args[0]);
goto exit; goto exit;
} }
return_value = _io__WindowsConsoleIO_readinto_impl(self, &buffer); return_value = _io__WindowsConsoleIO_readinto_impl(self, cls, &buffer);
exit: exit:
/* Cleanup for buffer */ /* Cleanup for buffer */
@ -278,28 +301,44 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_read__doc__,
"Return an empty bytes object at EOF."); "Return an empty bytes object at EOF.");
#define _IO__WINDOWSCONSOLEIO_READ_METHODDEF \ #define _IO__WINDOWSCONSOLEIO_READ_METHODDEF \
{"read", _PyCFunction_CAST(_io__WindowsConsoleIO_read), METH_FASTCALL, _io__WindowsConsoleIO_read__doc__}, {"read", _PyCFunction_CAST(_io__WindowsConsoleIO_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_read__doc__},
static PyObject * static PyObject *
_io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size); _io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
Py_ssize_t size);
static PyObject * static PyObject *
_io__WindowsConsoleIO_read(winconsoleio *self, PyObject *const *args, Py_ssize_t nargs) _io__WindowsConsoleIO_read(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "read",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_ssize_t size = -1; Py_ssize_t size = -1;
if (!_PyArg_CheckPositional("read", nargs, 0, 1)) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
if (!args) {
goto exit; goto exit;
} }
if (nargs < 1) { if (nargs < 1) {
goto skip_optional; goto skip_optional_posonly;
} }
if (!_Py_convert_optional_to_ssize_t(args[0], &size)) { if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
goto exit; goto exit;
} }
skip_optional: skip_optional_posonly:
return_value = _io__WindowsConsoleIO_read_impl(self, size); return_value = _io__WindowsConsoleIO_read_impl(self, cls, size);
exit: exit:
return return_value; return return_value;
@ -319,25 +358,44 @@ PyDoc_STRVAR(_io__WindowsConsoleIO_write__doc__,
"The number of bytes actually written is returned."); "The number of bytes actually written is returned.");
#define _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF \ #define _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF \
{"write", (PyCFunction)_io__WindowsConsoleIO_write, METH_O, _io__WindowsConsoleIO_write__doc__}, {"write", _PyCFunction_CAST(_io__WindowsConsoleIO_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_write__doc__},
static PyObject * static PyObject *
_io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b); _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
Py_buffer *b);
static PyObject * static PyObject *
_io__WindowsConsoleIO_write(winconsoleio *self, PyObject *arg) _io__WindowsConsoleIO_write(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
#else
# define KWTUPLE NULL
#endif
static const char * const _keywords[] = {"", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "write",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
Py_buffer b = {NULL, NULL}; Py_buffer b = {NULL, NULL};
if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) { args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &b, PyBUF_SIMPLE) != 0) {
goto exit; goto exit;
} }
if (!PyBuffer_IsContiguous(&b, 'C')) { if (!PyBuffer_IsContiguous(&b, 'C')) {
_PyArg_BadArgument("write", "argument", "contiguous buffer", arg); _PyArg_BadArgument("write", "argument 1", "contiguous buffer", args[0]);
goto exit; goto exit;
} }
return_value = _io__WindowsConsoleIO_write_impl(self, &b); return_value = _io__WindowsConsoleIO_write_impl(self, cls, &b);
exit: exit:
/* Cleanup for b */ /* Cleanup for b */
@ -407,4 +465,4 @@ _io__WindowsConsoleIO_isatty(winconsoleio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF #ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF #define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */ #endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
/*[clinic end generated code: output=163e934aa9b0ef16 input=a9049054013a1b77]*/ /*[clinic end generated code: output=235393758365c229 input=a9049054013a1b77]*/

View file

@ -549,13 +549,10 @@ err_closed(void)
} }
static PyObject * static PyObject *
err_mode(const char *action) err_mode(_PyIO_State *state, const char *action)
{ {
_PyIO_State *state = IO_STATE(); return PyErr_Format(state->unsupported_operation,
if (state != NULL) "File not open for %s", action);
PyErr_Format(state->unsupported_operation,
"File not open for %s", action);
return NULL;
} }
/*[clinic input] /*[clinic input]
@ -631,6 +628,7 @@ _io_FileIO_seekable_impl(fileio *self)
/*[clinic input] /*[clinic input]
_io.FileIO.readinto _io.FileIO.readinto
cls: defining_class
buffer: Py_buffer(accept={rwbuffer}) buffer: Py_buffer(accept={rwbuffer})
/ /
@ -638,16 +636,18 @@ Same as RawIOBase.readinto().
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer) _io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer)
/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/ /*[clinic end generated code: output=97f0f3d69534db34 input=fd20323e18ce1ec8]*/
{ {
Py_ssize_t n; Py_ssize_t n;
int err; int err;
if (self->fd < 0) if (self->fd < 0)
return err_closed(); return err_closed();
if (!self->readable) if (!self->readable) {
return err_mode("reading"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "reading");
}
n = _Py_read(self->fd, buffer->buf, buffer->len); n = _Py_read(self->fd, buffer->buf, buffer->len);
/* copy errno because PyBuffer_Release() can indirectly modify it */ /* copy errno because PyBuffer_Release() can indirectly modify it */
@ -784,6 +784,7 @@ _io_FileIO_readall_impl(fileio *self)
/*[clinic input] /*[clinic input]
_io.FileIO.read _io.FileIO.read
cls: defining_class
size: Py_ssize_t(accept={int, NoneType}) = -1 size: Py_ssize_t(accept={int, NoneType}) = -1
/ /
@ -795,8 +796,8 @@ Return an empty bytes object at EOF.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io_FileIO_read_impl(fileio *self, Py_ssize_t size) _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/ /*[clinic end generated code: output=bbd749c7c224143e input=f613d2057e4a1918]*/
{ {
char *ptr; char *ptr;
Py_ssize_t n; Py_ssize_t n;
@ -804,8 +805,10 @@ _io_FileIO_read_impl(fileio *self, Py_ssize_t size)
if (self->fd < 0) if (self->fd < 0)
return err_closed(); return err_closed();
if (!self->readable) if (!self->readable) {
return err_mode("reading"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "reading");
}
if (size < 0) if (size < 0)
return _io_FileIO_readall_impl(self); return _io_FileIO_readall_impl(self);
@ -843,6 +846,7 @@ _io_FileIO_read_impl(fileio *self, Py_ssize_t size)
/*[clinic input] /*[clinic input]
_io.FileIO.write _io.FileIO.write
cls: defining_class
b: Py_buffer b: Py_buffer
/ /
@ -854,16 +858,18 @@ returns None if the write would block.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io_FileIO_write_impl(fileio *self, Py_buffer *b) _io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b)
/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/ /*[clinic end generated code: output=927e25be80f3b77b input=2776314f043088f5]*/
{ {
Py_ssize_t n; Py_ssize_t n;
int err; int err;
if (self->fd < 0) if (self->fd < 0)
return err_closed(); return err_closed();
if (!self->writable) if (!self->writable) {
return err_mode("writing"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "writing");
}
n = _Py_write(self->fd, b->buf, b->len); n = _Py_write(self->fd, b->buf, b->len);
/* copy errno because PyBuffer_Release() can indirectly modify it */ /* copy errno because PyBuffer_Release() can indirectly modify it */
@ -994,6 +1000,7 @@ _io_FileIO_tell_impl(fileio *self)
#ifdef HAVE_FTRUNCATE #ifdef HAVE_FTRUNCATE
/*[clinic input] /*[clinic input]
_io.FileIO.truncate _io.FileIO.truncate
cls: defining_class
size as posobj: object = None size as posobj: object = None
/ /
@ -1004,8 +1011,8 @@ The current file position is changed to the value of size.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io_FileIO_truncate_impl(fileio *self, PyObject *posobj) _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj)
/*[clinic end generated code: output=e49ca7a916c176fa input=b0ac133939823875]*/ /*[clinic end generated code: output=d936732a49e8d5a2 input=c367fb45d6bb2c18]*/
{ {
Py_off_t pos; Py_off_t pos;
int ret; int ret;
@ -1014,8 +1021,10 @@ _io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
fd = self->fd; fd = self->fd;
if (fd < 0) if (fd < 0)
return err_closed(); return err_closed();
if (!self->writable) if (!self->writable) {
return err_mode("writing"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "writing");
}
if (posobj == Py_None) { if (posobj == Py_None) {
/* Get the current position. */ /* Get the current position. */

View file

@ -42,11 +42,9 @@ PyDoc_STRVAR(textiobase_doc,
); );
static PyObject * static PyObject *
_unsupported(const char *message) _unsupported(_PyIO_State *state, const char *message)
{ {
_PyIO_State *state = IO_STATE(); PyErr_SetString(state->unsupported_operation, message);
if (state != NULL)
PyErr_SetString(state->unsupported_operation, message);
return NULL; return NULL;
} }
@ -64,7 +62,8 @@ static PyObject *
_io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls) _io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
/*[clinic end generated code: output=50915f40c609eaa4 input=987ca3640d0a3776]*/ /*[clinic end generated code: output=50915f40c609eaa4 input=987ca3640d0a3776]*/
{ {
return _unsupported("detach"); _PyIO_State *state = IO_STATE();
return _unsupported(state, "detach");
} }
/*[clinic input] /*[clinic input]
@ -83,7 +82,8 @@ static PyObject *
_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls, PyObject *args) _io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls, PyObject *args)
/*[clinic end generated code: output=3adf28998831f461 input=cee1e84664a20de0]*/ /*[clinic end generated code: output=3adf28998831f461 input=cee1e84664a20de0]*/
{ {
return _unsupported("read"); _PyIO_State *state = IO_STATE();
return _unsupported(state, "read");
} }
/*[clinic input] /*[clinic input]
@ -102,7 +102,8 @@ _io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls,
PyObject *args) PyObject *args)
/*[clinic end generated code: output=3073a948d02319f3 input=58f801259f7ff3ef]*/ /*[clinic end generated code: output=3073a948d02319f3 input=58f801259f7ff3ef]*/
{ {
return _unsupported("readline"); _PyIO_State *state = IO_STATE();
return _unsupported(state, "readline");
} }
/*[clinic input] /*[clinic input]
@ -121,7 +122,8 @@ static PyObject *
_io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls, PyObject *args) _io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls, PyObject *args)
/*[clinic end generated code: output=5d985eb529472bc4 input=21b6961b5cba9496]*/ /*[clinic end generated code: output=5d985eb529472bc4 input=21b6961b5cba9496]*/
{ {
return _unsupported("write"); _PyIO_State *state = IO_STATE();
return _unsupported(state, "write");
} }
PyDoc_STRVAR(textiobase_encoding_doc, PyDoc_STRVAR(textiobase_encoding_doc,
@ -1381,7 +1383,8 @@ _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
/* Check if something is in the read buffer */ /* Check if something is in the read buffer */
if (self->decoded_chars != NULL) { if (self->decoded_chars != NULL) {
if (encoding != Py_None || errors != Py_None || newline_obj != 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"); "of stream after the first read");
return NULL; return NULL;
} }
@ -1648,8 +1651,9 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
CHECK_ATTACHED(self); CHECK_ATTACHED(self);
CHECK_CLOSED(self); CHECK_CLOSED(self);
if (self->encoder == NULL) if (self->encoder == NULL) {
return _unsupported("not writable"); return _unsupported(self->state, "not writable");
}
Py_INCREF(text); Py_INCREF(text);
@ -1830,7 +1834,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
*/ */
if (self->decoder == NULL) { if (self->decoder == NULL) {
_unsupported("not readable"); _unsupported(self->state, "not readable");
return -1; return -1;
} }
@ -1955,8 +1959,9 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
CHECK_ATTACHED(self); CHECK_ATTACHED(self);
CHECK_CLOSED(self); CHECK_CLOSED(self);
if (self->decoder == NULL) if (self->decoder == NULL) {
return _unsupported("not readable"); return _unsupported(self->state, "not readable");
}
if (_textiowrapper_writeflush(self) < 0) if (_textiowrapper_writeflush(self) < 0)
return NULL; return NULL;
@ -2487,7 +2492,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
Py_INCREF(cookieObj); Py_INCREF(cookieObj);
if (!self->seekable) { if (!self->seekable) {
_unsupported("underlying stream is not seekable"); _unsupported(self->state, "underlying stream is not seekable");
goto fail; goto fail;
} }
@ -2501,7 +2506,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail; goto fail;
if (cmp == 0) { if (cmp == 0) {
_unsupported("can't do nonzero cur-relative seeks"); _unsupported(self->state, "can't do nonzero cur-relative seeks");
goto fail; goto fail;
} }
@ -2521,7 +2526,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail; goto fail;
if (cmp == 0) { if (cmp == 0) {
_unsupported("can't do nonzero end-relative seeks"); _unsupported(self->state, "can't do nonzero end-relative seeks");
goto fail; goto fail;
} }
@ -2684,7 +2689,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
CHECK_CLOSED(self); CHECK_CLOSED(self);
if (!self->seekable) { if (!self->seekable) {
_unsupported("underlying stream is not seekable"); _unsupported(self->state, "underlying stream is not seekable");
goto fail; goto fail;
} }
if (!self->telling) { if (!self->telling) {

View file

@ -180,6 +180,8 @@ internal_close(winconsoleio *self)
/*[clinic input] /*[clinic input]
_io._WindowsConsoleIO.close _io._WindowsConsoleIO.close
cls: defining_class
/
Close the console object. Close the console object.
@ -188,13 +190,15 @@ close() may be called more than once without error.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io__WindowsConsoleIO_close_impl(winconsoleio *self) _io__WindowsConsoleIO_close_impl(winconsoleio *self, PyTypeObject *cls)
/*[clinic end generated code: output=27ef95b66c29057b input=68c4e5754f8136c2]*/ /*[clinic end generated code: output=e50c1808c063e1e2 input=161001bd2a649a4b]*/
{ {
PyObject *res; PyObject *res;
PyObject *exc; PyObject *exc;
int rc; int rc;
res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type,
_PyIO_State *state = get_io_state_by_cls(cls);
res = PyObject_CallMethodOneArg((PyObject*)state->PyRawIOBase_Type,
&_Py_ID(close), (PyObject*)self); &_Py_ID(close), (PyObject*)self);
if (!self->closefd) { if (!self->closefd) {
self->fd = -1; self->fd = -1;
@ -453,13 +457,10 @@ err_closed(void)
} }
static PyObject * static PyObject *
err_mode(const char *action) err_mode(_PyIO_State *state, const char *action)
{ {
_PyIO_State *state = IO_STATE(); return PyErr_Format(state->unsupported_operation,
if (state != NULL) "Console buffer does not support %s", action);
PyErr_Format(state->unsupported_operation,
"Console buffer does not support %s", action);
return NULL;
} }
/*[clinic input] /*[clinic input]
@ -636,14 +637,14 @@ error:
static Py_ssize_t static Py_ssize_t
readinto(winconsoleio *self, char *buf, Py_ssize_t len) readinto(_PyIO_State *state, winconsoleio *self, char *buf, Py_ssize_t len)
{ {
if (self->fd == -1) { if (self->fd == -1) {
err_closed(); err_closed();
return -1; return -1;
} }
if (!self->readable) { if (!self->readable) {
err_mode("reading"); err_mode(state, "reading");
return -1; return -1;
} }
if (len == 0) if (len == 0)
@ -733,6 +734,7 @@ readinto(winconsoleio *self, char *buf, Py_ssize_t len)
/*[clinic input] /*[clinic input]
_io._WindowsConsoleIO.readinto _io._WindowsConsoleIO.readinto
cls: defining_class
buffer: Py_buffer(accept={rwbuffer}) buffer: Py_buffer(accept={rwbuffer})
/ /
@ -740,10 +742,12 @@ Same as RawIOBase.readinto().
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer) _io__WindowsConsoleIO_readinto_impl(winconsoleio *self, PyTypeObject *cls,
/*[clinic end generated code: output=66d1bdfa3f20af39 input=4ed68da48a6baffe]*/ Py_buffer *buffer)
/*[clinic end generated code: output=96717c74f6204b79 input=4b0627c3b1645f78]*/
{ {
Py_ssize_t len = readinto(self, buffer->buf, buffer->len); _PyIO_State *state = get_io_state_by_cls(cls);
Py_ssize_t len = readinto(state, self, buffer->buf, buffer->len);
if (len < 0) if (len < 0)
return NULL; return NULL;
@ -897,6 +901,7 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
/*[clinic input] /*[clinic input]
_io._WindowsConsoleIO.read _io._WindowsConsoleIO.read
cls: defining_class
size: Py_ssize_t(accept={int, NoneType}) = -1 size: Py_ssize_t(accept={int, NoneType}) = -1
/ /
@ -908,16 +913,19 @@ Return an empty bytes object at EOF.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size) _io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
/*[clinic end generated code: output=57df68af9f4b22d0 input=8bc73bc15d0fa072]*/ Py_ssize_t size)
/*[clinic end generated code: output=7e569a586537c0ae input=a14570a5da273365]*/
{ {
PyObject *bytes; PyObject *bytes;
Py_ssize_t bytes_size; Py_ssize_t bytes_size;
if (self->fd == -1) if (self->fd == -1)
return err_closed(); return err_closed();
if (!self->readable) if (!self->readable) {
return err_mode("reading"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "reading");
}
if (size < 0) if (size < 0)
return _io__WindowsConsoleIO_readall_impl(self); return _io__WindowsConsoleIO_readall_impl(self);
@ -930,7 +938,9 @@ _io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size)
if (bytes == NULL) if (bytes == NULL)
return NULL; return NULL;
bytes_size = readinto(self, PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); _PyIO_State *state = get_io_state_by_cls(cls);
bytes_size = readinto(state, self, PyBytes_AS_STRING(bytes),
PyBytes_GET_SIZE(bytes));
if (bytes_size < 0) { if (bytes_size < 0) {
Py_CLEAR(bytes); Py_CLEAR(bytes);
return NULL; return NULL;
@ -948,6 +958,7 @@ _io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size)
/*[clinic input] /*[clinic input]
_io._WindowsConsoleIO.write _io._WindowsConsoleIO.write
cls: defining_class
b: Py_buffer b: Py_buffer
/ /
@ -958,8 +969,9 @@ The number of bytes actually written is returned.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
_io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b) _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
/*[clinic end generated code: output=775bdb16fbf9137b input=be35fb624f97c941]*/ Py_buffer *b)
/*[clinic end generated code: output=e8019f480243cb29 input=10ac37c19339dfbe]*/
{ {
BOOL res = TRUE; BOOL res = TRUE;
wchar_t *wbuf; wchar_t *wbuf;
@ -968,8 +980,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
if (self->fd == -1) if (self->fd == -1)
return err_closed(); return err_closed();
if (!self->writable) if (!self->writable) {
return err_mode("writing"); _PyIO_State *state = get_io_state_by_cls(cls);
return err_mode(state, "writing");
}
handle = _Py_get_osfhandle(self->fd); handle = _Py_get_osfhandle(self->fd);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)