aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Modules/_io
diff options
context:
space:
mode:
authorAlexSm <alex@ydb.tech>2024-03-05 10:40:59 +0100
committerGitHub <noreply@github.com>2024-03-05 12:40:59 +0300
commit1ac13c847b5358faba44dbb638a828e24369467b (patch)
tree07672b4dd3604ad3dee540a02c6494cb7d10dc3d /contrib/tools/python3/Modules/_io
parentffcca3e7f7958ddc6487b91d3df8c01054bd0638 (diff)
downloadydb-1ac13c847b5358faba44dbb638a828e24369467b.tar.gz
Library import 16 (#2433)
Co-authored-by: robot-piglet <robot-piglet@yandex-team.com> Co-authored-by: deshevoy <deshevoy@yandex-team.com> Co-authored-by: robot-contrib <robot-contrib@yandex-team.com> Co-authored-by: thegeorg <thegeorg@yandex-team.com> Co-authored-by: robot-ya-builder <robot-ya-builder@yandex-team.com> Co-authored-by: svidyuk <svidyuk@yandex-team.com> Co-authored-by: shadchin <shadchin@yandex-team.com> Co-authored-by: robot-ratatosk <robot-ratatosk@yandex-team.com> Co-authored-by: innokentii <innokentii@yandex-team.com> Co-authored-by: arkady-e1ppa <arkady-e1ppa@yandex-team.com> Co-authored-by: snermolaev <snermolaev@yandex-team.com> Co-authored-by: dimdim11 <dimdim11@yandex-team.com> Co-authored-by: kickbutt <kickbutt@yandex-team.com> Co-authored-by: abdullinsaid <abdullinsaid@yandex-team.com> Co-authored-by: korsunandrei <korsunandrei@yandex-team.com> Co-authored-by: petrk <petrk@yandex-team.com> Co-authored-by: miroslav2 <miroslav2@yandex-team.com> Co-authored-by: serjflint <serjflint@yandex-team.com> Co-authored-by: akhropov <akhropov@yandex-team.com> Co-authored-by: prettyboy <prettyboy@yandex-team.com> Co-authored-by: ilikepugs <ilikepugs@yandex-team.com> Co-authored-by: hiddenpath <hiddenpath@yandex-team.com> Co-authored-by: mikhnenko <mikhnenko@yandex-team.com> Co-authored-by: spreis <spreis@yandex-team.com> Co-authored-by: andreyshspb <andreyshspb@yandex-team.com> Co-authored-by: dimaandreev <dimaandreev@yandex-team.com> Co-authored-by: rashid <rashid@yandex-team.com> Co-authored-by: robot-ydb-importer <robot-ydb-importer@yandex-team.com> Co-authored-by: r-vetrov <r-vetrov@yandex-team.com> Co-authored-by: ypodlesov <ypodlesov@yandex-team.com> Co-authored-by: zaverden <zaverden@yandex-team.com> Co-authored-by: vpozdyayev <vpozdyayev@yandex-team.com> Co-authored-by: robot-cozmo <robot-cozmo@yandex-team.com> Co-authored-by: v-korovin <v-korovin@yandex-team.com> Co-authored-by: arikon <arikon@yandex-team.com> Co-authored-by: khoden <khoden@yandex-team.com> Co-authored-by: psydmm <psydmm@yandex-team.com> Co-authored-by: robot-javacom <robot-javacom@yandex-team.com> Co-authored-by: dtorilov <dtorilov@yandex-team.com> Co-authored-by: sennikovmv <sennikovmv@yandex-team.com> Co-authored-by: hcpp <hcpp@ydb.tech>
Diffstat (limited to 'contrib/tools/python3/Modules/_io')
-rw-r--r--contrib/tools/python3/Modules/_io/_iomodule.c739
-rw-r--r--contrib/tools/python3/Modules/_io/_iomodule.h197
-rw-r--r--contrib/tools/python3/Modules/_io/bufferedio.c2692
-rw-r--r--contrib/tools/python3/Modules/_io/bytesio.c1133
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/_iomodule.c.h410
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/bufferedio.c.h1101
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/bytesio.c.h541
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/fileio.c.h539
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/iobase.c.h444
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/stringio.c.h370
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/textio.c.h985
-rw-r--r--contrib/tools/python3/Modules/_io/clinic/winconsoleio.c.h468
-rw-r--r--contrib/tools/python3/Modules/_io/fileio.c1230
-rw-r--r--contrib/tools/python3/Modules/_io/iobase.c1061
-rw-r--r--contrib/tools/python3/Modules/_io/stringio.c1037
-rw-r--r--contrib/tools/python3/Modules/_io/textio.c3352
-rw-r--r--contrib/tools/python3/Modules/_io/winconsoleio.c1179
17 files changed, 17478 insertions, 0 deletions
diff --git a/contrib/tools/python3/Modules/_io/_iomodule.c b/contrib/tools/python3/Modules/_io/_iomodule.c
new file mode 100644
index 0000000000..7b06c1bee5
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/_iomodule.c
@@ -0,0 +1,739 @@
+/*
+ An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
+
+ Classes defined here: UnsupportedOperation, BlockingIOError.
+ Functions defined here: open().
+
+ Mostly written by Amaury Forgeot d'Arc
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "_iomodule.h"
+#include "pycore_pystate.h" // _PyInterpreterState_GET()
+#include "pycore_initconfig.h" // _PyStatus_OK()
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif /* HAVE_SYS_TYPES_H */
+
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif /* HAVE_SYS_STAT_H */
+
+#ifdef MS_WINDOWS
+#include <windows.h>
+#endif
+
+PyDoc_STRVAR(module_doc,
+"The io module provides the Python interfaces to stream handling. The\n"
+"builtin open function is defined in this module.\n"
+"\n"
+"At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
+"defines the basic interface to a stream. Note, however, that there is no\n"
+"separation between reading and writing to streams; implementations are\n"
+"allowed to raise an OSError if they do not support a given operation.\n"
+"\n"
+"Extending IOBase is RawIOBase which deals simply with the reading and\n"
+"writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
+"an interface to OS files.\n"
+"\n"
+"BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
+"subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
+"streams that are readable, writable, and both respectively.\n"
+"BufferedRandom provides a buffered interface to random access\n"
+"streams. BytesIO is a simple stream of in-memory bytes.\n"
+"\n"
+"Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
+"of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
+"interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
+"is an in-memory stream for text.\n"
+"\n"
+"Argument names are not part of the specification, and only the arguments\n"
+"of open() are intended to be used as keyword arguments.\n"
+"\n"
+"data:\n"
+"\n"
+"DEFAULT_BUFFER_SIZE\n"
+"\n"
+" An int containing the default buffer size used by the module's buffered\n"
+" I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
+" possible.\n"
+ );
+
+
+/*
+ * The main open() function
+ */
+/*[clinic input]
+module _io
+
+_io.open
+ file: object
+ mode: str = "r"
+ buffering: int = -1
+ encoding: str(accept={str, NoneType}) = None
+ errors: str(accept={str, NoneType}) = None
+ newline: str(accept={str, NoneType}) = None
+ closefd: bool = True
+ opener: object = None
+
+Open file and return a stream. Raise OSError upon failure.
+
+file is either a text or byte string giving the name (and the path
+if the file isn't in the current working directory) of the file to
+be opened or an integer file descriptor of the file to be
+wrapped. (If a file descriptor is given, it is closed when the
+returned I/O object is closed, unless closefd is set to False.)
+
+mode is an optional string that specifies the mode in which the file
+is opened. It defaults to 'r' which means open for reading in text
+mode. Other common values are 'w' for writing (truncating the file if
+it already exists), 'x' for creating and writing to a new file, and
+'a' for appending (which on some Unix systems, means that all writes
+append to the end of the file regardless of the current seek position).
+In text mode, if encoding is not specified the encoding used is platform
+dependent: locale.getencoding() is called to get the current locale encoding.
+(For reading and writing raw bytes use binary mode and leave encoding
+unspecified.) The available modes are:
+
+========= ===============================================================
+Character Meaning
+--------- ---------------------------------------------------------------
+'r' open for reading (default)
+'w' open for writing, truncating the file first
+'x' create a new file and open it for writing
+'a' open for writing, appending to the end of the file if it exists
+'b' binary mode
+'t' text mode (default)
+'+' open a disk file for updating (reading and writing)
+========= ===============================================================
+
+The default mode is 'rt' (open for reading text). For binary random
+access, the mode 'w+b' opens and truncates the file to 0 bytes, while
+'r+b' opens the file without truncation. The 'x' mode implies 'w' and
+raises an `FileExistsError` if the file already exists.
+
+Python distinguishes between files opened in binary and text modes,
+even when the underlying operating system doesn't. Files opened in
+binary mode (appending 'b' to the mode argument) return contents as
+bytes objects without any decoding. In text mode (the default, or when
+'t' is appended to the mode argument), the contents of the file are
+returned as strings, the bytes having been first decoded using a
+platform-dependent encoding or using the specified encoding if given.
+
+buffering is an optional integer used to set the buffering policy.
+Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
+line buffering (only usable in text mode), and an integer > 1 to indicate
+the size of a fixed-size chunk buffer. When no buffering argument is
+given, the default buffering policy works as follows:
+
+* Binary files are buffered in fixed-size chunks; the size of the buffer
+ is chosen using a heuristic trying to determine the underlying device's
+ "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
+ On many systems, the buffer will typically be 4096 or 8192 bytes long.
+
+* "Interactive" text files (files for which isatty() returns True)
+ use line buffering. Other text files use the policy described above
+ for binary files.
+
+encoding is the name of the encoding used to decode or encode the
+file. This should only be used in text mode. The default encoding is
+platform dependent, but any encoding supported by Python can be
+passed. See the codecs module for the list of supported encodings.
+
+errors is an optional string that specifies how encoding errors are to
+be handled---this argument should not be used in binary mode. Pass
+'strict' to raise a ValueError exception if there is an encoding error
+(the default of None has the same effect), or pass 'ignore' to ignore
+errors. (Note that ignoring encoding errors can lead to data loss.)
+See the documentation for codecs.register or run 'help(codecs.Codec)'
+for a list of the permitted encoding error strings.
+
+newline controls how universal newlines works (it only applies to text
+mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
+follows:
+
+* On input, if newline is None, universal newlines mode is
+ enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
+ these are translated into '\n' before being returned to the
+ caller. If it is '', universal newline mode is enabled, but line
+ endings are returned to the caller untranslated. If it has any of
+ the other legal values, input lines are only terminated by the given
+ string, and the line ending is returned to the caller untranslated.
+
+* On output, if newline is None, any '\n' characters written are
+ translated to the system default line separator, os.linesep. If
+ newline is '' or '\n', no translation takes place. If newline is any
+ of the other legal values, any '\n' characters written are translated
+ to the given string.
+
+If closefd is False, the underlying file descriptor will be kept open
+when the file is closed. This does not work when a file name is given
+and must be True in that case.
+
+A custom opener can be used by passing a callable as *opener*. The
+underlying file descriptor for the file object is then obtained by
+calling *opener* with (*file*, *flags*). *opener* must return an open
+file descriptor (passing os.open as *opener* results in functionality
+similar to passing None).
+
+open() returns a file object whose type depends on the mode, and
+through which the standard file operations such as reading and writing
+are performed. When open() is used to open a file in a text mode ('w',
+'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
+a file in a binary mode, the returned class varies: in read binary
+mode, it returns a BufferedReader; in write binary and append binary
+modes, it returns a BufferedWriter, and in read/write mode, it returns
+a BufferedRandom.
+
+It is also possible to use a string or bytearray as a file for both
+reading and writing. For strings StringIO can be used like a file
+opened in a text mode, and for bytes a BytesIO can be used like a file
+opened in a binary mode.
+[clinic start generated code]*/
+
+static PyObject *
+_io_open_impl(PyObject *module, PyObject *file, const char *mode,
+ int buffering, const char *encoding, const char *errors,
+ const char *newline, int closefd, PyObject *opener)
+/*[clinic end generated code: output=aefafc4ce2b46dc0 input=cd034e7cdfbf4e78]*/
+{
+ unsigned i;
+
+ int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0;
+ int text = 0, binary = 0;
+
+ char rawmode[6], *m;
+ int line_buffering, is_number, isatty = 0;
+
+ PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;
+
+ is_number = PyNumber_Check(file);
+
+ if (is_number) {
+ path_or_fd = Py_NewRef(file);
+ } else {
+ path_or_fd = PyOS_FSPath(file);
+ if (path_or_fd == NULL) {
+ return NULL;
+ }
+ }
+
+ if (!is_number &&
+ !PyUnicode_Check(path_or_fd) &&
+ !PyBytes_Check(path_or_fd)) {
+ PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
+ goto error;
+ }
+
+ /* Decode mode */
+ for (i = 0; i < strlen(mode); i++) {
+ char c = mode[i];
+
+ switch (c) {
+ case 'x':
+ creating = 1;
+ break;
+ case 'r':
+ reading = 1;
+ break;
+ case 'w':
+ writing = 1;
+ break;
+ case 'a':
+ appending = 1;
+ break;
+ case '+':
+ updating = 1;
+ break;
+ case 't':
+ text = 1;
+ break;
+ case 'b':
+ binary = 1;
+ break;
+ default:
+ goto invalid_mode;
+ }
+
+ /* c must not be duplicated */
+ if (strchr(mode+i+1, c)) {
+ invalid_mode:
+ PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
+ goto error;
+ }
+
+ }
+
+ m = rawmode;
+ if (creating) *(m++) = 'x';
+ if (reading) *(m++) = 'r';
+ if (writing) *(m++) = 'w';
+ if (appending) *(m++) = 'a';
+ if (updating) *(m++) = '+';
+ *m = '\0';
+
+ /* Parameters validation */
+ if (text && binary) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't have text and binary mode at once");
+ goto error;
+ }
+
+ if (creating + reading + writing + appending > 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "must have exactly one of create/read/write/append mode");
+ goto error;
+ }
+
+ if (binary && encoding != NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "binary mode doesn't take an encoding argument");
+ goto error;
+ }
+
+ if (binary && errors != NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "binary mode doesn't take an errors argument");
+ goto error;
+ }
+
+ if (binary && newline != NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "binary mode doesn't take a newline argument");
+ goto error;
+ }
+
+ if (binary && buffering == 1) {
+ if (PyErr_WarnEx(PyExc_RuntimeWarning,
+ "line buffering (buffering=1) isn't supported in "
+ "binary mode, the default buffer size will be used",
+ 1) < 0) {
+ goto error;
+ }
+ }
+
+ /* Create the Raw file stream */
+ _PyIO_State *state = get_io_state(module);
+ {
+ PyObject *RawIO_class = (PyObject *)state->PyFileIO_Type;
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+ const PyConfig *config = _Py_GetConfig();
+ if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
+ RawIO_class = (PyObject *)state->PyWindowsConsoleIO_Type;
+ encoding = "utf-8";
+ }
+#endif
+ raw = PyObject_CallFunction(RawIO_class, "OsOO",
+ path_or_fd, rawmode,
+ closefd ? Py_True : Py_False,
+ opener);
+ }
+
+ if (raw == NULL)
+ goto error;
+ result = raw;
+
+ Py_SETREF(path_or_fd, NULL);
+
+ modeobj = PyUnicode_FromString(mode);
+ if (modeobj == NULL)
+ goto error;
+
+ /* buffering */
+ if (buffering < 0) {
+ PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
+ if (res == NULL)
+ goto error;
+ isatty = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (isatty < 0)
+ goto error;
+ }
+
+ if (buffering == 1 || isatty) {
+ buffering = -1;
+ line_buffering = 1;
+ }
+ else
+ line_buffering = 0;
+
+ if (buffering < 0) {
+ PyObject *blksize_obj;
+ blksize_obj = PyObject_GetAttr(raw, &_Py_ID(_blksize));
+ if (blksize_obj == NULL)
+ goto error;
+ buffering = PyLong_AsLong(blksize_obj);
+ Py_DECREF(blksize_obj);
+ if (buffering == -1 && PyErr_Occurred())
+ goto error;
+ }
+ if (buffering < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid buffering size");
+ goto error;
+ }
+
+ /* if not buffering, returns the raw file object */
+ if (buffering == 0) {
+ if (!binary) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't have unbuffered text I/O");
+ goto error;
+ }
+
+ Py_DECREF(modeobj);
+ return result;
+ }
+
+ /* wraps into a buffered file */
+ {
+ PyObject *Buffered_class;
+
+ if (updating) {
+ Buffered_class = (PyObject *)state->PyBufferedRandom_Type;
+ }
+ else if (creating || writing || appending) {
+ Buffered_class = (PyObject *)state->PyBufferedWriter_Type;
+ }
+ else if (reading) {
+ Buffered_class = (PyObject *)state->PyBufferedReader_Type;
+ }
+ else {
+ PyErr_Format(PyExc_ValueError,
+ "unknown mode: '%s'", mode);
+ goto error;
+ }
+
+ buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
+ }
+ if (buffer == NULL)
+ goto error;
+ result = buffer;
+ Py_DECREF(raw);
+
+
+ /* if binary, returns the buffered file */
+ if (binary) {
+ Py_DECREF(modeobj);
+ return result;
+ }
+
+ /* wraps into a TextIOWrapper */
+ wrapper = PyObject_CallFunction((PyObject *)state->PyTextIOWrapper_Type,
+ "OsssO",
+ buffer,
+ encoding, errors, newline,
+ line_buffering ? Py_True : Py_False);
+ if (wrapper == NULL)
+ goto error;
+ result = wrapper;
+ Py_DECREF(buffer);
+
+ if (PyObject_SetAttr(wrapper, &_Py_ID(mode), modeobj) < 0)
+ goto error;
+ Py_DECREF(modeobj);
+ return result;
+
+ error:
+ if (result != NULL) {
+ PyObject *exc = PyErr_GetRaisedException();
+ PyObject *close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close));
+ _PyErr_ChainExceptions1(exc);
+ Py_XDECREF(close_result);
+ Py_DECREF(result);
+ }
+ Py_XDECREF(path_or_fd);
+ Py_XDECREF(modeobj);
+ return NULL;
+}
+
+
+/*[clinic input]
+_io.text_encoding
+ encoding: object
+ stacklevel: int = 2
+ /
+
+A helper function to choose the text encoding.
+
+When encoding is not None, this function returns it.
+Otherwise, this function returns the default text encoding
+(i.e. "locale" or "utf-8" depends on UTF-8 mode).
+
+This function emits an EncodingWarning if encoding is None and
+sys.flags.warn_default_encoding is true.
+
+This can be used in APIs with an encoding=None parameter.
+However, please consider using encoding="utf-8" for new APIs.
+[clinic start generated code]*/
+
+static PyObject *
+_io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
+/*[clinic end generated code: output=91b2cfea6934cc0c input=4999aa8b3d90f3d4]*/
+{
+ if (encoding == NULL || encoding == Py_None) {
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
+ if (PyErr_WarnEx(PyExc_EncodingWarning,
+ "'encoding' argument not specified", stacklevel)) {
+ return NULL;
+ }
+ }
+ const PyPreConfig *preconfig = &_PyRuntime.preconfig;
+ if (preconfig->utf8_mode) {
+ _Py_DECLARE_STR(utf_8, "utf-8");
+ encoding = &_Py_STR(utf_8);
+ }
+ else {
+ encoding = &_Py_ID(locale);
+ }
+ }
+ return Py_NewRef(encoding);
+}
+
+
+/*[clinic input]
+_io.open_code
+
+ path : unicode
+
+Opens the provided file with the intent to import the contents.
+
+This may perform extra validation beyond open(), but is otherwise interchangeable
+with calling open(path, 'rb').
+
+[clinic start generated code]*/
+
+static PyObject *
+_io_open_code_impl(PyObject *module, PyObject *path)
+/*[clinic end generated code: output=2fe4ecbd6f3d6844 input=f5c18e23f4b2ed9f]*/
+{
+ return PyFile_OpenCodeObject(path);
+}
+
+/*
+ * Private helpers for the io module.
+ */
+
+Py_off_t
+PyNumber_AsOff_t(PyObject *item, PyObject *err)
+{
+ Py_off_t result;
+ PyObject *runerr;
+ PyObject *value = _PyNumber_Index(item);
+ if (value == NULL)
+ return -1;
+
+ /* We're done if PyLong_AsSsize_t() returns without error. */
+ result = PyLong_AsOff_t(value);
+ if (result != -1 || !(runerr = PyErr_Occurred()))
+ goto finish;
+
+ /* Error handling code -- only manage OverflowError differently */
+ if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
+ goto finish;
+
+ PyErr_Clear();
+ /* If no error-handling desired then the default clipping
+ is sufficient.
+ */
+ if (!err) {
+ assert(PyLong_Check(value));
+ /* Whether or not it is less than or equal to
+ zero is determined by the sign of ob_size
+ */
+ if (_PyLong_Sign(value) < 0)
+ result = PY_OFF_T_MIN;
+ else
+ result = PY_OFF_T_MAX;
+ }
+ else {
+ /* Otherwise replace the error with caller's error object. */
+ PyErr_Format(err,
+ "cannot fit '%.200s' into an offset-sized integer",
+ Py_TYPE(item)->tp_name);
+ }
+
+ finish:
+ Py_DECREF(value);
+ return result;
+}
+
+static int
+iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
+ _PyIO_State *state = get_io_state(mod);
+ Py_VISIT(state->unsupported_operation);
+
+ Py_VISIT(state->PyIOBase_Type);
+ Py_VISIT(state->PyIncrementalNewlineDecoder_Type);
+ Py_VISIT(state->PyRawIOBase_Type);
+ Py_VISIT(state->PyBufferedIOBase_Type);
+ Py_VISIT(state->PyBufferedRWPair_Type);
+ Py_VISIT(state->PyBufferedRandom_Type);
+ Py_VISIT(state->PyBufferedReader_Type);
+ Py_VISIT(state->PyBufferedWriter_Type);
+ Py_VISIT(state->PyBytesIOBuffer_Type);
+ Py_VISIT(state->PyBytesIO_Type);
+ Py_VISIT(state->PyFileIO_Type);
+ Py_VISIT(state->PyStringIO_Type);
+ Py_VISIT(state->PyTextIOBase_Type);
+ Py_VISIT(state->PyTextIOWrapper_Type);
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+ Py_VISIT(state->PyWindowsConsoleIO_Type);
+#endif
+ return 0;
+}
+
+
+static int
+iomodule_clear(PyObject *mod) {
+ _PyIO_State *state = get_io_state(mod);
+ Py_CLEAR(state->unsupported_operation);
+
+ Py_CLEAR(state->PyIOBase_Type);
+ Py_CLEAR(state->PyIncrementalNewlineDecoder_Type);
+ Py_CLEAR(state->PyRawIOBase_Type);
+ Py_CLEAR(state->PyBufferedIOBase_Type);
+ Py_CLEAR(state->PyBufferedRWPair_Type);
+ Py_CLEAR(state->PyBufferedRandom_Type);
+ Py_CLEAR(state->PyBufferedReader_Type);
+ Py_CLEAR(state->PyBufferedWriter_Type);
+ Py_CLEAR(state->PyBytesIOBuffer_Type);
+ Py_CLEAR(state->PyBytesIO_Type);
+ Py_CLEAR(state->PyFileIO_Type);
+ Py_CLEAR(state->PyStringIO_Type);
+ Py_CLEAR(state->PyTextIOBase_Type);
+ Py_CLEAR(state->PyTextIOWrapper_Type);
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+ Py_CLEAR(state->PyWindowsConsoleIO_Type);
+#endif
+ return 0;
+}
+
+static void
+iomodule_free(void *mod)
+{
+ (void)iomodule_clear((PyObject *)mod);
+}
+
+
+/*
+ * Module definition
+ */
+
+#define clinic_state() (get_io_state(module))
+#include "clinic/_iomodule.c.h"
+#undef clinic_state
+
+static PyMethodDef module_methods[] = {
+ _IO_OPEN_METHODDEF
+ _IO_TEXT_ENCODING_METHODDEF
+ _IO_OPEN_CODE_METHODDEF
+ {NULL, NULL}
+};
+
+#define ADD_TYPE(module, type, spec, base) \
+do { \
+ type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, \
+ (PyObject *)base); \
+ if (type == NULL) { \
+ return -1; \
+ } \
+ if (PyModule_AddType(module, type) < 0) { \
+ return -1; \
+ } \
+} while (0)
+
+static int
+iomodule_exec(PyObject *m)
+{
+ _PyIO_State *state = get_io_state(m);
+
+ /* DEFAULT_BUFFER_SIZE */
+ if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
+ return -1;
+
+ /* UnsupportedOperation inherits from ValueError and OSError */
+ state->unsupported_operation = PyObject_CallFunction(
+ (PyObject *)&PyType_Type, "s(OO){}",
+ "UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
+ if (state->unsupported_operation == NULL)
+ return -1;
+ if (PyModule_AddObjectRef(m, "UnsupportedOperation",
+ state->unsupported_operation) < 0)
+ {
+ return -1;
+ }
+
+ /* BlockingIOError, for compatibility */
+ if (PyModule_AddObjectRef(m, "BlockingIOError",
+ (PyObject *) PyExc_BlockingIOError) < 0) {
+ return -1;
+ }
+
+ // Base classes
+ ADD_TYPE(m, state->PyIncrementalNewlineDecoder_Type, &nldecoder_spec, NULL);
+ ADD_TYPE(m, state->PyBytesIOBuffer_Type, &bytesiobuf_spec, NULL);
+ ADD_TYPE(m, state->PyIOBase_Type, &iobase_spec, NULL);
+
+ // PyIOBase_Type subclasses
+ ADD_TYPE(m, state->PyTextIOBase_Type, &textiobase_spec,
+ state->PyIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedIOBase_Type, &bufferediobase_spec,
+ state->PyIOBase_Type);
+ ADD_TYPE(m, state->PyRawIOBase_Type, &rawiobase_spec,
+ state->PyIOBase_Type);
+
+ // PyBufferedIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyBytesIO_Type, &bytesio_spec, state->PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedWriter_Type, &bufferedwriter_spec,
+ state->PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedReader_Type, &bufferedreader_spec,
+ state->PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedRWPair_Type, &bufferedrwpair_spec,
+ state->PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedRandom_Type, &bufferedrandom_spec,
+ state->PyBufferedIOBase_Type);
+
+ // PyRawIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, state->PyRawIOBase_Type);
+
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+ ADD_TYPE(m, state->PyWindowsConsoleIO_Type, &winconsoleio_spec,
+ state->PyRawIOBase_Type);
+#endif
+
+ // PyTextIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyStringIO_Type, &stringio_spec, state->PyTextIOBase_Type);
+ ADD_TYPE(m, state->PyTextIOWrapper_Type, &textiowrapper_spec,
+ state->PyTextIOBase_Type);
+
+#undef ADD_TYPE
+ return 0;
+}
+
+static struct PyModuleDef_Slot iomodule_slots[] = {
+ {Py_mod_exec, iomodule_exec},
+ {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
+ {0, NULL},
+};
+
+struct PyModuleDef _PyIO_Module = {
+ .m_base = PyModuleDef_HEAD_INIT,
+ .m_name = "io",
+ .m_doc = module_doc,
+ .m_size = sizeof(_PyIO_State),
+ .m_methods = module_methods,
+ .m_traverse = iomodule_traverse,
+ .m_clear = iomodule_clear,
+ .m_free = iomodule_free,
+ .m_slots = iomodule_slots,
+};
+
+PyMODINIT_FUNC
+PyInit__io(void)
+{
+ return PyModuleDef_Init(&_PyIO_Module);
+}
diff --git a/contrib/tools/python3/Modules/_io/_iomodule.h b/contrib/tools/python3/Modules/_io/_iomodule.h
new file mode 100644
index 0000000000..afd638a120
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/_iomodule.h
@@ -0,0 +1,197 @@
+/*
+ * Declarations shared between the different parts of the io module
+ */
+
+#include "exports.h"
+
+#include "pycore_moduleobject.h" // _PyModule_GetState()
+#include "pycore_typeobject.h" // _PyType_GetModuleState()
+#include "structmember.h"
+
+/* Type specs */
+extern PyType_Spec bufferediobase_spec;
+extern PyType_Spec bufferedrandom_spec;
+extern PyType_Spec bufferedreader_spec;
+extern PyType_Spec bufferedrwpair_spec;
+extern PyType_Spec bufferedwriter_spec;
+extern PyType_Spec bytesio_spec;
+extern PyType_Spec bytesiobuf_spec;
+extern PyType_Spec fileio_spec;
+extern PyType_Spec iobase_spec;
+extern PyType_Spec nldecoder_spec;
+extern PyType_Spec rawiobase_spec;
+extern PyType_Spec stringio_spec;
+extern PyType_Spec textiobase_spec;
+extern PyType_Spec textiowrapper_spec;
+
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+extern PyType_Spec winconsoleio_spec;
+#endif
+
+/* These functions are used as METH_NOARGS methods, are normally called
+ * with args=NULL, and return a new reference.
+ * BUT when args=Py_True is passed, they return a borrowed reference.
+ */
+typedef struct _io_state _PyIO_State; // Forward decl.
+extern PyObject* _PyIOBase_check_readable(_PyIO_State *state,
+ PyObject *self, PyObject *args);
+extern PyObject* _PyIOBase_check_writable(_PyIO_State *state,
+ PyObject *self, PyObject *args);
+extern PyObject* _PyIOBase_check_seekable(_PyIO_State *state,
+ PyObject *self, PyObject *args);
+extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args);
+
+/* Helper for finalization.
+ This function will revive an object ready to be deallocated and try to
+ close() it. It returns 0 if the object can be destroyed, or -1 if it
+ is alive again. */
+extern int _PyIOBase_finalize(PyObject *self);
+
+/* Returns true if the given FileIO object is closed.
+ Doesn't check the argument type, so be careful! */
+extern int _PyFileIO_closed(PyObject *self);
+
+/* Shortcut to the core of the IncrementalNewlineDecoder.decode method */
+extern PyObject *_PyIncrementalNewlineDecoder_decode(
+ PyObject *self, PyObject *input, int final);
+
+/* Finds the first line ending between `start` and `end`.
+ If found, returns the index after the line ending and doesn't touch
+ `*consumed`.
+ If not found, returns -1 and sets `*consumed` to the number of characters
+ which can be safely put aside until another search.
+
+ NOTE: for performance reasons, `end` must point to a NUL character ('\0').
+ Otherwise, the function will scan further and return garbage.
+
+ There are three modes, in order of priority:
+ * translated: Only find \n (assume newlines already translated)
+ * universal: Use universal newlines algorithm
+ * Otherwise, the line ending is specified by readnl, a str object */
+extern Py_ssize_t _PyIO_find_line_ending(
+ int translated, int universal, PyObject *readnl,
+ int kind, const char *start, const char *end, Py_ssize_t *consumed);
+
+/* Return 1 if an OSError with errno == EINTR is set (and then
+ clears the error indicator), 0 otherwise.
+ Should only be called when PyErr_Occurred() is true.
+*/
+extern int _PyIO_trap_eintr(void);
+
+#define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */
+
+/*
+ * Offset type for positioning.
+ */
+
+/* Printing a variable of type off_t (with e.g., PyUnicode_FromFormat)
+ correctly and without producing compiler warnings is surprisingly painful.
+ We identify an integer type whose size matches off_t and then: (1) cast the
+ off_t to that integer type and (2) use the appropriate conversion
+ specification. The cast is necessary: gcc complains about formatting a
+ long with "%lld" even when both long and long long have the same
+ precision. */
+
+#ifdef MS_WINDOWS
+
+/* Windows uses long long for offsets */
+typedef long long Py_off_t;
+# define PyLong_AsOff_t PyLong_AsLongLong
+# define PyLong_FromOff_t PyLong_FromLongLong
+# define PY_OFF_T_MAX LLONG_MAX
+# define PY_OFF_T_MIN LLONG_MIN
+# define PY_OFF_T_COMPAT long long /* type compatible with off_t */
+# define PY_PRIdOFF "lld" /* format to use for that type */
+
+#else
+
+/* Other platforms use off_t */
+typedef off_t Py_off_t;
+#if (SIZEOF_OFF_T == SIZEOF_SIZE_T)
+# define PyLong_AsOff_t PyLong_AsSsize_t
+# define PyLong_FromOff_t PyLong_FromSsize_t
+# define PY_OFF_T_MAX PY_SSIZE_T_MAX
+# define PY_OFF_T_MIN PY_SSIZE_T_MIN
+# define PY_OFF_T_COMPAT Py_ssize_t
+# define PY_PRIdOFF "zd"
+#elif (SIZEOF_OFF_T == SIZEOF_LONG_LONG)
+# define PyLong_AsOff_t PyLong_AsLongLong
+# define PyLong_FromOff_t PyLong_FromLongLong
+# define PY_OFF_T_MAX LLONG_MAX
+# define PY_OFF_T_MIN LLONG_MIN
+# define PY_OFF_T_COMPAT long long
+# define PY_PRIdOFF "lld"
+#elif (SIZEOF_OFF_T == SIZEOF_LONG)
+# define PyLong_AsOff_t PyLong_AsLong
+# define PyLong_FromOff_t PyLong_FromLong
+# define PY_OFF_T_MAX LONG_MAX
+# define PY_OFF_T_MIN LONG_MIN
+# define PY_OFF_T_COMPAT long
+# define PY_PRIdOFF "ld"
+#else
+# error off_t does not match either size_t, long, or long long!
+#endif
+
+#endif
+
+extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err);
+
+/* Implementation details */
+
+/* IO module structure */
+
+extern PyModuleDef _PyIO_Module;
+
+struct _io_state {
+ int initialized;
+ PyObject *unsupported_operation;
+
+ /* Types */
+ PyTypeObject *PyIOBase_Type;
+ PyTypeObject *PyIncrementalNewlineDecoder_Type;
+ PyTypeObject *PyRawIOBase_Type;
+ PyTypeObject *PyBufferedIOBase_Type;
+ PyTypeObject *PyBufferedRWPair_Type;
+ PyTypeObject *PyBufferedRandom_Type;
+ PyTypeObject *PyBufferedReader_Type;
+ PyTypeObject *PyBufferedWriter_Type;
+ PyTypeObject *PyBytesIOBuffer_Type;
+ PyTypeObject *PyBytesIO_Type;
+ PyTypeObject *PyFileIO_Type;
+ PyTypeObject *PyStringIO_Type;
+ PyTypeObject *PyTextIOBase_Type;
+ PyTypeObject *PyTextIOWrapper_Type;
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+ PyTypeObject *PyWindowsConsoleIO_Type;
+#endif
+};
+
+static inline _PyIO_State *
+get_io_state(PyObject *module)
+{
+ void *state = _PyModule_GetState(module);
+ assert(state != NULL);
+ return (_PyIO_State *)state;
+}
+
+static inline _PyIO_State *
+get_io_state_by_cls(PyTypeObject *cls)
+{
+ void *state = _PyType_GetModuleState(cls);
+ assert(state != NULL);
+ return (_PyIO_State *)state;
+}
+
+static inline _PyIO_State *
+find_io_state_by_def(PyTypeObject *type)
+{
+ PyObject *mod = PyType_GetModuleByDef(type, &_PyIO_Module);
+ assert(mod != NULL);
+ return get_io_state(mod);
+}
+
+extern PyObject *_PyIOBase_cannot_pickle(PyObject *self, PyObject *args);
+
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+extern char _PyIO_get_console_type(PyObject *);
+#endif
diff --git a/contrib/tools/python3/Modules/_io/bufferedio.c b/contrib/tools/python3/Modules/_io/bufferedio.c
new file mode 100644
index 0000000000..f30d54a5e1
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/bufferedio.c
@@ -0,0 +1,2692 @@
+/*
+ An implementation of Buffered I/O as defined by PEP 3116 - "New I/O"
+
+ Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter,
+ BufferedRandom.
+
+ Written by Amaury Forgeot d'Arc and Antoine Pitrou
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "pycore_call.h" // _PyObject_CallNoArgs()
+#include "pycore_object.h"
+#include "structmember.h" // PyMemberDef
+#include "_iomodule.h"
+
+/*[clinic input]
+module _io
+class _io._BufferedIOBase "PyObject *" "clinic_state()->PyBufferedIOBase_Type"
+class _io._Buffered "buffered *" "clinic_state()->PyBufferedIOBase_Type"
+class _io.BufferedReader "buffered *" "clinic_state()->PyBufferedReader_Type"
+class _io.BufferedWriter "buffered *" "clinic_state()->PyBufferedWriter_Type"
+class _io.BufferedRWPair "rwpair *" "clinic_state()->PyBufferedRWPair_Type"
+class _io.BufferedRandom "buffered *" "clinic_state()->PyBufferedRandom_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3b3ef9cbbbad4590]*/
+
+/*
+ * BufferedIOBase class, inherits from IOBase.
+ */
+PyDoc_STRVAR(bufferediobase_doc,
+ "Base class for buffered IO objects.\n"
+ "\n"
+ "The main difference with RawIOBase is that the read() method\n"
+ "supports omitting the size argument, and does not have a default\n"
+ "implementation that defers to readinto().\n"
+ "\n"
+ "In addition, read(), readinto() and write() may raise\n"
+ "BlockingIOError if the underlying raw stream is in non-blocking\n"
+ "mode and not ready; unlike their raw counterparts, they will never\n"
+ "return None.\n"
+ "\n"
+ "A typical implementation should not inherit from a RawIOBase\n"
+ "implementation, but wrap one.\n"
+ );
+
+static PyObject *
+_bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readinto1)
+{
+ Py_ssize_t len;
+ PyObject *data;
+
+ PyObject *attr = readinto1
+ ? &_Py_ID(read1)
+ : &_Py_ID(read);
+ data = _PyObject_CallMethod(self, attr, "n", buffer->len);
+ if (data == NULL)
+ return NULL;
+
+ if (!PyBytes_Check(data)) {
+ Py_DECREF(data);
+ PyErr_SetString(PyExc_TypeError, "read() should return bytes");
+ return NULL;
+ }
+
+ len = PyBytes_GET_SIZE(data);
+ if (len > buffer->len) {
+ PyErr_Format(PyExc_ValueError,
+ "read() returned too much data: "
+ "%zd bytes requested, %zd returned",
+ buffer->len, len);
+ Py_DECREF(data);
+ return NULL;
+ }
+ memcpy(buffer->buf, PyBytes_AS_STRING(data), len);
+
+ Py_DECREF(data);
+
+ return PyLong_FromSsize_t(len);
+}
+
+/*[clinic input]
+_io._BufferedIOBase.readinto
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_readinto_impl(PyObject *self, Py_buffer *buffer)
+/*[clinic end generated code: output=8c8cda6684af8038 input=00a6b9a38f29830a]*/
+{
+ return _bufferediobase_readinto_generic(self, buffer, 0);
+}
+
+/*[clinic input]
+_io._BufferedIOBase.readinto1
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_readinto1_impl(PyObject *self, Py_buffer *buffer)
+/*[clinic end generated code: output=358623e4fd2b69d3 input=ebad75b4aadfb9be]*/
+{
+ return _bufferediobase_readinto_generic(self, buffer, 1);
+}
+
+static PyObject *
+bufferediobase_unsupported(_PyIO_State *state, const char *message)
+{
+ PyErr_SetString(state->unsupported_operation, message);
+ return NULL;
+}
+
+/*[clinic input]
+_io._BufferedIOBase.detach
+
+ cls: defining_class
+ /
+
+Disconnect this buffer from its underlying raw stream and return it.
+
+After the raw stream has been detached, the buffer is in an unusable
+state.
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
+/*[clinic end generated code: output=b87b135d67cd4448 input=0b61a7b4357c1ea7]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return bufferediobase_unsupported(state, "detach");
+}
+
+/*[clinic input]
+_io._BufferedIOBase.read
+
+ cls: defining_class
+ size: int(unused=True) = -1
+ /
+
+Read and return up to n bytes.
+
+If the size argument is omitted, None, or negative, read and
+return all data until EOF.
+
+If the size argument is positive, and the underlying raw stream is
+not 'interactive', multiple raw reads may be issued to satisfy
+the byte count (unless EOF is reached first).
+However, for interactive raw streams (as well as sockets and pipes),
+at most one raw read will be issued, and a short result does not
+imply that EOF is imminent.
+
+Return an empty bytes object on EOF.
+
+Return None if the underlying raw stream was open in non-blocking
+mode and no data is available at the moment.
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_read_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size))
+/*[clinic end generated code: output=aceb2765587b0a29 input=824f6f910465e61a]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return bufferediobase_unsupported(state, "read");
+}
+
+/*[clinic input]
+_io._BufferedIOBase.read1
+
+ cls: defining_class
+ size: int(unused=True) = -1
+ /
+
+Read and return up to size bytes, with at most one read() call to the underlying raw stream.
+
+Return an empty bytes object on EOF.
+A short result does not imply that EOF is imminent.
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_read1_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size))
+/*[clinic end generated code: output=2e7fc62972487eaa input=af76380e020fd9e6]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return bufferediobase_unsupported(state, "read1");
+}
+
+/*[clinic input]
+_io._BufferedIOBase.write
+
+ cls: defining_class
+ b: object(unused=True)
+ /
+
+Write buffer b to the IO stream.
+
+Return the number of bytes written, which is always
+the length of b in bytes.
+
+Raise BlockingIOError if the buffer is full and the
+underlying raw stream cannot accept more data at the moment.
+[clinic start generated code]*/
+
+static PyObject *
+_io__BufferedIOBase_write_impl(PyObject *self, PyTypeObject *cls,
+ PyObject *Py_UNUSED(b))
+/*[clinic end generated code: output=712c635246bf2306 input=9793f5c8f71029ad]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return bufferediobase_unsupported(state, "write");
+}
+
+
+typedef struct {
+ PyObject_HEAD
+
+ PyObject *raw;
+ int ok; /* Initialized? */
+ int detached;
+ int readable;
+ int writable;
+ char finalizing;
+
+ /* True if this is a vanilla Buffered object (rather than a user derived
+ class) *and* the raw stream is a vanilla FileIO object. */
+ int fast_closed_checks;
+
+ /* Absolute position inside the raw stream (-1 if unknown). */
+ Py_off_t abs_pos;
+
+ /* A static buffer of size `buffer_size` */
+ char *buffer;
+ /* Current logical position in the buffer. */
+ Py_off_t pos;
+ /* Position of the raw stream in the buffer. */
+ Py_off_t raw_pos;
+
+ /* Just after the last buffered byte in the buffer, or -1 if the buffer
+ isn't ready for reading. */
+ Py_off_t read_end;
+
+ /* Just after the last byte actually written */
+ Py_off_t write_pos;
+ /* Just after the last byte waiting to be written, or -1 if the buffer
+ isn't ready for writing. */
+ Py_off_t write_end;
+
+ PyThread_type_lock lock;
+ volatile unsigned long owner;
+
+ Py_ssize_t buffer_size;
+ Py_ssize_t buffer_mask;
+
+ PyObject *dict;
+ PyObject *weakreflist;
+} buffered;
+
+/*
+ Implementation notes:
+
+ * BufferedReader, BufferedWriter and BufferedRandom try to share most
+ methods (this is helped by the members `readable` and `writable`, which
+ are initialized in the respective constructors)
+ * They also share a single buffer for reading and writing. This enables
+ interleaved reads and writes without flushing. It also makes the logic
+ a bit trickier to get right.
+ * The absolute position of the raw stream is cached, if possible, in the
+ `abs_pos` member. It must be updated every time an operation is done
+ on the raw stream. If not sure, it can be reinitialized by calling
+ _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek()
+ also does it). To read it, use RAW_TELL().
+ * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and
+ _bufferedwriter_flush_unlocked do a lot of useful housekeeping.
+
+ NOTE: we should try to maintain block alignment of reads and writes to the
+ raw stream (according to the buffer size), but for now it is only done
+ in read() and friends.
+
+*/
+
+/* These macros protect the buffered object against concurrent operations. */
+
+static int
+_enter_buffered_busy(buffered *self)
+{
+ int relax_locking;
+ PyLockStatus st;
+ if (self->owner == PyThread_get_thread_ident()) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %R", self);
+ return 0;
+ }
+ PyInterpreterState *interp = PyInterpreterState_Get();
+ relax_locking = _Py_IsInterpreterFinalizing(interp);
+ Py_BEGIN_ALLOW_THREADS
+ if (!relax_locking)
+ st = PyThread_acquire_lock(self->lock, 1);
+ else {
+ /* When finalizing, we don't want a deadlock to happen with daemon
+ * threads abruptly shut down while they owned the lock.
+ * Therefore, only wait for a grace period (1 s.).
+ * Note that non-daemon threads have already exited here, so this
+ * shouldn't affect carefully written threaded I/O code.
+ */
+ st = PyThread_acquire_lock_timed(self->lock, (PY_TIMEOUT_T)1e6, 0);
+ }
+ Py_END_ALLOW_THREADS
+ if (relax_locking && st != PY_LOCK_ACQUIRED) {
+ PyObject *ascii = PyObject_ASCII((PyObject*)self);
+ _Py_FatalErrorFormat(__func__,
+ "could not acquire lock for %s at interpreter "
+ "shutdown, possibly due to daemon threads",
+ ascii ? PyUnicode_AsUTF8(ascii) : "<ascii(self) failed>");
+ }
+ return 1;
+}
+
+#define ENTER_BUFFERED(self) \
+ ( (PyThread_acquire_lock(self->lock, 0) ? \
+ 1 : _enter_buffered_busy(self)) \
+ && (self->owner = PyThread_get_thread_ident(), 1) )
+
+#define LEAVE_BUFFERED(self) \
+ do { \
+ self->owner = 0; \
+ PyThread_release_lock(self->lock); \
+ } while(0);
+
+#define CHECK_INITIALIZED(self) \
+ if (self->ok <= 0) { \
+ if (self->detached) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "raw stream has been detached"); \
+ } else { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on uninitialized object"); \
+ } \
+ return NULL; \
+ }
+
+#define CHECK_INITIALIZED_INT(self) \
+ if (self->ok <= 0) { \
+ if (self->detached) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "raw stream has been detached"); \
+ } else { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on uninitialized object"); \
+ } \
+ return -1; \
+ }
+
+#define IS_CLOSED(self) \
+ (!self->buffer || \
+ (self->fast_closed_checks \
+ ? _PyFileIO_closed(self->raw) \
+ : buffered_closed(self)))
+
+#define CHECK_CLOSED(self, error_msg) \
+ if (IS_CLOSED(self) && (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) { \
+ PyErr_SetString(PyExc_ValueError, error_msg); \
+ return NULL; \
+ } \
+
+#define VALID_READ_BUFFER(self) \
+ (self->readable && self->read_end != -1)
+
+#define VALID_WRITE_BUFFER(self) \
+ (self->writable && self->write_end != -1)
+
+#define ADJUST_POSITION(self, _new_pos) \
+ do { \
+ self->pos = _new_pos; \
+ if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \
+ self->read_end = self->pos; \
+ } while(0)
+
+#define READAHEAD(self) \
+ ((self->readable && VALID_READ_BUFFER(self)) \
+ ? (self->read_end - self->pos) : 0)
+
+#define RAW_OFFSET(self) \
+ (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \
+ && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0)
+
+#define RAW_TELL(self) \
+ (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self))
+
+#define MINUS_LAST_BLOCK(self, size) \
+ (self->buffer_mask ? \
+ (size & ~self->buffer_mask) : \
+ (self->buffer_size * (size / self->buffer_size)))
+
+
+static int
+buffered_clear(buffered *self)
+{
+ self->ok = 0;
+ Py_CLEAR(self->raw);
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+buffered_dealloc(buffered *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ self->finalizing = 1;
+ if (_PyIOBase_finalize((PyObject *) self) < 0)
+ return;
+ _PyObject_GC_UNTRACK(self);
+ self->ok = 0;
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)self);
+ if (self->buffer) {
+ PyMem_Free(self->buffer);
+ self->buffer = NULL;
+ }
+ if (self->lock) {
+ PyThread_free_lock(self->lock);
+ self->lock = NULL;
+ }
+ (void)buffered_clear(self);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+/*[clinic input]
+_io._Buffered.__sizeof__
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered___sizeof___impl(buffered *self)
+/*[clinic end generated code: output=0231ef7f5053134e input=753c782d808d34df]*/
+{
+ size_t res = _PyObject_SIZE(Py_TYPE(self));
+ if (self->buffer) {
+ res += (size_t)self->buffer_size;
+ }
+ return PyLong_FromSize_t(res);
+}
+
+static int
+buffered_traverse(buffered *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->raw);
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+/* Because this can call arbitrary code, it shouldn't be called when
+ the refcount is 0 (that is, not directly from tp_dealloc unless
+ the refcount has been temporarily re-incremented). */
+/*[clinic input]
+_io._Buffered._dealloc_warn
+
+ source: object
+ /
+
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered__dealloc_warn(buffered *self, PyObject *source)
+/*[clinic end generated code: output=690dcc3df8967162 input=8f845f2a4786391c]*/
+{
+ if (self->ok && self->raw) {
+ PyObject *r;
+ r = PyObject_CallMethodOneArg(self->raw, &_Py_ID(_dealloc_warn), source);
+ if (r)
+ Py_DECREF(r);
+ else
+ PyErr_Clear();
+ }
+ Py_RETURN_NONE;
+}
+
+/*
+ * _BufferedIOMixin methods
+ * This is not a class, just a collection of methods that will be reused
+ * by BufferedReader and BufferedWriter
+ */
+
+/* Flush and close */
+/*[clinic input]
+_io._Buffered.flush as _io__Buffered_simple_flush
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_simple_flush_impl(buffered *self)
+/*[clinic end generated code: output=29ebb3820db1bdfd input=f33ef045e7250767]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(flush));
+}
+
+static int
+buffered_closed(buffered *self)
+{
+ int closed;
+ PyObject *res;
+ CHECK_INITIALIZED_INT(self)
+ res = PyObject_GetAttr(self->raw, &_Py_ID(closed));
+ if (res == NULL)
+ return -1;
+ closed = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ return closed;
+}
+
+static PyObject *
+buffered_closed_get(buffered *self, void *context)
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_GetAttr(self->raw, &_Py_ID(closed));
+}
+
+/*[clinic input]
+_io._Buffered.close
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_close_impl(buffered *self)
+/*[clinic end generated code: output=7280b7b42033be0c input=d20b83d1ddd7d805]*/
+{
+ PyObject *res = NULL;
+ int r;
+
+ CHECK_INITIALIZED(self)
+ if (!ENTER_BUFFERED(self)) {
+ return NULL;
+ }
+
+ r = buffered_closed(self);
+ if (r < 0)
+ goto end;
+ if (r > 0) {
+ res = Py_NewRef(Py_None);
+ goto end;
+ }
+
+ if (self->finalizing) {
+ PyObject *r = _io__Buffered__dealloc_warn(self, (PyObject *) self);
+ if (r)
+ Py_DECREF(r);
+ else
+ PyErr_Clear();
+ }
+ /* flush() will most probably re-take the lock, so drop it first */
+ LEAVE_BUFFERED(self)
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (!ENTER_BUFFERED(self)) {
+ return NULL;
+ }
+ PyObject *exc = NULL;
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
+ Py_DECREF(res);
+ }
+
+ res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
+
+ if (self->buffer) {
+ PyMem_Free(self->buffer);
+ self->buffer = NULL;
+ }
+
+ if (exc != NULL) {
+ _PyErr_ChainExceptions1(exc);
+ Py_CLEAR(res);
+ }
+
+ self->read_end = 0;
+ self->pos = 0;
+
+end:
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.detach
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_detach_impl(buffered *self)
+/*[clinic end generated code: output=dd0fc057b8b779f7 input=482762a345cc9f44]*/
+{
+ PyObject *raw, *res;
+ CHECK_INITIALIZED(self)
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res);
+ raw = self->raw;
+ self->raw = NULL;
+ self->detached = 1;
+ self->ok = 0;
+ return raw;
+}
+
+/* Inquiries */
+
+/*[clinic input]
+_io._Buffered.seekable
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_seekable_impl(buffered *self)
+/*[clinic end generated code: output=90172abb5ceb6e8f input=7d35764f5fb5262b]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(seekable));
+}
+
+/*[clinic input]
+_io._Buffered.readable
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_readable_impl(buffered *self)
+/*[clinic end generated code: output=92afa07661ecb698 input=640619addb513b8b]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(readable));
+}
+
+/*[clinic input]
+_io._Buffered.writable
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_writable_impl(buffered *self)
+/*[clinic end generated code: output=4e3eee8d6f9d8552 input=b35ea396b2201554]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(writable));
+}
+
+static PyObject *
+buffered_name_get(buffered *self, void *context)
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_GetAttr(self->raw, &_Py_ID(name));
+}
+
+static PyObject *
+buffered_mode_get(buffered *self, void *context)
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_GetAttr(self->raw, &_Py_ID(mode));
+}
+
+/* Lower-level APIs */
+
+/*[clinic input]
+_io._Buffered.fileno
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_fileno_impl(buffered *self)
+/*[clinic end generated code: output=b717648d58a95ee3 input=768ea30b3f6314a7]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(fileno));
+}
+
+/*[clinic input]
+_io._Buffered.isatty
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_isatty_impl(buffered *self)
+/*[clinic end generated code: output=c20e55caae67baea input=9ea007b11559bee4]*/
+{
+ CHECK_INITIALIZED(self)
+ return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(isatty));
+}
+
+/* Forward decls */
+static PyObject *
+_bufferedwriter_flush_unlocked(buffered *);
+static Py_ssize_t
+_bufferedreader_fill_buffer(buffered *self);
+static void
+_bufferedreader_reset_buf(buffered *self);
+static void
+_bufferedwriter_reset_buf(buffered *self);
+static PyObject *
+_bufferedreader_peek_unlocked(buffered *self);
+static PyObject *
+_bufferedreader_read_all(buffered *self);
+static PyObject *
+_bufferedreader_read_fast(buffered *self, Py_ssize_t);
+static PyObject *
+_bufferedreader_read_generic(buffered *self, Py_ssize_t);
+static Py_ssize_t
+_bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len);
+
+/*
+ * Helpers
+ */
+
+/* Sets the current error to BlockingIOError */
+static void
+_set_BlockingIOError(const char *msg, Py_ssize_t written)
+{
+ PyObject *err;
+ PyErr_Clear();
+ err = PyObject_CallFunction(PyExc_BlockingIOError, "isn",
+ errno, msg, written);
+ if (err)
+ PyErr_SetObject(PyExc_BlockingIOError, err);
+ Py_XDECREF(err);
+}
+
+/* Returns the address of the `written` member if a BlockingIOError was
+ raised, NULL otherwise. The error is always re-raised. */
+static Py_ssize_t *
+_buffered_check_blocking_error(void)
+{
+ PyObject *exc = PyErr_GetRaisedException();
+ if (exc == NULL || !PyErr_GivenExceptionMatches(exc, PyExc_BlockingIOError)) {
+ PyErr_SetRaisedException(exc);
+ return NULL;
+ }
+ PyOSErrorObject *err = (PyOSErrorObject *)exc;
+ /* TODO: sanity check (err->written >= 0) */
+ PyErr_SetRaisedException(exc);
+ return &err->written;
+}
+
+static Py_off_t
+_buffered_raw_tell(buffered *self)
+{
+ Py_off_t n;
+ PyObject *res;
+ res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(tell));
+ if (res == NULL)
+ return -1;
+ n = PyNumber_AsOff_t(res, PyExc_ValueError);
+ Py_DECREF(res);
+ if (n < 0) {
+ if (!PyErr_Occurred())
+ PyErr_Format(PyExc_OSError,
+ "Raw stream returned invalid position %" PY_PRIdOFF,
+ (PY_OFF_T_COMPAT)n);
+ return -1;
+ }
+ self->abs_pos = n;
+ return n;
+}
+
+static Py_off_t
+_buffered_raw_seek(buffered *self, Py_off_t target, int whence)
+{
+ PyObject *res, *posobj, *whenceobj;
+ Py_off_t n;
+
+ posobj = PyLong_FromOff_t(target);
+ if (posobj == NULL)
+ return -1;
+ whenceobj = PyLong_FromLong(whence);
+ if (whenceobj == NULL) {
+ Py_DECREF(posobj);
+ return -1;
+ }
+ res = PyObject_CallMethodObjArgs(self->raw, &_Py_ID(seek),
+ posobj, whenceobj, NULL);
+ Py_DECREF(posobj);
+ Py_DECREF(whenceobj);
+ if (res == NULL)
+ return -1;
+ n = PyNumber_AsOff_t(res, PyExc_ValueError);
+ Py_DECREF(res);
+ if (n < 0) {
+ if (!PyErr_Occurred())
+ PyErr_Format(PyExc_OSError,
+ "Raw stream returned invalid position %" PY_PRIdOFF,
+ (PY_OFF_T_COMPAT)n);
+ return -1;
+ }
+ self->abs_pos = n;
+ return n;
+}
+
+static int
+_buffered_init(buffered *self)
+{
+ Py_ssize_t n;
+ if (self->buffer_size <= 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "buffer size must be strictly positive");
+ return -1;
+ }
+ if (self->buffer)
+ PyMem_Free(self->buffer);
+ self->buffer = PyMem_Malloc(self->buffer_size);
+ if (self->buffer == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ if (self->lock)
+ PyThread_free_lock(self->lock);
+ self->lock = PyThread_allocate_lock();
+ if (self->lock == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
+ return -1;
+ }
+ self->owner = 0;
+ /* Find out whether buffer_size is a power of 2 */
+ /* XXX is this optimization useful? */
+ for (n = self->buffer_size - 1; n & 1; n >>= 1)
+ ;
+ if (n == 0)
+ self->buffer_mask = self->buffer_size - 1;
+ else
+ self->buffer_mask = 0;
+ if (_buffered_raw_tell(self) == -1)
+ PyErr_Clear();
+ return 0;
+}
+
+/* Return 1 if an OSError with errno == EINTR is set (and then
+ clears the error indicator), 0 otherwise.
+ Should only be called when PyErr_Occurred() is true.
+*/
+int
+_PyIO_trap_eintr(void)
+{
+ if (!PyErr_ExceptionMatches(PyExc_OSError)) {
+ return 0;
+ }
+ PyObject *exc = PyErr_GetRaisedException();
+ PyOSErrorObject *env_err = (PyOSErrorObject *)exc;
+ assert(env_err != NULL);
+ if (env_err->myerrno != NULL) {
+ assert(EINTR > 0 && EINTR < INT_MAX);
+ assert(PyLong_CheckExact(env_err->myerrno));
+ int overflow;
+ int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow);
+ PyErr_Clear();
+ if (myerrno == EINTR) {
+ Py_DECREF(exc);
+ return 1;
+ }
+ }
+ /* This silences any error set by PyObject_RichCompareBool() */
+ PyErr_SetRaisedException(exc);
+ return 0;
+}
+
+/*
+ * Shared methods and wrappers
+ */
+
+static PyObject *
+buffered_flush_and_rewind_unlocked(buffered *self)
+{
+ PyObject *res;
+
+ res = _bufferedwriter_flush_unlocked(self);
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res);
+
+ if (self->readable) {
+ /* Rewind the raw stream so that its position corresponds to
+ the current logical position. */
+ Py_off_t n;
+ n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1);
+ _bufferedreader_reset_buf(self);
+ if (n == -1)
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+/*[clinic input]
+_io._Buffered.flush
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_flush_impl(buffered *self)
+/*[clinic end generated code: output=da2674ef1ce71f3a input=fda63444697c6bf4]*/
+{
+ PyObject *res;
+
+ CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "flush of closed file")
+
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+ res = buffered_flush_and_rewind_unlocked(self);
+ LEAVE_BUFFERED(self)
+
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.peek
+ size: Py_ssize_t = 0
+ /
+
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_peek_impl(buffered *self, Py_ssize_t size)
+/*[clinic end generated code: output=ba7a097ca230102b input=37ffb97d06ff4adb]*/
+{
+ PyObject *res = NULL;
+
+ CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "peek of closed file")
+
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+
+ if (self->writable) {
+ res = buffered_flush_and_rewind_unlocked(self);
+ if (res == NULL)
+ goto end;
+ Py_CLEAR(res);
+ }
+ res = _bufferedreader_peek_unlocked(self);
+
+end:
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.read
+ size as n: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_read_impl(buffered *self, Py_ssize_t n)
+/*[clinic end generated code: output=f41c78bb15b9bbe9 input=7df81e82e08a68a2]*/
+{
+ PyObject *res;
+
+ CHECK_INITIALIZED(self)
+ if (n < -1) {
+ PyErr_SetString(PyExc_ValueError,
+ "read length must be non-negative or -1");
+ return NULL;
+ }
+
+ CHECK_CLOSED(self, "read of closed file")
+
+ if (n == -1) {
+ /* The number of bytes is unspecified, read until the end of stream */
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+ res = _bufferedreader_read_all(self);
+ }
+ else {
+ res = _bufferedreader_read_fast(self, n);
+ if (res != Py_None)
+ return res;
+ Py_DECREF(res);
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+ res = _bufferedreader_read_generic(self, n);
+ }
+
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.read1
+ size as n: Py_ssize_t = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
+/*[clinic end generated code: output=bcc4fb4e54d103a3 input=7d22de9630b61774]*/
+{
+ Py_ssize_t have, r;
+ PyObject *res = NULL;
+
+ CHECK_INITIALIZED(self)
+ if (n < 0) {
+ n = self->buffer_size;
+ }
+
+ CHECK_CLOSED(self, "read of closed file")
+
+ if (n == 0)
+ return PyBytes_FromStringAndSize(NULL, 0);
+
+ /* Return up to n bytes. If at least one byte is buffered, we
+ only return buffered bytes. Otherwise, we do one raw read. */
+
+ have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (have > 0) {
+ n = Py_MIN(have, n);
+ res = _bufferedreader_read_fast(self, n);
+ assert(res != Py_None);
+ return res;
+ }
+ res = PyBytes_FromStringAndSize(NULL, n);
+ if (res == NULL)
+ return NULL;
+ if (!ENTER_BUFFERED(self)) {
+ Py_DECREF(res);
+ return NULL;
+ }
+ _bufferedreader_reset_buf(self);
+ r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);
+ LEAVE_BUFFERED(self)
+ if (r == -1) {
+ Py_DECREF(res);
+ return NULL;
+ }
+ if (r == -2)
+ r = 0;
+ if (n > r)
+ _PyBytes_Resize(&res, r);
+ return res;
+}
+
+static PyObject *
+_buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1)
+{
+ Py_ssize_t n, written = 0, remaining;
+ PyObject *res = NULL;
+
+ CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "readinto of closed file")
+
+ n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (n > 0) {
+ if (n >= buffer->len) {
+ memcpy(buffer->buf, self->buffer + self->pos, buffer->len);
+ self->pos += buffer->len;
+ return PyLong_FromSsize_t(buffer->len);
+ }
+ memcpy(buffer->buf, self->buffer + self->pos, n);
+ self->pos += n;
+ written = n;
+ }
+
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+
+ if (self->writable) {
+ res = buffered_flush_and_rewind_unlocked(self);
+ if (res == NULL)
+ goto end;
+ Py_CLEAR(res);
+ }
+
+ _bufferedreader_reset_buf(self);
+ self->pos = 0;
+
+ for (remaining = buffer->len - written;
+ remaining > 0;
+ written += n, remaining -= n) {
+ /* If remaining bytes is larger than internal buffer size, copy
+ * directly into caller's buffer. */
+ if (remaining > self->buffer_size) {
+ n = _bufferedreader_raw_read(self, (char *) buffer->buf + written,
+ remaining);
+ }
+
+ /* In readinto1 mode, we do not want to fill the internal
+ buffer if we already have some data to return */
+ else if (!(readinto1 && written)) {
+ n = _bufferedreader_fill_buffer(self);
+ if (n > 0) {
+ if (n > remaining)
+ n = remaining;
+ memcpy((char *) buffer->buf + written,
+ self->buffer + self->pos, n);
+ self->pos += n;
+ continue; /* short circuit */
+ }
+ }
+ else
+ n = 0;
+
+ if (n == 0 || (n == -2 && written > 0))
+ break;
+ if (n < 0) {
+ if (n == -2) {
+ res = Py_NewRef(Py_None);
+ }
+ goto end;
+ }
+
+ /* At most one read in readinto1 mode */
+ if (readinto1) {
+ written += n;
+ break;
+ }
+ }
+ res = PyLong_FromSsize_t(written);
+
+end:
+ LEAVE_BUFFERED(self);
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.readinto
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer)
+/*[clinic end generated code: output=bcb376580b1d8170 input=ed6b98b7a20a3008]*/
+{
+ return _buffered_readinto_generic(self, buffer, 0);
+}
+
+/*[clinic input]
+_io._Buffered.readinto1
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer)
+/*[clinic end generated code: output=6e5c6ac5868205d6 input=4455c5d55fdf1687]*/
+{
+ return _buffered_readinto_generic(self, buffer, 1);
+}
+
+
+static PyObject *
+_buffered_readline(buffered *self, Py_ssize_t limit)
+{
+ PyObject *res = NULL;
+ PyObject *chunks = NULL;
+ Py_ssize_t n;
+ const char *start, *s, *end;
+
+ CHECK_CLOSED(self, "readline of closed file")
+
+ /* First, try to find a line in the buffer. This can run unlocked because
+ the calls to the C API are simple enough that they can't trigger
+ any thread switch. */
+ n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (limit >= 0 && n > limit)
+ n = limit;
+ start = self->buffer + self->pos;
+ s = memchr(start, '\n', n);
+ if (s != NULL) {
+ res = PyBytes_FromStringAndSize(start, s - start + 1);
+ if (res != NULL)
+ self->pos += s - start + 1;
+ goto end_unlocked;
+ }
+ if (n == limit) {
+ res = PyBytes_FromStringAndSize(start, n);
+ if (res != NULL)
+ self->pos += n;
+ goto end_unlocked;
+ }
+
+ if (!ENTER_BUFFERED(self))
+ goto end_unlocked;
+
+ /* Now we try to get some more from the raw stream */
+ chunks = PyList_New(0);
+ if (chunks == NULL)
+ goto end;
+ if (n > 0) {
+ res = PyBytes_FromStringAndSize(start, n);
+ if (res == NULL)
+ goto end;
+ if (PyList_Append(chunks, res) < 0) {
+ Py_CLEAR(res);
+ goto end;
+ }
+ Py_CLEAR(res);
+ self->pos += n;
+ if (limit >= 0)
+ limit -= n;
+ }
+ if (self->writable) {
+ PyObject *r = buffered_flush_and_rewind_unlocked(self);
+ if (r == NULL)
+ goto end;
+ Py_DECREF(r);
+ }
+
+ for (;;) {
+ _bufferedreader_reset_buf(self);
+ n = _bufferedreader_fill_buffer(self);
+ if (n == -1)
+ goto end;
+ if (n <= 0)
+ break;
+ if (limit >= 0 && n > limit)
+ n = limit;
+ start = self->buffer;
+ end = start + n;
+ s = start;
+ while (s < end) {
+ if (*s++ == '\n') {
+ res = PyBytes_FromStringAndSize(start, s - start);
+ if (res == NULL)
+ goto end;
+ self->pos = s - start;
+ goto found;
+ }
+ }
+ res = PyBytes_FromStringAndSize(start, n);
+ if (res == NULL)
+ goto end;
+ if (n == limit) {
+ self->pos = n;
+ break;
+ }
+ if (PyList_Append(chunks, res) < 0) {
+ Py_CLEAR(res);
+ goto end;
+ }
+ Py_CLEAR(res);
+ if (limit >= 0)
+ limit -= n;
+ }
+found:
+ if (res != NULL && PyList_Append(chunks, res) < 0) {
+ Py_CLEAR(res);
+ goto end;
+ }
+ Py_XSETREF(res, _PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks));
+
+end:
+ LEAVE_BUFFERED(self)
+end_unlocked:
+ Py_XDECREF(chunks);
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.readline
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_readline_impl(buffered *self, Py_ssize_t size)
+/*[clinic end generated code: output=24dd2aa6e33be83c input=673b6240e315ef8a]*/
+{
+ CHECK_INITIALIZED(self)
+ return _buffered_readline(self, size);
+}
+
+
+/*[clinic input]
+_io._Buffered.tell
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_tell_impl(buffered *self)
+/*[clinic end generated code: output=386972ae84716c1e input=ad61e04a6b349573]*/
+{
+ Py_off_t pos;
+
+ CHECK_INITIALIZED(self)
+ pos = _buffered_raw_tell(self);
+ if (pos == -1)
+ return NULL;
+ pos -= RAW_OFFSET(self);
+ /* TODO: sanity check (pos >= 0) */
+ return PyLong_FromOff_t(pos);
+}
+
+/*[clinic input]
+_io._Buffered.seek
+ target as targetobj: object
+ whence: int = 0
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)
+/*[clinic end generated code: output=7ae0e8dc46efdefb input=a9c4920bfcba6163]*/
+{
+ Py_off_t target, n;
+ PyObject *res = NULL;
+
+ CHECK_INITIALIZED(self)
+
+ /* Do some error checking instead of trusting OS 'seek()'
+ ** error detection, just in case.
+ */
+ if ((whence < 0 || whence >2)
+#ifdef SEEK_HOLE
+ && (whence != SEEK_HOLE)
+#endif
+#ifdef SEEK_DATA
+ && (whence != SEEK_DATA)
+#endif
+ ) {
+ PyErr_Format(PyExc_ValueError,
+ "whence value %d unsupported", whence);
+ return NULL;
+ }
+
+ CHECK_CLOSED(self, "seek of closed file")
+
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ if (_PyIOBase_check_seekable(state, self->raw, Py_True) == NULL) {
+ return NULL;
+ }
+
+ target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
+ if (target == -1 && PyErr_Occurred())
+ return NULL;
+
+ /* SEEK_SET and SEEK_CUR are special because we could seek inside the
+ buffer. Other whence values must be managed without this optimization.
+ Some Operating Systems can provide additional values, like
+ SEEK_HOLE/SEEK_DATA. */
+ if (((whence == 0) || (whence == 1)) && self->readable) {
+ Py_off_t current, avail;
+ /* Check if seeking leaves us inside the current buffer,
+ so as to return quickly if possible. Also, we needn't take the
+ lock in this fast path.
+ Don't know how to do that when whence == 2, though. */
+ /* NOTE: RAW_TELL() can release the GIL but the object is in a stable
+ state at this point. */
+ current = RAW_TELL(self);
+ avail = READAHEAD(self);
+ if (avail > 0) {
+ Py_off_t offset;
+ if (whence == 0)
+ offset = target - (current - RAW_OFFSET(self));
+ else
+ offset = target;
+ if (offset >= -self->pos && offset <= avail) {
+ self->pos += offset;
+ return PyLong_FromOff_t(current - avail + offset);
+ }
+ }
+ }
+
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+
+ /* Fallback: invoke raw seek() method and clear buffer */
+ if (self->writable) {
+ res = _bufferedwriter_flush_unlocked(self);
+ if (res == NULL)
+ goto end;
+ Py_CLEAR(res);
+ }
+
+ /* TODO: align on block boundary and read buffer if needed? */
+ if (whence == 1)
+ target -= RAW_OFFSET(self);
+ n = _buffered_raw_seek(self, target, whence);
+ if (n == -1)
+ goto end;
+ self->raw_pos = -1;
+ res = PyLong_FromOff_t(n);
+ if (res != NULL && self->readable)
+ _bufferedreader_reset_buf(self);
+
+end:
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+/*[clinic input]
+_io._Buffered.truncate
+ cls: defining_class
+ pos: object = None
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__Buffered_truncate_impl(buffered *self, PyTypeObject *cls, PyObject *pos)
+/*[clinic end generated code: output=fe3882fbffe79f1a input=f5b737d97d76303f]*/
+{
+ PyObject *res = NULL;
+
+ CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "truncate of closed file")
+ if (!self->writable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return bufferediobase_unsupported(state, "truncate");
+ }
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+
+ res = buffered_flush_and_rewind_unlocked(self);
+ if (res == NULL) {
+ goto end;
+ }
+ Py_CLEAR(res);
+
+ res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(truncate), pos);
+ if (res == NULL)
+ goto end;
+ /* Reset cached position */
+ if (_buffered_raw_tell(self) == -1)
+ PyErr_Clear();
+
+end:
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+static PyObject *
+buffered_iternext(buffered *self)
+{
+ PyObject *line;
+ PyTypeObject *tp;
+
+ CHECK_INITIALIZED(self);
+
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ tp = Py_TYPE(self);
+ if (Py_IS_TYPE(tp, state->PyBufferedReader_Type) ||
+ Py_IS_TYPE(tp, state->PyBufferedRandom_Type))
+ {
+ /* Skip method call overhead for speed */
+ line = _buffered_readline(self, -1);
+ }
+ else {
+ line = PyObject_CallMethodNoArgs((PyObject *)self,
+ &_Py_ID(readline));
+ if (line && !PyBytes_Check(line)) {
+ PyErr_Format(PyExc_OSError,
+ "readline() should have returned a bytes object, "
+ "not '%.200s'", Py_TYPE(line)->tp_name);
+ Py_DECREF(line);
+ return NULL;
+ }
+ }
+
+ if (line == NULL)
+ return NULL;
+
+ if (PyBytes_GET_SIZE(line) == 0) {
+ /* Reached EOF or would have blocked */
+ Py_DECREF(line);
+ return NULL;
+ }
+
+ return line;
+}
+
+static PyObject *
+buffered_repr(buffered *self)
+{
+ PyObject *nameobj, *res;
+
+ if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
+ if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
+ return NULL;
+ }
+ /* Ignore ValueError raised if the underlying stream was detached */
+ PyErr_Clear();
+ }
+ if (nameobj == NULL) {
+ res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
+ }
+ else {
+ int status = Py_ReprEnter((PyObject *)self);
+ res = NULL;
+ if (status == 0) {
+ res = PyUnicode_FromFormat("<%s name=%R>",
+ Py_TYPE(self)->tp_name, nameobj);
+ Py_ReprLeave((PyObject *)self);
+ }
+ else if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
+ Py_DECREF(nameobj);
+ }
+ return res;
+}
+
+/*
+ * class BufferedReader
+ */
+
+static void _bufferedreader_reset_buf(buffered *self)
+{
+ self->read_end = -1;
+}
+
+/*[clinic input]
+_io.BufferedReader.__init__
+ raw: object
+ buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
+
+Create a new buffered reader using the given readable raw IO object.
+[clinic start generated code]*/
+
+static int
+_io_BufferedReader___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size)
+/*[clinic end generated code: output=cddcfefa0ed294c4 input=fb887e06f11b4e48]*/
+{
+ self->ok = 0;
+ self->detached = 0;
+
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) {
+ return -1;
+ }
+
+ Py_XSETREF(self->raw, Py_NewRef(raw));
+ self->buffer_size = buffer_size;
+ self->readable = 1;
+ self->writable = 0;
+
+ if (_buffered_init(self) < 0)
+ return -1;
+ _bufferedreader_reset_buf(self);
+
+ self->fast_closed_checks = (
+ Py_IS_TYPE(self, state->PyBufferedReader_Type) &&
+ Py_IS_TYPE(raw, state->PyFileIO_Type)
+ );
+
+ self->ok = 1;
+ return 0;
+}
+
+static Py_ssize_t
+_bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
+{
+ Py_buffer buf;
+ PyObject *memobj, *res;
+ Py_ssize_t n;
+ /* NOTE: the buffer needn't be released as its object is NULL. */
+ if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
+ return -1;
+ memobj = PyMemoryView_FromBuffer(&buf);
+ if (memobj == NULL)
+ return -1;
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
+ occurs so we needn't do it ourselves.
+ We then retry reading, ignoring the signal if no handler has
+ raised (see issue #10956).
+ */
+ do {
+ res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(readinto), memobj);
+ } while (res == NULL && _PyIO_trap_eintr());
+ Py_DECREF(memobj);
+ if (res == NULL)
+ return -1;
+ if (res == Py_None) {
+ /* Non-blocking stream would have blocked. Special return code! */
+ Py_DECREF(res);
+ return -2;
+ }
+ n = PyNumber_AsSsize_t(res, PyExc_ValueError);
+ Py_DECREF(res);
+
+ if (n == -1 && PyErr_Occurred()) {
+ _PyErr_FormatFromCause(
+ PyExc_OSError,
+ "raw readinto() failed"
+ );
+ return -1;
+ }
+
+ if (n < 0 || n > len) {
+ PyErr_Format(PyExc_OSError,
+ "raw readinto() returned invalid length %zd "
+ "(should have been between 0 and %zd)", n, len);
+ return -1;
+ }
+ if (n > 0 && self->abs_pos != -1)
+ self->abs_pos += n;
+ return n;
+}
+
+static Py_ssize_t
+_bufferedreader_fill_buffer(buffered *self)
+{
+ Py_ssize_t start, len, n;
+ if (VALID_READ_BUFFER(self))
+ start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
+ else
+ start = 0;
+ len = self->buffer_size - start;
+ n = _bufferedreader_raw_read(self, self->buffer + start, len);
+ if (n <= 0)
+ return n;
+ self->read_end = start + n;
+ self->raw_pos = start + n;
+ return n;
+}
+
+static PyObject *
+_bufferedreader_read_all(buffered *self)
+{
+ Py_ssize_t current_size;
+ PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall;
+
+ /* First copy what we have in the current buffer. */
+ current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (current_size) {
+ data = PyBytes_FromStringAndSize(
+ self->buffer + self->pos, current_size);
+ if (data == NULL)
+ return NULL;
+ self->pos += current_size;
+ }
+ /* We're going past the buffer's bounds, flush it */
+ if (self->writable) {
+ tmp = buffered_flush_and_rewind_unlocked(self);
+ if (tmp == NULL)
+ goto cleanup;
+ Py_CLEAR(tmp);
+ }
+ _bufferedreader_reset_buf(self);
+
+ if (_PyObject_LookupAttr(self->raw, &_Py_ID(readall), &readall) < 0) {
+ goto cleanup;
+ }
+ if (readall) {
+ tmp = _PyObject_CallNoArgs(readall);
+ Py_DECREF(readall);
+ if (tmp == NULL)
+ goto cleanup;
+ if (tmp != Py_None && !PyBytes_Check(tmp)) {
+ PyErr_SetString(PyExc_TypeError, "readall() should return bytes");
+ goto cleanup;
+ }
+ if (current_size == 0) {
+ res = tmp;
+ } else {
+ if (tmp != Py_None) {
+ PyBytes_Concat(&data, tmp);
+ }
+ res = data;
+ }
+ goto cleanup;
+ }
+
+ chunks = PyList_New(0);
+ if (chunks == NULL)
+ goto cleanup;
+
+ while (1) {
+ if (data) {
+ if (PyList_Append(chunks, data) < 0)
+ goto cleanup;
+ Py_CLEAR(data);
+ }
+
+ /* Read until EOF or until read() would block. */
+ data = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(read));
+ if (data == NULL)
+ goto cleanup;
+ if (data != Py_None && !PyBytes_Check(data)) {
+ PyErr_SetString(PyExc_TypeError, "read() should return bytes");
+ goto cleanup;
+ }
+ if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
+ if (current_size == 0) {
+ res = data;
+ goto cleanup;
+ }
+ else {
+ tmp = _PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks);
+ res = tmp;
+ goto cleanup;
+ }
+ }
+ current_size += PyBytes_GET_SIZE(data);
+ if (self->abs_pos != -1)
+ self->abs_pos += PyBytes_GET_SIZE(data);
+ }
+cleanup:
+ /* res is either NULL or a borrowed ref */
+ Py_XINCREF(res);
+ Py_XDECREF(data);
+ Py_XDECREF(tmp);
+ Py_XDECREF(chunks);
+ return res;
+}
+
+/* Read n bytes from the buffer if it can, otherwise return None.
+ This function is simple enough that it can run unlocked. */
+static PyObject *
+_bufferedreader_read_fast(buffered *self, Py_ssize_t n)
+{
+ Py_ssize_t current_size;
+
+ current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (n <= current_size) {
+ /* Fast path: the data to read is fully buffered. */
+ PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
+ if (res != NULL)
+ self->pos += n;
+ return res;
+ }
+ Py_RETURN_NONE;
+}
+
+/* Generic read function: read from the stream until enough bytes are read,
+ * or until an EOF occurs or until read() would block.
+ */
+static PyObject *
+_bufferedreader_read_generic(buffered *self, Py_ssize_t n)
+{
+ PyObject *res = NULL;
+ Py_ssize_t current_size, remaining, written;
+ char *out;
+
+ current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ if (n <= current_size)
+ return _bufferedreader_read_fast(self, n);
+
+ res = PyBytes_FromStringAndSize(NULL, n);
+ if (res == NULL)
+ goto error;
+ out = PyBytes_AS_STRING(res);
+ remaining = n;
+ written = 0;
+ if (current_size > 0) {
+ memcpy(out, self->buffer + self->pos, current_size);
+ remaining -= current_size;
+ written += current_size;
+ self->pos += current_size;
+ }
+ /* Flush the write buffer if necessary */
+ if (self->writable) {
+ PyObject *r = buffered_flush_and_rewind_unlocked(self);
+ if (r == NULL)
+ goto error;
+ Py_DECREF(r);
+ }
+ _bufferedreader_reset_buf(self);
+ while (remaining > 0) {
+ /* We want to read a whole block at the end into buffer.
+ If we had readv() we could do this in one pass. */
+ Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
+ if (r == 0)
+ break;
+ r = _bufferedreader_raw_read(self, out + written, r);
+ if (r == -1)
+ goto error;
+ if (r == 0 || r == -2) {
+ /* EOF occurred or read() would block. */
+ if (r == 0 || written > 0) {
+ if (_PyBytes_Resize(&res, written))
+ goto error;
+ return res;
+ }
+ Py_DECREF(res);
+ Py_RETURN_NONE;
+ }
+ remaining -= r;
+ written += r;
+ }
+ assert(remaining <= self->buffer_size);
+ self->pos = 0;
+ self->raw_pos = 0;
+ self->read_end = 0;
+ /* NOTE: when the read is satisfied, we avoid issuing any additional
+ reads, which could block indefinitely (e.g. on a socket).
+ See issue #9550. */
+ while (remaining > 0 && self->read_end < self->buffer_size) {
+ Py_ssize_t r = _bufferedreader_fill_buffer(self);
+ if (r == -1)
+ goto error;
+ if (r == 0 || r == -2) {
+ /* EOF occurred or read() would block. */
+ if (r == 0 || written > 0) {
+ if (_PyBytes_Resize(&res, written))
+ goto error;
+ return res;
+ }
+ Py_DECREF(res);
+ Py_RETURN_NONE;
+ }
+ if (remaining > r) {
+ memcpy(out + written, self->buffer + self->pos, r);
+ written += r;
+ self->pos += r;
+ remaining -= r;
+ }
+ else if (remaining > 0) {
+ memcpy(out + written, self->buffer + self->pos, remaining);
+ written += remaining;
+ self->pos += remaining;
+ remaining = 0;
+ }
+ if (remaining == 0)
+ break;
+ }
+
+ return res;
+
+error:
+ Py_XDECREF(res);
+ return NULL;
+}
+
+static PyObject *
+_bufferedreader_peek_unlocked(buffered *self)
+{
+ Py_ssize_t have, r;
+
+ have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+ /* Constraints:
+ 1. we don't want to advance the file position.
+ 2. we don't want to lose block alignment, so we can't shift the buffer
+ to make some place.
+ Therefore, we either return `have` bytes (if > 0), or a full buffer.
+ */
+ if (have > 0) {
+ return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
+ }
+
+ /* Fill the buffer from the raw stream, and copy it to the result. */
+ _bufferedreader_reset_buf(self);
+ r = _bufferedreader_fill_buffer(self);
+ if (r == -1)
+ return NULL;
+ if (r == -2)
+ r = 0;
+ self->pos = 0;
+ return PyBytes_FromStringAndSize(self->buffer, r);
+}
+
+
+/*
+ * class BufferedWriter
+ */
+static void
+_bufferedwriter_reset_buf(buffered *self)
+{
+ self->write_pos = 0;
+ self->write_end = -1;
+}
+
+/*[clinic input]
+_io.BufferedWriter.__init__
+ raw: object
+ buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
+
+A buffer for a writeable sequential RawIO object.
+
+The constructor creates a BufferedWriter for the given writeable raw
+stream. If the buffer_size is not given, it defaults to
+DEFAULT_BUFFER_SIZE.
+[clinic start generated code]*/
+
+static int
+_io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size)
+/*[clinic end generated code: output=c8942a020c0dee64 input=914be9b95e16007b]*/
+{
+ self->ok = 0;
+ self->detached = 0;
+
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) {
+ return -1;
+ }
+
+ Py_INCREF(raw);
+ Py_XSETREF(self->raw, raw);
+ self->readable = 0;
+ self->writable = 1;
+
+ self->buffer_size = buffer_size;
+ if (_buffered_init(self) < 0)
+ return -1;
+ _bufferedwriter_reset_buf(self);
+ self->pos = 0;
+
+ self->fast_closed_checks = (
+ Py_IS_TYPE(self, state->PyBufferedWriter_Type) &&
+ Py_IS_TYPE(raw, state->PyFileIO_Type)
+ );
+
+ self->ok = 1;
+ return 0;
+}
+
+static Py_ssize_t
+_bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
+{
+ Py_buffer buf;
+ PyObject *memobj, *res;
+ Py_ssize_t n;
+ int errnum;
+ /* NOTE: the buffer needn't be released as its object is NULL. */
+ if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
+ return -1;
+ memobj = PyMemoryView_FromBuffer(&buf);
+ if (memobj == NULL)
+ return -1;
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
+ occurs so we needn't do it ourselves.
+ We then retry writing, ignoring the signal if no handler has
+ raised (see issue #10956).
+ */
+ do {
+ errno = 0;
+ res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(write), memobj);
+ errnum = errno;
+ } while (res == NULL && _PyIO_trap_eintr());
+ Py_DECREF(memobj);
+ if (res == NULL)
+ return -1;
+ if (res == Py_None) {
+ /* Non-blocking stream would have blocked. Special return code!
+ Being paranoid we reset errno in case it is changed by code
+ triggered by a decref. errno is used by _set_BlockingIOError(). */
+ Py_DECREF(res);
+ errno = errnum;
+ return -2;
+ }
+ n = PyNumber_AsSsize_t(res, PyExc_ValueError);
+ Py_DECREF(res);
+ if (n < 0 || n > len) {
+ PyErr_Format(PyExc_OSError,
+ "raw write() returned invalid length %zd "
+ "(should have been between 0 and %zd)", n, len);
+ return -1;
+ }
+ if (n > 0 && self->abs_pos != -1)
+ self->abs_pos += n;
+ return n;
+}
+
+static PyObject *
+_bufferedwriter_flush_unlocked(buffered *self)
+{
+ Py_off_t n, rewind;
+
+ if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
+ goto end;
+ /* First, rewind */
+ rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
+ if (rewind != 0) {
+ n = _buffered_raw_seek(self, -rewind, 1);
+ if (n < 0) {
+ goto error;
+ }
+ self->raw_pos -= rewind;
+ }
+ while (self->write_pos < self->write_end) {
+ n = _bufferedwriter_raw_write(self,
+ self->buffer + self->write_pos,
+ Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
+ Py_off_t, Py_ssize_t));
+ if (n == -1) {
+ goto error;
+ }
+ else if (n == -2) {
+ _set_BlockingIOError("write could not complete without blocking",
+ 0);
+ goto error;
+ }
+ self->write_pos += n;
+ self->raw_pos = self->write_pos;
+ /* Partial writes can return successfully when interrupted by a
+ signal (see write(2)). We must run signal handlers before
+ blocking another time, possibly indefinitely. */
+ if (PyErr_CheckSignals() < 0)
+ goto error;
+ }
+
+
+end:
+ /* This ensures that after return from this function,
+ VALID_WRITE_BUFFER(self) returns false.
+
+ This is a required condition because when a tell() is called
+ after flushing and if VALID_READ_BUFFER(self) is false, we need
+ VALID_WRITE_BUFFER(self) to be false to have
+ RAW_OFFSET(self) == 0.
+
+ Issue: https://bugs.python.org/issue32228 */
+ _bufferedwriter_reset_buf(self);
+ Py_RETURN_NONE;
+
+error:
+ return NULL;
+}
+
+/*[clinic input]
+_io.BufferedWriter.write
+ buffer: Py_buffer
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
+/*[clinic end generated code: output=7f8d1365759bfc6b input=dd87dd85fc7f8850]*/
+{
+ PyObject *res = NULL;
+ Py_ssize_t written, avail, remaining;
+ Py_off_t offset;
+
+ CHECK_INITIALIZED(self)
+
+ if (!ENTER_BUFFERED(self))
+ return NULL;
+
+ /* Issue #31976: Check for closed file after acquiring the lock. Another
+ thread could be holding the lock while closing the file. */
+ if (IS_CLOSED(self)) {
+ PyErr_SetString(PyExc_ValueError, "write to closed file");
+ goto error;
+ }
+
+ /* Fast path: the data to write can be fully buffered. */
+ if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
+ self->pos = 0;
+ self->raw_pos = 0;
+ }
+ avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
+ if (buffer->len <= avail) {
+ memcpy(self->buffer + self->pos, buffer->buf, buffer->len);
+ if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
+ self->write_pos = self->pos;
+ }
+ ADJUST_POSITION(self, self->pos + buffer->len);
+ if (self->pos > self->write_end)
+ self->write_end = self->pos;
+ written = buffer->len;
+ goto end;
+ }
+
+ /* First write the current buffer */
+ res = _bufferedwriter_flush_unlocked(self);
+ if (res == NULL) {
+ Py_ssize_t *w = _buffered_check_blocking_error();
+ if (w == NULL)
+ goto error;
+ if (self->readable)
+ _bufferedreader_reset_buf(self);
+ /* Make some place by shifting the buffer. */
+ assert(VALID_WRITE_BUFFER(self));
+ memmove(self->buffer, self->buffer + self->write_pos,
+ Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
+ Py_off_t, Py_ssize_t));
+ self->write_end -= self->write_pos;
+ self->raw_pos -= self->write_pos;
+ self->pos -= self->write_pos;
+ self->write_pos = 0;
+ avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
+ Py_off_t, Py_ssize_t);
+ if (buffer->len <= avail) {
+ /* Everything can be buffered */
+ PyErr_Clear();
+ memcpy(self->buffer + self->write_end, buffer->buf, buffer->len);
+ self->write_end += buffer->len;
+ self->pos += buffer->len;
+ written = buffer->len;
+ goto end;
+ }
+ /* Buffer as much as possible. */
+ memcpy(self->buffer + self->write_end, buffer->buf, avail);
+ self->write_end += avail;
+ self->pos += avail;
+ /* XXX Modifying the existing exception e using the pointer w
+ will change e.characters_written but not e.args[2].
+ Therefore we just replace with a new error. */
+ _set_BlockingIOError("write could not complete without blocking",
+ avail);
+ goto error;
+ }
+ Py_CLEAR(res);
+
+ /* Adjust the raw stream position if it is away from the logical stream
+ position. This happens if the read buffer has been filled but not
+ modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind
+ the raw stream by itself).
+ Fixes issue #6629.
+ */
+ offset = RAW_OFFSET(self);
+ if (offset != 0) {
+ if (_buffered_raw_seek(self, -offset, 1) < 0)
+ goto error;
+ self->raw_pos -= offset;
+ }
+
+ /* Then write buf itself. At this point the buffer has been emptied. */
+ remaining = buffer->len;
+ written = 0;
+ while (remaining > self->buffer_size) {
+ Py_ssize_t n = _bufferedwriter_raw_write(
+ self, (char *) buffer->buf + written, buffer->len - written);
+ if (n == -1) {
+ goto error;
+ } else if (n == -2) {
+ /* Write failed because raw file is non-blocking */
+ if (remaining > self->buffer_size) {
+ /* Can't buffer everything, still buffer as much as possible */
+ memcpy(self->buffer,
+ (char *) buffer->buf + written, self->buffer_size);
+ self->raw_pos = 0;
+ ADJUST_POSITION(self, self->buffer_size);
+ self->write_end = self->buffer_size;
+ written += self->buffer_size;
+ _set_BlockingIOError("write could not complete without "
+ "blocking", written);
+ goto error;
+ }
+ PyErr_Clear();
+ break;
+ }
+ written += n;
+ remaining -= n;
+ /* Partial writes can return successfully when interrupted by a
+ signal (see write(2)). We must run signal handlers before
+ blocking another time, possibly indefinitely. */
+ if (PyErr_CheckSignals() < 0)
+ goto error;
+ }
+ if (self->readable)
+ _bufferedreader_reset_buf(self);
+ if (remaining > 0) {
+ memcpy(self->buffer, (char *) buffer->buf + written, remaining);
+ written += remaining;
+ }
+ self->write_pos = 0;
+ /* TODO: sanity check (remaining >= 0) */
+ self->write_end = remaining;
+ ADJUST_POSITION(self, remaining);
+ self->raw_pos = 0;
+
+end:
+ res = PyLong_FromSsize_t(written);
+
+error:
+ LEAVE_BUFFERED(self)
+ return res;
+}
+
+
+/*
+ * BufferedRWPair
+ */
+
+/* XXX The usefulness of this (compared to having two separate IO objects) is
+ * questionable.
+ */
+
+typedef struct {
+ PyObject_HEAD
+ buffered *reader;
+ buffered *writer;
+ PyObject *dict;
+ PyObject *weakreflist;
+} rwpair;
+
+/*[clinic input]
+_io.BufferedRWPair.__init__
+ reader: object
+ writer: object
+ buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
+ /
+
+A buffered reader and writer object together.
+
+A buffered reader object and buffered writer object put together to
+form a sequential IO object that can read and write. This is typically
+used with a socket or two-way pipe.
+
+reader and writer are RawIOBase objects that are readable and
+writeable respectively. If the buffer_size is omitted it defaults to
+DEFAULT_BUFFER_SIZE.
+[clinic start generated code]*/
+
+static int
+_io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader,
+ PyObject *writer, Py_ssize_t buffer_size)
+/*[clinic end generated code: output=327e73d1aee8f984 input=620d42d71f33a031]*/
+{
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ if (_PyIOBase_check_readable(state, reader, Py_True) == NULL) {
+ return -1;
+ }
+ if (_PyIOBase_check_writable(state, writer, Py_True) == NULL) {
+ return -1;
+ }
+
+ self->reader = (buffered *) PyObject_CallFunction(
+ (PyObject *)state->PyBufferedReader_Type,
+ "On", reader, buffer_size);
+ if (self->reader == NULL)
+ return -1;
+
+ self->writer = (buffered *) PyObject_CallFunction(
+ (PyObject *)state->PyBufferedWriter_Type,
+ "On", writer, buffer_size);
+ if (self->writer == NULL) {
+ Py_CLEAR(self->reader);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+bufferedrwpair_traverse(rwpair *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->dict);
+ Py_VISIT(self->reader);
+ Py_VISIT(self->writer);
+ return 0;
+}
+
+static int
+bufferedrwpair_clear(rwpair *self)
+{
+ Py_CLEAR(self->reader);
+ Py_CLEAR(self->writer);
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+bufferedrwpair_dealloc(rwpair *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ _PyObject_GC_UNTRACK(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)self);
+ (void)bufferedrwpair_clear(self);
+ tp->tp_free((PyObject *) self);
+ Py_DECREF(tp);
+}
+
+static PyObject *
+_forward_call(buffered *self, PyObject *name, PyObject *args)
+{
+ PyObject *func, *ret;
+ if (self == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on uninitialized object");
+ return NULL;
+ }
+
+ func = PyObject_GetAttr((PyObject *)self, name);
+ if (func == NULL) {
+ PyErr_SetObject(PyExc_AttributeError, name);
+ return NULL;
+ }
+
+ ret = PyObject_CallObject(func, args);
+ Py_DECREF(func);
+ return ret;
+}
+
+static PyObject *
+bufferedrwpair_read(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &_Py_ID(read), args);
+}
+
+static PyObject *
+bufferedrwpair_peek(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &_Py_ID(peek), args);
+}
+
+static PyObject *
+bufferedrwpair_read1(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &_Py_ID(read1), args);
+}
+
+static PyObject *
+bufferedrwpair_readinto(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &_Py_ID(readinto), args);
+}
+
+static PyObject *
+bufferedrwpair_readinto1(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &_Py_ID(readinto1), args);
+}
+
+static PyObject *
+bufferedrwpair_write(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->writer, &_Py_ID(write), args);
+}
+
+static PyObject *
+bufferedrwpair_flush(rwpair *self, PyObject *Py_UNUSED(ignored))
+{
+ return _forward_call(self->writer, &_Py_ID(flush), NULL);
+}
+
+static PyObject *
+bufferedrwpair_readable(rwpair *self, PyObject *Py_UNUSED(ignored))
+{
+ return _forward_call(self->reader, &_Py_ID(readable), NULL);
+}
+
+static PyObject *
+bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored))
+{
+ return _forward_call(self->writer, &_Py_ID(writable), NULL);
+}
+
+static PyObject *
+bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *exc = NULL;
+ PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
+ if (ret == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
+ Py_DECREF(ret);
+ }
+ ret = _forward_call(self->reader, &_Py_ID(close), NULL);
+ if (exc != NULL) {
+ _PyErr_ChainExceptions1(exc);
+ Py_CLEAR(ret);
+ }
+ return ret;
+}
+
+static PyObject *
+bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL);
+
+ if (ret != Py_False) {
+ /* either True or exception */
+ return ret;
+ }
+ Py_DECREF(ret);
+
+ return _forward_call(self->reader, &_Py_ID(isatty), NULL);
+}
+
+static PyObject *
+bufferedrwpair_closed_get(rwpair *self, void *context)
+{
+ if (self->writer == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "the BufferedRWPair object is being garbage-collected");
+ return NULL;
+ }
+ return PyObject_GetAttr((PyObject *) self->writer, &_Py_ID(closed));
+}
+
+
+/*
+ * BufferedRandom
+ */
+
+/*[clinic input]
+_io.BufferedRandom.__init__
+ raw: object
+ buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
+
+A buffered interface to random access streams.
+
+The constructor creates a reader and writer for a seekable stream,
+raw, given in the first argument. If the buffer_size is omitted it
+defaults to DEFAULT_BUFFER_SIZE.
+[clinic start generated code]*/
+
+static int
+_io_BufferedRandom___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size)
+/*[clinic end generated code: output=d3d64eb0f64e64a3 input=a4e818fb86d0e50c]*/
+{
+ self->ok = 0;
+ self->detached = 0;
+
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ if (_PyIOBase_check_seekable(state, raw, Py_True) == NULL) {
+ return -1;
+ }
+ if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) {
+ return -1;
+ }
+ if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) {
+ return -1;
+ }
+
+ Py_INCREF(raw);
+ Py_XSETREF(self->raw, raw);
+ self->buffer_size = buffer_size;
+ self->readable = 1;
+ self->writable = 1;
+
+ if (_buffered_init(self) < 0)
+ return -1;
+ _bufferedreader_reset_buf(self);
+ _bufferedwriter_reset_buf(self);
+ self->pos = 0;
+
+ self->fast_closed_checks = (Py_IS_TYPE(self, state->PyBufferedRandom_Type) &&
+ Py_IS_TYPE(raw, state->PyFileIO_Type));
+
+ self->ok = 1;
+ return 0;
+}
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/bufferedio.c.h"
+#undef clinic_state
+
+static PyMethodDef bufferediobase_methods[] = {
+ _IO__BUFFEREDIOBASE_DETACH_METHODDEF
+ _IO__BUFFEREDIOBASE_READ_METHODDEF
+ _IO__BUFFEREDIOBASE_READ1_METHODDEF
+ _IO__BUFFEREDIOBASE_READINTO_METHODDEF
+ _IO__BUFFEREDIOBASE_READINTO1_METHODDEF
+ _IO__BUFFEREDIOBASE_WRITE_METHODDEF
+ {NULL, NULL}
+};
+
+static PyType_Slot bufferediobase_slots[] = {
+ {Py_tp_doc, (void *)bufferediobase_doc},
+ {Py_tp_methods, bufferediobase_methods},
+ {0, NULL},
+};
+
+/* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */
+PyType_Spec bufferediobase_spec = {
+ .name = "_io._BufferedIOBase",
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bufferediobase_slots,
+};
+
+static PyMethodDef bufferedreader_methods[] = {
+ /* BufferedIOMixin methods */
+ _IO__BUFFERED_DETACH_METHODDEF
+ _IO__BUFFERED_SIMPLE_FLUSH_METHODDEF
+ _IO__BUFFERED_CLOSE_METHODDEF
+ _IO__BUFFERED_SEEKABLE_METHODDEF
+ _IO__BUFFERED_READABLE_METHODDEF
+ _IO__BUFFERED_FILENO_METHODDEF
+ _IO__BUFFERED_ISATTY_METHODDEF
+ _IO__BUFFERED__DEALLOC_WARN_METHODDEF
+
+ _IO__BUFFERED_READ_METHODDEF
+ _IO__BUFFERED_PEEK_METHODDEF
+ _IO__BUFFERED_READ1_METHODDEF
+ _IO__BUFFERED_READINTO_METHODDEF
+ _IO__BUFFERED_READINTO1_METHODDEF
+ _IO__BUFFERED_READLINE_METHODDEF
+ _IO__BUFFERED_SEEK_METHODDEF
+ _IO__BUFFERED_TELL_METHODDEF
+ _IO__BUFFERED_TRUNCATE_METHODDEF
+ _IO__BUFFERED___SIZEOF___METHODDEF
+
+ {"__reduce__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {"__reduce_ex__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {NULL, NULL}
+};
+
+static PyMemberDef bufferedreader_members[] = {
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
+ {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(buffered, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(buffered, dict), READONLY},
+ {NULL}
+};
+
+static PyGetSetDef bufferedreader_getset[] = {
+ {"closed", (getter)buffered_closed_get, NULL, NULL},
+ {"name", (getter)buffered_name_get, NULL, NULL},
+ {"mode", (getter)buffered_mode_get, NULL, NULL},
+ {NULL}
+};
+
+
+static PyType_Slot bufferedreader_slots[] = {
+ {Py_tp_dealloc, buffered_dealloc},
+ {Py_tp_repr, buffered_repr},
+ {Py_tp_doc, (void *)_io_BufferedReader___init____doc__},
+ {Py_tp_traverse, buffered_traverse},
+ {Py_tp_clear, buffered_clear},
+ {Py_tp_iternext, buffered_iternext},
+ {Py_tp_methods, bufferedreader_methods},
+ {Py_tp_members, bufferedreader_members},
+ {Py_tp_getset, bufferedreader_getset},
+ {Py_tp_init, _io_BufferedReader___init__},
+ {0, NULL},
+};
+
+PyType_Spec bufferedreader_spec = {
+ .name = "_io.BufferedReader",
+ .basicsize = sizeof(buffered),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bufferedreader_slots,
+};
+
+static PyMethodDef bufferedwriter_methods[] = {
+ /* BufferedIOMixin methods */
+ _IO__BUFFERED_CLOSE_METHODDEF
+ _IO__BUFFERED_DETACH_METHODDEF
+ _IO__BUFFERED_SEEKABLE_METHODDEF
+ _IO__BUFFERED_WRITABLE_METHODDEF
+ _IO__BUFFERED_FILENO_METHODDEF
+ _IO__BUFFERED_ISATTY_METHODDEF
+ _IO__BUFFERED__DEALLOC_WARN_METHODDEF
+
+ _IO_BUFFEREDWRITER_WRITE_METHODDEF
+ _IO__BUFFERED_TRUNCATE_METHODDEF
+ _IO__BUFFERED_FLUSH_METHODDEF
+ _IO__BUFFERED_SEEK_METHODDEF
+ _IO__BUFFERED_TELL_METHODDEF
+ _IO__BUFFERED___SIZEOF___METHODDEF
+
+ {"__reduce__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {"__reduce_ex__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {NULL, NULL}
+};
+
+static PyMemberDef bufferedwriter_members[] = {
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
+ {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(buffered, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(buffered, dict), READONLY},
+ {NULL}
+};
+
+static PyGetSetDef bufferedwriter_getset[] = {
+ {"closed", (getter)buffered_closed_get, NULL, NULL},
+ {"name", (getter)buffered_name_get, NULL, NULL},
+ {"mode", (getter)buffered_mode_get, NULL, NULL},
+ {NULL}
+};
+
+
+static PyType_Slot bufferedwriter_slots[] = {
+ {Py_tp_dealloc, buffered_dealloc},
+ {Py_tp_repr, buffered_repr},
+ {Py_tp_doc, (void *)_io_BufferedWriter___init____doc__},
+ {Py_tp_traverse, buffered_traverse},
+ {Py_tp_clear, buffered_clear},
+ {Py_tp_methods, bufferedwriter_methods},
+ {Py_tp_members, bufferedwriter_members},
+ {Py_tp_getset, bufferedwriter_getset},
+ {Py_tp_init, _io_BufferedWriter___init__},
+ {0, NULL},
+};
+
+PyType_Spec bufferedwriter_spec = {
+ .name = "_io.BufferedWriter",
+ .basicsize = sizeof(buffered),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bufferedwriter_slots,
+};
+
+static PyMethodDef bufferedrwpair_methods[] = {
+ {"read", (PyCFunction)bufferedrwpair_read, METH_VARARGS},
+ {"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS},
+ {"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS},
+ {"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS},
+ {"readinto1", (PyCFunction)bufferedrwpair_readinto1, METH_VARARGS},
+
+ {"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS},
+ {"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS},
+
+ {"readable", (PyCFunction)bufferedrwpair_readable, METH_NOARGS},
+ {"writable", (PyCFunction)bufferedrwpair_writable, METH_NOARGS},
+
+ {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS},
+ {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS},
+
+ {NULL, NULL}
+};
+
+static PyMemberDef bufferedrwpair_members[] = {
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(rwpair, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(rwpair, dict), READONLY},
+ {NULL}
+};
+
+static PyGetSetDef bufferedrwpair_getset[] = {
+ {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL},
+ {NULL}
+};
+
+static PyType_Slot bufferedrwpair_slots[] = {
+ {Py_tp_dealloc, bufferedrwpair_dealloc},
+ {Py_tp_doc, (void *)_io_BufferedRWPair___init____doc__},
+ {Py_tp_traverse, bufferedrwpair_traverse},
+ {Py_tp_clear, bufferedrwpair_clear},
+ {Py_tp_methods, bufferedrwpair_methods},
+ {Py_tp_members, bufferedrwpair_members},
+ {Py_tp_getset, bufferedrwpair_getset},
+ {Py_tp_init, _io_BufferedRWPair___init__},
+ {0, NULL},
+};
+
+PyType_Spec bufferedrwpair_spec = {
+ .name = "_io.BufferedRWPair",
+ .basicsize = sizeof(rwpair),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bufferedrwpair_slots,
+};
+
+
+static PyMethodDef bufferedrandom_methods[] = {
+ /* BufferedIOMixin methods */
+ _IO__BUFFERED_CLOSE_METHODDEF
+ _IO__BUFFERED_DETACH_METHODDEF
+ _IO__BUFFERED_SEEKABLE_METHODDEF
+ _IO__BUFFERED_READABLE_METHODDEF
+ _IO__BUFFERED_WRITABLE_METHODDEF
+ _IO__BUFFERED_FILENO_METHODDEF
+ _IO__BUFFERED_ISATTY_METHODDEF
+ _IO__BUFFERED__DEALLOC_WARN_METHODDEF
+
+ _IO__BUFFERED_FLUSH_METHODDEF
+
+ _IO__BUFFERED_SEEK_METHODDEF
+ _IO__BUFFERED_TELL_METHODDEF
+ _IO__BUFFERED_TRUNCATE_METHODDEF
+ _IO__BUFFERED_READ_METHODDEF
+ _IO__BUFFERED_READ1_METHODDEF
+ _IO__BUFFERED_READINTO_METHODDEF
+ _IO__BUFFERED_READINTO1_METHODDEF
+ _IO__BUFFERED_READLINE_METHODDEF
+ _IO__BUFFERED_PEEK_METHODDEF
+ _IO_BUFFEREDWRITER_WRITE_METHODDEF
+ _IO__BUFFERED___SIZEOF___METHODDEF
+
+ {"__reduce__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {"__reduce_ex__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {NULL, NULL}
+};
+
+static PyMemberDef bufferedrandom_members[] = {
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
+ {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(buffered, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(buffered, dict), READONLY},
+ {NULL}
+};
+
+static PyGetSetDef bufferedrandom_getset[] = {
+ {"closed", (getter)buffered_closed_get, NULL, NULL},
+ {"name", (getter)buffered_name_get, NULL, NULL},
+ {"mode", (getter)buffered_mode_get, NULL, NULL},
+ {NULL}
+};
+
+
+static PyType_Slot bufferedrandom_slots[] = {
+ {Py_tp_dealloc, buffered_dealloc},
+ {Py_tp_repr, buffered_repr},
+ {Py_tp_doc, (void *)_io_BufferedRandom___init____doc__},
+ {Py_tp_traverse, buffered_traverse},
+ {Py_tp_clear, buffered_clear},
+ {Py_tp_iternext, buffered_iternext},
+ {Py_tp_methods, bufferedrandom_methods},
+ {Py_tp_members, bufferedrandom_members},
+ {Py_tp_getset, bufferedrandom_getset},
+ {Py_tp_init, _io_BufferedRandom___init__},
+ {0, NULL},
+};
+
+PyType_Spec bufferedrandom_spec = {
+ .name = "_io.BufferedRandom",
+ .basicsize = sizeof(buffered),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bufferedrandom_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/bytesio.c b/contrib/tools/python3/Modules/_io/bytesio.c
new file mode 100644
index 0000000000..9459affa4a
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/bytesio.c
@@ -0,0 +1,1133 @@
+#include "Python.h"
+#include "pycore_object.h"
+#include <stddef.h> // offsetof()
+#include "_iomodule.h"
+
+/*[clinic input]
+module _io
+class _io.BytesIO "bytesio *" "clinic_state()->PyBytesIO_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=48ede2f330f847c3]*/
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *buf;
+ Py_ssize_t pos;
+ Py_ssize_t string_size;
+ PyObject *dict;
+ PyObject *weakreflist;
+ Py_ssize_t exports;
+} bytesio;
+
+typedef struct {
+ PyObject_HEAD
+ bytesio *source;
+} bytesiobuf;
+
+/* The bytesio object can be in three states:
+ * Py_REFCNT(buf) == 1, exports == 0.
+ * Py_REFCNT(buf) > 1. exports == 0,
+ first modification or export causes the internal buffer copying.
+ * exports > 0. Py_REFCNT(buf) == 1, any modifications are forbidden.
+*/
+
+static int
+check_closed(bytesio *self)
+{
+ if (self->buf == NULL) {
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
+ return 1;
+ }
+ return 0;
+}
+
+static int
+check_exports(bytesio *self)
+{
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "Existing exports of data: object cannot be re-sized");
+ return 1;
+ }
+ return 0;
+}
+
+#define CHECK_CLOSED(self) \
+ if (check_closed(self)) { \
+ return NULL; \
+ }
+
+#define CHECK_EXPORTS(self) \
+ if (check_exports(self)) { \
+ return NULL; \
+ }
+
+#define SHARED_BUF(self) (Py_REFCNT((self)->buf) > 1)
+
+
+/* Internal routine to get a line from the buffer of a BytesIO
+ object. Returns the length between the current position to the
+ next newline character. */
+static Py_ssize_t
+scan_eol(bytesio *self, Py_ssize_t len)
+{
+ const char *start, *n;
+ Py_ssize_t maxlen;
+
+ assert(self->buf != NULL);
+ assert(self->pos >= 0);
+
+ if (self->pos >= self->string_size)
+ return 0;
+
+ /* Move to the end of the line, up to the end of the string, s. */
+ maxlen = self->string_size - self->pos;
+ if (len < 0 || len > maxlen)
+ len = maxlen;
+
+ if (len) {
+ start = PyBytes_AS_STRING(self->buf) + self->pos;
+ n = memchr(start, '\n', len);
+ if (n)
+ /* Get the length from the current position to the end of
+ the line. */
+ len = n - start + 1;
+ }
+ assert(len >= 0);
+ assert(self->pos < PY_SSIZE_T_MAX - len);
+
+ return len;
+}
+
+/* Internal routine for detaching the shared buffer of BytesIO objects.
+ The caller should ensure that the 'size' argument is non-negative and
+ not lesser than self->string_size. Returns 0 on success, -1 otherwise. */
+static int
+unshare_buffer(bytesio *self, size_t size)
+{
+ PyObject *new_buf;
+ assert(SHARED_BUF(self));
+ assert(self->exports == 0);
+ assert(size >= (size_t)self->string_size);
+ new_buf = PyBytes_FromStringAndSize(NULL, size);
+ if (new_buf == NULL)
+ return -1;
+ memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
+ self->string_size);
+ Py_SETREF(self->buf, new_buf);
+ return 0;
+}
+
+/* Internal routine for changing the size of the buffer of BytesIO objects.
+ The caller should ensure that the 'size' argument is non-negative. Returns
+ 0 on success, -1 otherwise. */
+static int
+resize_buffer(bytesio *self, size_t size)
+{
+ assert(self->buf != NULL);
+ assert(self->exports == 0);
+
+ /* Here, unsigned types are used to avoid dealing with signed integer
+ overflow, which is undefined in C. */
+ size_t alloc = PyBytes_GET_SIZE(self->buf);
+
+ /* For simplicity, stay in the range of the signed type. Anyway, Python
+ doesn't allow strings to be longer than this. */
+ if (size > PY_SSIZE_T_MAX)
+ goto overflow;
+
+ if (size < alloc / 2) {
+ /* Major downsize; resize down to exact size. */
+ alloc = size + 1;
+ }
+ else if (size < alloc) {
+ /* Within allocated size; quick exit */
+ return 0;
+ }
+ else if (size <= alloc * 1.125) {
+ /* Moderate upsize; overallocate similar to list_resize() */
+ alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
+ }
+ else {
+ /* Major upsize; resize up to exact size */
+ alloc = size + 1;
+ }
+
+ if (alloc > ((size_t)-1) / sizeof(char))
+ goto overflow;
+
+ if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, alloc) < 0)
+ return -1;
+ }
+ else {
+ if (_PyBytes_Resize(&self->buf, alloc) < 0)
+ return -1;
+ }
+
+ return 0;
+
+ overflow:
+ PyErr_SetString(PyExc_OverflowError,
+ "new buffer size too large");
+ return -1;
+}
+
+/* Internal routine for writing a string of bytes to the buffer of a BytesIO
+ object. Returns the number of bytes written, or -1 on error.
+ Inlining is disabled because it's significantly decreases performance
+ of writelines() in PGO build. */
+Py_NO_INLINE static Py_ssize_t
+write_bytes(bytesio *self, PyObject *b)
+{
+ if (check_closed(self)) {
+ return -1;
+ }
+ if (check_exports(self)) {
+ return -1;
+ }
+
+ Py_buffer buf;
+ if (PyObject_GetBuffer(b, &buf, PyBUF_CONTIG_RO) < 0) {
+ return -1;
+ }
+ Py_ssize_t len = buf.len;
+ if (len == 0) {
+ goto done;
+ }
+
+ assert(self->pos >= 0);
+ size_t endpos = (size_t)self->pos + len;
+ if (endpos > (size_t)PyBytes_GET_SIZE(self->buf)) {
+ if (resize_buffer(self, endpos) < 0) {
+ len = -1;
+ goto done;
+ }
+ }
+ else if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, Py_MAX(endpos, (size_t)self->string_size)) < 0) {
+ len = -1;
+ goto done;
+ }
+ }
+
+ if (self->pos > self->string_size) {
+ /* In case of overseek, pad with null bytes the buffer region between
+ the end of stream and the current position.
+
+ 0 lo string_size hi
+ | |<---used--->|<----------available----------->|
+ | | <--to pad-->|<---to write---> |
+ 0 buf position
+ */
+ memset(PyBytes_AS_STRING(self->buf) + self->string_size, '\0',
+ (self->pos - self->string_size) * sizeof(char));
+ }
+
+ /* Copy the data to the internal buffer, overwriting some of the existing
+ data if self->pos < self->string_size. */
+ memcpy(PyBytes_AS_STRING(self->buf) + self->pos, buf.buf, len);
+ self->pos = endpos;
+
+ /* Set the new length of the internal string if it has changed. */
+ if ((size_t)self->string_size < endpos) {
+ self->string_size = endpos;
+ }
+
+ done:
+ PyBuffer_Release(&buf);
+ return len;
+}
+
+static PyObject *
+bytesio_get_closed(bytesio *self, void *Py_UNUSED(ignored))
+{
+ if (self->buf == NULL) {
+ Py_RETURN_TRUE;
+ }
+ else {
+ Py_RETURN_FALSE;
+ }
+}
+
+/*[clinic input]
+_io.BytesIO.readable
+
+Returns True if the IO object can be read.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_readable_impl(bytesio *self)
+/*[clinic end generated code: output=4e93822ad5b62263 input=96c5d0cccfb29f5c]*/
+{
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/*[clinic input]
+_io.BytesIO.writable
+
+Returns True if the IO object can be written.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_writable_impl(bytesio *self)
+/*[clinic end generated code: output=64ff6a254b1150b8 input=700eed808277560a]*/
+{
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/*[clinic input]
+_io.BytesIO.seekable
+
+Returns True if the IO object can be seeked.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_seekable_impl(bytesio *self)
+/*[clinic end generated code: output=6b417f46dcc09b56 input=9421f65627a344dd]*/
+{
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/*[clinic input]
+_io.BytesIO.flush
+
+Does nothing.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_flush_impl(bytesio *self)
+/*[clinic end generated code: output=187e3d781ca134a0 input=561ea490be4581a7]*/
+{
+ CHECK_CLOSED(self);
+ Py_RETURN_NONE;
+}
+
+/*[clinic input]
+_io.BytesIO.getbuffer
+
+ cls: defining_class
+ /
+
+Get a read-write view over the contents of the BytesIO object.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_getbuffer_impl(bytesio *self, PyTypeObject *cls)
+/*[clinic end generated code: output=045091d7ce87fe4e input=0668fbb48f95dffa]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ PyTypeObject *type = state->PyBytesIOBuffer_Type;
+ bytesiobuf *buf;
+ PyObject *view;
+
+ CHECK_CLOSED(self);
+
+ buf = (bytesiobuf *) type->tp_alloc(type, 0);
+ if (buf == NULL)
+ return NULL;
+ buf->source = (bytesio*)Py_NewRef(self);
+ view = PyMemoryView_FromObject((PyObject *) buf);
+ Py_DECREF(buf);
+ return view;
+}
+
+/*[clinic input]
+_io.BytesIO.getvalue
+
+Retrieve the entire contents of the BytesIO object.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_getvalue_impl(bytesio *self)
+/*[clinic end generated code: output=b3f6a3233c8fd628 input=4b403ac0af3973ed]*/
+{
+ CHECK_CLOSED(self);
+ if (self->string_size <= 1 || self->exports > 0)
+ return PyBytes_FromStringAndSize(PyBytes_AS_STRING(self->buf),
+ self->string_size);
+
+ if (self->string_size != PyBytes_GET_SIZE(self->buf)) {
+ if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, self->string_size) < 0)
+ return NULL;
+ }
+ else {
+ if (_PyBytes_Resize(&self->buf, self->string_size) < 0)
+ return NULL;
+ }
+ }
+ return Py_NewRef(self->buf);
+}
+
+/*[clinic input]
+_io.BytesIO.isatty
+
+Always returns False.
+
+BytesIO objects are not connected to a TTY-like device.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_isatty_impl(bytesio *self)
+/*[clinic end generated code: output=df67712e669f6c8f input=6f97f0985d13f827]*/
+{
+ CHECK_CLOSED(self);
+ Py_RETURN_FALSE;
+}
+
+/*[clinic input]
+_io.BytesIO.tell
+
+Current file position, an integer.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_tell_impl(bytesio *self)
+/*[clinic end generated code: output=b54b0f93cd0e5e1d input=b106adf099cb3657]*/
+{
+ CHECK_CLOSED(self);
+ return PyLong_FromSsize_t(self->pos);
+}
+
+static PyObject *
+read_bytes(bytesio *self, Py_ssize_t size)
+{
+ const char *output;
+
+ assert(self->buf != NULL);
+ assert(size <= self->string_size);
+ if (size > 1 &&
+ self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
+ self->exports == 0) {
+ self->pos += size;
+ return Py_NewRef(self->buf);
+ }
+
+ output = PyBytes_AS_STRING(self->buf) + self->pos;
+ self->pos += size;
+ return PyBytes_FromStringAndSize(output, size);
+}
+
+/*[clinic input]
+_io.BytesIO.read
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read at most size bytes, returned as a bytes object.
+
+If the size argument is negative, read until EOF is reached.
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_read_impl(bytesio *self, Py_ssize_t size)
+/*[clinic end generated code: output=9cc025f21c75bdd2 input=74344a39f431c3d7]*/
+{
+ Py_ssize_t n;
+
+ CHECK_CLOSED(self);
+
+ /* adjust invalid sizes */
+ n = self->string_size - self->pos;
+ if (size < 0 || size > n) {
+ size = n;
+ if (size < 0)
+ size = 0;
+ }
+
+ return read_bytes(self, size);
+}
+
+
+/*[clinic input]
+_io.BytesIO.read1
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read at most size bytes, returned as a bytes object.
+
+If the size argument is negative or omitted, read until EOF is reached.
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size)
+/*[clinic end generated code: output=d0f843285aa95f1c input=440a395bf9129ef5]*/
+{
+ return _io_BytesIO_read_impl(self, size);
+}
+
+/*[clinic input]
+_io.BytesIO.readline
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Next line from the file, as a bytes object.
+
+Retain newline. A non-negative size argument limits the maximum
+number of bytes to return (an incomplete line may be returned then).
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size)
+/*[clinic end generated code: output=4bff3c251df8ffcd input=e7c3fbd1744e2783]*/
+{
+ Py_ssize_t n;
+
+ CHECK_CLOSED(self);
+
+ n = scan_eol(self, size);
+
+ return read_bytes(self, n);
+}
+
+/*[clinic input]
+_io.BytesIO.readlines
+ size as arg: object = None
+ /
+
+List of bytes objects, each a line from the file.
+
+Call readline() repeatedly and return a list of the lines so read.
+The optional size argument, if given, is an approximate bound on the
+total number of bytes in the lines returned.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_readlines_impl(bytesio *self, PyObject *arg)
+/*[clinic end generated code: output=09b8e34c880808ff input=691aa1314f2c2a87]*/
+{
+ Py_ssize_t maxsize, size, n;
+ PyObject *result, *line;
+ const char *output;
+
+ CHECK_CLOSED(self);
+
+ if (PyLong_Check(arg)) {
+ maxsize = PyLong_AsSsize_t(arg);
+ if (maxsize == -1 && PyErr_Occurred())
+ return NULL;
+ }
+ else if (arg == Py_None) {
+ /* No size limit, by default. */
+ maxsize = -1;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
+ Py_TYPE(arg)->tp_name);
+ return NULL;
+ }
+
+ size = 0;
+ result = PyList_New(0);
+ if (!result)
+ return NULL;
+
+ output = PyBytes_AS_STRING(self->buf) + self->pos;
+ while ((n = scan_eol(self, -1)) != 0) {
+ self->pos += n;
+ line = PyBytes_FromStringAndSize(output, n);
+ if (!line)
+ goto on_error;
+ if (PyList_Append(result, line) == -1) {
+ Py_DECREF(line);
+ goto on_error;
+ }
+ Py_DECREF(line);
+ size += n;
+ if (maxsize > 0 && size >= maxsize)
+ break;
+ output += n;
+ }
+ return result;
+
+ on_error:
+ Py_DECREF(result);
+ return NULL;
+}
+
+/*[clinic input]
+_io.BytesIO.readinto
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+
+Read bytes into buffer.
+
+Returns number of bytes read (0 for EOF), or None if the object
+is set not to block and has no data to read.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer)
+/*[clinic end generated code: output=a5d407217dcf0639 input=1424d0fdce857919]*/
+{
+ Py_ssize_t len, n;
+
+ CHECK_CLOSED(self);
+
+ /* adjust invalid sizes */
+ len = buffer->len;
+ n = self->string_size - self->pos;
+ if (len > n) {
+ len = n;
+ if (len < 0)
+ len = 0;
+ }
+
+ memcpy(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
+ assert(self->pos + len < PY_SSIZE_T_MAX);
+ assert(len >= 0);
+ self->pos += len;
+
+ return PyLong_FromSsize_t(len);
+}
+
+/*[clinic input]
+_io.BytesIO.truncate
+ size: Py_ssize_t(accept={int, NoneType}, c_default="self->pos") = None
+ /
+
+Truncate the file to at most size bytes.
+
+Size defaults to the current file position, as returned by tell().
+The current file position is unchanged. Returns the new size.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size)
+/*[clinic end generated code: output=9ad17650c15fa09b input=423759dd42d2f7c1]*/
+{
+ CHECK_CLOSED(self);
+ CHECK_EXPORTS(self);
+
+ if (size < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "negative size value %zd", size);
+ return NULL;
+ }
+
+ if (size < self->string_size) {
+ self->string_size = size;
+ if (resize_buffer(self, size) < 0)
+ return NULL;
+ }
+
+ return PyLong_FromSsize_t(size);
+}
+
+static PyObject *
+bytesio_iternext(bytesio *self)
+{
+ Py_ssize_t n;
+
+ CHECK_CLOSED(self);
+
+ n = scan_eol(self, -1);
+
+ if (n == 0)
+ return NULL;
+
+ return read_bytes(self, n);
+}
+
+/*[clinic input]
+_io.BytesIO.seek
+ pos: Py_ssize_t
+ whence: int = 0
+ /
+
+Change stream position.
+
+Seek to byte offset pos relative to position indicated by whence:
+ 0 Start of stream (the default). pos should be >= 0;
+ 1 Current position - pos may be negative;
+ 2 End of stream - pos usually negative.
+Returns the new absolute position.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence)
+/*[clinic end generated code: output=c26204a68e9190e4 input=1e875e6ebc652948]*/
+{
+ CHECK_CLOSED(self);
+
+ if (pos < 0 && whence == 0) {
+ PyErr_Format(PyExc_ValueError,
+ "negative seek value %zd", pos);
+ return NULL;
+ }
+
+ /* whence = 0: offset relative to beginning of the string.
+ whence = 1: offset relative to current position.
+ whence = 2: offset relative the end of the string. */
+ if (whence == 1) {
+ if (pos > PY_SSIZE_T_MAX - self->pos) {
+ PyErr_SetString(PyExc_OverflowError,
+ "new position too large");
+ return NULL;
+ }
+ pos += self->pos;
+ }
+ else if (whence == 2) {
+ if (pos > PY_SSIZE_T_MAX - self->string_size) {
+ PyErr_SetString(PyExc_OverflowError,
+ "new position too large");
+ return NULL;
+ }
+ pos += self->string_size;
+ }
+ else if (whence != 0) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid whence (%i, should be 0, 1 or 2)", whence);
+ return NULL;
+ }
+
+ if (pos < 0)
+ pos = 0;
+ self->pos = pos;
+
+ return PyLong_FromSsize_t(self->pos);
+}
+
+/*[clinic input]
+_io.BytesIO.write
+ b: object
+ /
+
+Write bytes to file.
+
+Return the number of bytes written.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_write(bytesio *self, PyObject *b)
+/*[clinic end generated code: output=53316d99800a0b95 input=f5ec7c8c64ed720a]*/
+{
+ Py_ssize_t n = write_bytes(self, b);
+ return n >= 0 ? PyLong_FromSsize_t(n) : NULL;
+}
+
+/*[clinic input]
+_io.BytesIO.writelines
+ lines: object
+ /
+
+Write lines to the file.
+
+Note that newlines are not added. lines can be any iterable object
+producing bytes-like objects. This is equivalent to calling write() for
+each element.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_writelines(bytesio *self, PyObject *lines)
+/*[clinic end generated code: output=7f33aa3271c91752 input=e972539176fc8fc1]*/
+{
+ PyObject *it, *item;
+
+ CHECK_CLOSED(self);
+
+ it = PyObject_GetIter(lines);
+ if (it == NULL)
+ return NULL;
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ Py_ssize_t ret = write_bytes(self, item);
+ Py_DECREF(item);
+ if (ret < 0) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ }
+ Py_DECREF(it);
+
+ /* See if PyIter_Next failed */
+ if (PyErr_Occurred())
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+/*[clinic input]
+_io.BytesIO.close
+
+Disable all I/O operations.
+[clinic start generated code]*/
+
+static PyObject *
+_io_BytesIO_close_impl(bytesio *self)
+/*[clinic end generated code: output=1471bb9411af84a0 input=37e1f55556e61f60]*/
+{
+ CHECK_EXPORTS(self);
+ Py_CLEAR(self->buf);
+ Py_RETURN_NONE;
+}
+
+/* Pickling support.
+
+ Note that only pickle protocol 2 and onward are supported since we use
+ extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
+
+ Providing support for protocol < 2 would require the __reduce_ex__ method
+ which is notably long-winded when defined properly.
+
+ For BytesIO, the implementation would similar to one coded for
+ object.__reduce_ex__, but slightly less general. To be more specific, we
+ could call bytesio_getstate directly and avoid checking for the presence of
+ a fallback __reduce__ method. However, we would still need a __newobj__
+ function to use the efficient instance representation of PEP 307.
+ */
+
+static PyObject *
+bytesio_getstate(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *initvalue = _io_BytesIO_getvalue_impl(self);
+ PyObject *dict;
+ PyObject *state;
+
+ if (initvalue == NULL)
+ return NULL;
+ if (self->dict == NULL) {
+ dict = Py_NewRef(Py_None);
+ }
+ else {
+ dict = PyDict_Copy(self->dict);
+ if (dict == NULL) {
+ Py_DECREF(initvalue);
+ return NULL;
+ }
+ }
+
+ state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
+ Py_DECREF(initvalue);
+ return state;
+}
+
+static PyObject *
+bytesio_setstate(bytesio *self, PyObject *state)
+{
+ PyObject *result;
+ PyObject *position_obj;
+ PyObject *dict;
+ Py_ssize_t pos;
+
+ assert(state != NULL);
+
+ /* We allow the state tuple to be longer than 3, because we may need
+ someday to extend the object's state without breaking
+ backward-compatibility. */
+ if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) < 3) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s.__setstate__ argument should be 3-tuple, got %.200s",
+ Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
+ return NULL;
+ }
+ CHECK_EXPORTS(self);
+ /* Reset the object to its default state. This is only needed to handle
+ the case of repeated calls to __setstate__. */
+ self->string_size = 0;
+ self->pos = 0;
+
+ /* Set the value of the internal buffer. If state[0] does not support the
+ buffer protocol, bytesio_write will raise the appropriate TypeError. */
+ result = _io_BytesIO_write(self, PyTuple_GET_ITEM(state, 0));
+ if (result == NULL)
+ return NULL;
+ Py_DECREF(result);
+
+ /* Set carefully the position value. Alternatively, we could use the seek
+ method instead of modifying self->pos directly to better protect the
+ object internal state against erroneous (or malicious) inputs. */
+ position_obj = PyTuple_GET_ITEM(state, 1);
+ if (!PyLong_Check(position_obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "second item of state must be an integer, not %.200s",
+ Py_TYPE(position_obj)->tp_name);
+ return NULL;
+ }
+ pos = PyLong_AsSsize_t(position_obj);
+ if (pos == -1 && PyErr_Occurred())
+ return NULL;
+ if (pos < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "position value cannot be negative");
+ return NULL;
+ }
+ self->pos = pos;
+
+ /* Set the dictionary of the instance variables. */
+ dict = PyTuple_GET_ITEM(state, 2);
+ if (dict != Py_None) {
+ if (!PyDict_Check(dict)) {
+ PyErr_Format(PyExc_TypeError,
+ "third item of state should be a dict, got a %.200s",
+ Py_TYPE(dict)->tp_name);
+ return NULL;
+ }
+ if (self->dict) {
+ /* Alternatively, we could replace the internal dictionary
+ completely. However, it seems more practical to just update it. */
+ if (PyDict_Update(self->dict, dict) < 0)
+ return NULL;
+ }
+ else {
+ self->dict = Py_NewRef(dict);
+ }
+ }
+
+ Py_RETURN_NONE;
+}
+
+static void
+bytesio_dealloc(bytesio *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ _PyObject_GC_UNTRACK(self);
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "deallocated BytesIO object has exported buffers");
+ PyErr_Print();
+ }
+ Py_CLEAR(self->buf);
+ Py_CLEAR(self->dict);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) self);
+ tp->tp_free(self);
+ Py_DECREF(tp);
+}
+
+static PyObject *
+bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ bytesio *self;
+
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = (bytesio *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ /* tp_alloc initializes all the fields to zero. So we don't have to
+ initialize them here. */
+
+ self->buf = PyBytes_FromStringAndSize(NULL, 0);
+ if (self->buf == NULL) {
+ Py_DECREF(self);
+ return PyErr_NoMemory();
+ }
+
+ return (PyObject *)self;
+}
+
+/*[clinic input]
+_io.BytesIO.__init__
+ initial_bytes as initvalue: object(c_default="NULL") = b''
+
+Buffered I/O implementation using an in-memory bytes buffer.
+[clinic start generated code]*/
+
+static int
+_io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
+/*[clinic end generated code: output=65c0c51e24c5b621 input=aac7f31b67bf0fb6]*/
+{
+ /* In case, __init__ is called multiple times. */
+ self->string_size = 0;
+ self->pos = 0;
+
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "Existing exports of data: object cannot be re-sized");
+ return -1;
+ }
+ if (initvalue && initvalue != Py_None) {
+ if (PyBytes_CheckExact(initvalue)) {
+ Py_XSETREF(self->buf, Py_NewRef(initvalue));
+ self->string_size = PyBytes_GET_SIZE(initvalue);
+ }
+ else {
+ PyObject *res;
+ res = _io_BytesIO_write(self, initvalue);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ self->pos = 0;
+ }
+ }
+
+ return 0;
+}
+
+static PyObject *
+bytesio_sizeof(bytesio *self, void *unused)
+{
+ size_t res = _PyObject_SIZE(Py_TYPE(self));
+ if (self->buf && !SHARED_BUF(self)) {
+ size_t s = _PySys_GetSizeOf(self->buf);
+ if (s == (size_t)-1) {
+ return NULL;
+ }
+ res += s;
+ }
+ return PyLong_FromSize_t(res);
+}
+
+static int
+bytesio_traverse(bytesio *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->dict);
+ Py_VISIT(self->buf);
+ return 0;
+}
+
+static int
+bytesio_clear(bytesio *self)
+{
+ Py_CLEAR(self->dict);
+ if (self->exports == 0) {
+ Py_CLEAR(self->buf);
+ }
+ return 0;
+}
+
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/bytesio.c.h"
+#undef clinic_state
+
+static PyGetSetDef bytesio_getsetlist[] = {
+ {"closed", (getter)bytesio_get_closed, NULL,
+ "True if the file is closed."},
+ {NULL}, /* sentinel */
+};
+
+static struct PyMethodDef bytesio_methods[] = {
+ _IO_BYTESIO_READABLE_METHODDEF
+ _IO_BYTESIO_SEEKABLE_METHODDEF
+ _IO_BYTESIO_WRITABLE_METHODDEF
+ _IO_BYTESIO_CLOSE_METHODDEF
+ _IO_BYTESIO_FLUSH_METHODDEF
+ _IO_BYTESIO_ISATTY_METHODDEF
+ _IO_BYTESIO_TELL_METHODDEF
+ _IO_BYTESIO_WRITE_METHODDEF
+ _IO_BYTESIO_WRITELINES_METHODDEF
+ _IO_BYTESIO_READ1_METHODDEF
+ _IO_BYTESIO_READINTO_METHODDEF
+ _IO_BYTESIO_READLINE_METHODDEF
+ _IO_BYTESIO_READLINES_METHODDEF
+ _IO_BYTESIO_READ_METHODDEF
+ _IO_BYTESIO_GETBUFFER_METHODDEF
+ _IO_BYTESIO_GETVALUE_METHODDEF
+ _IO_BYTESIO_SEEK_METHODDEF
+ _IO_BYTESIO_TRUNCATE_METHODDEF
+ {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
+ {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
+ {"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyMemberDef bytesio_members[] = {
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(bytesio, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(bytesio, dict), READONLY},
+ {NULL}
+};
+
+static PyType_Slot bytesio_slots[] = {
+ {Py_tp_dealloc, bytesio_dealloc},
+ {Py_tp_doc, (void *)_io_BytesIO___init____doc__},
+ {Py_tp_traverse, bytesio_traverse},
+ {Py_tp_clear, bytesio_clear},
+ {Py_tp_iter, PyObject_SelfIter},
+ {Py_tp_iternext, bytesio_iternext},
+ {Py_tp_methods, bytesio_methods},
+ {Py_tp_members, bytesio_members},
+ {Py_tp_getset, bytesio_getsetlist},
+ {Py_tp_init, _io_BytesIO___init__},
+ {Py_tp_new, bytesio_new},
+ {0, NULL},
+};
+
+PyType_Spec bytesio_spec = {
+ .name = "_io.BytesIO",
+ .basicsize = sizeof(bytesio),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = bytesio_slots,
+};
+
+/*
+ * Implementation of the small intermediate object used by getbuffer().
+ * getbuffer() returns a memoryview over this object, which should make it
+ * invisible from Python code.
+ */
+
+static int
+bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
+{
+ bytesio *b = (bytesio *) obj->source;
+
+ if (view == NULL) {
+ PyErr_SetString(PyExc_BufferError,
+ "bytesiobuf_getbuffer: view==NULL argument is obsolete");
+ return -1;
+ }
+ if (b->exports == 0 && SHARED_BUF(b)) {
+ if (unshare_buffer(b, b->string_size) < 0)
+ return -1;
+ }
+
+ /* cannot fail if view != NULL and readonly == 0 */
+ (void)PyBuffer_FillInfo(view, (PyObject*)obj,
+ PyBytes_AS_STRING(b->buf), b->string_size,
+ 0, flags);
+ b->exports++;
+ return 0;
+}
+
+static void
+bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
+{
+ bytesio *b = (bytesio *) obj->source;
+ b->exports--;
+}
+
+static int
+bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->source);
+ return 0;
+}
+
+static void
+bytesiobuf_dealloc(bytesiobuf *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(self);
+ Py_CLEAR(self->source);
+ tp->tp_free(self);
+ Py_DECREF(tp);
+}
+
+static PyType_Slot bytesiobuf_slots[] = {
+ {Py_tp_dealloc, bytesiobuf_dealloc},
+ {Py_tp_traverse, bytesiobuf_traverse},
+
+ // Buffer protocol
+ {Py_bf_getbuffer, bytesiobuf_getbuffer},
+ {Py_bf_releasebuffer, bytesiobuf_releasebuffer},
+ {0, NULL},
+};
+
+PyType_Spec bytesiobuf_spec = {
+ .name = "_io._BytesIOBuffer",
+ .basicsize = sizeof(bytesiobuf),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
+ .slots = bytesiobuf_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/clinic/_iomodule.c.h b/contrib/tools/python3/Modules/_io/clinic/_iomodule.c.h
new file mode 100644
index 0000000000..4d76e333b0
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/_iomodule.c.h
@@ -0,0 +1,410 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io_open__doc__,
+"open($module, /, file, mode=\'r\', buffering=-1, encoding=None,\n"
+" errors=None, newline=None, closefd=True, opener=None)\n"
+"--\n"
+"\n"
+"Open file and return a stream. Raise OSError upon failure.\n"
+"\n"
+"file is either a text or byte string giving the name (and the path\n"
+"if the file isn\'t in the current working directory) of the file to\n"
+"be opened or an integer file descriptor of the file to be\n"
+"wrapped. (If a file descriptor is given, it is closed when the\n"
+"returned I/O object is closed, unless closefd is set to False.)\n"
+"\n"
+"mode is an optional string that specifies the mode in which the file\n"
+"is opened. It defaults to \'r\' which means open for reading in text\n"
+"mode. Other common values are \'w\' for writing (truncating the file if\n"
+"it already exists), \'x\' for creating and writing to a new file, and\n"
+"\'a\' for appending (which on some Unix systems, means that all writes\n"
+"append to the end of the file regardless of the current seek position).\n"
+"In text mode, if encoding is not specified the encoding used is platform\n"
+"dependent: locale.getencoding() is called to get the current locale encoding.\n"
+"(For reading and writing raw bytes use binary mode and leave encoding\n"
+"unspecified.) The available modes are:\n"
+"\n"
+"========= ===============================================================\n"
+"Character Meaning\n"
+"--------- ---------------------------------------------------------------\n"
+"\'r\' open for reading (default)\n"
+"\'w\' open for writing, truncating the file first\n"
+"\'x\' create a new file and open it for writing\n"
+"\'a\' open for writing, appending to the end of the file if it exists\n"
+"\'b\' binary mode\n"
+"\'t\' text mode (default)\n"
+"\'+\' open a disk file for updating (reading and writing)\n"
+"========= ===============================================================\n"
+"\n"
+"The default mode is \'rt\' (open for reading text). For binary random\n"
+"access, the mode \'w+b\' opens and truncates the file to 0 bytes, while\n"
+"\'r+b\' opens the file without truncation. The \'x\' mode implies \'w\' and\n"
+"raises an `FileExistsError` if the file already exists.\n"
+"\n"
+"Python distinguishes between files opened in binary and text modes,\n"
+"even when the underlying operating system doesn\'t. Files opened in\n"
+"binary mode (appending \'b\' to the mode argument) return contents as\n"
+"bytes objects without any decoding. In text mode (the default, or when\n"
+"\'t\' is appended to the mode argument), the contents of the file are\n"
+"returned as strings, the bytes having been first decoded using a\n"
+"platform-dependent encoding or using the specified encoding if given.\n"
+"\n"
+"buffering is an optional integer used to set the buffering policy.\n"
+"Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n"
+"line buffering (only usable in text mode), and an integer > 1 to indicate\n"
+"the size of a fixed-size chunk buffer. When no buffering argument is\n"
+"given, the default buffering policy works as follows:\n"
+"\n"
+"* Binary files are buffered in fixed-size chunks; the size of the buffer\n"
+" is chosen using a heuristic trying to determine the underlying device\'s\n"
+" \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n"
+" On many systems, the buffer will typically be 4096 or 8192 bytes long.\n"
+"\n"
+"* \"Interactive\" text files (files for which isatty() returns True)\n"
+" use line buffering. Other text files use the policy described above\n"
+" for binary files.\n"
+"\n"
+"encoding is the name of the encoding used to decode or encode the\n"
+"file. This should only be used in text mode. The default encoding is\n"
+"platform dependent, but any encoding supported by Python can be\n"
+"passed. See the codecs module for the list of supported encodings.\n"
+"\n"
+"errors is an optional string that specifies how encoding errors are to\n"
+"be handled---this argument should not be used in binary mode. Pass\n"
+"\'strict\' to raise a ValueError exception if there is an encoding error\n"
+"(the default of None has the same effect), or pass \'ignore\' to ignore\n"
+"errors. (Note that ignoring encoding errors can lead to data loss.)\n"
+"See the documentation for codecs.register or run \'help(codecs.Codec)\'\n"
+"for a list of the permitted encoding error strings.\n"
+"\n"
+"newline controls how universal newlines works (it only applies to text\n"
+"mode). It can be None, \'\', \'\\n\', \'\\r\', and \'\\r\\n\'. It works as\n"
+"follows:\n"
+"\n"
+"* On input, if newline is None, universal newlines mode is\n"
+" enabled. Lines in the input can end in \'\\n\', \'\\r\', or \'\\r\\n\', and\n"
+" these are translated into \'\\n\' before being returned to the\n"
+" caller. If it is \'\', universal newline mode is enabled, but line\n"
+" endings are returned to the caller untranslated. If it has any of\n"
+" the other legal values, input lines are only terminated by the given\n"
+" string, and the line ending is returned to the caller untranslated.\n"
+"\n"
+"* On output, if newline is None, any \'\\n\' characters written are\n"
+" translated to the system default line separator, os.linesep. If\n"
+" newline is \'\' or \'\\n\', no translation takes place. If newline is any\n"
+" of the other legal values, any \'\\n\' characters written are translated\n"
+" to the given string.\n"
+"\n"
+"If closefd is False, the underlying file descriptor will be kept open\n"
+"when the file is closed. This does not work when a file name is given\n"
+"and must be True in that case.\n"
+"\n"
+"A custom opener can be used by passing a callable as *opener*. The\n"
+"underlying file descriptor for the file object is then obtained by\n"
+"calling *opener* with (*file*, *flags*). *opener* must return an open\n"
+"file descriptor (passing os.open as *opener* results in functionality\n"
+"similar to passing None).\n"
+"\n"
+"open() returns a file object whose type depends on the mode, and\n"
+"through which the standard file operations such as reading and writing\n"
+"are performed. When open() is used to open a file in a text mode (\'w\',\n"
+"\'r\', \'wt\', \'rt\', etc.), it returns a TextIOWrapper. When used to open\n"
+"a file in a binary mode, the returned class varies: in read binary\n"
+"mode, it returns a BufferedReader; in write binary and append binary\n"
+"modes, it returns a BufferedWriter, and in read/write mode, it returns\n"
+"a BufferedRandom.\n"
+"\n"
+"It is also possible to use a string or bytearray as a file for both\n"
+"reading and writing. For strings StringIO can be used like a file\n"
+"opened in a text mode, and for bytes a BytesIO can be used like a file\n"
+"opened in a binary mode.");
+
+#define _IO_OPEN_METHODDEF \
+ {"open", _PyCFunction_CAST(_io_open), METH_FASTCALL|METH_KEYWORDS, _io_open__doc__},
+
+static PyObject *
+_io_open_impl(PyObject *module, PyObject *file, const char *mode,
+ int buffering, const char *encoding, const char *errors,
+ const char *newline, int closefd, PyObject *opener);
+
+static PyObject *
+_io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 8
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(buffering), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(closefd), &_Py_ID(opener), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "open",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[8];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
+ PyObject *file;
+ const char *mode = "r";
+ int buffering = -1;
+ const char *encoding = NULL;
+ const char *errors = NULL;
+ const char *newline = NULL;
+ int closefd = 1;
+ PyObject *opener = Py_None;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 8, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ file = args[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (args[1]) {
+ if (!PyUnicode_Check(args[1])) {
+ _PyArg_BadArgument("open", "argument 'mode'", "str", args[1]);
+ goto exit;
+ }
+ Py_ssize_t mode_length;
+ mode = PyUnicode_AsUTF8AndSize(args[1], &mode_length);
+ if (mode == NULL) {
+ goto exit;
+ }
+ if (strlen(mode) != (size_t)mode_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[2]) {
+ buffering = _PyLong_AsInt(args[2]);
+ if (buffering == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[3]) {
+ if (args[3] == Py_None) {
+ encoding = NULL;
+ }
+ else if (PyUnicode_Check(args[3])) {
+ Py_ssize_t encoding_length;
+ encoding = PyUnicode_AsUTF8AndSize(args[3], &encoding_length);
+ if (encoding == NULL) {
+ goto exit;
+ }
+ if (strlen(encoding) != (size_t)encoding_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ }
+ else {
+ _PyArg_BadArgument("open", "argument 'encoding'", "str or None", args[3]);
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[4]) {
+ if (args[4] == Py_None) {
+ errors = NULL;
+ }
+ else if (PyUnicode_Check(args[4])) {
+ Py_ssize_t errors_length;
+ errors = PyUnicode_AsUTF8AndSize(args[4], &errors_length);
+ if (errors == NULL) {
+ goto exit;
+ }
+ if (strlen(errors) != (size_t)errors_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ }
+ else {
+ _PyArg_BadArgument("open", "argument 'errors'", "str or None", args[4]);
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[5]) {
+ if (args[5] == Py_None) {
+ newline = NULL;
+ }
+ else if (PyUnicode_Check(args[5])) {
+ Py_ssize_t newline_length;
+ newline = PyUnicode_AsUTF8AndSize(args[5], &newline_length);
+ if (newline == NULL) {
+ goto exit;
+ }
+ if (strlen(newline) != (size_t)newline_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ }
+ else {
+ _PyArg_BadArgument("open", "argument 'newline'", "str or None", args[5]);
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[6]) {
+ closefd = PyObject_IsTrue(args[6]);
+ if (closefd < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ opener = args[7];
+skip_optional_pos:
+ return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_text_encoding__doc__,
+"text_encoding($module, encoding, stacklevel=2, /)\n"
+"--\n"
+"\n"
+"A helper function to choose the text encoding.\n"
+"\n"
+"When encoding is not None, this function returns it.\n"
+"Otherwise, this function returns the default text encoding\n"
+"(i.e. \"locale\" or \"utf-8\" depends on UTF-8 mode).\n"
+"\n"
+"This function emits an EncodingWarning if encoding is None and\n"
+"sys.flags.warn_default_encoding is true.\n"
+"\n"
+"This can be used in APIs with an encoding=None parameter.\n"
+"However, please consider using encoding=\"utf-8\" for new APIs.");
+
+#define _IO_TEXT_ENCODING_METHODDEF \
+ {"text_encoding", _PyCFunction_CAST(_io_text_encoding), METH_FASTCALL, _io_text_encoding__doc__},
+
+static PyObject *
+_io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel);
+
+static PyObject *
+_io_text_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *encoding;
+ int stacklevel = 2;
+
+ if (!_PyArg_CheckPositional("text_encoding", nargs, 1, 2)) {
+ goto exit;
+ }
+ encoding = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ stacklevel = _PyLong_AsInt(args[1]);
+ if (stacklevel == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_text_encoding_impl(module, encoding, stacklevel);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_open_code__doc__,
+"open_code($module, /, path)\n"
+"--\n"
+"\n"
+"Opens the provided file with the intent to import the contents.\n"
+"\n"
+"This may perform extra validation beyond open(), but is otherwise interchangeable\n"
+"with calling open(path, \'rb\').");
+
+#define _IO_OPEN_CODE_METHODDEF \
+ {"open_code", _PyCFunction_CAST(_io_open_code), METH_FASTCALL|METH_KEYWORDS, _io_open_code__doc__},
+
+static PyObject *
+_io_open_code_impl(PyObject *module, PyObject *path);
+
+static PyObject *
+_io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(path), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"path", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "open_code",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ PyObject *path;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!PyUnicode_Check(args[0])) {
+ _PyArg_BadArgument("open_code", "argument 'path'", "str", args[0]);
+ goto exit;
+ }
+ if (PyUnicode_READY(args[0]) == -1) {
+ goto exit;
+ }
+ path = args[0];
+ return_value = _io_open_code_impl(module, path);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=f387eba3f4c0254a input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/bufferedio.c.h b/contrib/tools/python3/Modules/_io/clinic/bufferedio.c.h
new file mode 100644
index 0000000000..3becf4a5e3
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/bufferedio.c.h
@@ -0,0 +1,1101 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io__BufferedIOBase_readinto__doc__,
+"readinto($self, buffer, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFEREDIOBASE_READINTO_METHODDEF \
+ {"readinto", (PyCFunction)_io__BufferedIOBase_readinto, METH_O, _io__BufferedIOBase_readinto__doc__},
+
+static PyObject *
+_io__BufferedIOBase_readinto_impl(PyObject *self, Py_buffer *buffer);
+
+static PyObject *
+_io__BufferedIOBase_readinto(PyObject *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io__BufferedIOBase_readinto_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__BufferedIOBase_readinto1__doc__,
+"readinto1($self, buffer, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFEREDIOBASE_READINTO1_METHODDEF \
+ {"readinto1", (PyCFunction)_io__BufferedIOBase_readinto1, METH_O, _io__BufferedIOBase_readinto1__doc__},
+
+static PyObject *
+_io__BufferedIOBase_readinto1_impl(PyObject *self, Py_buffer *buffer);
+
+static PyObject *
+_io__BufferedIOBase_readinto1(PyObject *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ _PyArg_BadArgument("readinto1", "argument", "read-write bytes-like object", arg);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto1", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io__BufferedIOBase_readinto1_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__BufferedIOBase_detach__doc__,
+"detach($self, /)\n"
+"--\n"
+"\n"
+"Disconnect this buffer from its underlying raw stream and return it.\n"
+"\n"
+"After the raw stream has been detached, the buffer is in an unusable\n"
+"state.");
+
+#define _IO__BUFFEREDIOBASE_DETACH_METHODDEF \
+ {"detach", _PyCFunction_CAST(_io__BufferedIOBase_detach), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__BufferedIOBase_detach__doc__},
+
+static PyObject *
+_io__BufferedIOBase_detach_impl(PyObject *self, PyTypeObject *cls);
+
+static PyObject *
+_io__BufferedIOBase_detach(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "detach() takes no arguments");
+ return NULL;
+ }
+ return _io__BufferedIOBase_detach_impl(self, cls);
+}
+
+PyDoc_STRVAR(_io__BufferedIOBase_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read and return up to n bytes.\n"
+"\n"
+"If the size argument is omitted, None, or negative, read and\n"
+"return all data until EOF.\n"
+"\n"
+"If the size argument is positive, and the underlying raw stream is\n"
+"not \'interactive\', multiple raw reads may be issued to satisfy\n"
+"the byte count (unless EOF is reached first).\n"
+"However, for interactive raw streams (as well as sockets and pipes),\n"
+"at most one raw read will be issued, and a short result does not\n"
+"imply that EOF is imminent.\n"
+"\n"
+"Return an empty bytes object on EOF.\n"
+"\n"
+"Return None if the underlying raw stream was open in non-blocking\n"
+"mode and no data is available at the moment.");
+
+#define _IO__BUFFEREDIOBASE_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io__BufferedIOBase_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__BufferedIOBase_read__doc__},
+
+static PyObject *
+_io__BufferedIOBase_read_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size));
+
+static PyObject *
+_io__BufferedIOBase_read(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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];
+ int size = -1;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ size = _PyLong_AsInt(args[0]);
+ if (size == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__BufferedIOBase_read_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__BufferedIOBase_read1__doc__,
+"read1($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read and return up to size bytes, with at most one read() call to the underlying raw stream.\n"
+"\n"
+"Return an empty bytes object on EOF.\n"
+"A short result does not imply that EOF is imminent.");
+
+#define _IO__BUFFEREDIOBASE_READ1_METHODDEF \
+ {"read1", _PyCFunction_CAST(_io__BufferedIOBase_read1), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__BufferedIOBase_read1__doc__},
+
+static PyObject *
+_io__BufferedIOBase_read1_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size));
+
+static PyObject *
+_io__BufferedIOBase_read1(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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 = "read1",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ int size = -1;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ size = _PyLong_AsInt(args[0]);
+ if (size == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__BufferedIOBase_read1_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__BufferedIOBase_write__doc__,
+"write($self, b, /)\n"
+"--\n"
+"\n"
+"Write buffer b to the IO stream.\n"
+"\n"
+"Return the number of bytes written, which is always\n"
+"the length of b in bytes.\n"
+"\n"
+"Raise BlockingIOError if the buffer is full and the\n"
+"underlying raw stream cannot accept more data at the moment.");
+
+#define _IO__BUFFEREDIOBASE_WRITE_METHODDEF \
+ {"write", _PyCFunction_CAST(_io__BufferedIOBase_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__BufferedIOBase_write__doc__},
+
+static PyObject *
+_io__BufferedIOBase_write_impl(PyObject *self, PyTypeObject *cls,
+ PyObject *Py_UNUSED(b));
+
+static PyObject *
+_io__BufferedIOBase_write(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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];
+ PyObject *b;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ b = args[0];
+ return_value = _io__BufferedIOBase_write_impl(self, cls, b);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered___sizeof____doc__,
+"__sizeof__($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED___SIZEOF___METHODDEF \
+ {"__sizeof__", (PyCFunction)_io__Buffered___sizeof__, METH_NOARGS, _io__Buffered___sizeof____doc__},
+
+static PyObject *
+_io__Buffered___sizeof___impl(buffered *self);
+
+static PyObject *
+_io__Buffered___sizeof__(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered___sizeof___impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered__dealloc_warn__doc__,
+"_dealloc_warn($self, source, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED__DEALLOC_WARN_METHODDEF \
+ {"_dealloc_warn", (PyCFunction)_io__Buffered__dealloc_warn, METH_O, _io__Buffered__dealloc_warn__doc__},
+
+PyDoc_STRVAR(_io__Buffered_simple_flush__doc__,
+"flush($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_SIMPLE_FLUSH_METHODDEF \
+ {"flush", (PyCFunction)_io__Buffered_simple_flush, METH_NOARGS, _io__Buffered_simple_flush__doc__},
+
+static PyObject *
+_io__Buffered_simple_flush_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_simple_flush(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_simple_flush_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_CLOSE_METHODDEF \
+ {"close", (PyCFunction)_io__Buffered_close, METH_NOARGS, _io__Buffered_close__doc__},
+
+static PyObject *
+_io__Buffered_close_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_close(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_close_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_detach__doc__,
+"detach($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_DETACH_METHODDEF \
+ {"detach", (PyCFunction)_io__Buffered_detach, METH_NOARGS, _io__Buffered_detach__doc__},
+
+static PyObject *
+_io__Buffered_detach_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_detach(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_detach_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io__Buffered_seekable, METH_NOARGS, _io__Buffered_seekable__doc__},
+
+static PyObject *
+_io__Buffered_seekable_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_seekable(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_seekable_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io__Buffered_readable, METH_NOARGS, _io__Buffered_readable__doc__},
+
+static PyObject *
+_io__Buffered_readable_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_readable(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io__Buffered_writable, METH_NOARGS, _io__Buffered_writable__doc__},
+
+static PyObject *
+_io__Buffered_writable_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_writable(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_fileno__doc__,
+"fileno($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_FILENO_METHODDEF \
+ {"fileno", (PyCFunction)_io__Buffered_fileno, METH_NOARGS, _io__Buffered_fileno__doc__},
+
+static PyObject *
+_io__Buffered_fileno_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_fileno(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_fileno_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io__Buffered_isatty, METH_NOARGS, _io__Buffered_isatty__doc__},
+
+static PyObject *
+_io__Buffered_isatty_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_isatty_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_flush__doc__,
+"flush($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_FLUSH_METHODDEF \
+ {"flush", (PyCFunction)_io__Buffered_flush, METH_NOARGS, _io__Buffered_flush__doc__},
+
+static PyObject *
+_io__Buffered_flush_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_flush(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_flush_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_peek__doc__,
+"peek($self, size=0, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_PEEK_METHODDEF \
+ {"peek", _PyCFunction_CAST(_io__Buffered_peek), METH_FASTCALL, _io__Buffered_peek__doc__},
+
+static PyObject *
+_io__Buffered_peek_impl(buffered *self, Py_ssize_t size);
+
+static PyObject *
+_io__Buffered_peek(buffered *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = 0;
+
+ if (!_PyArg_CheckPositional("peek", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ size = ival;
+ }
+skip_optional:
+ return_value = _io__Buffered_peek_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io__Buffered_read), METH_FASTCALL, _io__Buffered_read__doc__},
+
+static PyObject *
+_io__Buffered_read_impl(buffered *self, Py_ssize_t n);
+
+static PyObject *
+_io__Buffered_read(buffered *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t n = -1;
+
+ if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &n)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io__Buffered_read_impl(self, n);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_read1__doc__,
+"read1($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READ1_METHODDEF \
+ {"read1", _PyCFunction_CAST(_io__Buffered_read1), METH_FASTCALL, _io__Buffered_read1__doc__},
+
+static PyObject *
+_io__Buffered_read1_impl(buffered *self, Py_ssize_t n);
+
+static PyObject *
+_io__Buffered_read1(buffered *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t n = -1;
+
+ if (!_PyArg_CheckPositional("read1", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ n = ival;
+ }
+skip_optional:
+ return_value = _io__Buffered_read1_impl(self, n);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_readinto__doc__,
+"readinto($self, buffer, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READINTO_METHODDEF \
+ {"readinto", (PyCFunction)_io__Buffered_readinto, METH_O, _io__Buffered_readinto__doc__},
+
+static PyObject *
+_io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer);
+
+static PyObject *
+_io__Buffered_readinto(buffered *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io__Buffered_readinto_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_readinto1__doc__,
+"readinto1($self, buffer, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READINTO1_METHODDEF \
+ {"readinto1", (PyCFunction)_io__Buffered_readinto1, METH_O, _io__Buffered_readinto1__doc__},
+
+static PyObject *
+_io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer);
+
+static PyObject *
+_io__Buffered_readinto1(buffered *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ _PyArg_BadArgument("readinto1", "argument", "read-write bytes-like object", arg);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto1", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io__Buffered_readinto1_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io__Buffered_readline), METH_FASTCALL, _io__Buffered_readline__doc__},
+
+static PyObject *
+_io__Buffered_readline_impl(buffered *self, Py_ssize_t size);
+
+static PyObject *
+_io__Buffered_readline(buffered *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io__Buffered_readline_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io__Buffered_tell, METH_NOARGS, _io__Buffered_tell__doc__},
+
+static PyObject *
+_io__Buffered_tell_impl(buffered *self);
+
+static PyObject *
+_io__Buffered_tell(buffered *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__Buffered_tell_impl(self);
+}
+
+PyDoc_STRVAR(_io__Buffered_seek__doc__,
+"seek($self, target, whence=0, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io__Buffered_seek), METH_FASTCALL, _io__Buffered_seek__doc__},
+
+static PyObject *
+_io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence);
+
+static PyObject *
+_io__Buffered_seek(buffered *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *targetobj;
+ int whence = 0;
+
+ if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ goto exit;
+ }
+ targetobj = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io__Buffered_seek_impl(self, targetobj, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__Buffered_truncate__doc__,
+"truncate($self, pos=None, /)\n"
+"--\n"
+"\n");
+
+#define _IO__BUFFERED_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io__Buffered_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__Buffered_truncate__doc__},
+
+static PyObject *
+_io__Buffered_truncate_impl(buffered *self, PyTypeObject *cls, PyObject *pos);
+
+static PyObject *
+_io__Buffered_truncate(buffered *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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 *pos = Py_None;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ pos = args[0];
+skip_optional_posonly:
+ return_value = _io__Buffered_truncate_impl(self, cls, pos);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BufferedReader___init____doc__,
+"BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n"
+"--\n"
+"\n"
+"Create a new buffered reader using the given readable raw IO object.");
+
+static int
+_io_BufferedReader___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size);
+
+static int
+_io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 2
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"raw", "buffer_size", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "BufferedReader",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *raw;
+ Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ raw = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(fastargs[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ buffer_size = ival;
+ }
+skip_optional_pos:
+ return_value = _io_BufferedReader___init___impl((buffered *)self, raw, buffer_size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BufferedWriter___init____doc__,
+"BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n"
+"--\n"
+"\n"
+"A buffer for a writeable sequential RawIO object.\n"
+"\n"
+"The constructor creates a BufferedWriter for the given writeable raw\n"
+"stream. If the buffer_size is not given, it defaults to\n"
+"DEFAULT_BUFFER_SIZE.");
+
+static int
+_io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size);
+
+static int
+_io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 2
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"raw", "buffer_size", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "BufferedWriter",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *raw;
+ Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ raw = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(fastargs[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ buffer_size = ival;
+ }
+skip_optional_pos:
+ return_value = _io_BufferedWriter___init___impl((buffered *)self, raw, buffer_size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BufferedWriter_write__doc__,
+"write($self, buffer, /)\n"
+"--\n"
+"\n");
+
+#define _IO_BUFFEREDWRITER_WRITE_METHODDEF \
+ {"write", (PyCFunction)_io_BufferedWriter_write, METH_O, _io_BufferedWriter_write__doc__},
+
+static PyObject *
+_io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer);
+
+static PyObject *
+_io_BufferedWriter_write(buffered *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("write", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io_BufferedWriter_write_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BufferedRWPair___init____doc__,
+"BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE, /)\n"
+"--\n"
+"\n"
+"A buffered reader and writer object together.\n"
+"\n"
+"A buffered reader object and buffered writer object put together to\n"
+"form a sequential IO object that can read and write. This is typically\n"
+"used with a socket or two-way pipe.\n"
+"\n"
+"reader and writer are RawIOBase objects that are readable and\n"
+"writeable respectively. If the buffer_size is omitted it defaults to\n"
+"DEFAULT_BUFFER_SIZE.");
+
+static int
+_io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader,
+ PyObject *writer, Py_ssize_t buffer_size);
+
+static int
+_io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ PyTypeObject *base_tp = clinic_state()->PyBufferedRWPair_Type;
+ PyObject *reader;
+ PyObject *writer;
+ Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+
+ if ((Py_IS_TYPE(self, base_tp) ||
+ Py_TYPE(self)->tp_new == base_tp->tp_new) &&
+ !_PyArg_NoKeywords("BufferedRWPair", kwargs)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("BufferedRWPair", PyTuple_GET_SIZE(args), 2, 3)) {
+ goto exit;
+ }
+ reader = PyTuple_GET_ITEM(args, 0);
+ writer = PyTuple_GET_ITEM(args, 1);
+ if (PyTuple_GET_SIZE(args) < 3) {
+ goto skip_optional;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(PyTuple_GET_ITEM(args, 2));
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ buffer_size = ival;
+ }
+skip_optional:
+ return_value = _io_BufferedRWPair___init___impl((rwpair *)self, reader, writer, buffer_size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BufferedRandom___init____doc__,
+"BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n"
+"--\n"
+"\n"
+"A buffered interface to random access streams.\n"
+"\n"
+"The constructor creates a reader and writer for a seekable stream,\n"
+"raw, given in the first argument. If the buffer_size is omitted it\n"
+"defaults to DEFAULT_BUFFER_SIZE.");
+
+static int
+_io_BufferedRandom___init___impl(buffered *self, PyObject *raw,
+ Py_ssize_t buffer_size);
+
+static int
+_io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 2
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"raw", "buffer_size", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "BufferedRandom",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *raw;
+ Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ raw = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(fastargs[1]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ buffer_size = ival;
+ }
+skip_optional_pos:
+ return_value = _io_BufferedRandom___init___impl((buffered *)self, raw, buffer_size);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=e51a6ca8bc8ed33d input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/bytesio.c.h b/contrib/tools/python3/Modules/_io/clinic/bytesio.c.h
new file mode 100644
index 0000000000..c816ef5d6b
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/bytesio.c.h
@@ -0,0 +1,541 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io_BytesIO_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be read.");
+
+#define _IO_BYTESIO_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io_BytesIO_readable, METH_NOARGS, _io_BytesIO_readable__doc__},
+
+static PyObject *
+_io_BytesIO_readable_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_readable(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be written.");
+
+#define _IO_BYTESIO_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io_BytesIO_writable, METH_NOARGS, _io_BytesIO_writable__doc__},
+
+static PyObject *
+_io_BytesIO_writable_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_writable(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be seeked.");
+
+#define _IO_BYTESIO_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io_BytesIO_seekable, METH_NOARGS, _io_BytesIO_seekable__doc__},
+
+static PyObject *
+_io_BytesIO_seekable_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_seekable(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_seekable_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_flush__doc__,
+"flush($self, /)\n"
+"--\n"
+"\n"
+"Does nothing.");
+
+#define _IO_BYTESIO_FLUSH_METHODDEF \
+ {"flush", (PyCFunction)_io_BytesIO_flush, METH_NOARGS, _io_BytesIO_flush__doc__},
+
+static PyObject *
+_io_BytesIO_flush_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_flush(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_flush_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_getbuffer__doc__,
+"getbuffer($self, /)\n"
+"--\n"
+"\n"
+"Get a read-write view over the contents of the BytesIO object.");
+
+#define _IO_BYTESIO_GETBUFFER_METHODDEF \
+ {"getbuffer", _PyCFunction_CAST(_io_BytesIO_getbuffer), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_BytesIO_getbuffer__doc__},
+
+static PyObject *
+_io_BytesIO_getbuffer_impl(bytesio *self, PyTypeObject *cls);
+
+static PyObject *
+_io_BytesIO_getbuffer(bytesio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "getbuffer() takes no arguments");
+ return NULL;
+ }
+ return _io_BytesIO_getbuffer_impl(self, cls);
+}
+
+PyDoc_STRVAR(_io_BytesIO_getvalue__doc__,
+"getvalue($self, /)\n"
+"--\n"
+"\n"
+"Retrieve the entire contents of the BytesIO object.");
+
+#define _IO_BYTESIO_GETVALUE_METHODDEF \
+ {"getvalue", (PyCFunction)_io_BytesIO_getvalue, METH_NOARGS, _io_BytesIO_getvalue__doc__},
+
+static PyObject *
+_io_BytesIO_getvalue_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_getvalue(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_getvalue_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n"
+"Always returns False.\n"
+"\n"
+"BytesIO objects are not connected to a TTY-like device.");
+
+#define _IO_BYTESIO_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io_BytesIO_isatty, METH_NOARGS, _io_BytesIO_isatty__doc__},
+
+static PyObject *
+_io_BytesIO_isatty_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_isatty(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_isatty_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n"
+"Current file position, an integer.");
+
+#define _IO_BYTESIO_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io_BytesIO_tell, METH_NOARGS, _io_BytesIO_tell__doc__},
+
+static PyObject *
+_io_BytesIO_tell_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_tell(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_tell_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size bytes, returned as a bytes object.\n"
+"\n"
+"If the size argument is negative, read until EOF is reached.\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO_BYTESIO_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io_BytesIO_read), METH_FASTCALL, _io_BytesIO_read__doc__},
+
+static PyObject *
+_io_BytesIO_read_impl(bytesio *self, Py_ssize_t size);
+
+static PyObject *
+_io_BytesIO_read(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_BytesIO_read_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_read1__doc__,
+"read1($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size bytes, returned as a bytes object.\n"
+"\n"
+"If the size argument is negative or omitted, read until EOF is reached.\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO_BYTESIO_READ1_METHODDEF \
+ {"read1", _PyCFunction_CAST(_io_BytesIO_read1), METH_FASTCALL, _io_BytesIO_read1__doc__},
+
+static PyObject *
+_io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size);
+
+static PyObject *
+_io_BytesIO_read1(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("read1", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_BytesIO_read1_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Next line from the file, as a bytes object.\n"
+"\n"
+"Retain newline. A non-negative size argument limits the maximum\n"
+"number of bytes to return (an incomplete line may be returned then).\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO_BYTESIO_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io_BytesIO_readline), METH_FASTCALL, _io_BytesIO_readline__doc__},
+
+static PyObject *
+_io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size);
+
+static PyObject *
+_io_BytesIO_readline(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_BytesIO_readline_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_readlines__doc__,
+"readlines($self, size=None, /)\n"
+"--\n"
+"\n"
+"List of bytes objects, each a line from the file.\n"
+"\n"
+"Call readline() repeatedly and return a list of the lines so read.\n"
+"The optional size argument, if given, is an approximate bound on the\n"
+"total number of bytes in the lines returned.");
+
+#define _IO_BYTESIO_READLINES_METHODDEF \
+ {"readlines", _PyCFunction_CAST(_io_BytesIO_readlines), METH_FASTCALL, _io_BytesIO_readlines__doc__},
+
+static PyObject *
+_io_BytesIO_readlines_impl(bytesio *self, PyObject *arg);
+
+static PyObject *
+_io_BytesIO_readlines(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *arg = Py_None;
+
+ if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ arg = args[0];
+skip_optional:
+ return_value = _io_BytesIO_readlines_impl(self, arg);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_readinto__doc__,
+"readinto($self, buffer, /)\n"
+"--\n"
+"\n"
+"Read bytes into buffer.\n"
+"\n"
+"Returns number of bytes read (0 for EOF), or None if the object\n"
+"is set not to block and has no data to read.");
+
+#define _IO_BYTESIO_READINTO_METHODDEF \
+ {"readinto", (PyCFunction)_io_BytesIO_readinto, METH_O, _io_BytesIO_readinto__doc__},
+
+static PyObject *
+_io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer);
+
+static PyObject *
+_io_BytesIO_readinto(bytesio *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
+ goto exit;
+ }
+ return_value = _io_BytesIO_readinto_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_truncate__doc__,
+"truncate($self, size=None, /)\n"
+"--\n"
+"\n"
+"Truncate the file to at most size bytes.\n"
+"\n"
+"Size defaults to the current file position, as returned by tell().\n"
+"The current file position is unchanged. Returns the new size.");
+
+#define _IO_BYTESIO_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io_BytesIO_truncate), METH_FASTCALL, _io_BytesIO_truncate__doc__},
+
+static PyObject *
+_io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size);
+
+static PyObject *
+_io_BytesIO_truncate(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = self->pos;
+
+ if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_BytesIO_truncate_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_seek__doc__,
+"seek($self, pos, whence=0, /)\n"
+"--\n"
+"\n"
+"Change stream position.\n"
+"\n"
+"Seek to byte offset pos relative to position indicated by whence:\n"
+" 0 Start of stream (the default). pos should be >= 0;\n"
+" 1 Current position - pos may be negative;\n"
+" 2 End of stream - pos usually negative.\n"
+"Returns the new absolute position.");
+
+#define _IO_BYTESIO_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io_BytesIO_seek), METH_FASTCALL, _io_BytesIO_seek__doc__},
+
+static PyObject *
+_io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence);
+
+static PyObject *
+_io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t pos;
+ int whence = 0;
+
+ if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ goto exit;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ pos = ival;
+ }
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_BytesIO_seek_impl(self, pos, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_BytesIO_write__doc__,
+"write($self, b, /)\n"
+"--\n"
+"\n"
+"Write bytes to file.\n"
+"\n"
+"Return the number of bytes written.");
+
+#define _IO_BYTESIO_WRITE_METHODDEF \
+ {"write", (PyCFunction)_io_BytesIO_write, METH_O, _io_BytesIO_write__doc__},
+
+PyDoc_STRVAR(_io_BytesIO_writelines__doc__,
+"writelines($self, lines, /)\n"
+"--\n"
+"\n"
+"Write lines to the file.\n"
+"\n"
+"Note that newlines are not added. lines can be any iterable object\n"
+"producing bytes-like objects. This is equivalent to calling write() for\n"
+"each element.");
+
+#define _IO_BYTESIO_WRITELINES_METHODDEF \
+ {"writelines", (PyCFunction)_io_BytesIO_writelines, METH_O, _io_BytesIO_writelines__doc__},
+
+PyDoc_STRVAR(_io_BytesIO_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n"
+"Disable all I/O operations.");
+
+#define _IO_BYTESIO_CLOSE_METHODDEF \
+ {"close", (PyCFunction)_io_BytesIO_close, METH_NOARGS, _io_BytesIO_close__doc__},
+
+static PyObject *
+_io_BytesIO_close_impl(bytesio *self);
+
+static PyObject *
+_io_BytesIO_close(bytesio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_BytesIO_close_impl(self);
+}
+
+PyDoc_STRVAR(_io_BytesIO___init____doc__,
+"BytesIO(initial_bytes=b\'\')\n"
+"--\n"
+"\n"
+"Buffered I/O implementation using an in-memory bytes buffer.");
+
+static int
+_io_BytesIO___init___impl(bytesio *self, PyObject *initvalue);
+
+static int
+_io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(initial_bytes), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"initial_bytes", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "BytesIO",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
+ PyObject *initvalue = NULL;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ initvalue = fastargs[0];
+skip_optional_pos:
+ return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=1c540b54a10919d7 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/fileio.c.h b/contrib/tools/python3/Modules/_io/clinic/fileio.c.h
new file mode 100644
index 0000000000..0d94e6cec6
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/fileio.c.h
@@ -0,0 +1,539 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io_FileIO_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n"
+"Close the file.\n"
+"\n"
+"A closed file cannot be used for further I/O operations. close() may be\n"
+"called more than once without error.");
+
+#define _IO_FILEIO_CLOSE_METHODDEF \
+ {"close", _PyCFunction_CAST(_io_FileIO_close), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_close__doc__},
+
+static PyObject *
+_io_FileIO_close_impl(fileio *self, PyTypeObject *cls);
+
+static PyObject *
+_io_FileIO_close(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "close() takes no arguments");
+ return NULL;
+ }
+ return _io_FileIO_close_impl(self, cls);
+}
+
+PyDoc_STRVAR(_io_FileIO___init____doc__,
+"FileIO(file, mode=\'r\', closefd=True, opener=None)\n"
+"--\n"
+"\n"
+"Open a file.\n"
+"\n"
+"The mode can be \'r\' (default), \'w\', \'x\' or \'a\' for reading,\n"
+"writing, exclusive creation or appending. The file will be created if it\n"
+"doesn\'t exist when opened for writing or appending; it will be truncated\n"
+"when opened for writing. A FileExistsError will be raised if it already\n"
+"exists when opened for creating. Opening a file for creating implies\n"
+"writing so this mode behaves in a similar way to \'w\'.Add a \'+\' to the mode\n"
+"to allow simultaneous reading and writing. A custom opener can be used by\n"
+"passing a callable as *opener*. The underlying file descriptor for the file\n"
+"object is then obtained by calling opener with (*name*, *flags*).\n"
+"*opener* must return an open file descriptor (passing os.open as *opener*\n"
+"results in functionality similar to passing None).");
+
+static int
+_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
+ int closefd, PyObject *opener);
+
+static int
+_io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 4
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(closefd), &_Py_ID(opener), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "FileIO",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[4];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *nameobj;
+ const char *mode = "r";
+ int closefd = 1;
+ PyObject *opener = Py_None;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 4, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ nameobj = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (fastargs[1]) {
+ if (!PyUnicode_Check(fastargs[1])) {
+ _PyArg_BadArgument("FileIO", "argument 'mode'", "str", fastargs[1]);
+ goto exit;
+ }
+ Py_ssize_t mode_length;
+ mode = PyUnicode_AsUTF8AndSize(fastargs[1], &mode_length);
+ if (mode == NULL) {
+ goto exit;
+ }
+ if (strlen(mode) != (size_t)mode_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (fastargs[2]) {
+ closefd = PyObject_IsTrue(fastargs[2]);
+ if (closefd < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ opener = fastargs[3];
+skip_optional_pos:
+ return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_FileIO_fileno__doc__,
+"fileno($self, /)\n"
+"--\n"
+"\n"
+"Return the underlying file descriptor (an integer).");
+
+#define _IO_FILEIO_FILENO_METHODDEF \
+ {"fileno", (PyCFunction)_io_FileIO_fileno, METH_NOARGS, _io_FileIO_fileno__doc__},
+
+static PyObject *
+_io_FileIO_fileno_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_fileno(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_fileno_impl(self);
+}
+
+PyDoc_STRVAR(_io_FileIO_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n"
+"True if file was opened in a read mode.");
+
+#define _IO_FILEIO_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io_FileIO_readable, METH_NOARGS, _io_FileIO_readable__doc__},
+
+static PyObject *
+_io_FileIO_readable_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_readable(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io_FileIO_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n"
+"True if file was opened in a write mode.");
+
+#define _IO_FILEIO_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io_FileIO_writable, METH_NOARGS, _io_FileIO_writable__doc__},
+
+static PyObject *
+_io_FileIO_writable_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_writable(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io_FileIO_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n"
+"True if file supports random-access.");
+
+#define _IO_FILEIO_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io_FileIO_seekable, METH_NOARGS, _io_FileIO_seekable__doc__},
+
+static PyObject *
+_io_FileIO_seekable_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_seekable(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_seekable_impl(self);
+}
+
+PyDoc_STRVAR(_io_FileIO_readinto__doc__,
+"readinto($self, buffer, /)\n"
+"--\n"
+"\n"
+"Same as RawIOBase.readinto().");
+
+#define _IO_FILEIO_READINTO_METHODDEF \
+ {"readinto", _PyCFunction_CAST(_io_FileIO_readinto), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_readinto__doc__},
+
+static PyObject *
+_io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer);
+
+static PyObject *
+_io_FileIO_readinto(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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};
+
+ 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();
+ _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto", "argument 1", "contiguous buffer", args[0]);
+ goto exit;
+ }
+ return_value = _io_FileIO_readinto_impl(self, cls, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_FileIO_readall__doc__,
+"readall($self, /)\n"
+"--\n"
+"\n"
+"Read all data from the file, returned as bytes.\n"
+"\n"
+"In non-blocking mode, returns as much as is immediately available,\n"
+"or None if no data is available. Return an empty bytes object at EOF.");
+
+#define _IO_FILEIO_READALL_METHODDEF \
+ {"readall", (PyCFunction)_io_FileIO_readall, METH_NOARGS, _io_FileIO_readall__doc__},
+
+static PyObject *
+_io_FileIO_readall_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_readall(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_readall_impl(self);
+}
+
+PyDoc_STRVAR(_io_FileIO_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size bytes, returned as bytes.\n"
+"\n"
+"Only makes one system call, so less data may be returned than requested.\n"
+"In non-blocking mode, returns None if no data is available.\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO_FILEIO_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io_FileIO_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_read__doc__},
+
+static PyObject *
+_io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size);
+
+static PyObject *
+_io_FileIO_read(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io_FileIO_read_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_FileIO_write__doc__,
+"write($self, b, /)\n"
+"--\n"
+"\n"
+"Write buffer b to file, return number of bytes written.\n"
+"\n"
+"Only makes one system call, so not all of the data may be written.\n"
+"The number of bytes actually written is returned. In non-blocking mode,\n"
+"returns None if the write would block.");
+
+#define _IO_FILEIO_WRITE_METHODDEF \
+ {"write", _PyCFunction_CAST(_io_FileIO_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_write__doc__},
+
+static PyObject *
+_io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b);
+
+static PyObject *
+_io_FileIO_write(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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};
+
+ 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;
+ }
+ if (!PyBuffer_IsContiguous(&b, 'C')) {
+ _PyArg_BadArgument("write", "argument 1", "contiguous buffer", args[0]);
+ goto exit;
+ }
+ return_value = _io_FileIO_write_impl(self, cls, &b);
+
+exit:
+ /* Cleanup for b */
+ if (b.obj) {
+ PyBuffer_Release(&b);
+ }
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_FileIO_seek__doc__,
+"seek($self, pos, whence=0, /)\n"
+"--\n"
+"\n"
+"Move to new file position and return the file position.\n"
+"\n"
+"Argument offset is a byte count. Optional argument whence defaults to\n"
+"SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values\n"
+"are SEEK_CUR or 1 (move relative to current position, positive or negative),\n"
+"and SEEK_END or 2 (move relative to end of file, usually negative, although\n"
+"many platforms allow seeking beyond the end of a file).\n"
+"\n"
+"Note that not all file objects are seekable.");
+
+#define _IO_FILEIO_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io_FileIO_seek), METH_FASTCALL, _io_FileIO_seek__doc__},
+
+static PyObject *
+_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence);
+
+static PyObject *
+_io_FileIO_seek(fileio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *pos;
+ int whence = 0;
+
+ if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ goto exit;
+ }
+ pos = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_FileIO_seek_impl(self, pos, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_FileIO_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n"
+"Current file position.\n"
+"\n"
+"Can raise OSError for non seekable files.");
+
+#define _IO_FILEIO_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io_FileIO_tell, METH_NOARGS, _io_FileIO_tell__doc__},
+
+static PyObject *
+_io_FileIO_tell_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_tell(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_tell_impl(self);
+}
+
+#if defined(HAVE_FTRUNCATE)
+
+PyDoc_STRVAR(_io_FileIO_truncate__doc__,
+"truncate($self, size=None, /)\n"
+"--\n"
+"\n"
+"Truncate the file to at most size bytes and return the truncated size.\n"
+"\n"
+"Size defaults to the current file position, as returned by tell().\n"
+"The current file position is changed to the value of size.");
+
+#define _IO_FILEIO_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io_FileIO_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_truncate__doc__},
+
+static PyObject *
+_io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj);
+
+static PyObject *
+_io_FileIO_truncate(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ posobj = args[0];
+skip_optional_posonly:
+ return_value = _io_FileIO_truncate_impl(self, cls, posobj);
+
+exit:
+ return return_value;
+}
+
+#endif /* defined(HAVE_FTRUNCATE) */
+
+PyDoc_STRVAR(_io_FileIO_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n"
+"True if the file is connected to a TTY device.");
+
+#define _IO_FILEIO_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io_FileIO_isatty, METH_NOARGS, _io_FileIO_isatty__doc__},
+
+static PyObject *
+_io_FileIO_isatty_impl(fileio *self);
+
+static PyObject *
+_io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_FileIO_isatty_impl(self);
+}
+
+#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
+ #define _IO_FILEIO_TRUNCATE_METHODDEF
+#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
+/*[clinic end generated code: output=17d5c63e9b37ccb1 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/iobase.c.h b/contrib/tools/python3/Modules/_io/clinic/iobase.c.h
new file mode 100644
index 0000000000..328ea95e3f
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/iobase.c.h
@@ -0,0 +1,444 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io__IOBase_seek__doc__,
+"seek($self, offset, whence=os.SEEK_SET, /)\n"
+"--\n"
+"\n"
+"Change the stream position to the given byte offset.\n"
+"\n"
+" offset\n"
+" The stream position, relative to \'whence\'.\n"
+" whence\n"
+" The relative position to seek from.\n"
+"\n"
+"The offset is interpreted relative to the position indicated by whence.\n"
+"Values for whence are:\n"
+"\n"
+"* os.SEEK_SET or 0 -- start of stream (the default); offset should be zero or positive\n"
+"* os.SEEK_CUR or 1 -- current stream position; offset may be negative\n"
+"* os.SEEK_END or 2 -- end of stream; offset is usually negative\n"
+"\n"
+"Return the new absolute position.");
+
+#define _IO__IOBASE_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io__IOBase_seek), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_seek__doc__},
+
+static PyObject *
+_io__IOBase_seek_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(offset), int Py_UNUSED(whence));
+
+static PyObject *
+_io__IOBase_seek(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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 = "seek",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ int offset;
+ int whence = 0;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ offset = _PyLong_AsInt(args[0]);
+ if (offset == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (nargs < 2) {
+ goto skip_optional_posonly;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__IOBase_seek_impl(self, cls, offset, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__IOBase_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n"
+"Return current stream position.");
+
+#define _IO__IOBASE_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io__IOBase_tell, METH_NOARGS, _io__IOBase_tell__doc__},
+
+static PyObject *
+_io__IOBase_tell_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_tell(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_tell_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_truncate__doc__,
+"truncate($self, size=None, /)\n"
+"--\n"
+"\n"
+"Truncate file to size bytes.\n"
+"\n"
+"File pointer is left unchanged. Size defaults to the current IO position\n"
+"as reported by tell(). Return the new size.");
+
+#define _IO__IOBASE_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io__IOBase_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_truncate__doc__},
+
+static PyObject *
+_io__IOBase_truncate_impl(PyObject *self, PyTypeObject *cls,
+ PyObject *Py_UNUSED(size));
+
+static PyObject *
+_io__IOBase_truncate(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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 *size = Py_None;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ size = args[0];
+skip_optional_posonly:
+ return_value = _io__IOBase_truncate_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__IOBase_flush__doc__,
+"flush($self, /)\n"
+"--\n"
+"\n"
+"Flush write buffers, if applicable.\n"
+"\n"
+"This is not implemented for read-only and non-blocking streams.");
+
+#define _IO__IOBASE_FLUSH_METHODDEF \
+ {"flush", (PyCFunction)_io__IOBase_flush, METH_NOARGS, _io__IOBase_flush__doc__},
+
+static PyObject *
+_io__IOBase_flush_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_flush(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_flush_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n"
+"Flush and close the IO object.\n"
+"\n"
+"This method has no effect if the file is already closed.");
+
+#define _IO__IOBASE_CLOSE_METHODDEF \
+ {"close", (PyCFunction)_io__IOBase_close, METH_NOARGS, _io__IOBase_close__doc__},
+
+static PyObject *
+_io__IOBase_close_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_close(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_close_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n"
+"Return whether object supports random access.\n"
+"\n"
+"If False, seek(), tell() and truncate() will raise OSError.\n"
+"This method may need to do a test seek().");
+
+#define _IO__IOBASE_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io__IOBase_seekable, METH_NOARGS, _io__IOBase_seekable__doc__},
+
+static PyObject *
+_io__IOBase_seekable_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_seekable(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_seekable_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n"
+"Return whether object was opened for reading.\n"
+"\n"
+"If False, read() will raise OSError.");
+
+#define _IO__IOBASE_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io__IOBase_readable, METH_NOARGS, _io__IOBase_readable__doc__},
+
+static PyObject *
+_io__IOBase_readable_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_readable(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n"
+"Return whether object was opened for writing.\n"
+"\n"
+"If False, write() will raise OSError.");
+
+#define _IO__IOBASE_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io__IOBase_writable, METH_NOARGS, _io__IOBase_writable__doc__},
+
+static PyObject *
+_io__IOBase_writable_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_writable(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_fileno__doc__,
+"fileno($self, /)\n"
+"--\n"
+"\n"
+"Return underlying file descriptor if one exists.\n"
+"\n"
+"Raise OSError if the IO object does not use a file descriptor.");
+
+#define _IO__IOBASE_FILENO_METHODDEF \
+ {"fileno", _PyCFunction_CAST(_io__IOBase_fileno), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_fileno__doc__},
+
+static PyObject *
+_io__IOBase_fileno_impl(PyObject *self, PyTypeObject *cls);
+
+static PyObject *
+_io__IOBase_fileno(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "fileno() takes no arguments");
+ return NULL;
+ }
+ return _io__IOBase_fileno_impl(self, cls);
+}
+
+PyDoc_STRVAR(_io__IOBase_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n"
+"Return whether this is an \'interactive\' stream.\n"
+"\n"
+"Return False if it can\'t be determined.");
+
+#define _IO__IOBASE_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io__IOBase_isatty, METH_NOARGS, _io__IOBase_isatty__doc__},
+
+static PyObject *
+_io__IOBase_isatty_impl(PyObject *self);
+
+static PyObject *
+_io__IOBase_isatty(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__IOBase_isatty_impl(self);
+}
+
+PyDoc_STRVAR(_io__IOBase_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read and return a line from the stream.\n"
+"\n"
+"If size is specified, at most size bytes will be read.\n"
+"\n"
+"The line terminator is always b\'\\n\' for binary files; for text\n"
+"files, the newlines argument to open can be used to select the line\n"
+"terminator(s) recognized.");
+
+#define _IO__IOBASE_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io__IOBase_readline), METH_FASTCALL, _io__IOBase_readline__doc__},
+
+static PyObject *
+_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit);
+
+static PyObject *
+_io__IOBase_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t limit = -1;
+
+ if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &limit)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io__IOBase_readline_impl(self, limit);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__IOBase_readlines__doc__,
+"readlines($self, hint=-1, /)\n"
+"--\n"
+"\n"
+"Return a list of lines from the stream.\n"
+"\n"
+"hint can be specified to control the number of lines read: no more\n"
+"lines will be read if the total size (in bytes/characters) of all\n"
+"lines so far exceeds hint.");
+
+#define _IO__IOBASE_READLINES_METHODDEF \
+ {"readlines", _PyCFunction_CAST(_io__IOBase_readlines), METH_FASTCALL, _io__IOBase_readlines__doc__},
+
+static PyObject *
+_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint);
+
+static PyObject *
+_io__IOBase_readlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t hint = -1;
+
+ if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &hint)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io__IOBase_readlines_impl(self, hint);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__IOBase_writelines__doc__,
+"writelines($self, lines, /)\n"
+"--\n"
+"\n"
+"Write a list of lines to stream.\n"
+"\n"
+"Line separators are not added, so it is usual for each of the\n"
+"lines provided to have a line separator at the end.");
+
+#define _IO__IOBASE_WRITELINES_METHODDEF \
+ {"writelines", (PyCFunction)_io__IOBase_writelines, METH_O, _io__IOBase_writelines__doc__},
+
+PyDoc_STRVAR(_io__RawIOBase_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO__RAWIOBASE_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io__RawIOBase_read), METH_FASTCALL, _io__RawIOBase_read__doc__},
+
+static PyObject *
+_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n);
+
+static PyObject *
+_io__RawIOBase_read(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t n = -1;
+
+ if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ n = ival;
+ }
+skip_optional:
+ return_value = _io__RawIOBase_read_impl(self, n);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__RawIOBase_readall__doc__,
+"readall($self, /)\n"
+"--\n"
+"\n"
+"Read until EOF, using multiple read() call.");
+
+#define _IO__RAWIOBASE_READALL_METHODDEF \
+ {"readall", (PyCFunction)_io__RawIOBase_readall, METH_NOARGS, _io__RawIOBase_readall__doc__},
+
+static PyObject *
+_io__RawIOBase_readall_impl(PyObject *self);
+
+static PyObject *
+_io__RawIOBase_readall(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__RawIOBase_readall_impl(self);
+}
+/*[clinic end generated code: output=60faa842b41185d2 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/stringio.c.h b/contrib/tools/python3/Modules/_io/clinic/stringio.c.h
new file mode 100644
index 0000000000..d495dd10c1
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/stringio.c.h
@@ -0,0 +1,370 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io_StringIO_getvalue__doc__,
+"getvalue($self, /)\n"
+"--\n"
+"\n"
+"Retrieve the entire contents of the object.");
+
+#define _IO_STRINGIO_GETVALUE_METHODDEF \
+ {"getvalue", (PyCFunction)_io_StringIO_getvalue, METH_NOARGS, _io_StringIO_getvalue__doc__},
+
+static PyObject *
+_io_StringIO_getvalue_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_getvalue(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_getvalue_impl(self);
+}
+
+PyDoc_STRVAR(_io_StringIO_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n"
+"Tell the current file position.");
+
+#define _IO_STRINGIO_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io_StringIO_tell, METH_NOARGS, _io_StringIO_tell__doc__},
+
+static PyObject *
+_io_StringIO_tell_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_tell(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_tell_impl(self);
+}
+
+PyDoc_STRVAR(_io_StringIO_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size characters, returned as a string.\n"
+"\n"
+"If the argument is negative or omitted, read until EOF\n"
+"is reached. Return an empty string at EOF.");
+
+#define _IO_STRINGIO_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io_StringIO_read), METH_FASTCALL, _io_StringIO_read__doc__},
+
+static PyObject *
+_io_StringIO_read_impl(stringio *self, Py_ssize_t size);
+
+static PyObject *
+_io_StringIO_read(stringio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_StringIO_read_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_StringIO_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read until newline or EOF.\n"
+"\n"
+"Returns an empty string if EOF is hit immediately.");
+
+#define _IO_STRINGIO_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io_StringIO_readline), METH_FASTCALL, _io_StringIO_readline__doc__},
+
+static PyObject *
+_io_StringIO_readline_impl(stringio *self, Py_ssize_t size);
+
+static PyObject *
+_io_StringIO_readline(stringio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_StringIO_readline_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_StringIO_truncate__doc__,
+"truncate($self, pos=None, /)\n"
+"--\n"
+"\n"
+"Truncate size to pos.\n"
+"\n"
+"The pos argument defaults to the current file position, as\n"
+"returned by tell(). The current file position is unchanged.\n"
+"Returns the new absolute position.");
+
+#define _IO_STRINGIO_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io_StringIO_truncate), METH_FASTCALL, _io_StringIO_truncate__doc__},
+
+static PyObject *
+_io_StringIO_truncate_impl(stringio *self, Py_ssize_t size);
+
+static PyObject *
+_io_StringIO_truncate(stringio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = self->pos;
+
+ if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_StringIO_truncate_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_StringIO_seek__doc__,
+"seek($self, pos, whence=0, /)\n"
+"--\n"
+"\n"
+"Change stream position.\n"
+"\n"
+"Seek to character offset pos relative to position indicated by whence:\n"
+" 0 Start of stream (the default). pos should be >= 0;\n"
+" 1 Current position - pos must be 0;\n"
+" 2 End of stream - pos must be 0.\n"
+"Returns the new absolute position.");
+
+#define _IO_STRINGIO_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io_StringIO_seek), METH_FASTCALL, _io_StringIO_seek__doc__},
+
+static PyObject *
+_io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence);
+
+static PyObject *
+_io_StringIO_seek(stringio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t pos;
+ int whence = 0;
+
+ if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ goto exit;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ pos = ival;
+ }
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_StringIO_seek_impl(self, pos, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_StringIO_write__doc__,
+"write($self, s, /)\n"
+"--\n"
+"\n"
+"Write string to file.\n"
+"\n"
+"Returns the number of characters written, which is always equal to\n"
+"the length of the string.");
+
+#define _IO_STRINGIO_WRITE_METHODDEF \
+ {"write", (PyCFunction)_io_StringIO_write, METH_O, _io_StringIO_write__doc__},
+
+PyDoc_STRVAR(_io_StringIO_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n"
+"Close the IO object.\n"
+"\n"
+"Attempting any further operation after the object is closed\n"
+"will raise a ValueError.\n"
+"\n"
+"This method has no effect if the file is already closed.");
+
+#define _IO_STRINGIO_CLOSE_METHODDEF \
+ {"close", (PyCFunction)_io_StringIO_close, METH_NOARGS, _io_StringIO_close__doc__},
+
+static PyObject *
+_io_StringIO_close_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_close(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_close_impl(self);
+}
+
+PyDoc_STRVAR(_io_StringIO___init____doc__,
+"StringIO(initial_value=\'\', newline=\'\\n\')\n"
+"--\n"
+"\n"
+"Text I/O implementation using an in-memory buffer.\n"
+"\n"
+"The initial_value argument sets the value of object. The newline\n"
+"argument is like the one of TextIOWrapper\'s constructor.");
+
+static int
+_io_StringIO___init___impl(stringio *self, PyObject *value,
+ PyObject *newline_obj);
+
+static int
+_io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 2
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"initial_value", "newline", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "StringIO",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
+ PyObject *value = NULL;
+ PyObject *newline_obj = NULL;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (fastargs[0]) {
+ value = fastargs[0];
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ newline_obj = fastargs[1];
+skip_optional_pos:
+ return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_StringIO_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be read.");
+
+#define _IO_STRINGIO_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io_StringIO_readable, METH_NOARGS, _io_StringIO_readable__doc__},
+
+static PyObject *
+_io_StringIO_readable_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_readable(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io_StringIO_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be written.");
+
+#define _IO_STRINGIO_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io_StringIO_writable, METH_NOARGS, _io_StringIO_writable__doc__},
+
+static PyObject *
+_io_StringIO_writable_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_writable(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io_StringIO_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n"
+"Returns True if the IO object can be seeked.");
+
+#define _IO_STRINGIO_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io_StringIO_seekable, METH_NOARGS, _io_StringIO_seekable__doc__},
+
+static PyObject *
+_io_StringIO_seekable_impl(stringio *self);
+
+static PyObject *
+_io_StringIO_seekable(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_StringIO_seekable_impl(self);
+}
+/*[clinic end generated code: output=533f20ae9b773126 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/textio.c.h b/contrib/tools/python3/Modules/_io/clinic/textio.c.h
new file mode 100644
index 0000000000..1f67434f88
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/textio.c.h
@@ -0,0 +1,985 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+PyDoc_STRVAR(_io__TextIOBase_detach__doc__,
+"detach($self, /)\n"
+"--\n"
+"\n"
+"Separate the underlying buffer from the TextIOBase and return it.\n"
+"\n"
+"After the underlying buffer has been detached, the TextIO is in an unusable state.");
+
+#define _IO__TEXTIOBASE_DETACH_METHODDEF \
+ {"detach", _PyCFunction_CAST(_io__TextIOBase_detach), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__TextIOBase_detach__doc__},
+
+static PyObject *
+_io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls);
+
+static PyObject *
+_io__TextIOBase_detach(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "detach() takes no arguments");
+ return NULL;
+ }
+ return _io__TextIOBase_detach_impl(self, cls);
+}
+
+PyDoc_STRVAR(_io__TextIOBase_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size characters from stream.\n"
+"\n"
+"Read from underlying buffer until we have size characters or we hit EOF.\n"
+"If size is negative or omitted, read until EOF.");
+
+#define _IO__TEXTIOBASE_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io__TextIOBase_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__TextIOBase_read__doc__},
+
+static PyObject *
+_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size));
+
+static PyObject *
+_io__TextIOBase_read(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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];
+ int size = -1;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ size = _PyLong_AsInt(args[0]);
+ if (size == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__TextIOBase_read_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__TextIOBase_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read until newline or EOF.\n"
+"\n"
+"Return an empty string if EOF is hit immediately.\n"
+"If size is specified, at most size characters will be read.");
+
+#define _IO__TEXTIOBASE_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io__TextIOBase_readline), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__TextIOBase_readline__doc__},
+
+static PyObject *
+_io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size));
+
+static PyObject *
+_io__TextIOBase_readline(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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 = "readline",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ int size = -1;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ size = _PyLong_AsInt(args[0]);
+ if (size == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__TextIOBase_readline_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io__TextIOBase_write__doc__,
+"write($self, s, /)\n"
+"--\n"
+"\n"
+"Write string s to stream.\n"
+"\n"
+"Return the number of characters written\n"
+"(which is always equal to the length of the string).");
+
+#define _IO__TEXTIOBASE_WRITE_METHODDEF \
+ {"write", _PyCFunction_CAST(_io__TextIOBase_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__TextIOBase_write__doc__},
+
+static PyObject *
+_io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls,
+ const char *Py_UNUSED(s));
+
+static PyObject *
+_io__TextIOBase_write(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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];
+ const char *s;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!PyUnicode_Check(args[0])) {
+ _PyArg_BadArgument("write", "argument 1", "str", args[0]);
+ goto exit;
+ }
+ Py_ssize_t s_length;
+ s = PyUnicode_AsUTF8AndSize(args[0], &s_length);
+ if (s == NULL) {
+ goto exit;
+ }
+ if (strlen(s) != (size_t)s_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ return_value = _io__TextIOBase_write_impl(self, cls, s);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_IncrementalNewlineDecoder___init____doc__,
+"IncrementalNewlineDecoder(decoder, translate, errors=\'strict\')\n"
+"--\n"
+"\n"
+"Codec used when reading a file in universal newlines mode.\n"
+"\n"
+"It wraps another incremental decoder, translating \\r\\n and \\r into \\n.\n"
+"It also records the types of newlines encountered. When used with\n"
+"translate=False, it ensures that the newline sequence is returned in\n"
+"one piece. When used with decoder=None, it expects unicode strings as\n"
+"decode input and translates newlines without first invoking an external\n"
+"decoder.");
+
+static int
+_io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
+ PyObject *decoder, int translate,
+ PyObject *errors);
+
+static int
+_io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 3
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(decoder), &_Py_ID(translate), &_Py_ID(errors), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"decoder", "translate", "errors", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "IncrementalNewlineDecoder",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[3];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
+ PyObject *decoder;
+ int translate;
+ PyObject *errors = NULL;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 3, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ decoder = fastargs[0];
+ translate = PyObject_IsTrue(fastargs[1]);
+ if (translate < 0) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ errors = fastargs[2];
+skip_optional_pos:
+ return_value = _io_IncrementalNewlineDecoder___init___impl((nldecoder_object *)self, decoder, translate, errors);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_IncrementalNewlineDecoder_decode__doc__,
+"decode($self, /, input, final=False)\n"
+"--\n"
+"\n");
+
+#define _IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF \
+ {"decode", _PyCFunction_CAST(_io_IncrementalNewlineDecoder_decode), METH_FASTCALL|METH_KEYWORDS, _io_IncrementalNewlineDecoder_decode__doc__},
+
+static PyObject *
+_io_IncrementalNewlineDecoder_decode_impl(nldecoder_object *self,
+ PyObject *input, int final);
+
+static PyObject *
+_io_IncrementalNewlineDecoder_decode(nldecoder_object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 2
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(input), &_Py_ID(final), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"input", "final", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "decode",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[2];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
+ PyObject *input;
+ int final = 0;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ input = args[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ final = PyObject_IsTrue(args[1]);
+ if (final < 0) {
+ goto exit;
+ }
+skip_optional_pos:
+ return_value = _io_IncrementalNewlineDecoder_decode_impl(self, input, final);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_IncrementalNewlineDecoder_getstate__doc__,
+"getstate($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF \
+ {"getstate", (PyCFunction)_io_IncrementalNewlineDecoder_getstate, METH_NOARGS, _io_IncrementalNewlineDecoder_getstate__doc__},
+
+static PyObject *
+_io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self);
+
+static PyObject *
+_io_IncrementalNewlineDecoder_getstate(nldecoder_object *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_IncrementalNewlineDecoder_getstate_impl(self);
+}
+
+PyDoc_STRVAR(_io_IncrementalNewlineDecoder_setstate__doc__,
+"setstate($self, state, /)\n"
+"--\n"
+"\n");
+
+#define _IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF \
+ {"setstate", (PyCFunction)_io_IncrementalNewlineDecoder_setstate, METH_O, _io_IncrementalNewlineDecoder_setstate__doc__},
+
+PyDoc_STRVAR(_io_IncrementalNewlineDecoder_reset__doc__,
+"reset($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF \
+ {"reset", (PyCFunction)_io_IncrementalNewlineDecoder_reset, METH_NOARGS, _io_IncrementalNewlineDecoder_reset__doc__},
+
+static PyObject *
+_io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self);
+
+static PyObject *
+_io_IncrementalNewlineDecoder_reset(nldecoder_object *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_IncrementalNewlineDecoder_reset_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper___init____doc__,
+"TextIOWrapper(buffer, encoding=None, errors=None, newline=None,\n"
+" line_buffering=False, write_through=False)\n"
+"--\n"
+"\n"
+"Character and line based layer over a BufferedIOBase object, buffer.\n"
+"\n"
+"encoding gives the name of the encoding that the stream will be\n"
+"decoded or encoded with. It defaults to locale.getencoding().\n"
+"\n"
+"errors determines the strictness of encoding and decoding (see\n"
+"help(codecs.Codec) or the documentation for codecs.register) and\n"
+"defaults to \"strict\".\n"
+"\n"
+"newline controls how line endings are handled. It can be None, \'\',\n"
+"\'\\n\', \'\\r\', and \'\\r\\n\'. It works as follows:\n"
+"\n"
+"* On input, if newline is None, universal newlines mode is\n"
+" enabled. Lines in the input can end in \'\\n\', \'\\r\', or \'\\r\\n\', and\n"
+" these are translated into \'\\n\' before being returned to the\n"
+" caller. If it is \'\', universal newline mode is enabled, but line\n"
+" endings are returned to the caller untranslated. If it has any of\n"
+" the other legal values, input lines are only terminated by the given\n"
+" string, and the line ending is returned to the caller untranslated.\n"
+"\n"
+"* On output, if newline is None, any \'\\n\' characters written are\n"
+" translated to the system default line separator, os.linesep. If\n"
+" newline is \'\' or \'\\n\', no translation takes place. If newline is any\n"
+" of the other legal values, any \'\\n\' characters written are translated\n"
+" to the given string.\n"
+"\n"
+"If line_buffering is True, a call to flush is implied when a call to\n"
+"write contains a newline character.");
+
+static int
+_io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
+ const char *encoding, PyObject *errors,
+ const char *newline, int line_buffering,
+ int write_through);
+
+static int
+_io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 6
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(buffer), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(line_buffering), &_Py_ID(write_through), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"buffer", "encoding", "errors", "newline", "line_buffering", "write_through", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "TextIOWrapper",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[6];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *buffer;
+ const char *encoding = NULL;
+ PyObject *errors = Py_None;
+ const char *newline = NULL;
+ int line_buffering = 0;
+ int write_through = 0;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 6, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ buffer = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (fastargs[1]) {
+ if (fastargs[1] == Py_None) {
+ encoding = NULL;
+ }
+ else if (PyUnicode_Check(fastargs[1])) {
+ Py_ssize_t encoding_length;
+ encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
+ if (encoding == NULL) {
+ goto exit;
+ }
+ if (strlen(encoding) != (size_t)encoding_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ }
+ else {
+ _PyArg_BadArgument("TextIOWrapper", "argument 'encoding'", "str or None", fastargs[1]);
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (fastargs[2]) {
+ errors = fastargs[2];
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (fastargs[3]) {
+ if (fastargs[3] == Py_None) {
+ newline = NULL;
+ }
+ else if (PyUnicode_Check(fastargs[3])) {
+ Py_ssize_t newline_length;
+ newline = PyUnicode_AsUTF8AndSize(fastargs[3], &newline_length);
+ if (newline == NULL) {
+ goto exit;
+ }
+ if (strlen(newline) != (size_t)newline_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ }
+ else {
+ _PyArg_BadArgument("TextIOWrapper", "argument 'newline'", "str or None", fastargs[3]);
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (fastargs[4]) {
+ line_buffering = PyObject_IsTrue(fastargs[4]);
+ if (line_buffering < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ write_through = PyObject_IsTrue(fastargs[5]);
+ if (write_through < 0) {
+ goto exit;
+ }
+skip_optional_pos:
+ return_value = _io_TextIOWrapper___init___impl((textio *)self, buffer, encoding, errors, newline, line_buffering, write_through);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_reconfigure__doc__,
+"reconfigure($self, /, *, encoding=None, errors=None, newline=None,\n"
+" line_buffering=None, write_through=None)\n"
+"--\n"
+"\n"
+"Reconfigure the text stream with new parameters.\n"
+"\n"
+"This also does an implicit stream flush.");
+
+#define _IO_TEXTIOWRAPPER_RECONFIGURE_METHODDEF \
+ {"reconfigure", _PyCFunction_CAST(_io_TextIOWrapper_reconfigure), METH_FASTCALL|METH_KEYWORDS, _io_TextIOWrapper_reconfigure__doc__},
+
+static PyObject *
+_io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
+ PyObject *errors, PyObject *newline_obj,
+ PyObject *line_buffering_obj,
+ PyObject *write_through_obj);
+
+static PyObject *
+_io_TextIOWrapper_reconfigure(textio *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 5
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(line_buffering), &_Py_ID(write_through), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"encoding", "errors", "newline", "line_buffering", "write_through", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "reconfigure",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[5];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ PyObject *encoding = Py_None;
+ PyObject *errors = Py_None;
+ PyObject *newline_obj = NULL;
+ PyObject *line_buffering_obj = Py_None;
+ PyObject *write_through_obj = Py_None;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ if (args[0]) {
+ encoding = args[0];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (args[1]) {
+ errors = args[1];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (args[2]) {
+ newline_obj = args[2];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (args[3]) {
+ line_buffering_obj = args[3];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ write_through_obj = args[4];
+skip_optional_kwonly:
+ return_value = _io_TextIOWrapper_reconfigure_impl(self, encoding, errors, newline_obj, line_buffering_obj, write_through_obj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_detach__doc__,
+"detach($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_DETACH_METHODDEF \
+ {"detach", (PyCFunction)_io_TextIOWrapper_detach, METH_NOARGS, _io_TextIOWrapper_detach__doc__},
+
+static PyObject *
+_io_TextIOWrapper_detach_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_detach(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_detach_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_write__doc__,
+"write($self, text, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_WRITE_METHODDEF \
+ {"write", (PyCFunction)_io_TextIOWrapper_write, METH_O, _io_TextIOWrapper_write__doc__},
+
+static PyObject *
+_io_TextIOWrapper_write_impl(textio *self, PyObject *text);
+
+static PyObject *
+_io_TextIOWrapper_write(textio *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ PyObject *text;
+
+ if (!PyUnicode_Check(arg)) {
+ _PyArg_BadArgument("write", "argument", "str", arg);
+ goto exit;
+ }
+ if (PyUnicode_READY(arg) == -1) {
+ goto exit;
+ }
+ text = arg;
+ return_value = _io_TextIOWrapper_write_impl(self, text);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io_TextIOWrapper_read), METH_FASTCALL, _io_TextIOWrapper_read__doc__},
+
+static PyObject *
+_io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n);
+
+static PyObject *
+_io_TextIOWrapper_read(textio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t n = -1;
+
+ if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &n)) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_TextIOWrapper_read_impl(self, n);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_readline__doc__,
+"readline($self, size=-1, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_READLINE_METHODDEF \
+ {"readline", _PyCFunction_CAST(_io_TextIOWrapper_readline), METH_FASTCALL, _io_TextIOWrapper_readline__doc__},
+
+static PyObject *
+_io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size);
+
+static PyObject *
+_io_TextIOWrapper_readline(textio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t size = -1;
+
+ if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = _PyNumber_Index(args[0]);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ size = ival;
+ }
+skip_optional:
+ return_value = _io_TextIOWrapper_readline_impl(self, size);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_seek__doc__,
+"seek($self, cookie, whence=os.SEEK_SET, /)\n"
+"--\n"
+"\n"
+"Set the stream position, and return the new stream position.\n"
+"\n"
+" cookie\n"
+" Zero or an opaque number returned by tell().\n"
+" whence\n"
+" The relative position to seek from.\n"
+"\n"
+"Four operations are supported, given by the following argument\n"
+"combinations:\n"
+"\n"
+"- seek(0, SEEK_SET): Rewind to the start of the stream.\n"
+"- seek(cookie, SEEK_SET): Restore a previous position;\n"
+" \'cookie\' must be a number returned by tell().\n"
+"- seek(0, SEEK_END): Fast-forward to the end of the stream.\n"
+"- seek(0, SEEK_CUR): Leave the current stream position unchanged.\n"
+"\n"
+"Any other argument combinations are invalid,\n"
+"and may raise exceptions.");
+
+#define _IO_TEXTIOWRAPPER_SEEK_METHODDEF \
+ {"seek", _PyCFunction_CAST(_io_TextIOWrapper_seek), METH_FASTCALL, _io_TextIOWrapper_seek__doc__},
+
+static PyObject *
+_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence);
+
+static PyObject *
+_io_TextIOWrapper_seek(textio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *cookieObj;
+ int whence = 0;
+
+ if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ goto exit;
+ }
+ cookieObj = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ whence = _PyLong_AsInt(args[1]);
+ if (whence == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_TextIOWrapper_seek_impl(self, cookieObj, whence);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_tell__doc__,
+"tell($self, /)\n"
+"--\n"
+"\n"
+"Return the stream position as an opaque number.\n"
+"\n"
+"The return value of tell() can be given as input to seek(), to restore a\n"
+"previous stream position.");
+
+#define _IO_TEXTIOWRAPPER_TELL_METHODDEF \
+ {"tell", (PyCFunction)_io_TextIOWrapper_tell, METH_NOARGS, _io_TextIOWrapper_tell__doc__},
+
+static PyObject *
+_io_TextIOWrapper_tell_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_tell(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_tell_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_truncate__doc__,
+"truncate($self, pos=None, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF \
+ {"truncate", _PyCFunction_CAST(_io_TextIOWrapper_truncate), METH_FASTCALL, _io_TextIOWrapper_truncate__doc__},
+
+static PyObject *
+_io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos);
+
+static PyObject *
+_io_TextIOWrapper_truncate(textio *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *pos = Py_None;
+
+ if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ pos = args[0];
+skip_optional:
+ return_value = _io_TextIOWrapper_truncate_impl(self, pos);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_fileno__doc__,
+"fileno($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_FILENO_METHODDEF \
+ {"fileno", (PyCFunction)_io_TextIOWrapper_fileno, METH_NOARGS, _io_TextIOWrapper_fileno__doc__},
+
+static PyObject *
+_io_TextIOWrapper_fileno_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_fileno(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_fileno_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_seekable__doc__,
+"seekable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF \
+ {"seekable", (PyCFunction)_io_TextIOWrapper_seekable, METH_NOARGS, _io_TextIOWrapper_seekable__doc__},
+
+static PyObject *
+_io_TextIOWrapper_seekable_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_seekable(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_seekable_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io_TextIOWrapper_readable, METH_NOARGS, _io_TextIOWrapper_readable__doc__},
+
+static PyObject *
+_io_TextIOWrapper_readable_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_readable(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_readable_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io_TextIOWrapper_writable, METH_NOARGS, _io_TextIOWrapper_writable__doc__},
+
+static PyObject *
+_io_TextIOWrapper_writable_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_writable(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_writable_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io_TextIOWrapper_isatty, METH_NOARGS, _io_TextIOWrapper_isatty__doc__},
+
+static PyObject *
+_io_TextIOWrapper_isatty_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_isatty(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_isatty_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_flush__doc__,
+"flush($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_FLUSH_METHODDEF \
+ {"flush", (PyCFunction)_io_TextIOWrapper_flush, METH_NOARGS, _io_TextIOWrapper_flush__doc__},
+
+static PyObject *
+_io_TextIOWrapper_flush_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_flush(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_flush_impl(self);
+}
+
+PyDoc_STRVAR(_io_TextIOWrapper_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n");
+
+#define _IO_TEXTIOWRAPPER_CLOSE_METHODDEF \
+ {"close", (PyCFunction)_io_TextIOWrapper_close, METH_NOARGS, _io_TextIOWrapper_close__doc__},
+
+static PyObject *
+_io_TextIOWrapper_close_impl(textio *self);
+
+static PyObject *
+_io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io_TextIOWrapper_close_impl(self);
+}
+/*[clinic end generated code: output=408adcf5b8c5d8a6 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/clinic/winconsoleio.c.h b/contrib/tools/python3/Modules/_io/clinic/winconsoleio.c.h
new file mode 100644
index 0000000000..064ed3814d
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/clinic/winconsoleio.c.h
@@ -0,0 +1,468 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+# include "pycore_gc.h" // PyGC_Head
+# include "pycore_runtime.h" // _Py_ID()
+#endif
+
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_close__doc__,
+"close($self, /)\n"
+"--\n"
+"\n"
+"Close the console object.\n"
+"\n"
+"A closed console object cannot be used for further I/O operations.\n"
+"close() may be called more than once without error.");
+
+#define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \
+ {"close", _PyCFunction_CAST(_io__WindowsConsoleIO_close), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_close__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_close_impl(winconsoleio *self, PyTypeObject *cls);
+
+static PyObject *
+_io__WindowsConsoleIO_close(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
+ PyErr_SetString(PyExc_TypeError, "close() takes no arguments");
+ return NULL;
+ }
+ return _io__WindowsConsoleIO_close_impl(self, cls);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO___init____doc__,
+"_WindowsConsoleIO(file, mode=\'r\', closefd=True, opener=None)\n"
+"--\n"
+"\n"
+"Open a console buffer by file descriptor.\n"
+"\n"
+"The mode can be \'rb\' (default), or \'wb\' for reading or writing bytes. All\n"
+"other mode characters will be ignored. Mode \'b\' will be assumed if it is\n"
+"omitted. The *opener* parameter is always ignored.");
+
+static int
+_io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
+ const char *mode, int closefd,
+ PyObject *opener);
+
+static int
+_io__WindowsConsoleIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ int return_value = -1;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 4
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(closefd), &_Py_ID(opener), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "_WindowsConsoleIO",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[4];
+ PyObject * const *fastargs;
+ Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ PyObject *nameobj;
+ const char *mode = "r";
+ int closefd = 1;
+ PyObject *opener = Py_None;
+
+ fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 4, 0, argsbuf);
+ if (!fastargs) {
+ goto exit;
+ }
+ nameobj = fastargs[0];
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (fastargs[1]) {
+ if (!PyUnicode_Check(fastargs[1])) {
+ _PyArg_BadArgument("_WindowsConsoleIO", "argument 'mode'", "str", fastargs[1]);
+ goto exit;
+ }
+ Py_ssize_t mode_length;
+ mode = PyUnicode_AsUTF8AndSize(fastargs[1], &mode_length);
+ if (mode == NULL) {
+ goto exit;
+ }
+ if (strlen(mode) != (size_t)mode_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (fastargs[2]) {
+ closefd = PyObject_IsTrue(fastargs[2]);
+ if (closefd < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ opener = fastargs[3];
+skip_optional_pos:
+ return_value = _io__WindowsConsoleIO___init___impl((winconsoleio *)self, nameobj, mode, closefd, opener);
+
+exit:
+ return return_value;
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_fileno__doc__,
+"fileno($self, /)\n"
+"--\n"
+"\n"
+"Return the underlying file descriptor (an integer).");
+
+#define _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF \
+ {"fileno", (PyCFunction)_io__WindowsConsoleIO_fileno, METH_NOARGS, _io__WindowsConsoleIO_fileno__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_fileno_impl(winconsoleio *self);
+
+static PyObject *
+_io__WindowsConsoleIO_fileno(winconsoleio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__WindowsConsoleIO_fileno_impl(self);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_readable__doc__,
+"readable($self, /)\n"
+"--\n"
+"\n"
+"True if console is an input buffer.");
+
+#define _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF \
+ {"readable", (PyCFunction)_io__WindowsConsoleIO_readable, METH_NOARGS, _io__WindowsConsoleIO_readable__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_readable_impl(winconsoleio *self);
+
+static PyObject *
+_io__WindowsConsoleIO_readable(winconsoleio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__WindowsConsoleIO_readable_impl(self);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_writable__doc__,
+"writable($self, /)\n"
+"--\n"
+"\n"
+"True if console is an output buffer.");
+
+#define _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF \
+ {"writable", (PyCFunction)_io__WindowsConsoleIO_writable, METH_NOARGS, _io__WindowsConsoleIO_writable__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_writable_impl(winconsoleio *self);
+
+static PyObject *
+_io__WindowsConsoleIO_writable(winconsoleio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__WindowsConsoleIO_writable_impl(self);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_readinto__doc__,
+"readinto($self, buffer, /)\n"
+"--\n"
+"\n"
+"Same as RawIOBase.readinto().");
+
+#define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF \
+ {"readinto", _PyCFunction_CAST(_io__WindowsConsoleIO_readinto), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_readinto__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_buffer *buffer);
+
+static PyObject *
+_io__WindowsConsoleIO_readinto(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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};
+
+ 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();
+ _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]);
+ goto exit;
+ }
+ if (!PyBuffer_IsContiguous(&buffer, 'C')) {
+ _PyArg_BadArgument("readinto", "argument 1", "contiguous buffer", args[0]);
+ goto exit;
+ }
+ return_value = _io__WindowsConsoleIO_readinto_impl(self, cls, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj) {
+ PyBuffer_Release(&buffer);
+ }
+
+ return return_value;
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_readall__doc__,
+"readall($self, /)\n"
+"--\n"
+"\n"
+"Read all data from the console, returned as bytes.\n"
+"\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO__WINDOWSCONSOLEIO_READALL_METHODDEF \
+ {"readall", (PyCFunction)_io__WindowsConsoleIO_readall, METH_NOARGS, _io__WindowsConsoleIO_readall__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_readall_impl(winconsoleio *self);
+
+static PyObject *
+_io__WindowsConsoleIO_readall(winconsoleio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__WindowsConsoleIO_readall_impl(self);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_read__doc__,
+"read($self, size=-1, /)\n"
+"--\n"
+"\n"
+"Read at most size bytes, returned as bytes.\n"
+"\n"
+"Only makes one system call when size is a positive integer,\n"
+"so less data may be returned than requested.\n"
+"Return an empty bytes object at EOF.");
+
+#define _IO__WINDOWSCONSOLEIO_READ_METHODDEF \
+ {"read", _PyCFunction_CAST(_io__WindowsConsoleIO_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_read__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_ssize_t size);
+
+static PyObject *
+_io__WindowsConsoleIO_read(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional_posonly;
+ }
+ if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
+ goto exit;
+ }
+skip_optional_posonly:
+ return_value = _io__WindowsConsoleIO_read_impl(self, cls, size);
+
+exit:
+ return return_value;
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_write__doc__,
+"write($self, b, /)\n"
+"--\n"
+"\n"
+"Write buffer b to file, return number of bytes written.\n"
+"\n"
+"Only makes one system call, so not all of the data may be written.\n"
+"The number of bytes actually written is returned.");
+
+#define _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF \
+ {"write", _PyCFunction_CAST(_io__WindowsConsoleIO_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__WindowsConsoleIO_write__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_buffer *b);
+
+static PyObject *
+_io__WindowsConsoleIO_write(winconsoleio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ 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};
+
+ 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;
+ }
+ if (!PyBuffer_IsContiguous(&b, 'C')) {
+ _PyArg_BadArgument("write", "argument 1", "contiguous buffer", args[0]);
+ goto exit;
+ }
+ return_value = _io__WindowsConsoleIO_write_impl(self, cls, &b);
+
+exit:
+ /* Cleanup for b */
+ if (b.obj) {
+ PyBuffer_Release(&b);
+ }
+
+ return return_value;
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#if defined(HAVE_WINDOWS_CONSOLE_IO)
+
+PyDoc_STRVAR(_io__WindowsConsoleIO_isatty__doc__,
+"isatty($self, /)\n"
+"--\n"
+"\n"
+"Always True.");
+
+#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF \
+ {"isatty", (PyCFunction)_io__WindowsConsoleIO_isatty, METH_NOARGS, _io__WindowsConsoleIO_isatty__doc__},
+
+static PyObject *
+_io__WindowsConsoleIO_isatty_impl(winconsoleio *self);
+
+static PyObject *
+_io__WindowsConsoleIO_isatty(winconsoleio *self, PyObject *Py_UNUSED(ignored))
+{
+ return _io__WindowsConsoleIO_isatty_impl(self);
+}
+
+#endif /* defined(HAVE_WINDOWS_CONSOLE_IO) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_FILENO_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_READABLE_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_READINTO_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_READALL_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_READALL_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_READALL_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_READ_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_READ_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_READ_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_WRITE_METHODDEF) */
+
+#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
+ #define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
+#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
+/*[clinic end generated code: output=9a9f95a1b52c95f9 input=a9049054013a1b77]*/
diff --git a/contrib/tools/python3/Modules/_io/fileio.c b/contrib/tools/python3/Modules/_io/fileio.c
new file mode 100644
index 0000000000..bab68077a2
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/fileio.c
@@ -0,0 +1,1230 @@
+/* Author: Daniel Stutzbach */
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
+#include "pycore_object.h" // _PyObject_GC_UNTRACK()
+#include "structmember.h" // PyMemberDef
+#include <stdbool.h>
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#include <stddef.h> /* For offsetof */
+#include "_iomodule.h"
+
+/*
+ * Known likely problems:
+ *
+ * - Files larger then 2**32-1
+ * - Files with unicode filenames
+ * - Passing numbers greater than 2**32-1 when an integer is expected
+ * - Making it work on Windows and other oddball platforms
+ *
+ * To Do:
+ *
+ * - autoconfify header file inclusion
+ */
+
+#ifdef MS_WINDOWS
+/* can simulate truncate with Win32 API functions; see file_truncate */
+#define HAVE_FTRUNCATE
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#endif
+
+#if BUFSIZ < (8*1024)
+#define SMALLCHUNK (8*1024)
+#elif (BUFSIZ >= (2 << 25))
+#error "unreasonable BUFSIZ > 64 MiB defined"
+#else
+#define SMALLCHUNK BUFSIZ
+#endif
+
+/*[clinic input]
+module _io
+class _io.FileIO "fileio *" "clinic_state()->PyFileIO_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ac25ec278f4d6703]*/
+
+typedef struct {
+ PyObject_HEAD
+ int fd;
+ unsigned int created : 1;
+ unsigned int readable : 1;
+ unsigned int writable : 1;
+ unsigned int appending : 1;
+ signed int seekable : 2; /* -1 means unknown */
+ unsigned int closefd : 1;
+ char finalizing;
+ unsigned int blksize;
+ PyObject *weakreflist;
+ PyObject *dict;
+} fileio;
+
+#define PyFileIO_Check(state, op) (PyObject_TypeCheck((op), state->PyFileIO_Type))
+
+/* Forward declarations */
+static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error);
+
+int
+_PyFileIO_closed(PyObject *self)
+{
+ return ((fileio *)self)->fd < 0;
+}
+
+/* Because this can call arbitrary code, it shouldn't be called when
+ the refcount is 0 (that is, not directly from tp_dealloc unless
+ the refcount has been temporarily re-incremented). */
+static PyObject *
+fileio_dealloc_warn(fileio *self, PyObject *source)
+{
+ if (self->fd >= 0 && self->closefd) {
+ PyObject *exc = PyErr_GetRaisedException();
+ if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
+ /* Spurious errors can appear at shutdown */
+ if (PyErr_ExceptionMatches(PyExc_Warning))
+ PyErr_WriteUnraisable((PyObject *) self);
+ }
+ PyErr_SetRaisedException(exc);
+ }
+ Py_RETURN_NONE;
+}
+
+/* Returns 0 on success, -1 with exception set on failure. */
+static int
+internal_close(fileio *self)
+{
+ int err = 0;
+ int save_errno = 0;
+ if (self->fd >= 0) {
+ int fd = self->fd;
+ self->fd = -1;
+ /* fd is accessible and someone else may have closed it */
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+ err = close(fd);
+ if (err < 0)
+ save_errno = errno;
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
+ }
+ if (err < 0) {
+ errno = save_errno;
+ PyErr_SetFromErrno(PyExc_OSError);
+ return -1;
+ }
+ return 0;
+}
+
+/*[clinic input]
+_io.FileIO.close
+
+ cls: defining_class
+ /
+
+Close the file.
+
+A closed file cannot be used for further I/O operations. close() may be
+called more than once without error.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
+/*[clinic end generated code: output=c30cbe9d1f23ca58 input=70da49e63db7c64d]*/
+{
+ PyObject *res;
+ int rc;
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ res = PyObject_CallMethodOneArg((PyObject*)state->PyRawIOBase_Type,
+ &_Py_ID(close), (PyObject *)self);
+ if (!self->closefd) {
+ self->fd = -1;
+ return res;
+ }
+
+ PyObject *exc = NULL;
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ if (self->finalizing) {
+ PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
+ if (r) {
+ Py_DECREF(r);
+ }
+ else {
+ PyErr_Clear();
+ }
+ }
+ rc = internal_close(self);
+ if (res == NULL) {
+ _PyErr_ChainExceptions1(exc);
+ }
+ if (rc < 0) {
+ Py_CLEAR(res);
+ }
+ return res;
+}
+
+static PyObject *
+fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ fileio *self;
+
+ assert(type != NULL && type->tp_alloc != NULL);
+
+ self = (fileio *) type->tp_alloc(type, 0);
+ if (self != NULL) {
+ self->fd = -1;
+ self->created = 0;
+ self->readable = 0;
+ self->writable = 0;
+ self->appending = 0;
+ self->seekable = -1;
+ self->blksize = 0;
+ self->closefd = 1;
+ self->weakreflist = NULL;
+ }
+
+ return (PyObject *) self;
+}
+
+#ifdef O_CLOEXEC
+extern int _Py_open_cloexec_works;
+#endif
+
+/*[clinic input]
+_io.FileIO.__init__
+ file as nameobj: object
+ mode: str = "r"
+ closefd: bool = True
+ opener: object = None
+
+Open a file.
+
+The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
+writing, exclusive creation or appending. The file will be created if it
+doesn't exist when opened for writing or appending; it will be truncated
+when opened for writing. A FileExistsError will be raised if it already
+exists when opened for creating. Opening a file for creating implies
+writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
+to allow simultaneous reading and writing. A custom opener can be used by
+passing a callable as *opener*. The underlying file descriptor for the file
+object is then obtained by calling opener with (*name*, *flags*).
+*opener* must return an open file descriptor (passing os.open as *opener*
+results in functionality similar to passing None).
+[clinic start generated code]*/
+
+static int
+_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
+ int closefd, PyObject *opener)
+/*[clinic end generated code: output=23413f68e6484bbd input=588aac967e0ba74b]*/
+{
+#ifdef MS_WINDOWS
+ Py_UNICODE *widename = NULL;
+#else
+ const char *name = NULL;
+#endif
+ PyObject *stringobj = NULL;
+ const char *s;
+ int ret = 0;
+ int rwa = 0, plus = 0;
+ int flags = 0;
+ int fd = -1;
+ int fd_is_own = 0;
+#ifdef O_CLOEXEC
+ int *atomic_flag_works = &_Py_open_cloexec_works;
+#elif !defined(MS_WINDOWS)
+ int *atomic_flag_works = NULL;
+#endif
+ struct _Py_stat_struct fdfstat;
+ int fstat_result;
+ int async_err = 0;
+
+#ifdef Py_DEBUG
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ assert(PyFileIO_Check(state, self));
+#endif
+ if (self->fd >= 0) {
+ if (self->closefd) {
+ /* Have to close the existing file first. */
+ if (internal_close(self) < 0)
+ return -1;
+ }
+ else
+ self->fd = -1;
+ }
+
+ fd = _PyLong_AsInt(nameobj);
+ if (fd < 0) {
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative file descriptor");
+ return -1;
+ }
+ PyErr_Clear();
+ }
+
+ if (fd < 0) {
+#ifdef MS_WINDOWS
+ if (!PyUnicode_FSDecoder(nameobj, &stringobj)) {
+ return -1;
+ }
+ widename = PyUnicode_AsWideCharString(stringobj, NULL);
+ if (widename == NULL)
+ return -1;
+#else
+ if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
+ return -1;
+ }
+ name = PyBytes_AS_STRING(stringobj);
+#endif
+ }
+
+ s = mode;
+ while (*s) {
+ switch (*s++) {
+ case 'x':
+ if (rwa) {
+ bad_mode:
+ PyErr_SetString(PyExc_ValueError,
+ "Must have exactly one of create/read/write/append "
+ "mode and at most one plus");
+ goto error;
+ }
+ rwa = 1;
+ self->created = 1;
+ self->writable = 1;
+ flags |= O_EXCL | O_CREAT;
+ break;
+ case 'r':
+ if (rwa)
+ goto bad_mode;
+ rwa = 1;
+ self->readable = 1;
+ break;
+ case 'w':
+ if (rwa)
+ goto bad_mode;
+ rwa = 1;
+ self->writable = 1;
+ flags |= O_CREAT | O_TRUNC;
+ break;
+ case 'a':
+ if (rwa)
+ goto bad_mode;
+ rwa = 1;
+ self->writable = 1;
+ self->appending = 1;
+ flags |= O_APPEND | O_CREAT;
+ break;
+ case 'b':
+ break;
+ case '+':
+ if (plus)
+ goto bad_mode;
+ self->readable = self->writable = 1;
+ plus = 1;
+ break;
+ default:
+ PyErr_Format(PyExc_ValueError,
+ "invalid mode: %.200s", mode);
+ goto error;
+ }
+ }
+
+ if (!rwa)
+ goto bad_mode;
+
+ if (self->readable && self->writable)
+ flags |= O_RDWR;
+ else if (self->readable)
+ flags |= O_RDONLY;
+ else
+ flags |= O_WRONLY;
+
+#ifdef O_BINARY
+ flags |= O_BINARY;
+#endif
+
+#ifdef MS_WINDOWS
+ flags |= O_NOINHERIT;
+#elif defined(O_CLOEXEC)
+ flags |= O_CLOEXEC;
+#endif
+
+ if (PySys_Audit("open", "Osi", nameobj, mode, flags) < 0) {
+ goto error;
+ }
+
+ if (fd >= 0) {
+ self->fd = fd;
+ self->closefd = closefd;
+ }
+ else {
+ self->closefd = 1;
+ if (!closefd) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot use closefd=False with file name");
+ goto error;
+ }
+
+ errno = 0;
+ if (opener == Py_None) {
+ do {
+ Py_BEGIN_ALLOW_THREADS
+#ifdef MS_WINDOWS
+ self->fd = _wopen(widename, flags, 0666);
+#else
+ self->fd = open(name, flags, 0666);
+#endif
+ Py_END_ALLOW_THREADS
+ } while (self->fd < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
+
+ if (async_err)
+ goto error;
+
+ if (self->fd < 0) {
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ goto error;
+ }
+ }
+ else {
+ PyObject *fdobj;
+
+#ifndef MS_WINDOWS
+ /* the opener may clear the atomic flag */
+ atomic_flag_works = NULL;
+#endif
+
+ fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
+ if (fdobj == NULL)
+ goto error;
+ if (!PyLong_Check(fdobj)) {
+ Py_DECREF(fdobj);
+ PyErr_SetString(PyExc_TypeError,
+ "expected integer from opener");
+ goto error;
+ }
+
+ self->fd = _PyLong_AsInt(fdobj);
+ Py_DECREF(fdobj);
+ if (self->fd < 0) {
+ if (!PyErr_Occurred()) {
+ /* The opener returned a negative but didn't set an
+ exception. See issue #27066 */
+ PyErr_Format(PyExc_ValueError,
+ "opener returned %d", self->fd);
+ }
+ goto error;
+ }
+ }
+ fd_is_own = 1;
+
+#ifndef MS_WINDOWS
+ if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
+ goto error;
+#endif
+ }
+
+ self->blksize = DEFAULT_BUFFER_SIZE;
+ Py_BEGIN_ALLOW_THREADS
+ fstat_result = _Py_fstat_noraise(self->fd, &fdfstat);
+ Py_END_ALLOW_THREADS
+ if (fstat_result < 0) {
+ /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
+ an anonymous file on a Virtual Box shared folder filesystem would
+ raise ENOENT. */
+#ifdef MS_WINDOWS
+ if (GetLastError() == ERROR_INVALID_HANDLE) {
+ PyErr_SetFromWindowsErr(0);
+#else
+ if (errno == EBADF) {
+ PyErr_SetFromErrno(PyExc_OSError);
+#endif
+ goto error;
+ }
+ }
+ else {
+#if defined(S_ISDIR) && defined(EISDIR)
+ /* On Unix, open will succeed for directories.
+ In Python, there should be no file objects referring to
+ directories, so we need a check. */
+ if (S_ISDIR(fdfstat.st_mode)) {
+ errno = EISDIR;
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ goto error;
+ }
+#endif /* defined(S_ISDIR) */
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+ if (fdfstat.st_blksize > 1)
+ self->blksize = fdfstat.st_blksize;
+#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
+ }
+
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+ /* don't translate newlines (\r\n <=> \n) */
+ _setmode(self->fd, O_BINARY);
+#endif
+
+ if (PyObject_SetAttr((PyObject *)self, &_Py_ID(name), nameobj) < 0)
+ goto error;
+
+ if (self->appending) {
+ /* For consistent behaviour, we explicitly seek to the
+ end of file (otherwise, it might be done only on the
+ first write()). */
+ PyObject *pos = portable_lseek(self, NULL, 2, true);
+ if (pos == NULL)
+ goto error;
+ Py_DECREF(pos);
+ }
+
+ goto done;
+
+ error:
+ ret = -1;
+ if (!fd_is_own)
+ self->fd = -1;
+ if (self->fd >= 0) {
+ PyObject *exc = PyErr_GetRaisedException();
+ internal_close(self);
+ _PyErr_ChainExceptions1(exc);
+ }
+
+ done:
+#ifdef MS_WINDOWS
+ PyMem_Free(widename);
+#endif
+ Py_CLEAR(stringobj);
+ return ret;
+}
+
+static int
+fileio_traverse(fileio *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static int
+fileio_clear(fileio *self)
+{
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+fileio_dealloc(fileio *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ self->finalizing = 1;
+ if (_PyIOBase_finalize((PyObject *) self) < 0)
+ return;
+ _PyObject_GC_UNTRACK(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) self);
+ (void)fileio_clear(self);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+static PyObject *
+err_closed(void)
+{
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
+ return NULL;
+}
+
+static PyObject *
+err_mode(_PyIO_State *state, const char *action)
+{
+ return PyErr_Format(state->unsupported_operation,
+ "File not open for %s", action);
+}
+
+/*[clinic input]
+_io.FileIO.fileno
+
+Return the underlying file descriptor (an integer).
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_fileno_impl(fileio *self)
+/*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+ return PyLong_FromLong((long) self->fd);
+}
+
+/*[clinic input]
+_io.FileIO.readable
+
+True if file was opened in a read mode.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_readable_impl(fileio *self)
+/*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+ return PyBool_FromLong((long) self->readable);
+}
+
+/*[clinic input]
+_io.FileIO.writable
+
+True if file was opened in a write mode.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_writable_impl(fileio *self)
+/*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+ return PyBool_FromLong((long) self->writable);
+}
+
+/*[clinic input]
+_io.FileIO.seekable
+
+True if file supports random-access.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_seekable_impl(fileio *self)
+/*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+ if (self->seekable < 0) {
+ /* portable_lseek() sets the seekable attribute */
+ PyObject *pos = portable_lseek(self, NULL, SEEK_CUR, false);
+ assert(self->seekable >= 0);
+ if (pos == NULL) {
+ PyErr_Clear();
+ }
+ else {
+ Py_DECREF(pos);
+ }
+ }
+ return PyBool_FromLong((long) self->seekable);
+}
+
+/*[clinic input]
+_io.FileIO.readinto
+ cls: defining_class
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+
+Same as RawIOBase.readinto().
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer)
+/*[clinic end generated code: output=97f0f3d69534db34 input=fd20323e18ce1ec8]*/
+{
+ Py_ssize_t n;
+ int err;
+
+ if (self->fd < 0)
+ return err_closed();
+ if (!self->readable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "reading");
+ }
+
+ n = _Py_read(self->fd, buffer->buf, buffer->len);
+ /* copy errno because PyBuffer_Release() can indirectly modify it */
+ err = errno;
+
+ if (n == -1) {
+ if (err == EAGAIN) {
+ PyErr_Clear();
+ Py_RETURN_NONE;
+ }
+ return NULL;
+ }
+
+ return PyLong_FromSsize_t(n);
+}
+
+static size_t
+new_buffersize(fileio *self, size_t currentsize)
+{
+ size_t addend;
+
+ /* Expand the buffer by an amount proportional to the current size,
+ giving us amortized linear-time behavior. For bigger sizes, use a
+ less-than-double growth factor to avoid excessive allocation. */
+ assert(currentsize <= PY_SSIZE_T_MAX);
+ if (currentsize > 65536)
+ addend = currentsize >> 3;
+ else
+ addend = 256 + currentsize;
+ if (addend < SMALLCHUNK)
+ /* Avoid tiny read() calls. */
+ addend = SMALLCHUNK;
+ return addend + currentsize;
+}
+
+/*[clinic input]
+_io.FileIO.readall
+
+Read all data from the file, returned as bytes.
+
+In non-blocking mode, returns as much as is immediately available,
+or None if no data is available. Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_readall_impl(fileio *self)
+/*[clinic end generated code: output=faa0292b213b4022 input=dbdc137f55602834]*/
+{
+ struct _Py_stat_struct status;
+ Py_off_t pos, end;
+ PyObject *result;
+ Py_ssize_t bytes_read = 0;
+ Py_ssize_t n;
+ size_t bufsize;
+ int fstat_result;
+
+ if (self->fd < 0)
+ return err_closed();
+
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+#ifdef MS_WINDOWS
+ pos = _lseeki64(self->fd, 0L, SEEK_CUR);
+#else
+ pos = lseek(self->fd, 0L, SEEK_CUR);
+#endif
+ _Py_END_SUPPRESS_IPH
+ fstat_result = _Py_fstat_noraise(self->fd, &status);
+ Py_END_ALLOW_THREADS
+
+ if (fstat_result == 0)
+ end = status.st_size;
+ else
+ end = (Py_off_t)-1;
+
+ if (end > 0 && end >= pos && pos >= 0 && end - pos < PY_SSIZE_T_MAX) {
+ /* This is probably a real file, so we try to allocate a
+ buffer one byte larger than the rest of the file. If the
+ calculation is right then we should get EOF without having
+ to enlarge the buffer. */
+ bufsize = (size_t)(end - pos + 1);
+ } else {
+ bufsize = SMALLCHUNK;
+ }
+
+ result = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (result == NULL)
+ return NULL;
+
+ while (1) {
+ if (bytes_read >= (Py_ssize_t)bufsize) {
+ bufsize = new_buffersize(self, bytes_read);
+ if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unbounded read returned more bytes "
+ "than a Python bytes object can hold");
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
+ if (_PyBytes_Resize(&result, bufsize) < 0)
+ return NULL;
+ }
+ }
+
+ n = _Py_read(self->fd,
+ PyBytes_AS_STRING(result) + bytes_read,
+ bufsize - bytes_read);
+
+ if (n == 0)
+ break;
+ if (n == -1) {
+ if (errno == EAGAIN) {
+ PyErr_Clear();
+ if (bytes_read > 0)
+ break;
+ Py_DECREF(result);
+ Py_RETURN_NONE;
+ }
+ Py_DECREF(result);
+ return NULL;
+ }
+ bytes_read += n;
+ pos += n;
+ }
+
+ if (PyBytes_GET_SIZE(result) > bytes_read) {
+ if (_PyBytes_Resize(&result, bytes_read) < 0)
+ return NULL;
+ }
+ return result;
+}
+
+/*[clinic input]
+_io.FileIO.read
+ cls: defining_class
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read at most size bytes, returned as bytes.
+
+Only makes one system call, so less data may be returned than requested.
+In non-blocking mode, returns None if no data is available.
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
+/*[clinic end generated code: output=bbd749c7c224143e input=f613d2057e4a1918]*/
+{
+ char *ptr;
+ Py_ssize_t n;
+ PyObject *bytes;
+
+ if (self->fd < 0)
+ return err_closed();
+ if (!self->readable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "reading");
+ }
+
+ if (size < 0)
+ return _io_FileIO_readall_impl(self);
+
+ if (size > _PY_READ_MAX) {
+ size = _PY_READ_MAX;
+ }
+
+ bytes = PyBytes_FromStringAndSize(NULL, size);
+ if (bytes == NULL)
+ return NULL;
+ ptr = PyBytes_AS_STRING(bytes);
+
+ n = _Py_read(self->fd, ptr, size);
+ if (n == -1) {
+ /* copy errno because Py_DECREF() can indirectly modify it */
+ int err = errno;
+ Py_DECREF(bytes);
+ if (err == EAGAIN) {
+ PyErr_Clear();
+ Py_RETURN_NONE;
+ }
+ return NULL;
+ }
+
+ if (n != size) {
+ if (_PyBytes_Resize(&bytes, n) < 0) {
+ Py_CLEAR(bytes);
+ return NULL;
+ }
+ }
+
+ return (PyObject *) bytes;
+}
+
+/*[clinic input]
+_io.FileIO.write
+ cls: defining_class
+ b: Py_buffer
+ /
+
+Write buffer b to file, return number of bytes written.
+
+Only makes one system call, so not all of the data may be written.
+The number of bytes actually written is returned. In non-blocking mode,
+returns None if the write would block.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b)
+/*[clinic end generated code: output=927e25be80f3b77b input=2776314f043088f5]*/
+{
+ Py_ssize_t n;
+ int err;
+
+ if (self->fd < 0)
+ return err_closed();
+ if (!self->writable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "writing");
+ }
+
+ n = _Py_write(self->fd, b->buf, b->len);
+ /* copy errno because PyBuffer_Release() can indirectly modify it */
+ err = errno;
+
+ if (n < 0) {
+ if (err == EAGAIN) {
+ PyErr_Clear();
+ Py_RETURN_NONE;
+ }
+ return NULL;
+ }
+
+ return PyLong_FromSsize_t(n);
+}
+
+/* XXX Windows support below is likely incomplete */
+
+/* Cribbed from posix_lseek() */
+static PyObject *
+portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error)
+{
+ Py_off_t pos, res;
+ int fd = self->fd;
+
+#ifdef SEEK_SET
+ /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
+ switch (whence) {
+#if SEEK_SET != 0
+ case 0: whence = SEEK_SET; break;
+#endif
+#if SEEK_CUR != 1
+ case 1: whence = SEEK_CUR; break;
+#endif
+#if SEEK_END != 2
+ case 2: whence = SEEK_END; break;
+#endif
+ }
+#endif /* SEEK_SET */
+
+ if (posobj == NULL) {
+ pos = 0;
+ }
+ else {
+#if defined(HAVE_LARGEFILE_SUPPORT)
+ pos = PyLong_AsLongLong(posobj);
+#else
+ pos = PyLong_AsLong(posobj);
+#endif
+ if (PyErr_Occurred())
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+#ifdef MS_WINDOWS
+ res = _lseeki64(fd, pos, whence);
+#else
+ res = lseek(fd, pos, whence);
+#endif
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
+
+ if (self->seekable < 0) {
+ self->seekable = (res >= 0);
+ }
+
+ if (res < 0) {
+ if (suppress_pipe_error && errno == ESPIPE) {
+ res = 0;
+ } else {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+ }
+
+#if defined(HAVE_LARGEFILE_SUPPORT)
+ return PyLong_FromLongLong(res);
+#else
+ return PyLong_FromLong(res);
+#endif
+}
+
+/*[clinic input]
+_io.FileIO.seek
+ pos: object
+ whence: int = 0
+ /
+
+Move to new file position and return the file position.
+
+Argument offset is a byte count. Optional argument whence defaults to
+SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
+are SEEK_CUR or 1 (move relative to current position, positive or negative),
+and SEEK_END or 2 (move relative to end of file, usually negative, although
+many platforms allow seeking beyond the end of a file).
+
+Note that not all file objects are seekable.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
+/*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+
+ return portable_lseek(self, pos, whence, false);
+}
+
+/*[clinic input]
+_io.FileIO.tell
+
+Current file position.
+
+Can raise OSError for non seekable files.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_tell_impl(fileio *self)
+/*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+
+ return portable_lseek(self, NULL, 1, false);
+}
+
+#ifdef HAVE_FTRUNCATE
+/*[clinic input]
+_io.FileIO.truncate
+ cls: defining_class
+ size as posobj: object = None
+ /
+
+Truncate the file to at most size bytes and return the truncated size.
+
+Size defaults to the current file position, as returned by tell().
+The current file position is changed to the value of size.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj)
+/*[clinic end generated code: output=d936732a49e8d5a2 input=c367fb45d6bb2c18]*/
+{
+ Py_off_t pos;
+ int ret;
+ int fd;
+
+ fd = self->fd;
+ if (fd < 0)
+ return err_closed();
+ if (!self->writable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "writing");
+ }
+
+ if (posobj == Py_None) {
+ /* Get the current position. */
+ posobj = portable_lseek(self, NULL, 1, false);
+ if (posobj == NULL)
+ return NULL;
+ }
+ else {
+ Py_INCREF(posobj);
+ }
+
+#if defined(HAVE_LARGEFILE_SUPPORT)
+ pos = PyLong_AsLongLong(posobj);
+#else
+ pos = PyLong_AsLong(posobj);
+#endif
+ if (PyErr_Occurred()){
+ Py_DECREF(posobj);
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+ errno = 0;
+#ifdef MS_WINDOWS
+ ret = _chsize_s(fd, pos);
+#else
+ ret = ftruncate(fd, pos);
+#endif
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
+
+ if (ret != 0) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(posobj);
+ return NULL;
+ }
+
+ return posobj;
+}
+#endif /* HAVE_FTRUNCATE */
+
+static const char *
+mode_string(fileio *self)
+{
+ if (self->created) {
+ if (self->readable)
+ return "xb+";
+ else
+ return "xb";
+ }
+ if (self->appending) {
+ if (self->readable)
+ return "ab+";
+ else
+ return "ab";
+ }
+ else if (self->readable) {
+ if (self->writable)
+ return "rb+";
+ else
+ return "rb";
+ }
+ else
+ return "wb";
+}
+
+static PyObject *
+fileio_repr(fileio *self)
+{
+ PyObject *nameobj, *res;
+
+ if (self->fd < 0)
+ return PyUnicode_FromFormat("<_io.FileIO [closed]>");
+
+ if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
+ return NULL;
+ }
+ if (nameobj == NULL) {
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO fd=%d mode='%s' closefd=%s>",
+ self->fd, mode_string(self), self->closefd ? "True" : "False");
+ }
+ else {
+ int status = Py_ReprEnter((PyObject *)self);
+ res = NULL;
+ if (status == 0) {
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO name=%R mode='%s' closefd=%s>",
+ nameobj, mode_string(self), self->closefd ? "True" : "False");
+ Py_ReprLeave((PyObject *)self);
+ }
+ else if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
+ Py_DECREF(nameobj);
+ }
+ return res;
+}
+
+/*[clinic input]
+_io.FileIO.isatty
+
+True if the file is connected to a TTY device.
+[clinic start generated code]*/
+
+static PyObject *
+_io_FileIO_isatty_impl(fileio *self)
+/*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
+{
+ long res;
+
+ if (self->fd < 0)
+ return err_closed();
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+ res = isatty(self->fd);
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
+ return PyBool_FromLong(res);
+}
+
+#include "clinic/fileio.c.h"
+
+static PyMethodDef fileio_methods[] = {
+ _IO_FILEIO_READ_METHODDEF
+ _IO_FILEIO_READALL_METHODDEF
+ _IO_FILEIO_READINTO_METHODDEF
+ _IO_FILEIO_WRITE_METHODDEF
+ _IO_FILEIO_SEEK_METHODDEF
+ _IO_FILEIO_TELL_METHODDEF
+ _IO_FILEIO_TRUNCATE_METHODDEF
+ _IO_FILEIO_CLOSE_METHODDEF
+ _IO_FILEIO_SEEKABLE_METHODDEF
+ _IO_FILEIO_READABLE_METHODDEF
+ _IO_FILEIO_WRITABLE_METHODDEF
+ _IO_FILEIO_FILENO_METHODDEF
+ _IO_FILEIO_ISATTY_METHODDEF
+ {"_dealloc_warn", (PyCFunction)fileio_dealloc_warn, METH_O, NULL},
+ {"__reduce__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {"__reduce_ex__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
+};
+
+/* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
+
+static PyObject *
+get_closed(fileio *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->fd < 0));
+}
+
+static PyObject *
+get_closefd(fileio *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->closefd));
+}
+
+static PyObject *
+get_mode(fileio *self, void *closure)
+{
+ return PyUnicode_FromString(mode_string(self));
+}
+
+static PyGetSetDef fileio_getsetlist[] = {
+ {"closed", (getter)get_closed, NULL, "True if the file is closed"},
+ {"closefd", (getter)get_closefd, NULL,
+ "True if the file descriptor will be closed by close()."},
+ {"mode", (getter)get_mode, NULL, "String giving the file mode"},
+ {NULL},
+};
+
+static PyMemberDef fileio_members[] = {
+ {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
+ {"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(fileio, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(fileio, dict), READONLY},
+ {NULL}
+};
+
+static PyType_Slot fileio_slots[] = {
+ {Py_tp_dealloc, fileio_dealloc},
+ {Py_tp_repr, fileio_repr},
+ {Py_tp_doc, (void *)_io_FileIO___init____doc__},
+ {Py_tp_traverse, fileio_traverse},
+ {Py_tp_clear, fileio_clear},
+ {Py_tp_methods, fileio_methods},
+ {Py_tp_members, fileio_members},
+ {Py_tp_getset, fileio_getsetlist},
+ {Py_tp_init, _io_FileIO___init__},
+ {Py_tp_new, fileio_new},
+ {0, NULL},
+};
+
+PyType_Spec fileio_spec = {
+ .name = "_io.FileIO",
+ .basicsize = sizeof(fileio),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = fileio_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/iobase.c b/contrib/tools/python3/Modules/_io/iobase.c
new file mode 100644
index 0000000000..bc2c9afa01
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/iobase.c
@@ -0,0 +1,1061 @@
+/*
+ An implementation of the I/O abstract base classes hierarchy
+ as defined by PEP 3116 - "New I/O"
+
+ Classes defined here: IOBase, RawIOBase.
+
+ Written by Amaury Forgeot d'Arc and Antoine Pitrou
+*/
+
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "pycore_long.h" // _PyLong_GetOne()
+#include "pycore_object.h"
+#include <stddef.h> // offsetof()
+#include "_iomodule.h"
+
+/*[clinic input]
+module _io
+class _io._IOBase "PyObject *" "clinic_state()->PyIOBase_Type"
+class _io._RawIOBase "PyObject *" "clinic_state()->PyRawIOBase_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9006b7802ab8ea85]*/
+
+/*
+ * IOBase class, an abstract class
+ */
+
+typedef struct {
+ PyObject_HEAD
+
+ PyObject *dict;
+ PyObject *weakreflist;
+} iobase;
+
+PyDoc_STRVAR(iobase_doc,
+ "The abstract base class for all I/O classes.\n"
+ "\n"
+ "This class provides dummy implementations for many methods that\n"
+ "derived classes can override selectively; the default implementations\n"
+ "represent a file that cannot be read, written or seeked.\n"
+ "\n"
+ "Even though IOBase does not declare read, readinto, or write because\n"
+ "their signatures will vary, implementations and clients should\n"
+ "consider those methods part of the interface. Also, implementations\n"
+ "may raise UnsupportedOperation when operations they do not support are\n"
+ "called.\n"
+ "\n"
+ "The basic type used for binary data read from or written to a file is\n"
+ "bytes. Other bytes-like objects are accepted as method arguments too.\n"
+ "In some cases (such as readinto), a writable object is required. Text\n"
+ "I/O classes work with str data.\n"
+ "\n"
+ "Note that calling any method (except additional calls to close(),\n"
+ "which are ignored) on a closed stream should raise a ValueError.\n"
+ "\n"
+ "IOBase (and its subclasses) support the iterator protocol, meaning\n"
+ "that an IOBase object can be iterated over yielding the lines in a\n"
+ "stream.\n"
+ "\n"
+ "IOBase also supports the :keyword:`with` statement. In this example,\n"
+ "fp is closed after the suite of the with statement is complete:\n"
+ "\n"
+ "with open('spam.txt', 'r') as fp:\n"
+ " fp.write('Spam and eggs!')\n");
+
+/* Use this macro whenever you want to check the internal `closed` status
+ of the IOBase object rather than the virtual `closed` attribute as returned
+ by whatever subclass. */
+
+
+/* Internal methods */
+static PyObject *
+iobase_unsupported(_PyIO_State *state, const char *message)
+{
+ PyErr_SetString(state->unsupported_operation, message);
+ return NULL;
+}
+
+/* Positioning */
+
+/*[clinic input]
+_io._IOBase.seek
+ cls: defining_class
+ offset: int(unused=True)
+ The stream position, relative to 'whence'.
+ whence: int(unused=True, c_default='0') = os.SEEK_SET
+ The relative position to seek from.
+ /
+
+Change the stream position to the given byte offset.
+
+The offset is interpreted relative to the position indicated by whence.
+Values for whence are:
+
+* os.SEEK_SET or 0 -- start of stream (the default); offset should be zero or positive
+* os.SEEK_CUR or 1 -- current stream position; offset may be negative
+* os.SEEK_END or 2 -- end of stream; offset is usually negative
+
+Return the new absolute position.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_seek_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(offset), int Py_UNUSED(whence))
+/*[clinic end generated code: output=8bd74ea6538ded53 input=74211232b363363e]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return iobase_unsupported(state, "seek");
+}
+
+/*[clinic input]
+_io._IOBase.tell
+
+Return current stream position.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_tell_impl(PyObject *self)
+/*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
+{
+ return _PyObject_CallMethod(self, &_Py_ID(seek), "ii", 0, 1);
+}
+
+/*[clinic input]
+_io._IOBase.truncate
+ cls: defining_class
+ size: object(unused=True) = None
+ /
+
+Truncate file to size bytes.
+
+File pointer is left unchanged. Size defaults to the current IO position
+as reported by tell(). Return the new size.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_truncate_impl(PyObject *self, PyTypeObject *cls,
+ PyObject *Py_UNUSED(size))
+/*[clinic end generated code: output=2013179bff1fe8ef input=660ac20936612c27]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return iobase_unsupported(state, "truncate");
+}
+
+static int
+iobase_is_closed(PyObject *self)
+{
+ PyObject *res;
+ int ret;
+ /* This gets the derived attribute, which is *not* __IOBase_closed
+ in most cases! */
+ ret = _PyObject_LookupAttr(self, &_Py_ID(__IOBase_closed), &res);
+ Py_XDECREF(res);
+ return ret;
+}
+
+/* Flush and close methods */
+
+/*[clinic input]
+_io._IOBase.flush
+
+Flush write buffers, if applicable.
+
+This is not implemented for read-only and non-blocking streams.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_flush_impl(PyObject *self)
+/*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
+{
+ /* XXX Should this return the number of bytes written??? */
+ int closed = iobase_is_closed(self);
+
+ if (!closed) {
+ Py_RETURN_NONE;
+ }
+ if (closed > 0) {
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
+ }
+ return NULL;
+}
+
+static PyObject *
+iobase_closed_get(PyObject *self, void *context)
+{
+ int closed = iobase_is_closed(self);
+ if (closed < 0) {
+ return NULL;
+ }
+ return PyBool_FromLong(closed);
+}
+
+static int
+iobase_check_closed(PyObject *self)
+{
+ PyObject *res;
+ int closed;
+ /* This gets the derived attribute, which is *not* __IOBase_closed
+ in most cases! */
+ closed = _PyObject_LookupAttr(self, &_Py_ID(closed), &res);
+ if (closed > 0) {
+ closed = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (closed > 0) {
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
+ return -1;
+ }
+ }
+ return closed;
+}
+
+PyObject *
+_PyIOBase_check_closed(PyObject *self, PyObject *args)
+{
+ if (iobase_check_closed(self)) {
+ return NULL;
+ }
+ if (args == Py_True) {
+ return Py_None;
+ }
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+iobase_check_seekable(PyObject *self, PyObject *args)
+{
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ return _PyIOBase_check_seekable(state, self, args);
+}
+
+static PyObject *
+iobase_check_readable(PyObject *self, PyObject *args)
+{
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ return _PyIOBase_check_readable(state, self, args);
+}
+
+static PyObject *
+iobase_check_writable(PyObject *self, PyObject *args)
+{
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ return _PyIOBase_check_writable(state, self, args);
+}
+
+PyObject *
+_PyIOBase_cannot_pickle(PyObject *self, PyObject *args)
+{
+ PyErr_Format(PyExc_TypeError,
+ "cannot pickle '%.100s' instances", _PyType_Name(Py_TYPE(self)));
+ return NULL;
+}
+
+/* XXX: IOBase thinks it has to maintain its own internal state in
+ `__IOBase_closed` and call flush() by itself, but it is redundant with
+ whatever behaviour a non-trivial derived class will implement. */
+
+/*[clinic input]
+_io._IOBase.close
+
+Flush and close the IO object.
+
+This method has no effect if the file is already closed.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_close_impl(PyObject *self)
+/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
+{
+ int rc, closed = iobase_is_closed(self);
+
+ if (closed < 0) {
+ return NULL;
+ }
+ if (closed) {
+ Py_RETURN_NONE;
+ }
+
+ PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush));
+
+ PyObject *exc = PyErr_GetRaisedException();
+ rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True);
+ _PyErr_ChainExceptions1(exc);
+ if (rc < 0) {
+ Py_CLEAR(res);
+ }
+
+ if (res == NULL)
+ return NULL;
+
+ Py_DECREF(res);
+ Py_RETURN_NONE;
+}
+
+/* Finalization and garbage collection support */
+
+static void
+iobase_finalize(PyObject *self)
+{
+ PyObject *res;
+ int closed;
+
+ /* Save the current exception, if any. */
+ PyObject *exc = PyErr_GetRaisedException();
+
+ /* If `closed` doesn't exist or can't be evaluated as bool, then the
+ object is probably in an unusable state, so ignore. */
+ if (_PyObject_LookupAttr(self, &_Py_ID(closed), &res) <= 0) {
+ PyErr_Clear();
+ closed = -1;
+ }
+ else {
+ closed = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (closed == -1)
+ PyErr_Clear();
+ }
+ if (closed == 0) {
+ /* Signal close() that it was called as part of the object
+ finalization process. */
+ if (PyObject_SetAttr(self, &_Py_ID(_finalizing), Py_True))
+ PyErr_Clear();
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(close));
+ /* Silencing I/O errors is bad, but printing spurious tracebacks is
+ equally as bad, and potentially more frequent (because of
+ shutdown issues). */
+ if (res == NULL) {
+#ifndef Py_DEBUG
+ if (_Py_GetConfig()->dev_mode) {
+ PyErr_WriteUnraisable(self);
+ }
+ else {
+ PyErr_Clear();
+ }
+#else
+ PyErr_WriteUnraisable(self);
+#endif
+ }
+ else {
+ Py_DECREF(res);
+ }
+ }
+
+ /* Restore the saved exception. */
+ PyErr_SetRaisedException(exc);
+}
+
+int
+_PyIOBase_finalize(PyObject *self)
+{
+ int is_zombie;
+
+ /* If _PyIOBase_finalize() is called from a destructor, we need to
+ resurrect the object as calling close() can invoke arbitrary code. */
+ is_zombie = (Py_REFCNT(self) == 0);
+ if (is_zombie)
+ return PyObject_CallFinalizerFromDealloc(self);
+ else {
+ PyObject_CallFinalizer(self);
+ return 0;
+ }
+}
+
+static int
+iobase_traverse(iobase *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static int
+iobase_clear(iobase *self)
+{
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+/* Destructor */
+
+static void
+iobase_dealloc(iobase *self)
+{
+ /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
+ are still available here for close() to use.
+ However, if the derived class declares a __slots__, those slots are
+ already gone.
+ */
+ if (_PyIOBase_finalize((PyObject *) self) < 0) {
+ /* When called from a heap type's dealloc, the type will be
+ decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
+ if (_PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) {
+ Py_INCREF(Py_TYPE(self));
+ }
+ return;
+ }
+ PyTypeObject *tp = Py_TYPE(self);
+ _PyObject_GC_UNTRACK(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) self);
+ Py_CLEAR(self->dict);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+/* Inquiry methods */
+
+/*[clinic input]
+_io._IOBase.seekable
+
+Return whether object supports random access.
+
+If False, seek(), tell() and truncate() will raise OSError.
+This method may need to do a test seek().
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_seekable_impl(PyObject *self)
+/*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/
+{
+ Py_RETURN_FALSE;
+}
+
+PyObject *
+_PyIOBase_check_seekable(_PyIO_State *state, PyObject *self, PyObject *args)
+{
+ PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(seekable));
+ if (res == NULL)
+ return NULL;
+ if (res != Py_True) {
+ Py_CLEAR(res);
+ iobase_unsupported(state, "File or stream is not seekable.");
+ return NULL;
+ }
+ if (args == Py_True) {
+ Py_DECREF(res);
+ }
+ return res;
+}
+
+/*[clinic input]
+_io._IOBase.readable
+
+Return whether object was opened for reading.
+
+If False, read() will raise OSError.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_readable_impl(PyObject *self)
+/*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/
+{
+ Py_RETURN_FALSE;
+}
+
+/* May be called with any object */
+PyObject *
+_PyIOBase_check_readable(_PyIO_State *state, PyObject *self, PyObject *args)
+{
+ PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(readable));
+ if (res == NULL)
+ return NULL;
+ if (res != Py_True) {
+ Py_CLEAR(res);
+ iobase_unsupported(state, "File or stream is not readable.");
+ return NULL;
+ }
+ if (args == Py_True) {
+ Py_DECREF(res);
+ }
+ return res;
+}
+
+/*[clinic input]
+_io._IOBase.writable
+
+Return whether object was opened for writing.
+
+If False, write() will raise OSError.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_writable_impl(PyObject *self)
+/*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/
+{
+ Py_RETURN_FALSE;
+}
+
+/* May be called with any object */
+PyObject *
+_PyIOBase_check_writable(_PyIO_State *state, PyObject *self, PyObject *args)
+{
+ PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(writable));
+ if (res == NULL)
+ return NULL;
+ if (res != Py_True) {
+ Py_CLEAR(res);
+ iobase_unsupported(state, "File or stream is not writable.");
+ return NULL;
+ }
+ if (args == Py_True) {
+ Py_DECREF(res);
+ }
+ return res;
+}
+
+/* Context manager */
+
+static PyObject *
+iobase_enter(PyObject *self, PyObject *args)
+{
+ if (iobase_check_closed(self))
+ return NULL;
+
+ return Py_NewRef(self);
+}
+
+static PyObject *
+iobase_exit(PyObject *self, PyObject *args)
+{
+ return PyObject_CallMethodNoArgs(self, &_Py_ID(close));
+}
+
+/* Lower-level APIs */
+
+/* XXX Should these be present even if unimplemented? */
+
+/*[clinic input]
+_io._IOBase.fileno
+ cls: defining_class
+ /
+
+Return underlying file descriptor if one exists.
+
+Raise OSError if the IO object does not use a file descriptor.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_fileno_impl(PyObject *self, PyTypeObject *cls)
+/*[clinic end generated code: output=7caaa32a6f4ada3d input=1927c8bea5c85099]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return iobase_unsupported(state, "fileno");
+}
+
+/*[clinic input]
+_io._IOBase.isatty
+
+Return whether this is an 'interactive' stream.
+
+Return False if it can't be determined.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_isatty_impl(PyObject *self)
+/*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
+{
+ if (iobase_check_closed(self))
+ return NULL;
+ Py_RETURN_FALSE;
+}
+
+/* Readline(s) and writelines */
+
+/*[clinic input]
+_io._IOBase.readline
+ size as limit: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read and return a line from the stream.
+
+If size is specified, at most size bytes will be read.
+
+The line terminator is always b'\n' for binary files; for text
+files, the newlines argument to open can be used to select the line
+terminator(s) recognized.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
+/*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/
+{
+ /* For backwards compatibility, a (slowish) readline(). */
+
+ PyObject *peek, *buffer, *result;
+ Py_ssize_t old_size = -1;
+
+ if (_PyObject_LookupAttr(self, &_Py_ID(peek), &peek) < 0) {
+ return NULL;
+ }
+
+ buffer = PyByteArray_FromStringAndSize(NULL, 0);
+ if (buffer == NULL) {
+ Py_XDECREF(peek);
+ return NULL;
+ }
+
+ while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) {
+ Py_ssize_t nreadahead = 1;
+ PyObject *b;
+
+ if (peek != NULL) {
+ PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_GetOne());
+ if (readahead == NULL) {
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
+ when EINTR occurs so we needn't do it ourselves. */
+ if (_PyIO_trap_eintr()) {
+ continue;
+ }
+ goto fail;
+ }
+ if (!PyBytes_Check(readahead)) {
+ PyErr_Format(PyExc_OSError,
+ "peek() should have returned a bytes object, "
+ "not '%.200s'", Py_TYPE(readahead)->tp_name);
+ Py_DECREF(readahead);
+ goto fail;
+ }
+ if (PyBytes_GET_SIZE(readahead) > 0) {
+ Py_ssize_t n = 0;
+ const char *buf = PyBytes_AS_STRING(readahead);
+ if (limit >= 0) {
+ do {
+ if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
+ break;
+ if (buf[n++] == '\n')
+ break;
+ } while (1);
+ }
+ else {
+ do {
+ if (n >= PyBytes_GET_SIZE(readahead))
+ break;
+ if (buf[n++] == '\n')
+ break;
+ } while (1);
+ }
+ nreadahead = n;
+ }
+ Py_DECREF(readahead);
+ }
+
+ b = _PyObject_CallMethod(self, &_Py_ID(read), "n", nreadahead);
+ if (b == NULL) {
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
+ when EINTR occurs so we needn't do it ourselves. */
+ if (_PyIO_trap_eintr()) {
+ continue;
+ }
+ goto fail;
+ }
+ if (!PyBytes_Check(b)) {
+ PyErr_Format(PyExc_OSError,
+ "read() should have returned a bytes object, "
+ "not '%.200s'", Py_TYPE(b)->tp_name);
+ Py_DECREF(b);
+ goto fail;
+ }
+ if (PyBytes_GET_SIZE(b) == 0) {
+ Py_DECREF(b);
+ break;
+ }
+
+ old_size = PyByteArray_GET_SIZE(buffer);
+ if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
+ Py_DECREF(b);
+ goto fail;
+ }
+ memcpy(PyByteArray_AS_STRING(buffer) + old_size,
+ PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
+
+ Py_DECREF(b);
+
+ if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
+ break;
+ }
+
+ result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
+ PyByteArray_GET_SIZE(buffer));
+ Py_XDECREF(peek);
+ Py_DECREF(buffer);
+ return result;
+ fail:
+ Py_XDECREF(peek);
+ Py_DECREF(buffer);
+ return NULL;
+}
+
+static PyObject *
+iobase_iter(PyObject *self)
+{
+ if (iobase_check_closed(self))
+ return NULL;
+
+ return Py_NewRef(self);
+}
+
+static PyObject *
+iobase_iternext(PyObject *self)
+{
+ PyObject *line = PyObject_CallMethodNoArgs(self, &_Py_ID(readline));
+
+ if (line == NULL)
+ return NULL;
+
+ if (PyObject_Size(line) <= 0) {
+ /* Error or empty */
+ Py_DECREF(line);
+ return NULL;
+ }
+
+ return line;
+}
+
+/*[clinic input]
+_io._IOBase.readlines
+ hint: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Return a list of lines from the stream.
+
+hint can be specified to control the number of lines read: no more
+lines will be read if the total size (in bytes/characters) of all
+lines so far exceeds hint.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
+/*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
+{
+ Py_ssize_t length = 0;
+ PyObject *result, *it = NULL;
+
+ result = PyList_New(0);
+ if (result == NULL)
+ return NULL;
+
+ if (hint <= 0) {
+ /* XXX special-casing this made sense in the Python version in order
+ to remove the bytecode interpretation overhead, but it could
+ probably be removed here. */
+ PyObject *ret = PyObject_CallMethodObjArgs(result, &_Py_ID(extend),
+ self, NULL);
+ if (ret == NULL) {
+ goto error;
+ }
+ Py_DECREF(ret);
+ return result;
+ }
+
+ it = PyObject_GetIter(self);
+ if (it == NULL) {
+ goto error;
+ }
+
+ while (1) {
+ Py_ssize_t line_length;
+ PyObject *line = PyIter_Next(it);
+ if (line == NULL) {
+ if (PyErr_Occurred()) {
+ goto error;
+ }
+ else
+ break; /* StopIteration raised */
+ }
+
+ if (PyList_Append(result, line) < 0) {
+ Py_DECREF(line);
+ goto error;
+ }
+ line_length = PyObject_Size(line);
+ Py_DECREF(line);
+ if (line_length < 0) {
+ goto error;
+ }
+ if (line_length > hint - length)
+ break;
+ length += line_length;
+ }
+
+ Py_DECREF(it);
+ return result;
+
+ error:
+ Py_XDECREF(it);
+ Py_DECREF(result);
+ return NULL;
+}
+
+/*[clinic input]
+_io._IOBase.writelines
+ lines: object
+ /
+
+Write a list of lines to stream.
+
+Line separators are not added, so it is usual for each of the
+lines provided to have a line separator at the end.
+[clinic start generated code]*/
+
+static PyObject *
+_io__IOBase_writelines(PyObject *self, PyObject *lines)
+/*[clinic end generated code: output=976eb0a9b60a6628 input=cac3fc8864183359]*/
+{
+ PyObject *iter, *res;
+
+ if (iobase_check_closed(self))
+ return NULL;
+
+ iter = PyObject_GetIter(lines);
+ if (iter == NULL)
+ return NULL;
+
+ while (1) {
+ PyObject *line = PyIter_Next(iter);
+ if (line == NULL) {
+ if (PyErr_Occurred()) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ else
+ break; /* Stop Iteration */
+ }
+
+ res = NULL;
+ do {
+ res = PyObject_CallMethodObjArgs(self, &_Py_ID(write), line, NULL);
+ } while (res == NULL && _PyIO_trap_eintr());
+ Py_DECREF(line);
+ if (res == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ Py_DECREF(res);
+ }
+ Py_DECREF(iter);
+ Py_RETURN_NONE;
+}
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/iobase.c.h"
+#undef clinic_state
+
+static PyMethodDef iobase_methods[] = {
+ _IO__IOBASE_SEEK_METHODDEF
+ _IO__IOBASE_TELL_METHODDEF
+ _IO__IOBASE_TRUNCATE_METHODDEF
+ _IO__IOBASE_FLUSH_METHODDEF
+ _IO__IOBASE_CLOSE_METHODDEF
+
+ _IO__IOBASE_SEEKABLE_METHODDEF
+ _IO__IOBASE_READABLE_METHODDEF
+ _IO__IOBASE_WRITABLE_METHODDEF
+
+ {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
+ {"_checkSeekable", iobase_check_seekable, METH_NOARGS},
+ {"_checkReadable", iobase_check_readable, METH_NOARGS},
+ {"_checkWritable", iobase_check_writable, METH_NOARGS},
+
+ _IO__IOBASE_FILENO_METHODDEF
+ _IO__IOBASE_ISATTY_METHODDEF
+
+ {"__enter__", iobase_enter, METH_NOARGS},
+ {"__exit__", iobase_exit, METH_VARARGS},
+
+ _IO__IOBASE_READLINE_METHODDEF
+ _IO__IOBASE_READLINES_METHODDEF
+ _IO__IOBASE_WRITELINES_METHODDEF
+
+ {NULL, NULL}
+};
+
+static PyGetSetDef iobase_getset[] = {
+ {"__dict__", PyObject_GenericGetDict, NULL, NULL},
+ {"closed", (getter)iobase_closed_get, NULL, NULL},
+ {NULL}
+};
+
+static struct PyMemberDef iobase_members[] = {
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(iobase, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(iobase, dict), READONLY},
+ {NULL},
+};
+
+
+static PyType_Slot iobase_slots[] = {
+ {Py_tp_dealloc, iobase_dealloc},
+ {Py_tp_doc, (void *)iobase_doc},
+ {Py_tp_traverse, iobase_traverse},
+ {Py_tp_clear, iobase_clear},
+ {Py_tp_iter, iobase_iter},
+ {Py_tp_iternext, iobase_iternext},
+ {Py_tp_methods, iobase_methods},
+ {Py_tp_members, iobase_members},
+ {Py_tp_getset, iobase_getset},
+ {Py_tp_finalize, iobase_finalize},
+ {0, NULL},
+};
+
+PyType_Spec iobase_spec = {
+ .name = "_io._IOBase",
+ .basicsize = sizeof(iobase),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = iobase_slots,
+};
+
+/*
+ * RawIOBase class, Inherits from IOBase.
+ */
+PyDoc_STRVAR(rawiobase_doc,
+ "Base class for raw binary I/O.");
+
+/*
+ * The read() method is implemented by calling readinto(); derived classes
+ * that want to support read() only need to implement readinto() as a
+ * primitive operation. In general, readinto() can be more efficient than
+ * read().
+ *
+ * (It would be tempting to also provide an implementation of readinto() in
+ * terms of read(), in case the latter is a more suitable primitive operation,
+ * but that would lead to nasty recursion in case a subclass doesn't implement
+ * either.)
+*/
+
+/*[clinic input]
+_io._RawIOBase.read
+ size as n: Py_ssize_t = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
+/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
+{
+ PyObject *b, *res;
+
+ if (n < 0) {
+ return PyObject_CallMethodNoArgs(self, &_Py_ID(readall));
+ }
+
+ /* TODO: allocate a bytes object directly instead and manually construct
+ a writable memoryview pointing to it. */
+ b = PyByteArray_FromStringAndSize(NULL, n);
+ if (b == NULL)
+ return NULL;
+
+ res = PyObject_CallMethodObjArgs(self, &_Py_ID(readinto), b, NULL);
+ if (res == NULL || res == Py_None) {
+ Py_DECREF(b);
+ return res;
+ }
+
+ n = PyNumber_AsSsize_t(res, PyExc_ValueError);
+ Py_DECREF(res);
+ if (n == -1 && PyErr_Occurred()) {
+ Py_DECREF(b);
+ return NULL;
+ }
+
+ res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
+ Py_DECREF(b);
+ return res;
+}
+
+
+/*[clinic input]
+_io._RawIOBase.readall
+
+Read until EOF, using multiple read() call.
+[clinic start generated code]*/
+
+static PyObject *
+_io__RawIOBase_readall_impl(PyObject *self)
+/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
+{
+ int r;
+ PyObject *chunks = PyList_New(0);
+ PyObject *result;
+
+ if (chunks == NULL)
+ return NULL;
+
+ while (1) {
+ PyObject *data = _PyObject_CallMethod(self, &_Py_ID(read),
+ "i", DEFAULT_BUFFER_SIZE);
+ if (!data) {
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
+ when EINTR occurs so we needn't do it ourselves. */
+ if (_PyIO_trap_eintr()) {
+ continue;
+ }
+ Py_DECREF(chunks);
+ return NULL;
+ }
+ if (data == Py_None) {
+ if (PyList_GET_SIZE(chunks) == 0) {
+ Py_DECREF(chunks);
+ return data;
+ }
+ Py_DECREF(data);
+ break;
+ }
+ if (!PyBytes_Check(data)) {
+ Py_DECREF(chunks);
+ Py_DECREF(data);
+ PyErr_SetString(PyExc_TypeError, "read() should return bytes");
+ return NULL;
+ }
+ if (PyBytes_GET_SIZE(data) == 0) {
+ /* EOF */
+ Py_DECREF(data);
+ break;
+ }
+ r = PyList_Append(chunks, data);
+ Py_DECREF(data);
+ if (r < 0) {
+ Py_DECREF(chunks);
+ return NULL;
+ }
+ }
+ result = _PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks);
+ Py_DECREF(chunks);
+ return result;
+}
+
+static PyObject *
+rawiobase_readinto(PyObject *self, PyObject *args)
+{
+ PyErr_SetNone(PyExc_NotImplementedError);
+ return NULL;
+}
+
+static PyObject *
+rawiobase_write(PyObject *self, PyObject *args)
+{
+ PyErr_SetNone(PyExc_NotImplementedError);
+ return NULL;
+}
+
+static PyMethodDef rawiobase_methods[] = {
+ _IO__RAWIOBASE_READ_METHODDEF
+ _IO__RAWIOBASE_READALL_METHODDEF
+ {"readinto", rawiobase_readinto, METH_VARARGS},
+ {"write", rawiobase_write, METH_VARARGS},
+ {NULL, NULL}
+};
+
+static PyType_Slot rawiobase_slots[] = {
+ {Py_tp_doc, (void *)rawiobase_doc},
+ {Py_tp_methods, rawiobase_methods},
+ {0, NULL},
+};
+
+/* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */
+PyType_Spec rawiobase_spec = {
+ .name = "_io._RawIOBase",
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = rawiobase_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/stringio.c b/contrib/tools/python3/Modules/_io/stringio.c
new file mode 100644
index 0000000000..3eb25704b4
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/stringio.c
@@ -0,0 +1,1037 @@
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include <stddef.h> // offsetof()
+#include "pycore_object.h"
+#include "_iomodule.h"
+
+/* Implementation note: the buffer is always at least one character longer
+ than the enclosed string, for proper functioning of _PyIO_find_line_ending.
+*/
+
+#define STATE_REALIZED 1
+#define STATE_ACCUMULATING 2
+
+/*[clinic input]
+module _io
+class _io.StringIO "stringio *" "clinic_state()->PyStringIO_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2693eada0658d470]*/
+
+typedef struct {
+ PyObject_HEAD
+ Py_UCS4 *buf;
+ Py_ssize_t pos;
+ Py_ssize_t string_size;
+ size_t buf_size;
+
+ /* The stringio object can be in two states: accumulating or realized.
+ In accumulating state, the internal buffer contains nothing and
+ the contents are given by the embedded _PyUnicodeWriter structure.
+ In realized state, the internal buffer is meaningful and the
+ _PyUnicodeWriter is destroyed.
+ */
+ int state;
+ _PyUnicodeWriter writer;
+
+ char ok; /* initialized? */
+ char closed;
+ char readuniversal;
+ char readtranslate;
+ PyObject *decoder;
+ PyObject *readnl;
+ PyObject *writenl;
+
+ PyObject *dict;
+ PyObject *weakreflist;
+ _PyIO_State *module_state;
+} stringio;
+
+static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs);
+
+#define CHECK_INITIALIZED(self) \
+ if (self->ok <= 0) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on uninitialized object"); \
+ return NULL; \
+ }
+
+#define CHECK_CLOSED(self) \
+ if (self->closed) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on closed file"); \
+ return NULL; \
+ }
+
+#define ENSURE_REALIZED(self) \
+ if (realize(self) < 0) { \
+ return NULL; \
+ }
+
+
+/* Internal routine for changing the size, in terms of characters, of the
+ buffer of StringIO objects. The caller should ensure that the 'size'
+ argument is non-negative. Returns 0 on success, -1 otherwise. */
+static int
+resize_buffer(stringio *self, size_t size)
+{
+ /* Here, unsigned types are used to avoid dealing with signed integer
+ overflow, which is undefined in C. */
+ size_t alloc = self->buf_size;
+ Py_UCS4 *new_buf = NULL;
+
+ assert(self->buf != NULL);
+
+ /* Reserve one more char for line ending detection. */
+ size = size + 1;
+ /* For simplicity, stay in the range of the signed type. Anyway, Python
+ doesn't allow strings to be longer than this. */
+ if (size > PY_SSIZE_T_MAX)
+ goto overflow;
+
+ if (size < alloc / 2) {
+ /* Major downsize; resize down to exact size. */
+ alloc = size + 1;
+ }
+ else if (size < alloc) {
+ /* Within allocated size; quick exit */
+ return 0;
+ }
+ else if (size <= alloc * 1.125) {
+ /* Moderate upsize; overallocate similar to list_resize() */
+ alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
+ }
+ else {
+ /* Major upsize; resize up to exact size */
+ alloc = size + 1;
+ }
+
+ if (alloc > PY_SIZE_MAX / sizeof(Py_UCS4))
+ goto overflow;
+ new_buf = (Py_UCS4 *)PyMem_Realloc(self->buf, alloc * sizeof(Py_UCS4));
+ if (new_buf == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ self->buf_size = alloc;
+ self->buf = new_buf;
+
+ return 0;
+
+ overflow:
+ PyErr_SetString(PyExc_OverflowError,
+ "new buffer size too large");
+ return -1;
+}
+
+static PyObject *
+make_intermediate(stringio *self)
+{
+ PyObject *intermediate = _PyUnicodeWriter_Finish(&self->writer);
+ self->state = STATE_REALIZED;
+ if (intermediate == NULL)
+ return NULL;
+
+ _PyUnicodeWriter_Init(&self->writer);
+ self->writer.overallocate = 1;
+ if (_PyUnicodeWriter_WriteStr(&self->writer, intermediate)) {
+ Py_DECREF(intermediate);
+ return NULL;
+ }
+ self->state = STATE_ACCUMULATING;
+ return intermediate;
+}
+
+static int
+realize(stringio *self)
+{
+ Py_ssize_t len;
+ PyObject *intermediate;
+
+ if (self->state == STATE_REALIZED)
+ return 0;
+ assert(self->state == STATE_ACCUMULATING);
+ self->state = STATE_REALIZED;
+
+ intermediate = _PyUnicodeWriter_Finish(&self->writer);
+ if (intermediate == NULL)
+ return -1;
+
+ /* Append the intermediate string to the internal buffer.
+ The length should be equal to the current cursor position.
+ */
+ len = PyUnicode_GET_LENGTH(intermediate);
+ if (resize_buffer(self, len) < 0) {
+ Py_DECREF(intermediate);
+ return -1;
+ }
+ if (!PyUnicode_AsUCS4(intermediate, self->buf, len, 0)) {
+ Py_DECREF(intermediate);
+ return -1;
+ }
+
+ Py_DECREF(intermediate);
+ return 0;
+}
+
+/* Internal routine for writing a whole PyUnicode object to the buffer of a
+ StringIO object. Returns 0 on success, or -1 on error. */
+static Py_ssize_t
+write_str(stringio *self, PyObject *obj)
+{
+ Py_ssize_t len;
+ PyObject *decoded = NULL;
+
+ assert(self->buf != NULL);
+ assert(self->pos >= 0);
+
+ if (self->decoder != NULL) {
+ decoded = _PyIncrementalNewlineDecoder_decode(
+ self->decoder, obj, 1 /* always final */);
+ }
+ else {
+ decoded = Py_NewRef(obj);
+ }
+ if (self->writenl) {
+ PyObject *translated = PyUnicode_Replace(
+ decoded, &_Py_STR(newline), self->writenl, -1);
+ Py_SETREF(decoded, translated);
+ }
+ if (decoded == NULL)
+ return -1;
+
+ assert(PyUnicode_Check(decoded));
+ if (PyUnicode_READY(decoded)) {
+ Py_DECREF(decoded);
+ return -1;
+ }
+ len = PyUnicode_GET_LENGTH(decoded);
+ assert(len >= 0);
+
+ /* This overflow check is not strictly necessary. However, it avoids us to
+ deal with funky things like comparing an unsigned and a signed
+ integer. */
+ if (self->pos > PY_SSIZE_T_MAX - len) {
+ PyErr_SetString(PyExc_OverflowError,
+ "new position too large");
+ goto fail;
+ }
+
+ if (self->state == STATE_ACCUMULATING) {
+ if (self->string_size == self->pos) {
+ if (_PyUnicodeWriter_WriteStr(&self->writer, decoded))
+ goto fail;
+ goto success;
+ }
+ if (realize(self))
+ goto fail;
+ }
+
+ if (self->pos + len > self->string_size) {
+ if (resize_buffer(self, self->pos + len) < 0)
+ goto fail;
+ }
+
+ if (self->pos > self->string_size) {
+ /* In case of overseek, pad with null bytes the buffer region between
+ the end of stream and the current position.
+
+ 0 lo string_size hi
+ | |<---used--->|<----------available----------->|
+ | | <--to pad-->|<---to write---> |
+ 0 buf position
+
+ */
+ memset(self->buf + self->string_size, '\0',
+ (self->pos - self->string_size) * sizeof(Py_UCS4));
+ }
+
+ /* Copy the data to the internal buffer, overwriting some of the
+ existing data if self->pos < self->string_size. */
+ if (!PyUnicode_AsUCS4(decoded,
+ self->buf + self->pos,
+ self->buf_size - self->pos,
+ 0))
+ goto fail;
+
+success:
+ /* Set the new length of the internal string if it has changed. */
+ self->pos += len;
+ if (self->string_size < self->pos)
+ self->string_size = self->pos;
+
+ Py_DECREF(decoded);
+ return 0;
+
+fail:
+ Py_XDECREF(decoded);
+ return -1;
+}
+
+/*[clinic input]
+_io.StringIO.getvalue
+
+Retrieve the entire contents of the object.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_getvalue_impl(stringio *self)
+/*[clinic end generated code: output=27b6a7bfeaebce01 input=d23cb81d6791cf88]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ if (self->state == STATE_ACCUMULATING)
+ return make_intermediate(self);
+ return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, self->buf,
+ self->string_size);
+}
+
+/*[clinic input]
+_io.StringIO.tell
+
+Tell the current file position.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_tell_impl(stringio *self)
+/*[clinic end generated code: output=2e87ac67b116c77b input=ec866ebaff02f405]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ return PyLong_FromSsize_t(self->pos);
+}
+
+/*[clinic input]
+_io.StringIO.read
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read at most size characters, returned as a string.
+
+If the argument is negative or omitted, read until EOF
+is reached. Return an empty string at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_read_impl(stringio *self, Py_ssize_t size)
+/*[clinic end generated code: output=ae8cf6002f71626c input=0921093383dfb92d]*/
+{
+ Py_ssize_t n;
+ Py_UCS4 *output;
+
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+
+ /* adjust invalid sizes */
+ n = self->string_size - self->pos;
+ if (size < 0 || size > n) {
+ size = n;
+ if (size < 0)
+ size = 0;
+ }
+
+ /* Optimization for seek(0); read() */
+ if (self->state == STATE_ACCUMULATING && self->pos == 0 && size == n) {
+ PyObject *result = make_intermediate(self);
+ self->pos = self->string_size;
+ return result;
+ }
+
+ ENSURE_REALIZED(self);
+ output = self->buf + self->pos;
+ self->pos += size;
+ return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, size);
+}
+
+/* Internal helper, used by stringio_readline and stringio_iternext */
+static PyObject *
+_stringio_readline(stringio *self, Py_ssize_t limit)
+{
+ Py_UCS4 *start, *end, old_char;
+ Py_ssize_t len, consumed;
+
+ /* In case of overseek, return the empty string */
+ if (self->pos >= self->string_size)
+ return PyUnicode_New(0, 0);
+
+ start = self->buf + self->pos;
+ if (limit < 0 || limit > self->string_size - self->pos)
+ limit = self->string_size - self->pos;
+
+ end = start + limit;
+ old_char = *end;
+ *end = '\0';
+ len = _PyIO_find_line_ending(
+ self->readtranslate, self->readuniversal, self->readnl,
+ PyUnicode_4BYTE_KIND, (char*)start, (char*)end, &consumed);
+ *end = old_char;
+ /* If we haven't found any line ending, we just return everything
+ (`consumed` is ignored). */
+ if (len < 0)
+ len = limit;
+ self->pos += len;
+ return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, start, len);
+}
+
+/*[clinic input]
+_io.StringIO.readline
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read until newline or EOF.
+
+Returns an empty string if EOF is hit immediately.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_readline_impl(stringio *self, Py_ssize_t size)
+/*[clinic end generated code: output=cabd6452f1b7e85d input=a5bd70bf682aa276]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ ENSURE_REALIZED(self);
+
+ return _stringio_readline(self, size);
+}
+
+static PyObject *
+stringio_iternext(stringio *self)
+{
+ PyObject *line;
+
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ ENSURE_REALIZED(self);
+
+ if (Py_IS_TYPE(self, self->module_state->PyStringIO_Type)) {
+ /* Skip method call overhead for speed */
+ line = _stringio_readline(self, -1);
+ }
+ else {
+ /* XXX is subclassing StringIO really supported? */
+ line = PyObject_CallMethodNoArgs((PyObject *)self,
+ &_Py_ID(readline));
+ if (line && !PyUnicode_Check(line)) {
+ PyErr_Format(PyExc_OSError,
+ "readline() should have returned a str object, "
+ "not '%.200s'", Py_TYPE(line)->tp_name);
+ Py_DECREF(line);
+ return NULL;
+ }
+ }
+
+ if (line == NULL)
+ return NULL;
+
+ if (PyUnicode_GET_LENGTH(line) == 0) {
+ /* Reached EOF */
+ Py_DECREF(line);
+ return NULL;
+ }
+
+ return line;
+}
+
+/*[clinic input]
+_io.StringIO.truncate
+ pos as size: Py_ssize_t(accept={int, NoneType}, c_default="self->pos") = None
+ /
+
+Truncate size to pos.
+
+The pos argument defaults to the current file position, as
+returned by tell(). The current file position is unchanged.
+Returns the new absolute position.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_truncate_impl(stringio *self, Py_ssize_t size)
+/*[clinic end generated code: output=eb3aef8e06701365 input=5505cff90ca48b96]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+
+ if (size < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "Negative size value %zd", size);
+ return NULL;
+ }
+
+ if (size < self->string_size) {
+ ENSURE_REALIZED(self);
+ if (resize_buffer(self, size) < 0)
+ return NULL;
+ self->string_size = size;
+ }
+
+ return PyLong_FromSsize_t(size);
+}
+
+/*[clinic input]
+_io.StringIO.seek
+ pos: Py_ssize_t
+ whence: int = 0
+ /
+
+Change stream position.
+
+Seek to character offset pos relative to position indicated by whence:
+ 0 Start of stream (the default). pos should be >= 0;
+ 1 Current position - pos must be 0;
+ 2 End of stream - pos must be 0.
+Returns the new absolute position.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence)
+/*[clinic end generated code: output=e9e0ac9a8ae71c25 input=e3855b24e7cae06a]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+
+ if (whence != 0 && whence != 1 && whence != 2) {
+ PyErr_Format(PyExc_ValueError,
+ "Invalid whence (%i, should be 0, 1 or 2)", whence);
+ return NULL;
+ }
+ else if (pos < 0 && whence == 0) {
+ PyErr_Format(PyExc_ValueError,
+ "Negative seek position %zd", pos);
+ return NULL;
+ }
+ else if (whence != 0 && pos != 0) {
+ PyErr_SetString(PyExc_OSError,
+ "Can't do nonzero cur-relative seeks");
+ return NULL;
+ }
+
+ /* whence = 0: offset relative to beginning of the string.
+ whence = 1: no change to current position.
+ whence = 2: change position to end of file. */
+ if (whence == 1) {
+ pos = self->pos;
+ }
+ else if (whence == 2) {
+ pos = self->string_size;
+ }
+
+ self->pos = pos;
+
+ return PyLong_FromSsize_t(self->pos);
+}
+
+/*[clinic input]
+_io.StringIO.write
+ s as obj: object
+ /
+
+Write string to file.
+
+Returns the number of characters written, which is always equal to
+the length of the string.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_write(stringio *self, PyObject *obj)
+/*[clinic end generated code: output=0deaba91a15b94da input=cf96f3b16586e669]*/
+{
+ Py_ssize_t size;
+
+ CHECK_INITIALIZED(self);
+ if (!PyUnicode_Check(obj)) {
+ PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'",
+ Py_TYPE(obj)->tp_name);
+ return NULL;
+ }
+ if (PyUnicode_READY(obj))
+ return NULL;
+ CHECK_CLOSED(self);
+ size = PyUnicode_GET_LENGTH(obj);
+
+ if (size > 0 && write_str(self, obj) < 0)
+ return NULL;
+
+ return PyLong_FromSsize_t(size);
+}
+
+/*[clinic input]
+_io.StringIO.close
+
+Close the IO object.
+
+Attempting any further operation after the object is closed
+will raise a ValueError.
+
+This method has no effect if the file is already closed.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_close_impl(stringio *self)
+/*[clinic end generated code: output=04399355cbe518f1 input=cbc10b45f35d6d46]*/
+{
+ self->closed = 1;
+ /* Free up some memory */
+ if (resize_buffer(self, 0) < 0)
+ return NULL;
+ _PyUnicodeWriter_Dealloc(&self->writer);
+ Py_CLEAR(self->readnl);
+ Py_CLEAR(self->writenl);
+ Py_CLEAR(self->decoder);
+ Py_RETURN_NONE;
+}
+
+static int
+stringio_traverse(stringio *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->readnl);
+ Py_VISIT(self->writenl);
+ Py_VISIT(self->decoder);
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static int
+stringio_clear(stringio *self)
+{
+ Py_CLEAR(self->readnl);
+ Py_CLEAR(self->writenl);
+ Py_CLEAR(self->decoder);
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+stringio_dealloc(stringio *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ _PyObject_GC_UNTRACK(self);
+ self->ok = 0;
+ if (self->buf) {
+ PyMem_Free(self->buf);
+ self->buf = NULL;
+ }
+ _PyUnicodeWriter_Dealloc(&self->writer);
+ (void)stringio_clear(self);
+ if (self->weakreflist != NULL) {
+ PyObject_ClearWeakRefs((PyObject *) self);
+ }
+ tp->tp_free(self);
+ Py_DECREF(tp);
+}
+
+static PyObject *
+stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ stringio *self;
+
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = (stringio *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ /* tp_alloc initializes all the fields to zero. So we don't have to
+ initialize them here. */
+
+ self->buf = (Py_UCS4 *)PyMem_Malloc(0);
+ if (self->buf == NULL) {
+ Py_DECREF(self);
+ return PyErr_NoMemory();
+ }
+
+ return (PyObject *)self;
+}
+
+/*[clinic input]
+_io.StringIO.__init__
+ initial_value as value: object(c_default="NULL") = ''
+ newline as newline_obj: object(c_default="NULL") = '\n'
+
+Text I/O implementation using an in-memory buffer.
+
+The initial_value argument sets the value of object. The newline
+argument is like the one of TextIOWrapper's constructor.
+[clinic start generated code]*/
+
+static int
+_io_StringIO___init___impl(stringio *self, PyObject *value,
+ PyObject *newline_obj)
+/*[clinic end generated code: output=a421ea023b22ef4e input=cee2d9181b2577a3]*/
+{
+ const char *newline = "\n";
+ Py_ssize_t value_len;
+
+ /* Parse the newline argument. We only want to allow unicode objects or
+ None. */
+ if (newline_obj == Py_None) {
+ newline = NULL;
+ }
+ else if (newline_obj) {
+ if (!PyUnicode_Check(newline_obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "newline must be str or None, not %.200s",
+ Py_TYPE(newline_obj)->tp_name);
+ return -1;
+ }
+ newline = PyUnicode_AsUTF8(newline_obj);
+ if (newline == NULL)
+ return -1;
+ }
+
+ if (newline && newline[0] != '\0'
+ && !(newline[0] == '\n' && newline[1] == '\0')
+ && !(newline[0] == '\r' && newline[1] == '\0')
+ && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
+ PyErr_Format(PyExc_ValueError,
+ "illegal newline value: %R", newline_obj);
+ return -1;
+ }
+ if (value && value != Py_None && !PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "initial_value must be str or None, not %.200s",
+ Py_TYPE(value)->tp_name);
+ return -1;
+ }
+
+ self->ok = 0;
+
+ _PyUnicodeWriter_Dealloc(&self->writer);
+ Py_CLEAR(self->readnl);
+ Py_CLEAR(self->writenl);
+ Py_CLEAR(self->decoder);
+
+ assert((newline != NULL && newline_obj != Py_None) ||
+ (newline == NULL && newline_obj == Py_None));
+
+ if (newline) {
+ self->readnl = PyUnicode_FromString(newline);
+ if (self->readnl == NULL)
+ return -1;
+ }
+ self->readuniversal = (newline == NULL || newline[0] == '\0');
+ self->readtranslate = (newline == NULL);
+ /* If newline == "", we don't translate anything.
+ If newline == "\n" or newline == None, we translate to "\n", which is
+ a no-op.
+ (for newline == None, TextIOWrapper translates to os.linesep, but it
+ is pointless for StringIO)
+ */
+ if (newline != NULL && newline[0] == '\r') {
+ self->writenl = Py_NewRef(self->readnl);
+ }
+
+ _PyIO_State *module_state = find_io_state_by_def(Py_TYPE(self));
+ if (self->readuniversal) {
+ self->decoder = PyObject_CallFunctionObjArgs(
+ (PyObject *)module_state->PyIncrementalNewlineDecoder_Type,
+ Py_None, self->readtranslate ? Py_True : Py_False, NULL);
+ if (self->decoder == NULL)
+ return -1;
+ }
+
+ /* Now everything is set up, resize buffer to size of initial value,
+ and copy it */
+ self->string_size = 0;
+ if (value && value != Py_None)
+ value_len = PyUnicode_GetLength(value);
+ else
+ value_len = 0;
+ if (value_len > 0) {
+ /* This is a heuristic, for newline translation might change
+ the string length. */
+ if (resize_buffer(self, 0) < 0)
+ return -1;
+ self->state = STATE_REALIZED;
+ self->pos = 0;
+ if (write_str(self, value) < 0)
+ return -1;
+ }
+ else {
+ /* Empty stringio object, we can start by accumulating */
+ if (resize_buffer(self, 0) < 0)
+ return -1;
+ _PyUnicodeWriter_Init(&self->writer);
+ self->writer.overallocate = 1;
+ self->state = STATE_ACCUMULATING;
+ }
+ self->pos = 0;
+ self->module_state = module_state;
+ self->closed = 0;
+ self->ok = 1;
+ return 0;
+}
+
+/* Properties and pseudo-properties */
+
+/*[clinic input]
+_io.StringIO.readable
+
+Returns True if the IO object can be read.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_readable_impl(stringio *self)
+/*[clinic end generated code: output=b19d44dd8b1ceb99 input=39ce068b224c21ad]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/*[clinic input]
+_io.StringIO.writable
+
+Returns True if the IO object can be written.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_writable_impl(stringio *self)
+/*[clinic end generated code: output=13e4dd77187074ca input=7a691353aac38835]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/*[clinic input]
+_io.StringIO.seekable
+
+Returns True if the IO object can be seeked.
+[clinic start generated code]*/
+
+static PyObject *
+_io_StringIO_seekable_impl(stringio *self)
+/*[clinic end generated code: output=4d20b4641c756879 input=4c606d05b32952e6]*/
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ Py_RETURN_TRUE;
+}
+
+/* Pickling support.
+
+ The implementation of __getstate__ is similar to the one for BytesIO,
+ except that we also save the newline parameter. For __setstate__ and unlike
+ BytesIO, we call __init__ to restore the object's state. Doing so allows us
+ to avoid decoding the complex newline state while keeping the object
+ representation compact.
+
+ See comment in bytesio.c regarding why only pickle protocols and onward are
+ supported.
+*/
+
+static PyObject *
+stringio_getstate(stringio *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *initvalue = _io_StringIO_getvalue_impl(self);
+ PyObject *dict;
+ PyObject *state;
+
+ if (initvalue == NULL)
+ return NULL;
+ if (self->dict == NULL) {
+ dict = Py_NewRef(Py_None);
+ }
+ else {
+ dict = PyDict_Copy(self->dict);
+ if (dict == NULL) {
+ Py_DECREF(initvalue);
+ return NULL;
+ }
+ }
+
+ state = Py_BuildValue("(OOnN)", initvalue,
+ self->readnl ? self->readnl : Py_None,
+ self->pos, dict);
+ Py_DECREF(initvalue);
+ return state;
+}
+
+static PyObject *
+stringio_setstate(stringio *self, PyObject *state)
+{
+ PyObject *initarg;
+ PyObject *position_obj;
+ PyObject *dict;
+ Py_ssize_t pos;
+
+ assert(state != NULL);
+ CHECK_CLOSED(self);
+
+ /* We allow the state tuple to be longer than 4, because we may need
+ someday to extend the object's state without breaking
+ backward-compatibility. */
+ if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) < 4) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s.__setstate__ argument should be 4-tuple, got %.200s",
+ Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
+ return NULL;
+ }
+
+ /* Initialize the object's state. */
+ initarg = PyTuple_GetSlice(state, 0, 2);
+ if (initarg == NULL)
+ return NULL;
+ if (_io_StringIO___init__((PyObject *)self, initarg, NULL) < 0) {
+ Py_DECREF(initarg);
+ return NULL;
+ }
+ Py_DECREF(initarg);
+
+ /* Restore the buffer state. Even if __init__ did initialize the buffer,
+ we have to initialize it again since __init__ may translate the
+ newlines in the initial_value string. We clearly do not want that
+ because the string value in the state tuple has already been translated
+ once by __init__. So we do not take any chance and replace object's
+ buffer completely. */
+ {
+ PyObject *item;
+ Py_UCS4 *buf;
+ Py_ssize_t bufsize;
+
+ item = PyTuple_GET_ITEM(state, 0);
+ buf = PyUnicode_AsUCS4Copy(item);
+ if (buf == NULL)
+ return NULL;
+ bufsize = PyUnicode_GET_LENGTH(item);
+
+ if (resize_buffer(self, bufsize) < 0) {
+ PyMem_Free(buf);
+ return NULL;
+ }
+ memcpy(self->buf, buf, bufsize * sizeof(Py_UCS4));
+ PyMem_Free(buf);
+ self->string_size = bufsize;
+ }
+
+ /* Set carefully the position value. Alternatively, we could use the seek
+ method instead of modifying self->pos directly to better protect the
+ object internal state against erroneous (or malicious) inputs. */
+ position_obj = PyTuple_GET_ITEM(state, 2);
+ if (!PyLong_Check(position_obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "third item of state must be an integer, got %.200s",
+ Py_TYPE(position_obj)->tp_name);
+ return NULL;
+ }
+ pos = PyLong_AsSsize_t(position_obj);
+ if (pos == -1 && PyErr_Occurred())
+ return NULL;
+ if (pos < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "position value cannot be negative");
+ return NULL;
+ }
+ self->pos = pos;
+
+ /* Set the dictionary of the instance variables. */
+ dict = PyTuple_GET_ITEM(state, 3);
+ if (dict != Py_None) {
+ if (!PyDict_Check(dict)) {
+ PyErr_Format(PyExc_TypeError,
+ "fourth item of state should be a dict, got a %.200s",
+ Py_TYPE(dict)->tp_name);
+ return NULL;
+ }
+ if (self->dict) {
+ /* Alternatively, we could replace the internal dictionary
+ completely. However, it seems more practical to just update it. */
+ if (PyDict_Update(self->dict, dict) < 0)
+ return NULL;
+ }
+ else {
+ self->dict = Py_NewRef(dict);
+ }
+ }
+
+ Py_RETURN_NONE;
+}
+
+
+static PyObject *
+stringio_closed(stringio *self, void *context)
+{
+ CHECK_INITIALIZED(self);
+ return PyBool_FromLong(self->closed);
+}
+
+static PyObject *
+stringio_line_buffering(stringio *self, void *context)
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ Py_RETURN_FALSE;
+}
+
+static PyObject *
+stringio_newlines(stringio *self, void *context)
+{
+ CHECK_INITIALIZED(self);
+ CHECK_CLOSED(self);
+ if (self->decoder == NULL)
+ Py_RETURN_NONE;
+ return PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
+}
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/stringio.c.h"
+#undef clinic_state
+
+static struct PyMethodDef stringio_methods[] = {
+ _IO_STRINGIO_CLOSE_METHODDEF
+ _IO_STRINGIO_GETVALUE_METHODDEF
+ _IO_STRINGIO_READ_METHODDEF
+ _IO_STRINGIO_READLINE_METHODDEF
+ _IO_STRINGIO_TELL_METHODDEF
+ _IO_STRINGIO_TRUNCATE_METHODDEF
+ _IO_STRINGIO_SEEK_METHODDEF
+ _IO_STRINGIO_WRITE_METHODDEF
+
+ _IO_STRINGIO_SEEKABLE_METHODDEF
+ _IO_STRINGIO_READABLE_METHODDEF
+ _IO_STRINGIO_WRITABLE_METHODDEF
+
+ {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS},
+ {"__setstate__", (PyCFunction)stringio_setstate, METH_O},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyGetSetDef stringio_getset[] = {
+ {"closed", (getter)stringio_closed, NULL, NULL},
+ {"newlines", (getter)stringio_newlines, NULL, NULL},
+ /* (following comments straight off of the original Python wrapper:)
+ XXX Cruft to support the TextIOWrapper API. This would only
+ be meaningful if StringIO supported the buffer attribute.
+ Hopefully, a better solution, than adding these pseudo-attributes,
+ will be found.
+ */
+ {"line_buffering", (getter)stringio_line_buffering, NULL, NULL},
+ {NULL}
+};
+
+static struct PyMemberDef stringio_members[] = {
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(stringio, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(stringio, dict), READONLY},
+ {NULL},
+};
+
+static PyType_Slot stringio_slots[] = {
+ {Py_tp_dealloc, stringio_dealloc},
+ {Py_tp_doc, (void *)_io_StringIO___init____doc__},
+ {Py_tp_traverse, stringio_traverse},
+ {Py_tp_clear, stringio_clear},
+ {Py_tp_iternext, stringio_iternext},
+ {Py_tp_methods, stringio_methods},
+ {Py_tp_members, stringio_members},
+ {Py_tp_getset, stringio_getset},
+ {Py_tp_init, _io_StringIO___init__},
+ {Py_tp_new, stringio_new},
+ {0, NULL},
+};
+
+PyType_Spec stringio_spec = {
+ .name = "_io.StringIO",
+ .basicsize = sizeof(stringio),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = stringio_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/textio.c b/contrib/tools/python3/Modules/_io/textio.c
new file mode 100644
index 0000000000..14dd19d95c
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/textio.c
@@ -0,0 +1,3352 @@
+/*
+ An implementation of Text I/O as defined by PEP 3116 - "New I/O"
+
+ Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper.
+
+ Written by Amaury Forgeot d'Arc and Antoine Pitrou
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "pycore_interp.h" // PyInterpreterState.fs_codec
+#include "pycore_long.h" // _PyLong_GetZero()
+#include "pycore_fileutils.h" // _Py_GetLocaleEncoding()
+#include "pycore_object.h"
+#include "pycore_pystate.h" // _PyInterpreterState_GET()
+#include "structmember.h" // PyMemberDef
+#include "_iomodule.h"
+
+/*[clinic input]
+module _io
+class _io.IncrementalNewlineDecoder "nldecoder_object *" "clinic_state()->PyIncrementalNewlineDecoder_Type"
+class _io.TextIOWrapper "textio *" "clinic_state()->TextIOWrapper_Type"
+class _io._TextIOBase "PyObject *" "&PyTextIOBase_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8b7f24fa13bfdd7f]*/
+
+typedef struct nldecoder_object nldecoder_object;
+typedef struct textio textio;
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/textio.c.h"
+#undef clinic_state
+
+/* TextIOBase */
+
+PyDoc_STRVAR(textiobase_doc,
+ "Base class for text I/O.\n"
+ "\n"
+ "This class provides a character and line based interface to stream\n"
+ "I/O. There is no readinto method because Python's character strings\n"
+ "are immutable.\n"
+ );
+
+static PyObject *
+_unsupported(_PyIO_State *state, const char *message)
+{
+ PyErr_SetString(state->unsupported_operation, message);
+ return NULL;
+}
+
+/*[clinic input]
+_io._TextIOBase.detach
+ cls: defining_class
+ /
+
+Separate the underlying buffer from the TextIOBase and return it.
+
+After the underlying buffer has been detached, the TextIO is in an unusable state.
+[clinic start generated code]*/
+
+static PyObject *
+_io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
+/*[clinic end generated code: output=50915f40c609eaa4 input=987ca3640d0a3776]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return _unsupported(state, "detach");
+}
+
+/*[clinic input]
+_io._TextIOBase.read
+ cls: defining_class
+ size: int(unused=True) = -1
+ /
+
+Read at most size characters from stream.
+
+Read from underlying buffer until we have size characters or we hit EOF.
+If size is negative or omitted, read until EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size))
+/*[clinic end generated code: output=51a5178a309ce647 input=f5e37720f9fc563f]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return _unsupported(state, "read");
+}
+
+/*[clinic input]
+_io._TextIOBase.readline
+ cls: defining_class
+ size: int(unused=True) = -1
+ /
+
+Read until newline or EOF.
+
+Return an empty string if EOF is hit immediately.
+If size is specified, at most size characters will be read.
+[clinic start generated code]*/
+
+static PyObject *
+_io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls,
+ int Py_UNUSED(size))
+/*[clinic end generated code: output=3f47d7966d6d074e input=42eafec94107fa27]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return _unsupported(state, "readline");
+}
+
+/*[clinic input]
+_io._TextIOBase.write
+ cls: defining_class
+ s: str(unused=True)
+ /
+
+Write string s to stream.
+
+Return the number of characters written
+(which is always equal to the length of the string).
+[clinic start generated code]*/
+
+static PyObject *
+_io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls,
+ const char *Py_UNUSED(s))
+/*[clinic end generated code: output=18b28231460275de input=e9cabaa5f6732b07]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return _unsupported(state, "write");
+}
+
+PyDoc_STRVAR(textiobase_encoding_doc,
+ "Encoding of the text stream.\n"
+ "\n"
+ "Subclasses should override.\n"
+ );
+
+static PyObject *
+textiobase_encoding_get(PyObject *self, void *context)
+{
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(textiobase_newlines_doc,
+ "Line endings translated so far.\n"
+ "\n"
+ "Only line endings translated during reading are considered.\n"
+ "\n"
+ "Subclasses should override.\n"
+ );
+
+static PyObject *
+textiobase_newlines_get(PyObject *self, void *context)
+{
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(textiobase_errors_doc,
+ "The error setting of the decoder or encoder.\n"
+ "\n"
+ "Subclasses should override.\n"
+ );
+
+static PyObject *
+textiobase_errors_get(PyObject *self, void *context)
+{
+ Py_RETURN_NONE;
+}
+
+
+static PyMethodDef textiobase_methods[] = {
+ _IO__TEXTIOBASE_DETACH_METHODDEF
+ _IO__TEXTIOBASE_READ_METHODDEF
+ _IO__TEXTIOBASE_READLINE_METHODDEF
+ _IO__TEXTIOBASE_WRITE_METHODDEF
+ {NULL, NULL}
+};
+
+static PyGetSetDef textiobase_getset[] = {
+ {"encoding", (getter)textiobase_encoding_get, NULL, textiobase_encoding_doc},
+ {"newlines", (getter)textiobase_newlines_get, NULL, textiobase_newlines_doc},
+ {"errors", (getter)textiobase_errors_get, NULL, textiobase_errors_doc},
+ {NULL}
+};
+
+static PyType_Slot textiobase_slots[] = {
+ {Py_tp_doc, (void *)textiobase_doc},
+ {Py_tp_methods, textiobase_methods},
+ {Py_tp_getset, textiobase_getset},
+ {0, NULL},
+};
+
+/* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */
+PyType_Spec textiobase_spec = {
+ .name = "_io._TextIOBase",
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = textiobase_slots,
+};
+
+/* IncrementalNewlineDecoder */
+
+struct nldecoder_object {
+ PyObject_HEAD
+ PyObject *decoder;
+ PyObject *errors;
+ unsigned int pendingcr: 1;
+ unsigned int translate: 1;
+ unsigned int seennl: 3;
+};
+
+/*[clinic input]
+_io.IncrementalNewlineDecoder.__init__
+ decoder: object
+ translate: bool
+ errors: object(c_default="NULL") = "strict"
+
+Codec used when reading a file in universal newlines mode.
+
+It wraps another incremental decoder, translating \r\n and \r into \n.
+It also records the types of newlines encountered. When used with
+translate=False, it ensures that the newline sequence is returned in
+one piece. When used with decoder=None, it expects unicode strings as
+decode input and translates newlines without first invoking an external
+decoder.
+[clinic start generated code]*/
+
+static int
+_io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
+ PyObject *decoder, int translate,
+ PyObject *errors)
+/*[clinic end generated code: output=fbd04d443e764ec2 input=ed547aa257616b0e]*/
+{
+
+ if (errors == NULL) {
+ errors = Py_NewRef(&_Py_ID(strict));
+ }
+ else {
+ errors = Py_NewRef(errors);
+ }
+
+ Py_XSETREF(self->errors, errors);
+ Py_XSETREF(self->decoder, Py_NewRef(decoder));
+ self->translate = translate ? 1 : 0;
+ self->seennl = 0;
+ self->pendingcr = 0;
+
+ return 0;
+}
+
+static int
+incrementalnewlinedecoder_traverse(nldecoder_object *self, visitproc visit,
+ void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->decoder);
+ Py_VISIT(self->errors);
+ return 0;
+}
+
+static int
+incrementalnewlinedecoder_clear(nldecoder_object *self)
+{
+ Py_CLEAR(self->decoder);
+ Py_CLEAR(self->errors);
+ return 0;
+}
+
+static void
+incrementalnewlinedecoder_dealloc(nldecoder_object *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ _PyObject_GC_UNTRACK(self);
+ (void)incrementalnewlinedecoder_clear(self);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+static int
+check_decoded(PyObject *decoded)
+{
+ if (decoded == NULL)
+ return -1;
+ if (!PyUnicode_Check(decoded)) {
+ PyErr_Format(PyExc_TypeError,
+ "decoder should return a string result, not '%.200s'",
+ Py_TYPE(decoded)->tp_name);
+ Py_DECREF(decoded);
+ return -1;
+ }
+ if (PyUnicode_READY(decoded) < 0) {
+ Py_DECREF(decoded);
+ return -1;
+ }
+ return 0;
+}
+
+#define CHECK_INITIALIZED_DECODER(self) \
+ if (self->errors == NULL) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "IncrementalNewlineDecoder.__init__() not called"); \
+ return NULL; \
+ }
+
+#define SEEN_CR 1
+#define SEEN_LF 2
+#define SEEN_CRLF 4
+#define SEEN_ALL (SEEN_CR | SEEN_LF | SEEN_CRLF)
+
+PyObject *
+_PyIncrementalNewlineDecoder_decode(PyObject *myself,
+ PyObject *input, int final)
+{
+ PyObject *output;
+ Py_ssize_t output_len;
+ nldecoder_object *self = (nldecoder_object *) myself;
+
+ CHECK_INITIALIZED_DECODER(self);
+
+ /* decode input (with the eventual \r from a previous pass) */
+ if (self->decoder != Py_None) {
+ output = PyObject_CallMethodObjArgs(self->decoder,
+ &_Py_ID(decode), input, final ? Py_True : Py_False, NULL);
+ }
+ else {
+ output = Py_NewRef(input);
+ }
+
+ if (check_decoded(output) < 0)
+ return NULL;
+
+ output_len = PyUnicode_GET_LENGTH(output);
+ if (self->pendingcr && (final || output_len > 0)) {
+ /* Prefix output with CR */
+ int kind;
+ PyObject *modified;
+ char *out;
+
+ modified = PyUnicode_New(output_len + 1,
+ PyUnicode_MAX_CHAR_VALUE(output));
+ if (modified == NULL)
+ goto error;
+ kind = PyUnicode_KIND(modified);
+ out = PyUnicode_DATA(modified);
+ PyUnicode_WRITE(kind, out, 0, '\r');
+ memcpy(out + kind, PyUnicode_DATA(output), kind * output_len);
+ Py_SETREF(output, modified); /* output remains ready */
+ self->pendingcr = 0;
+ output_len++;
+ }
+
+ /* retain last \r even when not translating data:
+ * then readline() is sure to get \r\n in one pass
+ */
+ if (!final) {
+ if (output_len > 0
+ && PyUnicode_READ_CHAR(output, output_len - 1) == '\r')
+ {
+ PyObject *modified = PyUnicode_Substring(output, 0, output_len -1);
+ if (modified == NULL)
+ goto error;
+ Py_SETREF(output, modified);
+ self->pendingcr = 1;
+ }
+ }
+
+ /* Record which newlines are read and do newline translation if desired,
+ all in one pass. */
+ {
+ const void *in_str;
+ Py_ssize_t len;
+ int seennl = self->seennl;
+ int only_lf = 0;
+ int kind;
+
+ in_str = PyUnicode_DATA(output);
+ len = PyUnicode_GET_LENGTH(output);
+ kind = PyUnicode_KIND(output);
+
+ if (len == 0)
+ return output;
+
+ /* If, up to now, newlines are consistently \n, do a quick check
+ for the \r *byte* with the libc's optimized memchr.
+ */
+ if (seennl == SEEN_LF || seennl == 0) {
+ only_lf = (memchr(in_str, '\r', kind * len) == NULL);
+ }
+
+ if (only_lf) {
+ /* If not already seen, quick scan for a possible "\n" character.
+ (there's nothing else to be done, even when in translation mode)
+ */
+ if (seennl == 0 &&
+ memchr(in_str, '\n', kind * len) != NULL) {
+ if (kind == PyUnicode_1BYTE_KIND)
+ seennl |= SEEN_LF;
+ else {
+ Py_ssize_t i = 0;
+ for (;;) {
+ Py_UCS4 c;
+ /* Fast loop for non-control characters */
+ while (PyUnicode_READ(kind, in_str, i) > '\n')
+ i++;
+ c = PyUnicode_READ(kind, in_str, i++);
+ if (c == '\n') {
+ seennl |= SEEN_LF;
+ break;
+ }
+ if (i >= len)
+ break;
+ }
+ }
+ }
+ /* Finished: we have scanned for newlines, and none of them
+ need translating */
+ }
+ else if (!self->translate) {
+ Py_ssize_t i = 0;
+ /* We have already seen all newline types, no need to scan again */
+ if (seennl == SEEN_ALL)
+ goto endscan;
+ for (;;) {
+ Py_UCS4 c;
+ /* Fast loop for non-control characters */
+ while (PyUnicode_READ(kind, in_str, i) > '\r')
+ i++;
+ c = PyUnicode_READ(kind, in_str, i++);
+ if (c == '\n')
+ seennl |= SEEN_LF;
+ else if (c == '\r') {
+ if (PyUnicode_READ(kind, in_str, i) == '\n') {
+ seennl |= SEEN_CRLF;
+ i++;
+ }
+ else
+ seennl |= SEEN_CR;
+ }
+ if (i >= len)
+ break;
+ if (seennl == SEEN_ALL)
+ break;
+ }
+ endscan:
+ ;
+ }
+ else {
+ void *translated;
+ int kind = PyUnicode_KIND(output);
+ const void *in_str = PyUnicode_DATA(output);
+ Py_ssize_t in, out;
+ /* XXX: Previous in-place translation here is disabled as
+ resizing is not possible anymore */
+ /* We could try to optimize this so that we only do a copy
+ when there is something to translate. On the other hand,
+ we already know there is a \r byte, so chances are high
+ that something needs to be done. */
+ translated = PyMem_Malloc(kind * len);
+ if (translated == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ in = out = 0;
+ for (;;) {
+ Py_UCS4 c;
+ /* Fast loop for non-control characters */
+ while ((c = PyUnicode_READ(kind, in_str, in++)) > '\r')
+ PyUnicode_WRITE(kind, translated, out++, c);
+ if (c == '\n') {
+ PyUnicode_WRITE(kind, translated, out++, c);
+ seennl |= SEEN_LF;
+ continue;
+ }
+ if (c == '\r') {
+ if (PyUnicode_READ(kind, in_str, in) == '\n') {
+ in++;
+ seennl |= SEEN_CRLF;
+ }
+ else
+ seennl |= SEEN_CR;
+ PyUnicode_WRITE(kind, translated, out++, '\n');
+ continue;
+ }
+ if (in > len)
+ break;
+ PyUnicode_WRITE(kind, translated, out++, c);
+ }
+ Py_DECREF(output);
+ output = PyUnicode_FromKindAndData(kind, translated, out);
+ PyMem_Free(translated);
+ if (!output)
+ return NULL;
+ }
+ self->seennl |= seennl;
+ }
+
+ return output;
+
+ error:
+ Py_DECREF(output);
+ return NULL;
+}
+
+/*[clinic input]
+_io.IncrementalNewlineDecoder.decode
+ input: object
+ final: bool = False
+[clinic start generated code]*/
+
+static PyObject *
+_io_IncrementalNewlineDecoder_decode_impl(nldecoder_object *self,
+ PyObject *input, int final)
+/*[clinic end generated code: output=0d486755bb37a66e input=90e223c70322c5cd]*/
+{
+ return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final);
+}
+
+/*[clinic input]
+_io.IncrementalNewlineDecoder.getstate
+[clinic start generated code]*/
+
+static PyObject *
+_io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
+/*[clinic end generated code: output=f0d2c9c136f4e0d0 input=f8ff101825e32e7f]*/
+{
+ PyObject *buffer;
+ unsigned long long flag;
+
+ CHECK_INITIALIZED_DECODER(self);
+
+ if (self->decoder != Py_None) {
+ PyObject *state = PyObject_CallMethodNoArgs(self->decoder,
+ &_Py_ID(getstate));
+ if (state == NULL)
+ return NULL;
+ if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError,
+ "illegal decoder state");
+ Py_DECREF(state);
+ return NULL;
+ }
+ if (!PyArg_ParseTuple(state, "OK;illegal decoder state",
+ &buffer, &flag))
+ {
+ Py_DECREF(state);
+ return NULL;
+ }
+ Py_INCREF(buffer);
+ Py_DECREF(state);
+ }
+ else {
+ buffer = PyBytes_FromString("");
+ flag = 0;
+ }
+ flag <<= 1;
+ if (self->pendingcr)
+ flag |= 1;
+ return Py_BuildValue("NK", buffer, flag);
+}
+
+/*[clinic input]
+_io.IncrementalNewlineDecoder.setstate
+ state: object
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
+ PyObject *state)
+/*[clinic end generated code: output=c10c622508b576cb input=c53fb505a76dbbe2]*/
+{
+ PyObject *buffer;
+ unsigned long long flag;
+
+ CHECK_INITIALIZED_DECODER(self);
+
+ if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError, "state argument must be a tuple");
+ return NULL;
+ }
+ if (!PyArg_ParseTuple(state, "OK;setstate(): illegal state argument",
+ &buffer, &flag))
+ {
+ return NULL;
+ }
+
+ self->pendingcr = (int) (flag & 1);
+ flag >>= 1;
+
+ if (self->decoder != Py_None) {
+ return _PyObject_CallMethod(self->decoder, &_Py_ID(setstate),
+ "((OK))", buffer, flag);
+ }
+ else {
+ Py_RETURN_NONE;
+ }
+}
+
+/*[clinic input]
+_io.IncrementalNewlineDecoder.reset
+[clinic start generated code]*/
+
+static PyObject *
+_io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self)
+/*[clinic end generated code: output=32fa40c7462aa8ff input=728678ddaea776df]*/
+{
+ CHECK_INITIALIZED_DECODER(self);
+
+ self->seennl = 0;
+ self->pendingcr = 0;
+ if (self->decoder != Py_None)
+ return PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
+ else
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
+{
+ CHECK_INITIALIZED_DECODER(self);
+
+ switch (self->seennl) {
+ case SEEN_CR:
+ return PyUnicode_FromString("\r");
+ case SEEN_LF:
+ return PyUnicode_FromString("\n");
+ case SEEN_CRLF:
+ return PyUnicode_FromString("\r\n");
+ case SEEN_CR | SEEN_LF:
+ return Py_BuildValue("ss", "\r", "\n");
+ case SEEN_CR | SEEN_CRLF:
+ return Py_BuildValue("ss", "\r", "\r\n");
+ case SEEN_LF | SEEN_CRLF:
+ return Py_BuildValue("ss", "\n", "\r\n");
+ case SEEN_CR | SEEN_LF | SEEN_CRLF:
+ return Py_BuildValue("sss", "\r", "\n", "\r\n");
+ default:
+ Py_RETURN_NONE;
+ }
+
+}
+
+/* TextIOWrapper */
+
+typedef PyObject *
+ (*encodefunc_t)(PyObject *, PyObject *);
+
+struct textio
+{
+ PyObject_HEAD
+ int ok; /* initialized? */
+ int detached;
+ Py_ssize_t chunk_size;
+ PyObject *buffer;
+ PyObject *encoding;
+ PyObject *encoder;
+ PyObject *decoder;
+ PyObject *readnl;
+ PyObject *errors;
+ const char *writenl; /* ASCII-encoded; NULL stands for \n */
+ char line_buffering;
+ char write_through;
+ char readuniversal;
+ char readtranslate;
+ char writetranslate;
+ char seekable;
+ char has_read1;
+ char telling;
+ char finalizing;
+ /* Specialized encoding func (see below) */
+ encodefunc_t encodefunc;
+ /* Whether or not it's the start of the stream */
+ char encoding_start_of_stream;
+
+ /* Reads and writes are internally buffered in order to speed things up.
+ However, any read will first flush the write buffer if itsn't empty.
+
+ Please also note that text to be written is first encoded before being
+ buffered. This is necessary so that encoding errors are immediately
+ reported to the caller, but it unfortunately means that the
+ IncrementalEncoder (whose encode() method is always written in Python)
+ becomes a bottleneck for small writes.
+ */
+ PyObject *decoded_chars; /* buffer for text returned from decoder */
+ Py_ssize_t decoded_chars_used; /* offset into _decoded_chars for read() */
+ PyObject *pending_bytes; // data waiting to be written.
+ // ascii unicode, bytes, or list of them.
+ Py_ssize_t pending_bytes_count;
+
+ /* snapshot is either NULL, or a tuple (dec_flags, next_input) where
+ * dec_flags is the second (integer) item of the decoder state and
+ * next_input is the chunk of input bytes that comes next after the
+ * snapshot point. We use this to reconstruct decoder states in tell().
+ */
+ PyObject *snapshot;
+ /* Bytes-to-characters ratio for the current chunk. Serves as input for
+ the heuristic in tell(). */
+ double b2cratio;
+
+ /* Cache raw object if it's a FileIO object */
+ PyObject *raw;
+
+ PyObject *weakreflist;
+ PyObject *dict;
+
+ _PyIO_State *state;
+};
+
+static void
+textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
+
+/* A couple of specialized cases in order to bypass the slow incremental
+ encoding methods for the most popular encodings. */
+
+static PyObject *
+ascii_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_AsASCIIString(text, PyUnicode_AsUTF8(self->errors));
+}
+
+static PyObject *
+utf16be_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_EncodeUTF16(text,
+ PyUnicode_AsUTF8(self->errors), 1);
+}
+
+static PyObject *
+utf16le_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_EncodeUTF16(text,
+ PyUnicode_AsUTF8(self->errors), -1);
+}
+
+static PyObject *
+utf16_encode(textio *self, PyObject *text)
+{
+ if (!self->encoding_start_of_stream) {
+ /* Skip the BOM and use native byte ordering */
+#if PY_BIG_ENDIAN
+ return utf16be_encode(self, text);
+#else
+ return utf16le_encode(self, text);
+#endif
+ }
+ return _PyUnicode_EncodeUTF16(text,
+ PyUnicode_AsUTF8(self->errors), 0);
+}
+
+static PyObject *
+utf32be_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_EncodeUTF32(text,
+ PyUnicode_AsUTF8(self->errors), 1);
+}
+
+static PyObject *
+utf32le_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_EncodeUTF32(text,
+ PyUnicode_AsUTF8(self->errors), -1);
+}
+
+static PyObject *
+utf32_encode(textio *self, PyObject *text)
+{
+ if (!self->encoding_start_of_stream) {
+ /* Skip the BOM and use native byte ordering */
+#if PY_BIG_ENDIAN
+ return utf32be_encode(self, text);
+#else
+ return utf32le_encode(self, text);
+#endif
+ }
+ return _PyUnicode_EncodeUTF32(text,
+ PyUnicode_AsUTF8(self->errors), 0);
+}
+
+static PyObject *
+utf8_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_AsUTF8String(text, PyUnicode_AsUTF8(self->errors));
+}
+
+static PyObject *
+latin1_encode(textio *self, PyObject *text)
+{
+ return _PyUnicode_AsLatin1String(text, PyUnicode_AsUTF8(self->errors));
+}
+
+// Return true when encoding can be skipped when text is ascii.
+static inline int
+is_asciicompat_encoding(encodefunc_t f)
+{
+ return f == (encodefunc_t) ascii_encode
+ || f == (encodefunc_t) latin1_encode
+ || f == (encodefunc_t) utf8_encode;
+}
+
+/* Map normalized encoding names onto the specialized encoding funcs */
+
+typedef struct {
+ const char *name;
+ encodefunc_t encodefunc;
+} encodefuncentry;
+
+static const encodefuncentry encodefuncs[] = {
+ {"ascii", (encodefunc_t) ascii_encode},
+ {"iso8859-1", (encodefunc_t) latin1_encode},
+ {"utf-8", (encodefunc_t) utf8_encode},
+ {"utf-16-be", (encodefunc_t) utf16be_encode},
+ {"utf-16-le", (encodefunc_t) utf16le_encode},
+ {"utf-16", (encodefunc_t) utf16_encode},
+ {"utf-32-be", (encodefunc_t) utf32be_encode},
+ {"utf-32-le", (encodefunc_t) utf32le_encode},
+ {"utf-32", (encodefunc_t) utf32_encode},
+ {NULL, NULL}
+};
+
+static int
+validate_newline(const char *newline)
+{
+ if (newline && newline[0] != '\0'
+ && !(newline[0] == '\n' && newline[1] == '\0')
+ && !(newline[0] == '\r' && newline[1] == '\0')
+ && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
+ PyErr_Format(PyExc_ValueError,
+ "illegal newline value: %s", newline);
+ return -1;
+ }
+ return 0;
+}
+
+static int
+set_newline(textio *self, const char *newline)
+{
+ PyObject *old = self->readnl;
+ if (newline == NULL) {
+ self->readnl = NULL;
+ }
+ else {
+ self->readnl = PyUnicode_FromString(newline);
+ if (self->readnl == NULL) {
+ self->readnl = old;
+ return -1;
+ }
+ }
+ self->readuniversal = (newline == NULL || newline[0] == '\0');
+ self->readtranslate = (newline == NULL);
+ self->writetranslate = (newline == NULL || newline[0] != '\0');
+ if (!self->readuniversal && self->readnl != NULL) {
+ // validate_newline() accepts only ASCII newlines.
+ assert(PyUnicode_KIND(self->readnl) == PyUnicode_1BYTE_KIND);
+ self->writenl = (const char *)PyUnicode_1BYTE_DATA(self->readnl);
+ if (strcmp(self->writenl, "\n") == 0) {
+ self->writenl = NULL;
+ }
+ }
+ else {
+#ifdef MS_WINDOWS
+ self->writenl = "\r\n";
+#else
+ self->writenl = NULL;
+#endif
+ }
+ Py_XDECREF(old);
+ return 0;
+}
+
+static int
+_textiowrapper_set_decoder(textio *self, PyObject *codec_info,
+ const char *errors)
+{
+ PyObject *res;
+ int r;
+
+ res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable));
+ if (res == NULL)
+ return -1;
+
+ r = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (r == -1)
+ return -1;
+
+ if (r != 1)
+ return 0;
+
+ Py_CLEAR(self->decoder);
+ self->decoder = _PyCodecInfo_GetIncrementalDecoder(codec_info, errors);
+ if (self->decoder == NULL)
+ return -1;
+
+ if (self->readuniversal) {
+ _PyIO_State *state = self->state;
+ PyObject *incrementalDecoder = PyObject_CallFunctionObjArgs(
+ (PyObject *)state->PyIncrementalNewlineDecoder_Type,
+ self->decoder, self->readtranslate ? Py_True : Py_False, NULL);
+ if (incrementalDecoder == NULL)
+ return -1;
+ Py_XSETREF(self->decoder, incrementalDecoder);
+ }
+
+ return 0;
+}
+
+static PyObject*
+_textiowrapper_decode(_PyIO_State *state, PyObject *decoder, PyObject *bytes,
+ int eof)
+{
+ PyObject *chars;
+
+ if (Py_IS_TYPE(decoder, state->PyIncrementalNewlineDecoder_Type))
+ chars = _PyIncrementalNewlineDecoder_decode(decoder, bytes, eof);
+ else
+ chars = PyObject_CallMethodObjArgs(decoder, &_Py_ID(decode), bytes,
+ eof ? Py_True : Py_False, NULL);
+
+ if (check_decoded(chars) < 0)
+ // check_decoded already decreases refcount
+ return NULL;
+
+ return chars;
+}
+
+static int
+_textiowrapper_set_encoder(textio *self, PyObject *codec_info,
+ const char *errors)
+{
+ PyObject *res;
+ int r;
+
+ res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable));
+ if (res == NULL)
+ return -1;
+
+ r = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (r == -1)
+ return -1;
+
+ if (r != 1)
+ return 0;
+
+ Py_CLEAR(self->encoder);
+ self->encodefunc = NULL;
+ self->encoder = _PyCodecInfo_GetIncrementalEncoder(codec_info, errors);
+ if (self->encoder == NULL)
+ return -1;
+
+ /* Get the normalized named of the codec */
+ if (_PyObject_LookupAttr(codec_info, &_Py_ID(name), &res) < 0) {
+ return -1;
+ }
+ if (res != NULL && PyUnicode_Check(res)) {
+ const encodefuncentry *e = encodefuncs;
+ while (e->name != NULL) {
+ if (_PyUnicode_EqualToASCIIString(res, e->name)) {
+ self->encodefunc = e->encodefunc;
+ break;
+ }
+ e++;
+ }
+ }
+ Py_XDECREF(res);
+
+ return 0;
+}
+
+static int
+_textiowrapper_fix_encoder_state(textio *self)
+{
+ if (!self->seekable || !self->encoder) {
+ return 0;
+ }
+
+ self->encoding_start_of_stream = 1;
+
+ PyObject *cookieObj = PyObject_CallMethodNoArgs(
+ self->buffer, &_Py_ID(tell));
+ if (cookieObj == NULL) {
+ return -1;
+ }
+
+ int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_GetZero(), Py_EQ);
+ Py_DECREF(cookieObj);
+ if (cmp < 0) {
+ return -1;
+ }
+
+ if (cmp == 0) {
+ self->encoding_start_of_stream = 0;
+ PyObject *res = PyObject_CallMethodOneArg(
+ self->encoder, &_Py_ID(setstate), _PyLong_GetZero());
+ if (res == NULL) {
+ return -1;
+ }
+ Py_DECREF(res);
+ }
+
+ return 0;
+}
+
+static int
+io_check_errors(PyObject *errors)
+{
+ assert(errors != NULL && errors != Py_None);
+
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+#ifndef Py_DEBUG
+ /* In release mode, only check in development mode (-X dev) */
+ if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
+ return 0;
+ }
+#else
+ /* Always check in debug mode */
+#endif
+
+ /* Avoid calling PyCodec_LookupError() before the codec registry is ready:
+ before_PyUnicode_InitEncodings() is called. */
+ if (!interp->unicode.fs_codec.encoding) {
+ return 0;
+ }
+
+ Py_ssize_t name_length;
+ const char *name = PyUnicode_AsUTF8AndSize(errors, &name_length);
+ if (name == NULL) {
+ return -1;
+ }
+ if (strlen(name) != (size_t)name_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character in errors");
+ return -1;
+ }
+ PyObject *handler = PyCodec_LookupError(name);
+ if (handler != NULL) {
+ Py_DECREF(handler);
+ return 0;
+ }
+ return -1;
+}
+
+
+
+/*[clinic input]
+_io.TextIOWrapper.__init__
+ buffer: object
+ encoding: str(accept={str, NoneType}) = None
+ errors: object = None
+ newline: str(accept={str, NoneType}) = None
+ line_buffering: bool = False
+ write_through: bool = False
+
+Character and line based layer over a BufferedIOBase object, buffer.
+
+encoding gives the name of the encoding that the stream will be
+decoded or encoded with. It defaults to locale.getencoding().
+
+errors determines the strictness of encoding and decoding (see
+help(codecs.Codec) or the documentation for codecs.register) and
+defaults to "strict".
+
+newline controls how line endings are handled. It can be None, '',
+'\n', '\r', and '\r\n'. It works as follows:
+
+* On input, if newline is None, universal newlines mode is
+ enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
+ these are translated into '\n' before being returned to the
+ caller. If it is '', universal newline mode is enabled, but line
+ endings are returned to the caller untranslated. If it has any of
+ the other legal values, input lines are only terminated by the given
+ string, and the line ending is returned to the caller untranslated.
+
+* On output, if newline is None, any '\n' characters written are
+ translated to the system default line separator, os.linesep. If
+ newline is '' or '\n', no translation takes place. If newline is any
+ of the other legal values, any '\n' characters written are translated
+ to the given string.
+
+If line_buffering is True, a call to flush is implied when a call to
+write contains a newline character.
+[clinic start generated code]*/
+
+static int
+_io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
+ const char *encoding, PyObject *errors,
+ const char *newline, int line_buffering,
+ int write_through)
+/*[clinic end generated code: output=72267c0c01032ed2 input=e6cfaaaf6059d4f5]*/
+{
+ PyObject *raw, *codec_info = NULL;
+ PyObject *res;
+ int r;
+
+ self->ok = 0;
+ self->detached = 0;
+
+ if (encoding == NULL) {
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
+ if (PyErr_WarnEx(PyExc_EncodingWarning,
+ "'encoding' argument not specified", 1)) {
+ return -1;
+ }
+ }
+ }
+
+ if (errors == Py_None) {
+ errors = &_Py_ID(strict);
+ }
+ else if (!PyUnicode_Check(errors)) {
+ // Check 'errors' argument here because Argument Clinic doesn't support
+ // 'str(accept={str, NoneType})' converter.
+ PyErr_Format(
+ PyExc_TypeError,
+ "TextIOWrapper() argument 'errors' must be str or None, not %.50s",
+ Py_TYPE(errors)->tp_name);
+ return -1;
+ }
+ else if (io_check_errors(errors)) {
+ return -1;
+ }
+ Py_ssize_t errors_len;
+ const char *errors_str = PyUnicode_AsUTF8AndSize(errors, &errors_len);
+ if (errors_str == NULL) {
+ return -1;
+ }
+ if (strlen(errors_str) != (size_t)errors_len) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ return -1;
+ }
+
+ if (validate_newline(newline) < 0) {
+ return -1;
+ }
+
+ Py_CLEAR(self->buffer);
+ Py_CLEAR(self->encoding);
+ Py_CLEAR(self->encoder);
+ Py_CLEAR(self->decoder);
+ Py_CLEAR(self->readnl);
+ Py_CLEAR(self->decoded_chars);
+ Py_CLEAR(self->pending_bytes);
+ Py_CLEAR(self->snapshot);
+ Py_CLEAR(self->errors);
+ Py_CLEAR(self->raw);
+ self->decoded_chars_used = 0;
+ self->pending_bytes_count = 0;
+ self->encodefunc = NULL;
+ self->b2cratio = 0.0;
+
+ if (encoding == NULL && _PyRuntime.preconfig.utf8_mode) {
+ _Py_DECLARE_STR(utf_8, "utf-8");
+ self->encoding = Py_NewRef(&_Py_STR(utf_8));
+ }
+ else if (encoding == NULL || (strcmp(encoding, "locale") == 0)) {
+ self->encoding = _Py_GetLocaleEncodingObject();
+ if (self->encoding == NULL) {
+ goto error;
+ }
+ assert(PyUnicode_Check(self->encoding));
+ }
+
+ if (self->encoding != NULL) {
+ encoding = PyUnicode_AsUTF8(self->encoding);
+ if (encoding == NULL)
+ goto error;
+ }
+ else if (encoding != NULL) {
+ self->encoding = PyUnicode_FromString(encoding);
+ if (self->encoding == NULL)
+ goto error;
+ }
+ else {
+ PyErr_SetString(PyExc_OSError,
+ "could not determine default encoding");
+ goto error;
+ }
+
+ /* Check we have been asked for a real text encoding */
+ codec_info = _PyCodec_LookupTextEncoding(encoding, "codecs.open()");
+ if (codec_info == NULL) {
+ Py_CLEAR(self->encoding);
+ goto error;
+ }
+
+ /* XXX: Failures beyond this point have the potential to leak elements
+ * of the partially constructed object (like self->encoding)
+ */
+
+ self->errors = Py_NewRef(errors);
+ self->chunk_size = 8192;
+ self->line_buffering = line_buffering;
+ self->write_through = write_through;
+ if (set_newline(self, newline) < 0) {
+ goto error;
+ }
+
+ self->buffer = Py_NewRef(buffer);
+
+ /* Build the decoder object */
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ self->state = state;
+ if (_textiowrapper_set_decoder(self, codec_info, errors_str) != 0)
+ goto error;
+
+ /* Build the encoder object */
+ if (_textiowrapper_set_encoder(self, codec_info, errors_str) != 0)
+ goto error;
+
+ /* Finished sorting out the codec details */
+ Py_CLEAR(codec_info);
+
+ if (Py_IS_TYPE(buffer, state->PyBufferedReader_Type) ||
+ Py_IS_TYPE(buffer, state->PyBufferedWriter_Type) ||
+ Py_IS_TYPE(buffer, state->PyBufferedRandom_Type))
+ {
+ if (_PyObject_LookupAttr(buffer, &_Py_ID(raw), &raw) < 0)
+ goto error;
+ /* Cache the raw FileIO object to speed up 'closed' checks */
+ if (raw != NULL) {
+ if (Py_IS_TYPE(raw, state->PyFileIO_Type))
+ self->raw = raw;
+ else
+ Py_DECREF(raw);
+ }
+ }
+
+ res = PyObject_CallMethodNoArgs(buffer, &_Py_ID(seekable));
+ if (res == NULL)
+ goto error;
+ r = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (r < 0)
+ goto error;
+ self->seekable = self->telling = r;
+
+ r = _PyObject_LookupAttr(buffer, &_Py_ID(read1), &res);
+ if (r < 0) {
+ goto error;
+ }
+ Py_XDECREF(res);
+ self->has_read1 = r;
+
+ self->encoding_start_of_stream = 0;
+ if (_textiowrapper_fix_encoder_state(self) < 0) {
+ goto error;
+ }
+
+ self->ok = 1;
+ return 0;
+
+ error:
+ Py_XDECREF(codec_info);
+ return -1;
+}
+
+/* Return *default_value* if ob is None, 0 if ob is false, 1 if ob is true,
+ * -1 on error.
+ */
+static int
+convert_optional_bool(PyObject *obj, int default_value)
+{
+ long v;
+ if (obj == Py_None) {
+ v = default_value;
+ }
+ else {
+ v = PyLong_AsLong(obj);
+ if (v == -1 && PyErr_Occurred())
+ return -1;
+ }
+ return v != 0;
+}
+
+static int
+textiowrapper_change_encoding(textio *self, PyObject *encoding,
+ PyObject *errors, int newline_changed)
+{
+ /* Use existing settings where new settings are not specified */
+ if (encoding == Py_None && errors == Py_None && !newline_changed) {
+ return 0; // no change
+ }
+
+ if (encoding == Py_None) {
+ encoding = self->encoding;
+ if (errors == Py_None) {
+ errors = self->errors;
+ }
+ Py_INCREF(encoding);
+ }
+ else {
+ if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
+ encoding = _Py_GetLocaleEncodingObject();
+ if (encoding == NULL) {
+ return -1;
+ }
+ } else {
+ Py_INCREF(encoding);
+ }
+ if (errors == Py_None) {
+ errors = &_Py_ID(strict);
+ }
+ }
+ Py_INCREF(errors);
+
+ const char *c_encoding = PyUnicode_AsUTF8(encoding);
+ if (c_encoding == NULL) {
+ Py_DECREF(encoding);
+ Py_DECREF(errors);
+ return -1;
+ }
+ const char *c_errors = PyUnicode_AsUTF8(errors);
+ if (c_errors == NULL) {
+ Py_DECREF(encoding);
+ Py_DECREF(errors);
+ return -1;
+ }
+
+ // Create new encoder & decoder
+ PyObject *codec_info = _PyCodec_LookupTextEncoding(
+ c_encoding, "codecs.open()");
+ if (codec_info == NULL) {
+ Py_DECREF(encoding);
+ Py_DECREF(errors);
+ return -1;
+ }
+ if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 ||
+ _textiowrapper_set_encoder(self, codec_info, c_errors) != 0) {
+ Py_DECREF(codec_info);
+ Py_DECREF(encoding);
+ Py_DECREF(errors);
+ return -1;
+ }
+ Py_DECREF(codec_info);
+
+ Py_SETREF(self->encoding, encoding);
+ Py_SETREF(self->errors, errors);
+
+ return _textiowrapper_fix_encoder_state(self);
+}
+
+/*[clinic input]
+_io.TextIOWrapper.reconfigure
+ *
+ encoding: object = None
+ errors: object = None
+ newline as newline_obj: object(c_default="NULL") = None
+ line_buffering as line_buffering_obj: object = None
+ write_through as write_through_obj: object = None
+
+Reconfigure the text stream with new parameters.
+
+This also does an implicit stream flush.
+
+[clinic start generated code]*/
+
+static PyObject *
+_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]*/
+{
+ int line_buffering;
+ int write_through;
+ const char *newline = NULL;
+
+ if (encoding != Py_None && !PyUnicode_Check(encoding)) {
+ PyErr_Format(PyExc_TypeError,
+ "reconfigure() argument 'encoding' must be str or None, not %s",
+ Py_TYPE(encoding)->tp_name);
+ return NULL;
+ }
+ if (errors != Py_None && !PyUnicode_Check(errors)) {
+ PyErr_Format(PyExc_TypeError,
+ "reconfigure() argument 'errors' must be str or None, not %s",
+ Py_TYPE(errors)->tp_name);
+ return NULL;
+ }
+ if (newline_obj != NULL && newline_obj != Py_None &&
+ !PyUnicode_Check(newline_obj))
+ {
+ PyErr_Format(PyExc_TypeError,
+ "reconfigure() argument 'newline' must be str or None, not %s",
+ Py_TYPE(newline_obj)->tp_name);
+ return NULL;
+ }
+ /* Check if something is in the read buffer */
+ if (self->decoded_chars != NULL) {
+ if (encoding != Py_None || errors != Py_None || newline_obj != NULL) {
+ _unsupported(self->state,
+ "It is not possible to set the encoding or newline "
+ "of stream after the first read");
+ return NULL;
+ }
+ }
+
+ if (newline_obj != NULL && newline_obj != Py_None) {
+ newline = PyUnicode_AsUTF8(newline_obj);
+ if (newline == NULL || validate_newline(newline) < 0) {
+ return NULL;
+ }
+ }
+
+ line_buffering = convert_optional_bool(line_buffering_obj,
+ self->line_buffering);
+ if (line_buffering < 0) {
+ return NULL;
+ }
+ write_through = convert_optional_bool(write_through_obj,
+ self->write_through);
+ if (write_through < 0) {
+ return NULL;
+ }
+
+ PyObject *res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL) {
+ return NULL;
+ }
+ Py_DECREF(res);
+ self->b2cratio = 0;
+
+ if (newline_obj != NULL && set_newline(self, newline) < 0) {
+ return NULL;
+ }
+
+ if (textiowrapper_change_encoding(
+ self, encoding, errors, newline_obj != NULL) < 0) {
+ return NULL;
+ }
+
+ self->line_buffering = line_buffering;
+ self->write_through = write_through;
+ Py_RETURN_NONE;
+}
+
+static int
+textiowrapper_clear(textio *self)
+{
+ self->ok = 0;
+ Py_CLEAR(self->buffer);
+ Py_CLEAR(self->encoding);
+ Py_CLEAR(self->encoder);
+ Py_CLEAR(self->decoder);
+ Py_CLEAR(self->readnl);
+ Py_CLEAR(self->decoded_chars);
+ Py_CLEAR(self->pending_bytes);
+ Py_CLEAR(self->snapshot);
+ Py_CLEAR(self->errors);
+ Py_CLEAR(self->raw);
+
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+textiowrapper_dealloc(textio *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ self->finalizing = 1;
+ if (_PyIOBase_finalize((PyObject *) self) < 0)
+ return;
+ self->ok = 0;
+ _PyObject_GC_UNTRACK(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)self);
+ (void)textiowrapper_clear(self);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+static int
+textiowrapper_traverse(textio *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->buffer);
+ Py_VISIT(self->encoding);
+ Py_VISIT(self->encoder);
+ Py_VISIT(self->decoder);
+ Py_VISIT(self->readnl);
+ Py_VISIT(self->decoded_chars);
+ Py_VISIT(self->pending_bytes);
+ Py_VISIT(self->snapshot);
+ Py_VISIT(self->errors);
+ Py_VISIT(self->raw);
+
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static PyObject *
+textiowrapper_closed_get(textio *self, void *context);
+
+/* This macro takes some shortcuts to make the common case faster. */
+#define CHECK_CLOSED(self) \
+ do { \
+ int r; \
+ PyObject *_res; \
+ if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) { \
+ if (self->raw != NULL) \
+ r = _PyFileIO_closed(self->raw); \
+ else { \
+ _res = textiowrapper_closed_get(self, NULL); \
+ if (_res == NULL) \
+ return NULL; \
+ r = PyObject_IsTrue(_res); \
+ Py_DECREF(_res); \
+ if (r < 0) \
+ return NULL; \
+ } \
+ if (r > 0) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on closed file."); \
+ return NULL; \
+ } \
+ } \
+ else if (_PyIOBase_check_closed((PyObject *)self, Py_True) == NULL) \
+ return NULL; \
+ } while (0)
+
+#define CHECK_INITIALIZED(self) \
+ if (self->ok <= 0) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on uninitialized object"); \
+ return NULL; \
+ }
+
+#define CHECK_ATTACHED(self) \
+ CHECK_INITIALIZED(self); \
+ if (self->detached) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "underlying buffer has been detached"); \
+ return NULL; \
+ }
+
+#define CHECK_ATTACHED_INT(self) \
+ if (self->ok <= 0) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "I/O operation on uninitialized object"); \
+ return -1; \
+ } else if (self->detached) { \
+ PyErr_SetString(PyExc_ValueError, \
+ "underlying buffer has been detached"); \
+ return -1; \
+ }
+
+
+/*[clinic input]
+_io.TextIOWrapper.detach
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_detach_impl(textio *self)
+/*[clinic end generated code: output=7ba3715cd032d5f2 input=e5a71fbda9e1d9f9]*/
+{
+ PyObject *buffer, *res;
+ CHECK_ATTACHED(self);
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res);
+ buffer = self->buffer;
+ self->buffer = NULL;
+ self->detached = 1;
+ return buffer;
+}
+
+/* Flush the internal write buffer. This doesn't explicitly flush the
+ underlying buffered object, though. */
+static int
+_textiowrapper_writeflush(textio *self)
+{
+ if (self->pending_bytes == NULL)
+ return 0;
+
+ PyObject *pending = self->pending_bytes;
+ PyObject *b;
+
+ if (PyBytes_Check(pending)) {
+ b = Py_NewRef(pending);
+ }
+ else if (PyUnicode_Check(pending)) {
+ assert(PyUnicode_IS_ASCII(pending));
+ assert(PyUnicode_GET_LENGTH(pending) == self->pending_bytes_count);
+ b = PyBytes_FromStringAndSize(
+ PyUnicode_DATA(pending), PyUnicode_GET_LENGTH(pending));
+ if (b == NULL) {
+ return -1;
+ }
+ }
+ else {
+ assert(PyList_Check(pending));
+ b = PyBytes_FromStringAndSize(NULL, self->pending_bytes_count);
+ if (b == NULL) {
+ return -1;
+ }
+
+ char *buf = PyBytes_AsString(b);
+ Py_ssize_t pos = 0;
+
+ for (Py_ssize_t i = 0; i < PyList_GET_SIZE(pending); i++) {
+ PyObject *obj = PyList_GET_ITEM(pending, i);
+ char *src;
+ Py_ssize_t len;
+ if (PyUnicode_Check(obj)) {
+ assert(PyUnicode_IS_ASCII(obj));
+ src = PyUnicode_DATA(obj);
+ len = PyUnicode_GET_LENGTH(obj);
+ }
+ else {
+ assert(PyBytes_Check(obj));
+ if (PyBytes_AsStringAndSize(obj, &src, &len) < 0) {
+ Py_DECREF(b);
+ return -1;
+ }
+ }
+ memcpy(buf + pos, src, len);
+ pos += len;
+ }
+ assert(pos == self->pending_bytes_count);
+ }
+
+ self->pending_bytes_count = 0;
+ self->pending_bytes = NULL;
+ Py_DECREF(pending);
+
+ PyObject *ret;
+ do {
+ ret = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(write), b);
+ } while (ret == NULL && _PyIO_trap_eintr());
+ Py_DECREF(b);
+ // NOTE: We cleared buffer but we don't know how many bytes are actually written
+ // when an error occurred.
+ if (ret == NULL)
+ return -1;
+ Py_DECREF(ret);
+ return 0;
+}
+
+/*[clinic input]
+_io.TextIOWrapper.write
+ text: unicode
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_write_impl(textio *self, PyObject *text)
+/*[clinic end generated code: output=d2deb0d50771fcec input=fdf19153584a0e44]*/
+{
+ PyObject *ret;
+ PyObject *b;
+ Py_ssize_t textlen;
+ int haslf = 0;
+ int needflush = 0, text_needflush = 0;
+
+ if (PyUnicode_READY(text) == -1)
+ return NULL;
+
+ CHECK_ATTACHED(self);
+ CHECK_CLOSED(self);
+
+ if (self->encoder == NULL) {
+ return _unsupported(self->state, "not writable");
+ }
+
+ Py_INCREF(text);
+
+ textlen = PyUnicode_GET_LENGTH(text);
+
+ if ((self->writetranslate && self->writenl != NULL) || self->line_buffering)
+ if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1)
+ haslf = 1;
+
+ if (haslf && self->writetranslate && self->writenl != NULL) {
+ PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace),
+ "ss", "\n", self->writenl);
+ Py_DECREF(text);
+ if (newtext == NULL)
+ return NULL;
+ text = newtext;
+ }
+
+ if (self->write_through)
+ text_needflush = 1;
+ if (self->line_buffering &&
+ (haslf ||
+ PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1))
+ needflush = 1;
+
+ /* XXX What if we were just reading? */
+ if (self->encodefunc != NULL) {
+ if (PyUnicode_IS_ASCII(text) &&
+ // See bpo-43260
+ PyUnicode_GET_LENGTH(text) <= self->chunk_size &&
+ is_asciicompat_encoding(self->encodefunc)) {
+ b = Py_NewRef(text);
+ }
+ else {
+ b = (*self->encodefunc)((PyObject *) self, text);
+ }
+ self->encoding_start_of_stream = 0;
+ }
+ else {
+ b = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(encode), text);
+ }
+
+ Py_DECREF(text);
+ if (b == NULL)
+ return NULL;
+ if (b != text && !PyBytes_Check(b)) {
+ PyErr_Format(PyExc_TypeError,
+ "encoder should return a bytes object, not '%.200s'",
+ Py_TYPE(b)->tp_name);
+ Py_DECREF(b);
+ return NULL;
+ }
+
+ Py_ssize_t bytes_len;
+ if (b == text) {
+ bytes_len = PyUnicode_GET_LENGTH(b);
+ }
+ else {
+ bytes_len = PyBytes_GET_SIZE(b);
+ }
+
+ if (self->pending_bytes == NULL) {
+ self->pending_bytes_count = 0;
+ self->pending_bytes = b;
+ }
+ else if (self->pending_bytes_count + bytes_len > self->chunk_size) {
+ // Prevent to concatenate more than chunk_size data.
+ if (_textiowrapper_writeflush(self) < 0) {
+ Py_DECREF(b);
+ return NULL;
+ }
+ self->pending_bytes = b;
+ }
+ else if (!PyList_CheckExact(self->pending_bytes)) {
+ PyObject *list = PyList_New(2);
+ if (list == NULL) {
+ Py_DECREF(b);
+ return NULL;
+ }
+ PyList_SET_ITEM(list, 0, self->pending_bytes);
+ PyList_SET_ITEM(list, 1, b);
+ self->pending_bytes = list;
+ }
+ else {
+ if (PyList_Append(self->pending_bytes, b) < 0) {
+ Py_DECREF(b);
+ return NULL;
+ }
+ Py_DECREF(b);
+ }
+
+ self->pending_bytes_count += bytes_len;
+ if (self->pending_bytes_count >= self->chunk_size || needflush ||
+ text_needflush) {
+ if (_textiowrapper_writeflush(self) < 0)
+ return NULL;
+ }
+
+ if (needflush) {
+ ret = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush));
+ if (ret == NULL)
+ return NULL;
+ Py_DECREF(ret);
+ }
+
+ if (self->snapshot != NULL) {
+ textiowrapper_set_decoded_chars(self, NULL);
+ Py_CLEAR(self->snapshot);
+ }
+
+ if (self->decoder) {
+ ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
+ if (ret == NULL)
+ return NULL;
+ Py_DECREF(ret);
+ }
+
+ return PyLong_FromSsize_t(textlen);
+}
+
+/* Steal a reference to chars and store it in the decoded_char buffer;
+ */
+static void
+textiowrapper_set_decoded_chars(textio *self, PyObject *chars)
+{
+ Py_XSETREF(self->decoded_chars, chars);
+ self->decoded_chars_used = 0;
+}
+
+static PyObject *
+textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
+{
+ PyObject *chars;
+ Py_ssize_t avail;
+
+ if (self->decoded_chars == NULL)
+ return PyUnicode_FromStringAndSize(NULL, 0);
+
+ /* decoded_chars is guaranteed to be "ready". */
+ avail = (PyUnicode_GET_LENGTH(self->decoded_chars)
+ - self->decoded_chars_used);
+
+ assert(avail >= 0);
+
+ if (n < 0 || n > avail)
+ n = avail;
+
+ if (self->decoded_chars_used > 0 || n < avail) {
+ chars = PyUnicode_Substring(self->decoded_chars,
+ self->decoded_chars_used,
+ self->decoded_chars_used + n);
+ if (chars == NULL)
+ return NULL;
+ }
+ else {
+ chars = Py_NewRef(self->decoded_chars);
+ }
+
+ self->decoded_chars_used += n;
+ return chars;
+}
+
+/* Read and decode the next chunk of data from the BufferedReader.
+ */
+static int
+textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
+{
+ PyObject *dec_buffer = NULL;
+ PyObject *dec_flags = NULL;
+ PyObject *input_chunk = NULL;
+ Py_buffer input_chunk_buf;
+ PyObject *decoded_chars, *chunk_size;
+ Py_ssize_t nbytes, nchars;
+ int eof;
+
+ /* The return value is True unless EOF was reached. The decoded string is
+ * placed in self._decoded_chars (replacing its previous value). The
+ * entire input chunk is sent to the decoder, though some of it may remain
+ * buffered in the decoder, yet to be converted.
+ */
+
+ if (self->decoder == NULL) {
+ _unsupported(self->state, "not readable");
+ return -1;
+ }
+
+ if (self->telling) {
+ /* To prepare for tell(), we need to snapshot a point in the file
+ * where the decoder's input buffer is empty.
+ */
+ PyObject *state = PyObject_CallMethodNoArgs(self->decoder,
+ &_Py_ID(getstate));
+ if (state == NULL)
+ return -1;
+ /* Given this, we know there was a valid snapshot point
+ * len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
+ */
+ if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError,
+ "illegal decoder state");
+ Py_DECREF(state);
+ return -1;
+ }
+ if (!PyArg_ParseTuple(state,
+ "OO;illegal decoder state", &dec_buffer, &dec_flags))
+ {
+ Py_DECREF(state);
+ return -1;
+ }
+
+ if (!PyBytes_Check(dec_buffer)) {
+ PyErr_Format(PyExc_TypeError,
+ "illegal decoder state: the first item should be a "
+ "bytes object, not '%.200s'",
+ Py_TYPE(dec_buffer)->tp_name);
+ Py_DECREF(state);
+ return -1;
+ }
+ Py_INCREF(dec_buffer);
+ Py_INCREF(dec_flags);
+ Py_DECREF(state);
+ }
+
+ /* Read a chunk, decode it, and put the result in self._decoded_chars. */
+ if (size_hint > 0) {
+ size_hint = (Py_ssize_t)(Py_MAX(self->b2cratio, 1.0) * size_hint);
+ }
+ chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint));
+ if (chunk_size == NULL)
+ goto fail;
+
+ input_chunk = PyObject_CallMethodOneArg(self->buffer,
+ (self->has_read1 ? &_Py_ID(read1): &_Py_ID(read)),
+ chunk_size);
+ Py_DECREF(chunk_size);
+ if (input_chunk == NULL)
+ goto fail;
+
+ if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) {
+ PyErr_Format(PyExc_TypeError,
+ "underlying %s() should have returned a bytes-like object, "
+ "not '%.200s'", (self->has_read1 ? "read1": "read"),
+ Py_TYPE(input_chunk)->tp_name);
+ goto fail;
+ }
+
+ nbytes = input_chunk_buf.len;
+ eof = (nbytes == 0);
+
+ decoded_chars = _textiowrapper_decode(self->state, self->decoder,
+ input_chunk, eof);
+ PyBuffer_Release(&input_chunk_buf);
+ if (decoded_chars == NULL)
+ goto fail;
+
+ textiowrapper_set_decoded_chars(self, decoded_chars);
+ nchars = PyUnicode_GET_LENGTH(decoded_chars);
+ if (nchars > 0)
+ self->b2cratio = (double) nbytes / nchars;
+ else
+ self->b2cratio = 0.0;
+ if (nchars > 0)
+ eof = 0;
+
+ if (self->telling) {
+ /* At the snapshot point, len(dec_buffer) bytes before the read, the
+ * next input to be decoded is dec_buffer + input_chunk.
+ */
+ PyObject *next_input = dec_buffer;
+ PyBytes_Concat(&next_input, input_chunk);
+ dec_buffer = NULL; /* Reference lost to PyBytes_Concat */
+ if (next_input == NULL) {
+ goto fail;
+ }
+ PyObject *snapshot = Py_BuildValue("NN", dec_flags, next_input);
+ if (snapshot == NULL) {
+ dec_flags = NULL;
+ goto fail;
+ }
+ Py_XSETREF(self->snapshot, snapshot);
+ }
+ Py_DECREF(input_chunk);
+
+ return (eof == 0);
+
+ fail:
+ Py_XDECREF(dec_buffer);
+ Py_XDECREF(dec_flags);
+ Py_XDECREF(input_chunk);
+ return -1;
+}
+
+/*[clinic input]
+_io.TextIOWrapper.read
+ size as n: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
+/*[clinic end generated code: output=7e651ce6cc6a25a6 input=123eecbfe214aeb8]*/
+{
+ PyObject *result = NULL, *chunks = NULL;
+
+ CHECK_ATTACHED(self);
+ CHECK_CLOSED(self);
+
+ if (self->decoder == NULL) {
+ return _unsupported(self->state, "not readable");
+ }
+
+ if (_textiowrapper_writeflush(self) < 0)
+ return NULL;
+
+ if (n < 0) {
+ /* Read everything */
+ PyObject *bytes = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(read));
+ PyObject *decoded;
+ if (bytes == NULL)
+ goto fail;
+
+ _PyIO_State *state = self->state;
+ if (Py_IS_TYPE(self->decoder, state->PyIncrementalNewlineDecoder_Type))
+ decoded = _PyIncrementalNewlineDecoder_decode(self->decoder,
+ bytes, 1);
+ else
+ decoded = PyObject_CallMethodObjArgs(
+ self->decoder, &_Py_ID(decode), bytes, Py_True, NULL);
+ Py_DECREF(bytes);
+ if (check_decoded(decoded) < 0)
+ goto fail;
+
+ result = textiowrapper_get_decoded_chars(self, -1);
+
+ if (result == NULL) {
+ Py_DECREF(decoded);
+ return NULL;
+ }
+
+ PyUnicode_AppendAndDel(&result, decoded);
+ if (result == NULL)
+ goto fail;
+
+ if (self->snapshot != NULL) {
+ textiowrapper_set_decoded_chars(self, NULL);
+ Py_CLEAR(self->snapshot);
+ }
+ return result;
+ }
+ else {
+ int res = 1;
+ Py_ssize_t remaining = n;
+
+ result = textiowrapper_get_decoded_chars(self, n);
+ if (result == NULL)
+ goto fail;
+ if (PyUnicode_READY(result) == -1)
+ goto fail;
+ remaining -= PyUnicode_GET_LENGTH(result);
+
+ /* Keep reading chunks until we have n characters to return */
+ while (remaining > 0) {
+ res = textiowrapper_read_chunk(self, remaining);
+ if (res < 0) {
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
+ when EINTR occurs so we needn't do it ourselves. */
+ if (_PyIO_trap_eintr()) {
+ continue;
+ }
+ goto fail;
+ }
+ if (res == 0) /* EOF */
+ break;
+ if (chunks == NULL) {
+ chunks = PyList_New(0);
+ if (chunks == NULL)
+ goto fail;
+ }
+ if (PyUnicode_GET_LENGTH(result) > 0 &&
+ PyList_Append(chunks, result) < 0)
+ goto fail;
+ Py_DECREF(result);
+ result = textiowrapper_get_decoded_chars(self, remaining);
+ if (result == NULL)
+ goto fail;
+ remaining -= PyUnicode_GET_LENGTH(result);
+ }
+ if (chunks != NULL) {
+ if (result != NULL && PyList_Append(chunks, result) < 0)
+ goto fail;
+ _Py_DECLARE_STR(empty, "");
+ Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks));
+ if (result == NULL)
+ goto fail;
+ Py_CLEAR(chunks);
+ }
+ return result;
+ }
+ fail:
+ Py_XDECREF(result);
+ Py_XDECREF(chunks);
+ return NULL;
+}
+
+
+/* NOTE: `end` must point to the real end of the Py_UCS4 storage,
+ that is to the NUL character. Otherwise the function will produce
+ incorrect results. */
+static const char *
+find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
+{
+ if (kind == PyUnicode_1BYTE_KIND) {
+ assert(ch < 256);
+ return (char *) memchr((const void *) s, (char) ch, end - s);
+ }
+ for (;;) {
+ while (PyUnicode_READ(kind, s, 0) > ch)
+ s += kind;
+ if (PyUnicode_READ(kind, s, 0) == ch)
+ return s;
+ if (s == end)
+ return NULL;
+ s += kind;
+ }
+}
+
+Py_ssize_t
+_PyIO_find_line_ending(
+ int translated, int universal, PyObject *readnl,
+ int kind, const char *start, const char *end, Py_ssize_t *consumed)
+{
+ Py_ssize_t len = (end - start)/kind;
+
+ if (translated) {
+ /* Newlines are already translated, only search for \n */
+ const char *pos = find_control_char(kind, start, end, '\n');
+ if (pos != NULL)
+ return (pos - start)/kind + 1;
+ else {
+ *consumed = len;
+ return -1;
+ }
+ }
+ else if (universal) {
+ /* Universal newline search. Find any of \r, \r\n, \n
+ * The decoder ensures that \r\n are not split in two pieces
+ */
+ const char *s = start;
+ for (;;) {
+ Py_UCS4 ch;
+ /* Fast path for non-control chars. The loop always ends
+ since the Unicode string is NUL-terminated. */
+ while (PyUnicode_READ(kind, s, 0) > '\r')
+ s += kind;
+ if (s >= end) {
+ *consumed = len;
+ return -1;
+ }
+ ch = PyUnicode_READ(kind, s, 0);
+ s += kind;
+ if (ch == '\n')
+ return (s - start)/kind;
+ if (ch == '\r') {
+ if (PyUnicode_READ(kind, s, 0) == '\n')
+ return (s - start)/kind + 1;
+ else
+ return (s - start)/kind;
+ }
+ }
+ }
+ else {
+ /* Non-universal mode. */
+ Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl);
+ const Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl);
+ /* Assume that readnl is an ASCII character. */
+ assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
+ if (readnl_len == 1) {
+ const char *pos = find_control_char(kind, start, end, nl[0]);
+ if (pos != NULL)
+ return (pos - start)/kind + 1;
+ *consumed = len;
+ return -1;
+ }
+ else {
+ const char *s = start;
+ const char *e = end - (readnl_len - 1)*kind;
+ const char *pos;
+ if (e < s)
+ e = s;
+ while (s < e) {
+ Py_ssize_t i;
+ const char *pos = find_control_char(kind, s, end, nl[0]);
+ if (pos == NULL || pos >= e)
+ break;
+ for (i = 1; i < readnl_len; i++) {
+ if (PyUnicode_READ(kind, pos, i) != nl[i])
+ break;
+ }
+ if (i == readnl_len)
+ return (pos - start)/kind + readnl_len;
+ s = pos + kind;
+ }
+ pos = find_control_char(kind, e, end, nl[0]);
+ if (pos == NULL)
+ *consumed = len;
+ else
+ *consumed = (pos - start)/kind;
+ return -1;
+ }
+ }
+}
+
+static PyObject *
+_textiowrapper_readline(textio *self, Py_ssize_t limit)
+{
+ PyObject *line = NULL, *chunks = NULL, *remaining = NULL;
+ Py_ssize_t start, endpos, chunked, offset_to_buffer;
+ int res;
+
+ CHECK_CLOSED(self);
+
+ if (_textiowrapper_writeflush(self) < 0)
+ return NULL;
+
+ chunked = 0;
+
+ while (1) {
+ const char *ptr;
+ Py_ssize_t line_len;
+ int kind;
+ Py_ssize_t consumed = 0;
+
+ /* First, get some data if necessary */
+ res = 1;
+ while (!self->decoded_chars ||
+ !PyUnicode_GET_LENGTH(self->decoded_chars)) {
+ res = textiowrapper_read_chunk(self, 0);
+ if (res < 0) {
+ /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
+ when EINTR occurs so we needn't do it ourselves. */
+ if (_PyIO_trap_eintr()) {
+ continue;
+ }
+ goto error;
+ }
+ if (res == 0)
+ break;
+ }
+ if (res == 0) {
+ /* end of file */
+ textiowrapper_set_decoded_chars(self, NULL);
+ Py_CLEAR(self->snapshot);
+ start = endpos = offset_to_buffer = 0;
+ break;
+ }
+
+ if (remaining == NULL) {
+ line = Py_NewRef(self->decoded_chars);
+ start = self->decoded_chars_used;
+ offset_to_buffer = 0;
+ }
+ else {
+ assert(self->decoded_chars_used == 0);
+ line = PyUnicode_Concat(remaining, self->decoded_chars);
+ start = 0;
+ offset_to_buffer = PyUnicode_GET_LENGTH(remaining);
+ Py_CLEAR(remaining);
+ if (line == NULL)
+ goto error;
+ if (PyUnicode_READY(line) == -1)
+ goto error;
+ }
+
+ ptr = PyUnicode_DATA(line);
+ line_len = PyUnicode_GET_LENGTH(line);
+ kind = PyUnicode_KIND(line);
+
+ endpos = _PyIO_find_line_ending(
+ self->readtranslate, self->readuniversal, self->readnl,
+ kind,
+ ptr + kind * start,
+ ptr + kind * line_len,
+ &consumed);
+ if (endpos >= 0) {
+ endpos += start;
+ if (limit >= 0 && (endpos - start) + chunked >= limit)
+ endpos = start + limit - chunked;
+ break;
+ }
+
+ /* We can put aside up to `endpos` */
+ endpos = consumed + start;
+ if (limit >= 0 && (endpos - start) + chunked >= limit) {
+ /* Didn't find line ending, but reached length limit */
+ endpos = start + limit - chunked;
+ break;
+ }
+
+ if (endpos > start) {
+ /* No line ending seen yet - put aside current data */
+ PyObject *s;
+ if (chunks == NULL) {
+ chunks = PyList_New(0);
+ if (chunks == NULL)
+ goto error;
+ }
+ s = PyUnicode_Substring(line, start, endpos);
+ if (s == NULL)
+ goto error;
+ if (PyList_Append(chunks, s) < 0) {
+ Py_DECREF(s);
+ goto error;
+ }
+ chunked += PyUnicode_GET_LENGTH(s);
+ Py_DECREF(s);
+ }
+ /* There may be some remaining bytes we'll have to prepend to the
+ next chunk of data */
+ if (endpos < line_len) {
+ remaining = PyUnicode_Substring(line, endpos, line_len);
+ if (remaining == NULL)
+ goto error;
+ }
+ Py_CLEAR(line);
+ /* We have consumed the buffer */
+ textiowrapper_set_decoded_chars(self, NULL);
+ }
+
+ if (line != NULL) {
+ /* Our line ends in the current buffer */
+ self->decoded_chars_used = endpos - offset_to_buffer;
+ if (start > 0 || endpos < PyUnicode_GET_LENGTH(line)) {
+ PyObject *s = PyUnicode_Substring(line, start, endpos);
+ Py_CLEAR(line);
+ if (s == NULL)
+ goto error;
+ line = s;
+ }
+ }
+ if (remaining != NULL) {
+ if (chunks == NULL) {
+ chunks = PyList_New(0);
+ if (chunks == NULL)
+ goto error;
+ }
+ if (PyList_Append(chunks, remaining) < 0)
+ goto error;
+ Py_CLEAR(remaining);
+ }
+ if (chunks != NULL) {
+ if (line != NULL) {
+ if (PyList_Append(chunks, line) < 0)
+ goto error;
+ Py_DECREF(line);
+ }
+ line = PyUnicode_Join(&_Py_STR(empty), chunks);
+ if (line == NULL)
+ goto error;
+ Py_CLEAR(chunks);
+ }
+ if (line == NULL) {
+ line = Py_NewRef(&_Py_STR(empty));
+ }
+
+ return line;
+
+ error:
+ Py_XDECREF(chunks);
+ Py_XDECREF(remaining);
+ Py_XDECREF(line);
+ return NULL;
+}
+
+/*[clinic input]
+_io.TextIOWrapper.readline
+ size: Py_ssize_t = -1
+ /
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size)
+/*[clinic end generated code: output=344afa98804e8b25 input=56c7172483b36db6]*/
+{
+ CHECK_ATTACHED(self);
+ return _textiowrapper_readline(self, size);
+}
+
+/* Seek and Tell */
+
+typedef struct {
+ Py_off_t start_pos;
+ int dec_flags;
+ int bytes_to_feed;
+ int chars_to_skip;
+ char need_eof;
+} cookie_type;
+
+/*
+ To speed up cookie packing/unpacking, we store the fields in a temporary
+ string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.).
+ The following macros define at which offsets in the intermediary byte
+ string the various CookieStruct fields will be stored.
+ */
+
+#define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))
+
+#if PY_BIG_ENDIAN
+/* We want the least significant byte of start_pos to also be the least
+ significant byte of the cookie, which means that in big-endian mode we
+ must copy the fields in reverse order. */
+
+# define OFF_START_POS (sizeof(char) + 3 * sizeof(int))
+# define OFF_DEC_FLAGS (sizeof(char) + 2 * sizeof(int))
+# define OFF_BYTES_TO_FEED (sizeof(char) + sizeof(int))
+# define OFF_CHARS_TO_SKIP (sizeof(char))
+# define OFF_NEED_EOF 0
+
+#else
+/* Little-endian mode: the least significant byte of start_pos will
+ naturally end up the least significant byte of the cookie. */
+
+# define OFF_START_POS 0
+# define OFF_DEC_FLAGS (sizeof(Py_off_t))
+# define OFF_BYTES_TO_FEED (sizeof(Py_off_t) + sizeof(int))
+# define OFF_CHARS_TO_SKIP (sizeof(Py_off_t) + 2 * sizeof(int))
+# define OFF_NEED_EOF (sizeof(Py_off_t) + 3 * sizeof(int))
+
+#endif
+
+static int
+textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
+{
+ unsigned char buffer[COOKIE_BUF_LEN];
+ PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
+ if (cookieLong == NULL)
+ return -1;
+
+ if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
+ PY_LITTLE_ENDIAN, 0) < 0) {
+ Py_DECREF(cookieLong);
+ return -1;
+ }
+ Py_DECREF(cookieLong);
+
+ memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos));
+ memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags));
+ memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed));
+ memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip));
+ memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof));
+
+ return 0;
+}
+
+static PyObject *
+textiowrapper_build_cookie(cookie_type *cookie)
+{
+ unsigned char buffer[COOKIE_BUF_LEN];
+
+ memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos));
+ memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags));
+ memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed));
+ memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip));
+ memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof));
+
+ return _PyLong_FromByteArray(buffer, sizeof(buffer),
+ PY_LITTLE_ENDIAN, 0);
+}
+
+static int
+_textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
+{
+ PyObject *res;
+ /* When seeking to the start of the stream, we call decoder.reset()
+ rather than decoder.getstate().
+ This is for a few decoders such as utf-16 for which the state value
+ at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of
+ utf-16, that we are expecting a BOM).
+ */
+ if (cookie->start_pos == 0 && cookie->dec_flags == 0) {
+ res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
+ }
+ else {
+ res = _PyObject_CallMethod(self->decoder, &_Py_ID(setstate),
+ "((yi))", "", cookie->dec_flags);
+ }
+ if (res == NULL) {
+ return -1;
+ }
+ Py_DECREF(res);
+ return 0;
+}
+
+static int
+_textiowrapper_encoder_reset(textio *self, int start_of_stream)
+{
+ PyObject *res;
+ if (start_of_stream) {
+ res = PyObject_CallMethodNoArgs(self->encoder, &_Py_ID(reset));
+ self->encoding_start_of_stream = 1;
+ }
+ else {
+ res = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(setstate),
+ _PyLong_GetZero());
+ self->encoding_start_of_stream = 0;
+ }
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
+}
+
+static int
+_textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
+{
+ /* Same as _textiowrapper_decoder_setstate() above. */
+ return _textiowrapper_encoder_reset(
+ self, cookie->start_pos == 0 && cookie->dec_flags == 0);
+}
+
+/*[clinic input]
+_io.TextIOWrapper.seek
+ cookie as cookieObj: object
+ Zero or an opaque number returned by tell().
+ whence: int(c_default='0') = os.SEEK_SET
+ The relative position to seek from.
+ /
+
+Set the stream position, and return the new stream position.
+
+Four operations are supported, given by the following argument
+combinations:
+
+- seek(0, SEEK_SET): Rewind to the start of the stream.
+- seek(cookie, SEEK_SET): Restore a previous position;
+ 'cookie' must be a number returned by tell().
+- seek(0, SEEK_END): Fast-forward to the end of the stream.
+- seek(0, SEEK_CUR): Leave the current stream position unchanged.
+
+Any other argument combinations are invalid,
+and may raise exceptions.
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
+/*[clinic end generated code: output=0a15679764e2d04d input=0f68adcb02cf2823]*/
+{
+ PyObject *posobj;
+ cookie_type cookie;
+ PyObject *res;
+ int cmp;
+ PyObject *snapshot;
+
+ CHECK_ATTACHED(self);
+ CHECK_CLOSED(self);
+
+ Py_INCREF(cookieObj);
+
+ if (!self->seekable) {
+ _unsupported(self->state, "underlying stream is not seekable");
+ goto fail;
+ }
+
+ PyObject *zero = _PyLong_GetZero(); // borrowed reference
+
+ switch (whence) {
+ case SEEK_CUR:
+ /* seek relative to current position */
+ cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
+ if (cmp < 0)
+ goto fail;
+
+ if (cmp == 0) {
+ _unsupported(self->state, "can't do nonzero cur-relative seeks");
+ goto fail;
+ }
+
+ /* Seeking to the current position should attempt to
+ * sync the underlying buffer with the current position.
+ */
+ Py_DECREF(cookieObj);
+ cookieObj = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(tell));
+ if (cookieObj == NULL)
+ goto fail;
+ break;
+
+ case SEEK_END:
+ /* seek relative to end of file */
+ cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
+ if (cmp < 0)
+ goto fail;
+
+ if (cmp == 0) {
+ _unsupported(self->state, "can't do nonzero end-relative seeks");
+ goto fail;
+ }
+
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL)
+ goto fail;
+ Py_DECREF(res);
+
+ textiowrapper_set_decoded_chars(self, NULL);
+ Py_CLEAR(self->snapshot);
+ if (self->decoder) {
+ res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
+ if (res == NULL)
+ goto fail;
+ Py_DECREF(res);
+ }
+
+ res = _PyObject_CallMethod(self->buffer, &_Py_ID(seek), "ii", 0, 2);
+ Py_CLEAR(cookieObj);
+ if (res == NULL)
+ goto fail;
+ if (self->encoder) {
+ /* If seek() == 0, we are at the start of stream, otherwise not */
+ cmp = PyObject_RichCompareBool(res, zero, Py_EQ);
+ if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
+ Py_DECREF(res);
+ goto fail;
+ }
+ }
+ return res;
+
+ case SEEK_SET:
+ break;
+
+ default:
+ PyErr_Format(PyExc_ValueError,
+ "invalid whence (%d, should be %d, %d or %d)", whence,
+ SEEK_SET, SEEK_CUR, SEEK_END);
+ goto fail;
+ }
+
+ cmp = PyObject_RichCompareBool(cookieObj, zero, Py_LT);
+ if (cmp < 0)
+ goto fail;
+
+ if (cmp == 1) {
+ PyErr_Format(PyExc_ValueError,
+ "negative seek position %R", cookieObj);
+ goto fail;
+ }
+
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL)
+ 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.
+ */
+ if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
+ goto fail;
+
+ /* Seek back to the safe start point. */
+ posobj = PyLong_FromOff_t(cookie.start_pos);
+ if (posobj == NULL)
+ goto fail;
+ res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj);
+ Py_DECREF(posobj);
+ if (res == NULL)
+ goto fail;
+ Py_DECREF(res);
+
+ textiowrapper_set_decoded_chars(self, NULL);
+ Py_CLEAR(self->snapshot);
+
+ /* Restore the decoder to its state from the safe start point. */
+ if (self->decoder) {
+ if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
+ goto fail;
+ }
+
+ if (cookie.chars_to_skip) {
+ /* Just like _read_chunk, feed the decoder and save a snapshot. */
+ PyObject *input_chunk = _PyObject_CallMethod(self->buffer, &_Py_ID(read),
+ "i", cookie.bytes_to_feed);
+ PyObject *decoded;
+
+ if (input_chunk == NULL)
+ goto fail;
+
+ if (!PyBytes_Check(input_chunk)) {
+ PyErr_Format(PyExc_TypeError,
+ "underlying read() should have returned a bytes "
+ "object, not '%.200s'",
+ Py_TYPE(input_chunk)->tp_name);
+ Py_DECREF(input_chunk);
+ goto fail;
+ }
+
+ snapshot = Py_BuildValue("iN", cookie.dec_flags, input_chunk);
+ if (snapshot == NULL) {
+ goto fail;
+ }
+ Py_XSETREF(self->snapshot, snapshot);
+
+ decoded = PyObject_CallMethodObjArgs(self->decoder, &_Py_ID(decode),
+ input_chunk, cookie.need_eof ? Py_True : Py_False, NULL);
+
+ if (check_decoded(decoded) < 0)
+ goto fail;
+
+ textiowrapper_set_decoded_chars(self, decoded);
+
+ /* Skip chars_to_skip of the decoded characters. */
+ if (PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) {
+ PyErr_SetString(PyExc_OSError, "can't restore logical file position");
+ goto fail;
+ }
+ self->decoded_chars_used = cookie.chars_to_skip;
+ }
+ else {
+ snapshot = Py_BuildValue("iy", cookie.dec_flags, "");
+ if (snapshot == NULL)
+ goto fail;
+ Py_XSETREF(self->snapshot, snapshot);
+ }
+
+ /* Finally, reset the encoder (merely useful for proper BOM handling) */
+ if (self->encoder) {
+ if (_textiowrapper_encoder_setstate(self, &cookie) < 0)
+ goto fail;
+ }
+ return cookieObj;
+ fail:
+ Py_XDECREF(cookieObj);
+ return NULL;
+
+}
+
+/*[clinic input]
+_io.TextIOWrapper.tell
+
+Return the stream position as an opaque number.
+
+The return value of tell() can be given as input to seek(), to restore a
+previous stream position.
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_tell_impl(textio *self)
+/*[clinic end generated code: output=4f168c08bf34ad5f input=0852d627d76fb520]*/
+{
+ PyObject *res;
+ PyObject *posobj = NULL;
+ cookie_type cookie = {0,0,0,0,0};
+ PyObject *next_input;
+ Py_ssize_t chars_to_skip, chars_decoded;
+ Py_ssize_t skip_bytes, skip_back;
+ PyObject *saved_state = NULL;
+ const char *input, *input_end;
+ Py_ssize_t dec_buffer_len;
+ int dec_flags;
+
+ CHECK_ATTACHED(self);
+ CHECK_CLOSED(self);
+
+ if (!self->seekable) {
+ _unsupported(self->state, "underlying stream is not seekable");
+ goto fail;
+ }
+ if (!self->telling) {
+ PyErr_SetString(PyExc_OSError,
+ "telling position disabled by next() call");
+ goto fail;
+ }
+
+ if (_textiowrapper_writeflush(self) < 0)
+ return NULL;
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL)
+ goto fail;
+ Py_DECREF(res);
+
+ posobj = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(tell));
+ if (posobj == NULL)
+ goto fail;
+
+ if (self->decoder == NULL || self->snapshot == NULL) {
+ assert (self->decoded_chars == NULL || PyUnicode_GetLength(self->decoded_chars) == 0);
+ return posobj;
+ }
+
+#if defined(HAVE_LARGEFILE_SUPPORT)
+ cookie.start_pos = PyLong_AsLongLong(posobj);
+#else
+ cookie.start_pos = PyLong_AsLong(posobj);
+#endif
+ Py_DECREF(posobj);
+ if (PyErr_Occurred())
+ goto fail;
+
+ /* Skip backward to the snapshot point (see _read_chunk). */
+ assert(PyTuple_Check(self->snapshot));
+ if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input))
+ goto fail;
+
+ assert (PyBytes_Check(next_input));
+
+ cookie.start_pos -= PyBytes_GET_SIZE(next_input);
+
+ /* How many decoded characters have been used up since the snapshot? */
+ if (self->decoded_chars_used == 0) {
+ /* We haven't moved from the snapshot point. */
+ return textiowrapper_build_cookie(&cookie);
+ }
+
+ chars_to_skip = self->decoded_chars_used;
+
+ /* Decoder state will be restored at the end */
+ saved_state = PyObject_CallMethodNoArgs(self->decoder,
+ &_Py_ID(getstate));
+ if (saved_state == NULL)
+ goto fail;
+
+#define DECODER_GETSTATE() do { \
+ PyObject *dec_buffer; \
+ PyObject *_state = PyObject_CallMethodNoArgs(self->decoder, \
+ &_Py_ID(getstate)); \
+ if (_state == NULL) \
+ goto fail; \
+ if (!PyTuple_Check(_state)) { \
+ PyErr_SetString(PyExc_TypeError, \
+ "illegal decoder state"); \
+ Py_DECREF(_state); \
+ goto fail; \
+ } \
+ if (!PyArg_ParseTuple(_state, "Oi;illegal decoder state", \
+ &dec_buffer, &dec_flags)) \
+ { \
+ Py_DECREF(_state); \
+ goto fail; \
+ } \
+ if (!PyBytes_Check(dec_buffer)) { \
+ PyErr_Format(PyExc_TypeError, \
+ "illegal decoder state: the first item should be a " \
+ "bytes object, not '%.200s'", \
+ Py_TYPE(dec_buffer)->tp_name); \
+ Py_DECREF(_state); \
+ goto fail; \
+ } \
+ dec_buffer_len = PyBytes_GET_SIZE(dec_buffer); \
+ Py_DECREF(_state); \
+ } while (0)
+
+#define DECODER_DECODE(start, len, res) do { \
+ PyObject *_decoded = _PyObject_CallMethod( \
+ self->decoder, &_Py_ID(decode), "y#", start, len); \
+ if (check_decoded(_decoded) < 0) \
+ goto fail; \
+ res = PyUnicode_GET_LENGTH(_decoded); \
+ Py_DECREF(_decoded); \
+ } while (0)
+
+ /* Fast search for an acceptable start point, close to our
+ current pos */
+ skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
+ skip_back = 1;
+ assert(skip_back <= PyBytes_GET_SIZE(next_input));
+ input = PyBytes_AS_STRING(next_input);
+ while (skip_bytes > 0) {
+ /* Decode up to temptative start point */
+ if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
+ goto fail;
+ DECODER_DECODE(input, skip_bytes, chars_decoded);
+ if (chars_decoded <= chars_to_skip) {
+ DECODER_GETSTATE();
+ if (dec_buffer_len == 0) {
+ /* Before pos and no bytes buffered in decoder => OK */
+ cookie.dec_flags = dec_flags;
+ chars_to_skip -= chars_decoded;
+ break;
+ }
+ /* Skip back by buffered amount and reset heuristic */
+ skip_bytes -= dec_buffer_len;
+ skip_back = 1;
+ }
+ else {
+ /* We're too far ahead, skip back a bit */
+ skip_bytes -= skip_back;
+ skip_back *= 2;
+ }
+ }
+ if (skip_bytes <= 0) {
+ skip_bytes = 0;
+ if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
+ goto fail;
+ }
+
+ /* Note our initial start point. */
+ cookie.start_pos += skip_bytes;
+ cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
+ if (chars_to_skip == 0)
+ goto finally;
+
+ /* We should be close to the desired position. Now feed the decoder one
+ * byte at a time until we reach the `chars_to_skip` target.
+ * As we go, note the nearest "safe start point" before the current
+ * location (a point where the decoder has nothing buffered, so seek()
+ * can safely start from there and advance to this location).
+ */
+ chars_decoded = 0;
+ input = PyBytes_AS_STRING(next_input);
+ input_end = input + PyBytes_GET_SIZE(next_input);
+ input += skip_bytes;
+ while (input < input_end) {
+ Py_ssize_t n;
+
+ DECODER_DECODE(input, (Py_ssize_t)1, n);
+ /* We got n chars for 1 byte */
+ chars_decoded += n;
+ cookie.bytes_to_feed += 1;
+ DECODER_GETSTATE();
+
+ if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) {
+ /* Decoder buffer is empty, so this is a safe start point. */
+ cookie.start_pos += cookie.bytes_to_feed;
+ chars_to_skip -= chars_decoded;
+ cookie.dec_flags = dec_flags;
+ cookie.bytes_to_feed = 0;
+ chars_decoded = 0;
+ }
+ if (chars_decoded >= chars_to_skip)
+ break;
+ input++;
+ }
+ if (input == input_end) {
+ /* We didn't get enough decoded data; signal EOF to get more. */
+ PyObject *decoded = _PyObject_CallMethod(
+ self->decoder, &_Py_ID(decode), "yO", "", /* final = */ Py_True);
+ if (check_decoded(decoded) < 0)
+ goto fail;
+ chars_decoded += PyUnicode_GET_LENGTH(decoded);
+ Py_DECREF(decoded);
+ cookie.need_eof = 1;
+
+ if (chars_decoded < chars_to_skip) {
+ PyErr_SetString(PyExc_OSError,
+ "can't reconstruct logical file position");
+ goto fail;
+ }
+ }
+
+finally:
+ res = PyObject_CallMethodOneArg(
+ self->decoder, &_Py_ID(setstate), saved_state);
+ Py_DECREF(saved_state);
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res);
+
+ /* The returned cookie corresponds to the last safe start point. */
+ cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
+ return textiowrapper_build_cookie(&cookie);
+
+fail:
+ if (saved_state) {
+ PyObject *exc = PyErr_GetRaisedException();
+ res = PyObject_CallMethodOneArg(
+ self->decoder, &_Py_ID(setstate), saved_state);
+ _PyErr_ChainExceptions1(exc);
+ Py_DECREF(saved_state);
+ Py_XDECREF(res);
+ }
+ return NULL;
+}
+
+/*[clinic input]
+_io.TextIOWrapper.truncate
+ pos: object = None
+ /
+[clinic start generated code]*/
+
+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)
+ return NULL;
+ Py_DECREF(res);
+
+ return PyObject_CallMethodOneArg(self->buffer, &_Py_ID(truncate), pos);
+}
+
+static PyObject *
+textiowrapper_repr(textio *self)
+{
+ PyObject *nameobj, *modeobj, *res, *s;
+ int status;
+
+ CHECK_INITIALIZED(self);
+
+ res = PyUnicode_FromString("<_io.TextIOWrapper");
+ if (res == NULL)
+ return NULL;
+
+ status = Py_ReprEnter((PyObject *)self);
+ if (status != 0) {
+ if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
+ goto error;
+ }
+ if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
+ if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
+ goto error;
+ }
+ /* Ignore ValueError raised if the underlying stream was detached */
+ PyErr_Clear();
+ }
+ if (nameobj != NULL) {
+ s = PyUnicode_FromFormat(" name=%R", nameobj);
+ Py_DECREF(nameobj);
+ if (s == NULL)
+ goto error;
+ PyUnicode_AppendAndDel(&res, s);
+ if (res == NULL)
+ goto error;
+ }
+ if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(mode), &modeobj) < 0) {
+ goto error;
+ }
+ if (modeobj != NULL) {
+ s = PyUnicode_FromFormat(" mode=%R", modeobj);
+ Py_DECREF(modeobj);
+ if (s == NULL)
+ goto error;
+ PyUnicode_AppendAndDel(&res, s);
+ if (res == NULL)
+ goto error;
+ }
+ s = PyUnicode_FromFormat("%U encoding=%R>",
+ res, self->encoding);
+ Py_DECREF(res);
+ if (status == 0) {
+ Py_ReprLeave((PyObject *)self);
+ }
+ return s;
+
+ error:
+ Py_XDECREF(res);
+ if (status == 0) {
+ Py_ReprLeave((PyObject *)self);
+ }
+ return NULL;
+}
+
+
+/* Inquiries */
+
+/*[clinic input]
+_io.TextIOWrapper.fileno
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_fileno_impl(textio *self)
+/*[clinic end generated code: output=21490a4c3da13e6c input=c488ca83d0069f9b]*/
+{
+ CHECK_ATTACHED(self);
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(fileno));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.seekable
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_seekable_impl(textio *self)
+/*[clinic end generated code: output=ab223dbbcffc0f00 input=8b005ca06e1fca13]*/
+{
+ CHECK_ATTACHED(self);
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(seekable));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.readable
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_readable_impl(textio *self)
+/*[clinic end generated code: output=72ff7ba289a8a91b input=0704ea7e01b0d3eb]*/
+{
+ CHECK_ATTACHED(self);
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.writable
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_writable_impl(textio *self)
+/*[clinic end generated code: output=a728c71790d03200 input=c41740bc9d8636e8]*/
+{
+ CHECK_ATTACHED(self);
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.isatty
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_isatty_impl(textio *self)
+/*[clinic end generated code: output=12be1a35bace882e input=fb68d9f2c99bbfff]*/
+{
+ CHECK_ATTACHED(self);
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(isatty));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.flush
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_flush_impl(textio *self)
+/*[clinic end generated code: output=59de9165f9c2e4d2 input=928c60590694ab85]*/
+{
+ CHECK_ATTACHED(self);
+ CHECK_CLOSED(self);
+ self->telling = self->seekable;
+ if (_textiowrapper_writeflush(self) < 0)
+ return NULL;
+ return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush));
+}
+
+/*[clinic input]
+_io.TextIOWrapper.close
+[clinic start generated code]*/
+
+static PyObject *
+_io_TextIOWrapper_close_impl(textio *self)
+/*[clinic end generated code: output=056ccf8b4876e4f4 input=9c2114315eae1948]*/
+{
+ PyObject *res;
+ int r;
+ CHECK_ATTACHED(self);
+
+ res = textiowrapper_closed_get(self, NULL);
+ if (res == NULL)
+ return NULL;
+ r = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (r < 0)
+ return NULL;
+
+ if (r > 0) {
+ Py_RETURN_NONE; /* stream already closed */
+ }
+ else {
+ PyObject *exc = NULL;
+ if (self->finalizing) {
+ res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn),
+ (PyObject *)self);
+ if (res) {
+ Py_DECREF(res);
+ }
+ else {
+ PyErr_Clear();
+ }
+ }
+ res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
+ Py_DECREF(res);
+ }
+
+ res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));
+ if (exc != NULL) {
+ _PyErr_ChainExceptions1(exc);
+ Py_CLEAR(res);
+ }
+ return res;
+ }
+}
+
+static PyObject *
+textiowrapper_iternext(textio *self)
+{
+ PyObject *line;
+
+ CHECK_ATTACHED(self);
+
+ self->telling = 0;
+ if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) {
+ /* Skip method call overhead for speed */
+ line = _textiowrapper_readline(self, -1);
+ }
+ else {
+ line = PyObject_CallMethodNoArgs((PyObject *)self,
+ &_Py_ID(readline));
+ if (line && !PyUnicode_Check(line)) {
+ PyErr_Format(PyExc_OSError,
+ "readline() should have returned a str object, "
+ "not '%.200s'", Py_TYPE(line)->tp_name);
+ Py_DECREF(line);
+ return NULL;
+ }
+ }
+
+ if (line == NULL || PyUnicode_READY(line) == -1)
+ return NULL;
+
+ if (PyUnicode_GET_LENGTH(line) == 0) {
+ /* Reached EOF or would have blocked */
+ Py_DECREF(line);
+ Py_CLEAR(self->snapshot);
+ self->telling = self->seekable;
+ return NULL;
+ }
+
+ return line;
+}
+
+static PyObject *
+textiowrapper_name_get(textio *self, void *context)
+{
+ CHECK_ATTACHED(self);
+ return PyObject_GetAttr(self->buffer, &_Py_ID(name));
+}
+
+static PyObject *
+textiowrapper_closed_get(textio *self, void *context)
+{
+ CHECK_ATTACHED(self);
+ return PyObject_GetAttr(self->buffer, &_Py_ID(closed));
+}
+
+static PyObject *
+textiowrapper_newlines_get(textio *self, void *context)
+{
+ PyObject *res;
+ CHECK_ATTACHED(self);
+ if (self->decoder == NULL ||
+ _PyObject_LookupAttr(self->decoder, &_Py_ID(newlines), &res) == 0)
+ {
+ Py_RETURN_NONE;
+ }
+ return res;
+}
+
+static PyObject *
+textiowrapper_errors_get(textio *self, void *context)
+{
+ CHECK_INITIALIZED(self);
+ return Py_NewRef(self->errors);
+}
+
+static PyObject *
+textiowrapper_chunk_size_get(textio *self, void *context)
+{
+ CHECK_ATTACHED(self);
+ return PyLong_FromSsize_t(self->chunk_size);
+}
+
+static int
+textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
+{
+ Py_ssize_t n;
+ CHECK_ATTACHED_INT(self);
+ if (arg == NULL) {
+ PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
+ return -1;
+ }
+ n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
+ if (n == -1 && PyErr_Occurred())
+ return -1;
+ if (n <= 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "a strictly positive integer is required");
+ return -1;
+ }
+ self->chunk_size = n;
+ return 0;
+}
+
+static PyMethodDef incrementalnewlinedecoder_methods[] = {
+ _IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF
+ {NULL}
+};
+
+static PyGetSetDef incrementalnewlinedecoder_getset[] = {
+ {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL, NULL},
+ {NULL}
+};
+
+static PyType_Slot nldecoder_slots[] = {
+ {Py_tp_dealloc, incrementalnewlinedecoder_dealloc},
+ {Py_tp_doc, (void *)_io_IncrementalNewlineDecoder___init____doc__},
+ {Py_tp_methods, incrementalnewlinedecoder_methods},
+ {Py_tp_getset, incrementalnewlinedecoder_getset},
+ {Py_tp_traverse, incrementalnewlinedecoder_traverse},
+ {Py_tp_clear, incrementalnewlinedecoder_clear},
+ {Py_tp_init, _io_IncrementalNewlineDecoder___init__},
+ {0, NULL},
+};
+
+PyType_Spec nldecoder_spec = {
+ .name = "_io.IncrementalNewlineDecoder",
+ .basicsize = sizeof(nldecoder_object),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = nldecoder_slots,
+};
+
+
+static PyMethodDef textiowrapper_methods[] = {
+ _IO_TEXTIOWRAPPER_DETACH_METHODDEF
+ _IO_TEXTIOWRAPPER_RECONFIGURE_METHODDEF
+ _IO_TEXTIOWRAPPER_WRITE_METHODDEF
+ _IO_TEXTIOWRAPPER_READ_METHODDEF
+ _IO_TEXTIOWRAPPER_READLINE_METHODDEF
+ _IO_TEXTIOWRAPPER_FLUSH_METHODDEF
+ _IO_TEXTIOWRAPPER_CLOSE_METHODDEF
+
+ _IO_TEXTIOWRAPPER_FILENO_METHODDEF
+ _IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_READABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_ISATTY_METHODDEF
+
+ _IO_TEXTIOWRAPPER_SEEK_METHODDEF
+ _IO_TEXTIOWRAPPER_TELL_METHODDEF
+ _IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF
+
+ {"__reduce__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {"__reduce_ex__", _PyIOBase_cannot_pickle, METH_VARARGS},
+ {NULL, NULL}
+};
+
+static PyMemberDef textiowrapper_members[] = {
+ {"encoding", T_OBJECT, offsetof(textio, encoding), READONLY},
+ {"buffer", T_OBJECT, offsetof(textio, buffer), READONLY},
+ {"line_buffering", T_BOOL, offsetof(textio, line_buffering), READONLY},
+ {"write_through", T_BOOL, offsetof(textio, write_through), READONLY},
+ {"_finalizing", T_BOOL, offsetof(textio, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(textio, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(textio, dict), READONLY},
+ {NULL}
+};
+
+static PyGetSetDef textiowrapper_getset[] = {
+ {"name", (getter)textiowrapper_name_get, NULL, NULL},
+ {"closed", (getter)textiowrapper_closed_get, NULL, NULL},
+/* {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL},
+*/
+ {"newlines", (getter)textiowrapper_newlines_get, NULL, NULL},
+ {"errors", (getter)textiowrapper_errors_get, NULL, NULL},
+ {"_CHUNK_SIZE", (getter)textiowrapper_chunk_size_get,
+ (setter)textiowrapper_chunk_size_set, NULL},
+ {NULL}
+};
+
+PyType_Slot textiowrapper_slots[] = {
+ {Py_tp_dealloc, textiowrapper_dealloc},
+ {Py_tp_repr, textiowrapper_repr},
+ {Py_tp_doc, (void *)_io_TextIOWrapper___init____doc__},
+ {Py_tp_traverse, textiowrapper_traverse},
+ {Py_tp_clear, textiowrapper_clear},
+ {Py_tp_iternext, textiowrapper_iternext},
+ {Py_tp_methods, textiowrapper_methods},
+ {Py_tp_members, textiowrapper_members},
+ {Py_tp_getset, textiowrapper_getset},
+ {Py_tp_init, _io_TextIOWrapper___init__},
+ {0, NULL},
+};
+
+PyType_Spec textiowrapper_spec = {
+ .name = "_io.TextIOWrapper",
+ .basicsize = sizeof(textio),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = textiowrapper_slots,
+};
diff --git a/contrib/tools/python3/Modules/_io/winconsoleio.c b/contrib/tools/python3/Modules/_io/winconsoleio.c
new file mode 100644
index 0000000000..c2c365e080
--- /dev/null
+++ b/contrib/tools/python3/Modules/_io/winconsoleio.c
@@ -0,0 +1,1179 @@
+/*
+ An implementation of Windows console I/O
+
+ Classes defined here: _WindowsConsoleIO
+
+ Written by Steve Dower
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
+#include "pycore_object.h" // _PyObject_GC_UNTRACK()
+
+#ifdef HAVE_WINDOWS_CONSOLE_IO
+
+#include "structmember.h" // PyMemberDef
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#include <stddef.h> /* For offsetof */
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#include <fcntl.h>
+
+#include "_iomodule.h"
+
+/* BUFSIZ determines how many characters can be typed at the console
+ before it starts blocking. */
+#if BUFSIZ < (16*1024)
+#define SMALLCHUNK (2*1024)
+#elif (BUFSIZ >= (2 << 25))
+#error "unreasonable BUFSIZ > 64 MiB defined"
+#else
+#define SMALLCHUNK BUFSIZ
+#endif
+
+/* BUFMAX determines how many bytes can be read in one go. */
+#define BUFMAX (32*1024*1024)
+
+/* SMALLBUF determines how many utf-8 characters will be
+ buffered within the stream, in order to support reads
+ of less than one character */
+#define SMALLBUF 4
+
+char _get_console_type(HANDLE handle) {
+ DWORD mode, peek_count;
+
+ if (handle == INVALID_HANDLE_VALUE)
+ return '\0';
+
+ if (!GetConsoleMode(handle, &mode))
+ return '\0';
+
+ /* Peek at the handle to see whether it is an input or output handle */
+ if (GetNumberOfConsoleInputEvents(handle, &peek_count))
+ return 'r';
+ return 'w';
+}
+
+char _PyIO_get_console_type(PyObject *path_or_fd) {
+ int fd = PyLong_AsLong(path_or_fd);
+ PyErr_Clear();
+ if (fd >= 0) {
+ HANDLE handle = _Py_get_osfhandle_noraise(fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return '\0';
+ return _get_console_type(handle);
+ }
+
+ PyObject *decoded;
+ wchar_t *decoded_wstr;
+
+ if (!PyUnicode_FSDecoder(path_or_fd, &decoded)) {
+ PyErr_Clear();
+ return '\0';
+ }
+ decoded_wstr = PyUnicode_AsWideCharString(decoded, NULL);
+ Py_CLEAR(decoded);
+ if (!decoded_wstr) {
+ PyErr_Clear();
+ return '\0';
+ }
+
+ char m = '\0';
+ if (!_wcsicmp(decoded_wstr, L"CONIN$")) {
+ m = 'r';
+ } else if (!_wcsicmp(decoded_wstr, L"CONOUT$")) {
+ m = 'w';
+ } else if (!_wcsicmp(decoded_wstr, L"CON")) {
+ m = 'x';
+ }
+ if (m) {
+ PyMem_Free(decoded_wstr);
+ return m;
+ }
+
+ DWORD length;
+ wchar_t name_buf[MAX_PATH], *pname_buf = name_buf;
+
+ length = GetFullPathNameW(decoded_wstr, MAX_PATH, pname_buf, NULL);
+ if (length > MAX_PATH) {
+ pname_buf = PyMem_New(wchar_t, length);
+ if (pname_buf)
+ length = GetFullPathNameW(decoded_wstr, length, pname_buf, NULL);
+ else
+ length = 0;
+ }
+ PyMem_Free(decoded_wstr);
+
+ if (length) {
+ wchar_t *name = pname_buf;
+ if (length >= 4 && name[3] == L'\\' &&
+ (name[2] == L'.' || name[2] == L'?') &&
+ name[1] == L'\\' && name[0] == L'\\') {
+ name += 4;
+ }
+ if (!_wcsicmp(name, L"CONIN$")) {
+ m = 'r';
+ } else if (!_wcsicmp(name, L"CONOUT$")) {
+ m = 'w';
+ } else if (!_wcsicmp(name, L"CON")) {
+ m = 'x';
+ }
+ }
+
+ if (pname_buf != name_buf)
+ PyMem_Free(pname_buf);
+ return m;
+}
+
+static DWORD
+_find_last_utf8_boundary(const char *buf, DWORD len)
+{
+ /* This function never returns 0, returns the original len instead */
+ DWORD count = 1;
+ if (len == 0 || (buf[len - 1] & 0x80) == 0) {
+ return len;
+ }
+ for (;; count++) {
+ if (count > 3 || count >= len) {
+ return len;
+ }
+ if ((buf[len - count] & 0xc0) != 0x80) {
+ return len - count;
+ }
+ }
+}
+
+/*[clinic input]
+module _io
+class _io._WindowsConsoleIO "winconsoleio *" "clinic_state()->PyWindowsConsoleIO_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=05526e723011ab36]*/
+
+typedef struct {
+ PyObject_HEAD
+ int fd;
+ unsigned int created : 1;
+ unsigned int readable : 1;
+ unsigned int writable : 1;
+ unsigned int closefd : 1;
+ char finalizing;
+ unsigned int blksize;
+ PyObject *weakreflist;
+ PyObject *dict;
+ char buf[SMALLBUF];
+ wchar_t wbuf;
+} winconsoleio;
+
+int
+_PyWindowsConsoleIO_closed(PyObject *self)
+{
+ return ((winconsoleio *)self)->fd == -1;
+}
+
+
+/* Returns 0 on success, -1 with exception set on failure. */
+static int
+internal_close(winconsoleio *self)
+{
+ if (self->fd != -1) {
+ if (self->closefd) {
+ _Py_BEGIN_SUPPRESS_IPH
+ close(self->fd);
+ _Py_END_SUPPRESS_IPH
+ }
+ self->fd = -1;
+ }
+ return 0;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.close
+ cls: defining_class
+ /
+
+Close the console object.
+
+A closed console object cannot be used for further I/O operations.
+close() may be called more than once without error.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_close_impl(winconsoleio *self, PyTypeObject *cls)
+/*[clinic end generated code: output=e50c1808c063e1e2 input=161001bd2a649a4b]*/
+{
+ PyObject *res;
+ PyObject *exc;
+ int rc;
+
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ res = PyObject_CallMethodOneArg((PyObject*)state->PyRawIOBase_Type,
+ &_Py_ID(close), (PyObject*)self);
+ if (!self->closefd) {
+ self->fd = -1;
+ return res;
+ }
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ rc = internal_close(self);
+ if (res == NULL) {
+ _PyErr_ChainExceptions1(exc);
+ }
+ if (rc < 0) {
+ Py_CLEAR(res);
+ }
+ return res;
+}
+
+static PyObject *
+winconsoleio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ winconsoleio *self;
+
+ assert(type != NULL && type->tp_alloc != NULL);
+
+ self = (winconsoleio *) type->tp_alloc(type, 0);
+ if (self != NULL) {
+ self->fd = -1;
+ self->created = 0;
+ self->readable = 0;
+ self->writable = 0;
+ self->closefd = 0;
+ self->blksize = 0;
+ self->weakreflist = NULL;
+ }
+
+ return (PyObject *) self;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.__init__
+ file as nameobj: object
+ mode: str = "r"
+ closefd: bool = True
+ opener: object = None
+
+Open a console buffer by file descriptor.
+
+The mode can be 'rb' (default), or 'wb' for reading or writing bytes. All
+other mode characters will be ignored. Mode 'b' will be assumed if it is
+omitted. The *opener* parameter is always ignored.
+[clinic start generated code]*/
+
+static int
+_io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
+ const char *mode, int closefd,
+ PyObject *opener)
+/*[clinic end generated code: output=3fd9cbcdd8d95429 input=7a3eed6bbe998fd9]*/
+{
+ const char *s;
+ wchar_t *name = NULL;
+ char console_type = '\0';
+ int ret = 0;
+ int rwa = 0;
+ int fd = -1;
+ int fd_is_own = 0;
+ HANDLE handle = NULL;
+
+#ifndef NDEBUG
+ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
+ assert(PyObject_TypeCheck(self, state->PyWindowsConsoleIO_Type));
+#endif
+ if (self->fd >= 0) {
+ if (self->closefd) {
+ /* Have to close the existing file first. */
+ if (internal_close(self) < 0)
+ return -1;
+ }
+ else
+ self->fd = -1;
+ }
+
+ fd = _PyLong_AsInt(nameobj);
+ if (fd < 0) {
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative file descriptor");
+ return -1;
+ }
+ PyErr_Clear();
+ }
+ self->fd = fd;
+
+ if (fd < 0) {
+ PyObject *decodedname;
+
+ int d = PyUnicode_FSDecoder(nameobj, (void*)&decodedname);
+ if (!d)
+ return -1;
+
+ name = PyUnicode_AsWideCharString(decodedname, NULL);
+ console_type = _PyIO_get_console_type(decodedname);
+ Py_CLEAR(decodedname);
+ if (name == NULL)
+ return -1;
+ }
+
+ s = mode;
+ while (*s) {
+ switch (*s++) {
+ case '+':
+ case 'a':
+ case 'b':
+ case 'x':
+ break;
+ case 'r':
+ if (rwa)
+ goto bad_mode;
+ rwa = 1;
+ self->readable = 1;
+ if (console_type == 'x')
+ console_type = 'r';
+ break;
+ case 'w':
+ if (rwa)
+ goto bad_mode;
+ rwa = 1;
+ self->writable = 1;
+ if (console_type == 'x')
+ console_type = 'w';
+ break;
+ default:
+ PyErr_Format(PyExc_ValueError,
+ "invalid mode: %.200s", mode);
+ goto error;
+ }
+ }
+
+ if (!rwa)
+ goto bad_mode;
+
+ if (fd >= 0) {
+ handle = _Py_get_osfhandle_noraise(fd);
+ self->closefd = 0;
+ } else {
+ DWORD access = GENERIC_READ;
+
+ self->closefd = 1;
+ if (!closefd) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot use closefd=False with file name");
+ goto error;
+ }
+
+ if (self->writable)
+ access = GENERIC_WRITE;
+
+ Py_BEGIN_ALLOW_THREADS
+ /* Attempt to open for read/write initially, then fall back
+ on the specific access. This is required for modern names
+ CONIN$ and CONOUT$, which allow reading/writing state as
+ well as reading/writing content. */
+ handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+ if (handle == INVALID_HANDLE_VALUE)
+ handle = CreateFileW(name, access,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+ Py_END_ALLOW_THREADS
+
+ if (handle == INVALID_HANDLE_VALUE) {
+ PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, GetLastError(), nameobj);
+ goto error;
+ }
+
+ if (self->writable)
+ self->fd = _Py_open_osfhandle_noraise(handle, _O_WRONLY | _O_BINARY);
+ else
+ self->fd = _Py_open_osfhandle_noraise(handle, _O_RDONLY | _O_BINARY);
+ if (self->fd < 0) {
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ CloseHandle(handle);
+ goto error;
+ }
+ }
+
+ if (console_type == '\0')
+ console_type = _get_console_type(handle);
+
+ if (self->writable && console_type != 'w') {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot open console input buffer for writing");
+ goto error;
+ }
+ if (self->readable && console_type != 'r') {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot open console output buffer for reading");
+ goto error;
+ }
+
+ self->blksize = DEFAULT_BUFFER_SIZE;
+ memset(self->buf, 0, 4);
+
+ if (PyObject_SetAttr((PyObject *)self, &_Py_ID(name), nameobj) < 0)
+ goto error;
+
+ goto done;
+
+bad_mode:
+ PyErr_SetString(PyExc_ValueError,
+ "Must have exactly one of read or write mode");
+error:
+ ret = -1;
+ internal_close(self);
+
+done:
+ if (name)
+ PyMem_Free(name);
+ return ret;
+}
+
+static int
+winconsoleio_traverse(winconsoleio *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static int
+winconsoleio_clear(winconsoleio *self)
+{
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
+static void
+winconsoleio_dealloc(winconsoleio *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ self->finalizing = 1;
+ if (_PyIOBase_finalize((PyObject *) self) < 0)
+ return;
+ _PyObject_GC_UNTRACK(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) self);
+ Py_CLEAR(self->dict);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
+}
+
+static PyObject *
+err_closed(void)
+{
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
+ return NULL;
+}
+
+static PyObject *
+err_mode(_PyIO_State *state, const char *action)
+{
+ return PyErr_Format(state->unsupported_operation,
+ "Console buffer does not support %s", action);
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.fileno
+
+Return the underlying file descriptor (an integer).
+
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_fileno_impl(winconsoleio *self)
+/*[clinic end generated code: output=006fa74ce3b5cfbf input=845c47ebbc3a2f67]*/
+{
+ if (self->fd < 0)
+ return err_closed();
+ return PyLong_FromLong(self->fd);
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.readable
+
+True if console is an input buffer.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_readable_impl(winconsoleio *self)
+/*[clinic end generated code: output=daf9cef2743becf0 input=6be9defb5302daae]*/
+{
+ if (self->fd == -1)
+ return err_closed();
+ return PyBool_FromLong((long) self->readable);
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.writable
+
+True if console is an output buffer.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_writable_impl(winconsoleio *self)
+/*[clinic end generated code: output=e0a2ad7eae5abf67 input=cefbd8abc24df6a0]*/
+{
+ if (self->fd == -1)
+ return err_closed();
+ return PyBool_FromLong((long) self->writable);
+}
+
+static DWORD
+_buflen(winconsoleio *self)
+{
+ for (DWORD i = 0; i < SMALLBUF; ++i) {
+ if (!self->buf[i])
+ return i;
+ }
+ return SMALLBUF;
+}
+
+static DWORD
+_copyfrombuf(winconsoleio *self, char *buf, DWORD len)
+{
+ DWORD n = 0;
+
+ while (self->buf[0] && len--) {
+ buf[n++] = self->buf[0];
+ for (int i = 1; i < SMALLBUF; ++i)
+ self->buf[i - 1] = self->buf[i];
+ self->buf[SMALLBUF - 1] = 0;
+ }
+
+ return n;
+}
+
+static wchar_t *
+read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) {
+ int err = 0, sig = 0;
+
+ wchar_t *buf = (wchar_t*)PyMem_Malloc(maxlen * sizeof(wchar_t));
+ if (!buf)
+ goto error;
+
+ *readlen = 0;
+
+ //DebugBreak();
+ Py_BEGIN_ALLOW_THREADS
+ DWORD off = 0;
+ while (off < maxlen) {
+ DWORD n = (DWORD)-1;
+ DWORD len = min(maxlen - off, BUFSIZ);
+ SetLastError(0);
+ BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL);
+
+ if (!res) {
+ err = GetLastError();
+ break;
+ }
+ if (n == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
+ break;
+ }
+ if (n == 0) {
+ err = GetLastError();
+ if (err != ERROR_OPERATION_ABORTED)
+ break;
+ err = 0;
+ HANDLE hInterruptEvent = _PyOS_SigintEvent();
+ if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE)
+ == WAIT_OBJECT_0) {
+ ResetEvent(hInterruptEvent);
+ Py_BLOCK_THREADS
+ sig = PyErr_CheckSignals();
+ Py_UNBLOCK_THREADS
+ if (sig < 0)
+ break;
+ }
+ }
+ *readlen += n;
+
+ /* If we didn't read a full buffer that time, don't try
+ again or we will block a second time. */
+ if (n < len)
+ break;
+ /* If the buffer ended with a newline, break out */
+ if (buf[*readlen - 1] == '\n')
+ break;
+ /* If the buffer ends with a high surrogate, expand the
+ buffer and read an extra character. */
+ WORD char_type;
+ if (off + BUFSIZ >= maxlen &&
+ GetStringTypeW(CT_CTYPE3, &buf[*readlen - 1], 1, &char_type) &&
+ char_type == C3_HIGHSURROGATE) {
+ wchar_t *newbuf;
+ maxlen += 1;
+ Py_BLOCK_THREADS
+ newbuf = (wchar_t*)PyMem_Realloc(buf, maxlen * sizeof(wchar_t));
+ Py_UNBLOCK_THREADS
+ if (!newbuf) {
+ sig = -1;
+ break;
+ }
+ buf = newbuf;
+ /* Only advance by n and not BUFSIZ in this case */
+ off += n;
+ continue;
+ }
+
+ off += BUFSIZ;
+ }
+
+ Py_END_ALLOW_THREADS
+
+ if (sig)
+ goto error;
+ if (err) {
+ PyErr_SetFromWindowsErr(err);
+ goto error;
+ }
+
+ if (*readlen > 0 && buf[0] == L'\x1a') {
+ PyMem_Free(buf);
+ buf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t));
+ if (!buf)
+ goto error;
+ buf[0] = L'\0';
+ *readlen = 0;
+ }
+
+ return buf;
+
+error:
+ if (buf)
+ PyMem_Free(buf);
+ return NULL;
+}
+
+
+static Py_ssize_t
+readinto(_PyIO_State *state, winconsoleio *self, char *buf, Py_ssize_t len)
+{
+ if (self->fd == -1) {
+ err_closed();
+ return -1;
+ }
+ if (!self->readable) {
+ err_mode(state, "reading");
+ return -1;
+ }
+ if (len == 0)
+ return 0;
+ if (len > BUFMAX) {
+ PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX);
+ return -1;
+ }
+
+ HANDLE handle = _Py_get_osfhandle(self->fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return -1;
+
+ /* Each character may take up to 4 bytes in the final buffer.
+ This is highly conservative, but necessary to avoid
+ failure for any given Unicode input (e.g. \U0010ffff).
+ If the caller requests fewer than 4 bytes, we buffer one
+ character.
+ */
+ DWORD wlen = (DWORD)(len / 4);
+ if (wlen == 0) {
+ wlen = 1;
+ }
+
+ DWORD read_len = _copyfrombuf(self, buf, (DWORD)len);
+ if (read_len) {
+ buf = &buf[read_len];
+ len -= read_len;
+ wlen -= 1;
+ }
+ if (len == read_len || wlen == 0)
+ return read_len;
+
+ DWORD n;
+ wchar_t *wbuf = read_console_w(handle, wlen, &n);
+ if (wbuf == NULL)
+ return -1;
+ if (n == 0) {
+ PyMem_Free(wbuf);
+ return read_len;
+ }
+
+ int err = 0;
+ DWORD u8n = 0;
+
+ Py_BEGIN_ALLOW_THREADS
+ if (len < 4) {
+ if (WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
+ self->buf, sizeof(self->buf) / sizeof(self->buf[0]),
+ NULL, NULL))
+ u8n = _copyfrombuf(self, buf, (DWORD)len);
+ } else {
+ u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
+ buf, (DWORD)len, NULL, NULL);
+ }
+
+ if (u8n) {
+ read_len += u8n;
+ u8n = 0;
+ } else {
+ err = GetLastError();
+ if (err == ERROR_INSUFFICIENT_BUFFER) {
+ /* Calculate the needed buffer for a more useful error, as this
+ means our "/ 4" logic above is insufficient for some input.
+ */
+ u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
+ NULL, 0, NULL, NULL);
+ }
+ }
+ Py_END_ALLOW_THREADS
+
+ PyMem_Free(wbuf);
+
+ if (u8n) {
+ PyErr_Format(PyExc_SystemError,
+ "Buffer had room for %zd bytes but %u bytes required",
+ len, u8n);
+ return -1;
+ }
+ if (err) {
+ PyErr_SetFromWindowsErr(err);
+ return -1;
+ }
+
+ return read_len;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.readinto
+ cls: defining_class
+ buffer: Py_buffer(accept={rwbuffer})
+ /
+
+Same as RawIOBase.readinto().
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_buffer *buffer)
+/*[clinic end generated code: output=96717c74f6204b79 input=4b0627c3b1645f78]*/
+{
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ Py_ssize_t len = readinto(state, self, buffer->buf, buffer->len);
+ if (len < 0)
+ return NULL;
+
+ return PyLong_FromSsize_t(len);
+}
+
+static DWORD
+new_buffersize(winconsoleio *self, DWORD currentsize)
+{
+ DWORD addend;
+
+ /* Expand the buffer by an amount proportional to the current size,
+ giving us amortized linear-time behavior. For bigger sizes, use a
+ less-than-double growth factor to avoid excessive allocation. */
+ if (currentsize > 65536)
+ addend = currentsize >> 3;
+ else
+ addend = 256 + currentsize;
+ if (addend < SMALLCHUNK)
+ /* Avoid tiny read() calls. */
+ addend = SMALLCHUNK;
+ return addend + currentsize;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.readall
+
+Read all data from the console, returned as bytes.
+
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_readall_impl(winconsoleio *self)
+/*[clinic end generated code: output=e6d312c684f6e23b input=4024d649a1006e69]*/
+{
+ wchar_t *buf;
+ DWORD bufsize, n, len = 0;
+ PyObject *bytes;
+ DWORD bytes_size, rn;
+ HANDLE handle;
+
+ if (self->fd == -1)
+ return err_closed();
+
+ handle = _Py_get_osfhandle(self->fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return NULL;
+
+ bufsize = BUFSIZ;
+
+ buf = (wchar_t*)PyMem_Malloc((bufsize + 1) * sizeof(wchar_t));
+ if (buf == NULL)
+ return NULL;
+
+ while (1) {
+ wchar_t *subbuf;
+
+ if (len >= (Py_ssize_t)bufsize) {
+ DWORD newsize = new_buffersize(self, len);
+ if (newsize > BUFMAX)
+ break;
+ if (newsize < bufsize) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unbounded read returned more bytes "
+ "than a Python bytes object can hold");
+ PyMem_Free(buf);
+ return NULL;
+ }
+ bufsize = newsize;
+
+ wchar_t *tmp = PyMem_Realloc(buf,
+ (bufsize + 1) * sizeof(wchar_t));
+ if (tmp == NULL) {
+ PyMem_Free(buf);
+ return NULL;
+ }
+ buf = tmp;
+ }
+
+ subbuf = read_console_w(handle, bufsize - len, &n);
+
+ if (subbuf == NULL) {
+ PyMem_Free(buf);
+ return NULL;
+ }
+
+ if (n > 0)
+ wcsncpy_s(&buf[len], bufsize - len + 1, subbuf, n);
+
+ PyMem_Free(subbuf);
+
+ /* when the read is empty we break */
+ if (n == 0)
+ break;
+
+ len += n;
+ }
+
+ if (len == 0 && _buflen(self) == 0) {
+ /* when the result starts with ^Z we return an empty buffer */
+ PyMem_Free(buf);
+ return PyBytes_FromStringAndSize(NULL, 0);
+ }
+
+ if (len) {
+ Py_BEGIN_ALLOW_THREADS
+ bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
+ NULL, 0, NULL, NULL);
+ Py_END_ALLOW_THREADS
+
+ if (!bytes_size) {
+ DWORD err = GetLastError();
+ PyMem_Free(buf);
+ return PyErr_SetFromWindowsErr(err);
+ }
+ } else {
+ bytes_size = 0;
+ }
+
+ bytes_size += _buflen(self);
+ bytes = PyBytes_FromStringAndSize(NULL, bytes_size);
+ rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size);
+
+ if (len) {
+ Py_BEGIN_ALLOW_THREADS
+ bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
+ &PyBytes_AS_STRING(bytes)[rn], bytes_size - rn, NULL, NULL);
+ Py_END_ALLOW_THREADS
+
+ if (!bytes_size) {
+ DWORD err = GetLastError();
+ PyMem_Free(buf);
+ Py_CLEAR(bytes);
+ return PyErr_SetFromWindowsErr(err);
+ }
+
+ /* add back the number of preserved bytes */
+ bytes_size += rn;
+ }
+
+ PyMem_Free(buf);
+ if (bytes_size < (size_t)PyBytes_GET_SIZE(bytes)) {
+ if (_PyBytes_Resize(&bytes, n * sizeof(wchar_t)) < 0) {
+ Py_CLEAR(bytes);
+ return NULL;
+ }
+ }
+ return bytes;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.read
+ cls: defining_class
+ size: Py_ssize_t(accept={int, NoneType}) = -1
+ /
+
+Read at most size bytes, returned as bytes.
+
+Only makes one system call when size is a positive integer,
+so less data may be returned than requested.
+Return an empty bytes object at EOF.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_ssize_t size)
+/*[clinic end generated code: output=7e569a586537c0ae input=a14570a5da273365]*/
+{
+ PyObject *bytes;
+ Py_ssize_t bytes_size;
+
+ if (self->fd == -1)
+ return err_closed();
+ if (!self->readable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "reading");
+ }
+
+ if (size < 0)
+ return _io__WindowsConsoleIO_readall_impl(self);
+ if (size > BUFMAX) {
+ PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX);
+ return NULL;
+ }
+
+ bytes = PyBytes_FromStringAndSize(NULL, size);
+ if (bytes == NULL)
+ return NULL;
+
+ _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) {
+ Py_CLEAR(bytes);
+ return NULL;
+ }
+
+ if (bytes_size < PyBytes_GET_SIZE(bytes)) {
+ if (_PyBytes_Resize(&bytes, bytes_size) < 0) {
+ Py_CLEAR(bytes);
+ return NULL;
+ }
+ }
+
+ return bytes;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.write
+ cls: defining_class
+ b: Py_buffer
+ /
+
+Write buffer b to file, return number of bytes written.
+
+Only makes one system call, so not all of the data may be written.
+The number of bytes actually written is returned.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
+ Py_buffer *b)
+/*[clinic end generated code: output=e8019f480243cb29 input=10ac37c19339dfbe]*/
+{
+ BOOL res = TRUE;
+ wchar_t *wbuf;
+ DWORD len, wlen, n = 0;
+ HANDLE handle;
+
+ if (self->fd == -1)
+ return err_closed();
+ if (!self->writable) {
+ _PyIO_State *state = get_io_state_by_cls(cls);
+ return err_mode(state, "writing");
+ }
+
+ handle = _Py_get_osfhandle(self->fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return NULL;
+
+ if (!b->len) {
+ return PyLong_FromLong(0);
+ }
+ if (b->len > BUFMAX)
+ len = BUFMAX;
+ else
+ len = (DWORD)b->len;
+
+ Py_BEGIN_ALLOW_THREADS
+ wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
+
+ /* issue11395 there is an unspecified upper bound on how many bytes
+ can be written at once. We cap at 32k - the caller will have to
+ handle partial writes.
+ Since we don't know how many input bytes are being ignored, we
+ have to reduce and recalculate. */
+ while (wlen > 32766 / sizeof(wchar_t)) {
+ len /= 2;
+ /* Fix for github issues gh-110913 and gh-82052. */
+ len = _find_last_utf8_boundary(b->buf, len);
+ wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
+ }
+ Py_END_ALLOW_THREADS
+
+ if (!wlen)
+ return PyErr_SetFromWindowsErr(0);
+
+ wbuf = (wchar_t*)PyMem_Malloc(wlen * sizeof(wchar_t));
+
+ Py_BEGIN_ALLOW_THREADS
+ wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, wbuf, wlen);
+ if (wlen) {
+ res = WriteConsoleW(handle, wbuf, wlen, &n, NULL);
+ if (res && n < wlen) {
+ /* Wrote fewer characters than expected, which means our
+ * len value may be wrong. So recalculate it from the
+ * characters that were written. As this could potentially
+ * result in a different value, we also validate that value.
+ */
+ len = WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
+ NULL, 0, NULL, NULL);
+ if (len) {
+ wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len,
+ NULL, 0);
+ assert(wlen == len);
+ }
+ }
+ } else
+ res = 0;
+ Py_END_ALLOW_THREADS
+
+ if (!res) {
+ DWORD err = GetLastError();
+ PyMem_Free(wbuf);
+ return PyErr_SetFromWindowsErr(err);
+ }
+
+ PyMem_Free(wbuf);
+ return PyLong_FromSsize_t(len);
+}
+
+static PyObject *
+winconsoleio_repr(winconsoleio *self)
+{
+ if (self->fd == -1)
+ return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>");
+
+ if (self->readable)
+ return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>",
+ self->closefd ? "True" : "False");
+ if (self->writable)
+ return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>",
+ self->closefd ? "True" : "False");
+
+ PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");
+ return NULL;
+}
+
+/*[clinic input]
+_io._WindowsConsoleIO.isatty
+
+Always True.
+[clinic start generated code]*/
+
+static PyObject *
+_io__WindowsConsoleIO_isatty_impl(winconsoleio *self)
+/*[clinic end generated code: output=9eac09d287c11bd7 input=9b91591dbe356f86]*/
+{
+ if (self->fd == -1)
+ return err_closed();
+
+ Py_RETURN_TRUE;
+}
+
+#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
+#include "clinic/winconsoleio.c.h"
+#undef clinic_state
+
+static PyMethodDef winconsoleio_methods[] = {
+ _IO__WINDOWSCONSOLEIO_READ_METHODDEF
+ _IO__WINDOWSCONSOLEIO_READALL_METHODDEF
+ _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF
+ _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF
+ _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF
+ _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF
+ _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
+ _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
+ _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
+ {NULL, NULL} /* sentinel */
+};
+
+/* 'closed' and 'mode' are attributes for compatibility with FileIO. */
+
+static PyObject *
+get_closed(winconsoleio *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->fd == -1));
+}
+
+static PyObject *
+get_closefd(winconsoleio *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->closefd));
+}
+
+static PyObject *
+get_mode(winconsoleio *self, void *closure)
+{
+ return PyUnicode_FromString(self->readable ? "rb" : "wb");
+}
+
+static PyGetSetDef winconsoleio_getsetlist[] = {
+ {"closed", (getter)get_closed, NULL, "True if the file is closed"},
+ {"closefd", (getter)get_closefd, NULL,
+ "True if the file descriptor will be closed by close()."},
+ {"mode", (getter)get_mode, NULL, "String giving the file mode"},
+ {NULL},
+};
+
+static PyMemberDef winconsoleio_members[] = {
+ {"_blksize", T_UINT, offsetof(winconsoleio, blksize), 0},
+ {"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), 0},
+ {"__weaklistoffset__", T_PYSSIZET, offsetof(winconsoleio, weakreflist), READONLY},
+ {"__dictoffset__", T_PYSSIZET, offsetof(winconsoleio, dict), READONLY},
+ {NULL}
+};
+
+static PyType_Slot winconsoleio_slots[] = {
+ {Py_tp_dealloc, winconsoleio_dealloc},
+ {Py_tp_repr, winconsoleio_repr},
+ {Py_tp_getattro, PyObject_GenericGetAttr},
+ {Py_tp_doc, (void *)_io__WindowsConsoleIO___init____doc__},
+ {Py_tp_traverse, winconsoleio_traverse},
+ {Py_tp_clear, winconsoleio_clear},
+ {Py_tp_methods, winconsoleio_methods},
+ {Py_tp_members, winconsoleio_members},
+ {Py_tp_getset, winconsoleio_getsetlist},
+ {Py_tp_init, _io__WindowsConsoleIO___init__},
+ {Py_tp_new, winconsoleio_new},
+ {0, NULL},
+};
+
+PyType_Spec winconsoleio_spec = {
+ .name = "_io._WindowsConsoleIO",
+ .basicsize = sizeof(winconsoleio),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = winconsoleio_slots,
+};
+
+#endif /* HAVE_WINDOWS_CONSOLE_IO */