diff options
Diffstat (limited to 'contrib/tools/python3/src/Modules/_io')
17 files changed, 14889 insertions, 14889 deletions
diff --git a/contrib/tools/python3/src/Modules/_io/_iomodule.c b/contrib/tools/python3/src/Modules/_io/_iomodule.c index d7cadacea1b..f81f7a8f78f 100644 --- a/contrib/tools/python3/src/Modules/_io/_iomodule.c +++ b/contrib/tools/python3/src/Modules/_io/_iomodule.c @@ -1,367 +1,367 @@ -/* - 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" - -#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 - -/* Various interned strings */ - -PyObject *_PyIO_str_close = NULL; -PyObject *_PyIO_str_closed = NULL; -PyObject *_PyIO_str_decode = NULL; -PyObject *_PyIO_str_encode = NULL; -PyObject *_PyIO_str_fileno = NULL; -PyObject *_PyIO_str_flush = NULL; -PyObject *_PyIO_str_getstate = NULL; -PyObject *_PyIO_str_isatty = NULL; -PyObject *_PyIO_str_newlines = NULL; -PyObject *_PyIO_str_nl = NULL; -PyObject *_PyIO_str_peek = NULL; -PyObject *_PyIO_str_read = NULL; -PyObject *_PyIO_str_read1 = NULL; -PyObject *_PyIO_str_readable = NULL; -PyObject *_PyIO_str_readall = NULL; -PyObject *_PyIO_str_readinto = NULL; -PyObject *_PyIO_str_readline = NULL; -PyObject *_PyIO_str_reset = NULL; -PyObject *_PyIO_str_seek = NULL; -PyObject *_PyIO_str_seekable = NULL; -PyObject *_PyIO_str_setstate = NULL; -PyObject *_PyIO_str_tell = NULL; -PyObject *_PyIO_str_truncate = NULL; -PyObject *_PyIO_str_writable = NULL; -PyObject *_PyIO_str_write = NULL; - -PyObject *_PyIO_empty_str = NULL; -PyObject *_PyIO_empty_bytes = NULL; - -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 +/* + 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" + +#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 + +/* Various interned strings */ + +PyObject *_PyIO_str_close = NULL; +PyObject *_PyIO_str_closed = NULL; +PyObject *_PyIO_str_decode = NULL; +PyObject *_PyIO_str_encode = NULL; +PyObject *_PyIO_str_fileno = NULL; +PyObject *_PyIO_str_flush = NULL; +PyObject *_PyIO_str_getstate = NULL; +PyObject *_PyIO_str_isatty = NULL; +PyObject *_PyIO_str_newlines = NULL; +PyObject *_PyIO_str_nl = NULL; +PyObject *_PyIO_str_peek = NULL; +PyObject *_PyIO_str_read = NULL; +PyObject *_PyIO_str_read1 = NULL; +PyObject *_PyIO_str_readable = NULL; +PyObject *_PyIO_str_readall = NULL; +PyObject *_PyIO_str_readinto = NULL; +PyObject *_PyIO_str_readline = NULL; +PyObject *_PyIO_str_reset = NULL; +PyObject *_PyIO_str_seek = NULL; +PyObject *_PyIO_str_seekable = NULL; +PyObject *_PyIO_str_setstate = NULL; +PyObject *_PyIO_str_tell = NULL; +PyObject *_PyIO_str_truncate = NULL; +PyObject *_PyIO_str_writable = NULL; +PyObject *_PyIO_str_write = NULL; + +PyObject *_PyIO_empty_str = NULL; +PyObject *_PyIO_empty_bytes = NULL; + +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(accept={int}) = 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.getpreferredencoding(False) 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) -'U' universal newline mode (deprecated) -========= =============================================================== - -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. - -'U' mode is deprecated and will raise an exception in future versions -of Python. It has no effect in Python 3. Use newline to control -universal newlines mode. - -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) + closefd: bool(accept={int}) = 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.getpreferredencoding(False) 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) +'U' universal newline mode (deprecated) +========= =============================================================== + +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. + +'U' mode is deprecated and will raise an exception in future versions +of Python. It has no effect in Python 3. Use newline to control +universal newlines mode. + +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=7295902222e6b311]*/ -{ - unsigned i; - - int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0; - int text = 0, binary = 0, universal = 0; - - char rawmode[6], *m; - int line_buffering, is_number; +{ + unsigned i; + + int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0; + int text = 0, binary = 0, universal = 0; + + char rawmode[6], *m; + int line_buffering, is_number; long isatty = 0; - - PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL; - - _Py_IDENTIFIER(_blksize); - _Py_IDENTIFIER(isatty); - _Py_IDENTIFIER(mode); - _Py_IDENTIFIER(close); - - is_number = PyNumber_Check(file); - - if (is_number) { - path_or_fd = file; - Py_INCREF(path_or_fd); - } 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; - case 'U': - universal = 1; - reading = 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 (universal) { - if (creating || writing || appending || updating) { - PyErr_SetString(PyExc_ValueError, + + PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL; + + _Py_IDENTIFIER(_blksize); + _Py_IDENTIFIER(isatty); + _Py_IDENTIFIER(mode); + _Py_IDENTIFIER(close); + + is_number = PyNumber_Check(file); + + if (is_number) { + path_or_fd = file; + Py_INCREF(path_or_fd); + } 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; + case 'U': + universal = 1; + reading = 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 (universal) { + if (creating || writing || appending || updating) { + PyErr_SetString(PyExc_ValueError, "mode U cannot be combined with 'x', 'w', 'a', or '+'"); - goto error; - } - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "'U' mode is deprecated", 1) < 0) - goto error; - reading = 1; - } - - 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; - } - + goto error; + } + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "'U' mode is deprecated", 1) < 0) + goto error; + reading = 1; + } + + 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 " @@ -371,138 +371,138 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, } } - /* Create the Raw file stream */ - { - PyObject *RawIO_class = (PyObject *)&PyFileIO_Type; -#ifdef MS_WINDOWS + /* Create the Raw file stream */ + { + PyObject *RawIO_class = (PyObject *)&PyFileIO_Type; +#ifdef MS_WINDOWS const PyConfig *config = _Py_GetConfig(); if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') { - RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type; - encoding = "utf-8"; - } -#endif + RawIO_class = (PyObject *)&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_DECREF(path_or_fd); - path_or_fd = NULL; - - modeobj = PyUnicode_FromString(mode); - if (modeobj == NULL) - goto error; - - /* buffering */ + } + + if (raw == NULL) + goto error; + result = raw; + + Py_DECREF(path_or_fd); + path_or_fd = NULL; + + modeobj = PyUnicode_FromString(mode); + if (modeobj == NULL) + goto error; + + /* buffering */ if (buffering < 0) { PyObject *res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty); - if (res == NULL) - goto error; - isatty = PyLong_AsLong(res); - Py_DECREF(res); - if (isatty == -1 && PyErr_Occurred()) - goto error; - } - + if (res == NULL) + goto error; + isatty = PyLong_AsLong(res); + Py_DECREF(res); + if (isatty == -1 && PyErr_Occurred()) + goto error; + } + if (buffering == 1 || isatty) { - buffering = -1; - line_buffering = 1; - } - else - line_buffering = 0; - - if (buffering < 0) { - PyObject *blksize_obj; - blksize_obj = _PyObject_GetAttrId(raw, &PyId__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 *)&PyBufferedRandom_Type; - else if (creating || writing || appending) - Buffered_class = (PyObject *)&PyBufferedWriter_Type; - else if (reading) - Buffered_class = (PyObject *)&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 *)&PyTextIOWrapper_Type, + buffering = -1; + line_buffering = 1; + } + else + line_buffering = 0; + + if (buffering < 0) { + PyObject *blksize_obj; + blksize_obj = _PyObject_GetAttrId(raw, &PyId__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 *)&PyBufferedRandom_Type; + else if (creating || writing || appending) + Buffered_class = (PyObject *)&PyBufferedWriter_Type; + else if (reading) + Buffered_class = (PyObject *)&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 *)&PyTextIOWrapper_Type, "OsssO", - buffer, - encoding, errors, newline, + buffer, + encoding, errors, newline, line_buffering ? Py_True : Py_False); - if (wrapper == NULL) - goto error; - result = wrapper; - Py_DECREF(buffer); - - if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0) - goto error; - Py_DECREF(modeobj); - return result; - - error: - if (result != NULL) { - PyObject *exc, *val, *tb, *close_result; - PyErr_Fetch(&exc, &val, &tb); + if (wrapper == NULL) + goto error; + result = wrapper; + Py_DECREF(buffer); + + if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0) + goto error; + Py_DECREF(modeobj); + return result; + + error: + if (result != NULL) { + PyObject *exc, *val, *tb, *close_result; + PyErr_Fetch(&exc, &val, &tb); close_result = _PyObject_CallMethodIdNoArgs(result, &PyId_close); - _PyErr_ChainExceptions(exc, val, tb); - Py_XDECREF(close_result); - Py_DECREF(result); - } - Py_XDECREF(path_or_fd); - Py_XDECREF(modeobj); - return NULL; -} + _PyErr_ChainExceptions(exc, val, tb); + Py_XDECREF(close_result); + Py_DECREF(result); + } + Py_XDECREF(path_or_fd); + Py_XDECREF(modeobj); + return NULL; +} /*[clinic input] _io.open_code @@ -522,55 +522,55 @@ _io_open_code_impl(PyObject *module, PyObject *path) { 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", + +/* + * 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; -} - + } + + finish: + Py_DECREF(value); + return result; +} + static inline _PyIO_State* get_io_state(PyObject *module) { @@ -578,236 +578,236 @@ get_io_state(PyObject *module) assert(state != NULL); return (_PyIO_State *)state; } - -_PyIO_State * -_PyIO_get_module_state(void) -{ - PyObject *mod = PyState_FindModule(&_PyIO_Module); - _PyIO_State *state; + +_PyIO_State * +_PyIO_get_module_state(void) +{ + PyObject *mod = PyState_FindModule(&_PyIO_Module); + _PyIO_State *state; if (mod == NULL || (state = get_io_state(mod)) == NULL) { - PyErr_SetString(PyExc_RuntimeError, - "could not find io module state " - "(interpreter shutdown?)"); - return NULL; - } - return state; -} - -PyObject * -_PyIO_get_locale_module(_PyIO_State *state) -{ - PyObject *mod; - if (state->locale_module != NULL) { - assert(PyWeakref_CheckRef(state->locale_module)); - mod = PyWeakref_GET_OBJECT(state->locale_module); - if (mod != Py_None) { - Py_INCREF(mod); - return mod; - } - Py_CLEAR(state->locale_module); - } - mod = PyImport_ImportModule("_bootlocale"); - if (mod == NULL) - return NULL; - state->locale_module = PyWeakref_NewRef(mod, NULL); - if (state->locale_module == NULL) { - Py_DECREF(mod); - return NULL; - } - return mod; -} - - -static int -iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { + PyErr_SetString(PyExc_RuntimeError, + "could not find io module state " + "(interpreter shutdown?)"); + return NULL; + } + return state; +} + +PyObject * +_PyIO_get_locale_module(_PyIO_State *state) +{ + PyObject *mod; + if (state->locale_module != NULL) { + assert(PyWeakref_CheckRef(state->locale_module)); + mod = PyWeakref_GET_OBJECT(state->locale_module); + if (mod != Py_None) { + Py_INCREF(mod); + return mod; + } + Py_CLEAR(state->locale_module); + } + mod = PyImport_ImportModule("_bootlocale"); + if (mod == NULL) + return NULL; + state->locale_module = PyWeakref_NewRef(mod, NULL); + if (state->locale_module == NULL) { + Py_DECREF(mod); + return NULL; + } + return mod; +} + + +static int +iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { _PyIO_State *state = get_io_state(mod); - if (!state->initialized) - return 0; - if (state->locale_module != NULL) { - Py_VISIT(state->locale_module); - } - Py_VISIT(state->unsupported_operation); - return 0; -} - - -static int -iomodule_clear(PyObject *mod) { + if (!state->initialized) + return 0; + if (state->locale_module != NULL) { + Py_VISIT(state->locale_module); + } + Py_VISIT(state->unsupported_operation); + return 0; +} + + +static int +iomodule_clear(PyObject *mod) { _PyIO_State *state = get_io_state(mod); - if (!state->initialized) - return 0; - if (state->locale_module != NULL) - Py_CLEAR(state->locale_module); - Py_CLEAR(state->unsupported_operation); - return 0; -} - -static void -iomodule_free(PyObject *mod) { - iomodule_clear(mod); -} - - -/* - * Module definition - */ - -#include "clinic/_iomodule.c.h" - -static PyMethodDef module_methods[] = { - _IO_OPEN_METHODDEF + if (!state->initialized) + return 0; + if (state->locale_module != NULL) + Py_CLEAR(state->locale_module); + Py_CLEAR(state->unsupported_operation); + return 0; +} + +static void +iomodule_free(PyObject *mod) { + iomodule_clear(mod); +} + + +/* + * Module definition + */ + +#include "clinic/_iomodule.c.h" + +static PyMethodDef module_methods[] = { + _IO_OPEN_METHODDEF _IO_OPEN_CODE_METHODDEF - {NULL, NULL} -}; - -struct PyModuleDef _PyIO_Module = { - PyModuleDef_HEAD_INIT, - "io", - module_doc, - sizeof(_PyIO_State), - module_methods, - NULL, - iomodule_traverse, - iomodule_clear, - (freefunc)iomodule_free, -}; - -PyMODINIT_FUNC -PyInit__io(void) -{ - PyObject *m = PyModule_Create(&_PyIO_Module); - _PyIO_State *state = NULL; - if (m == NULL) - return NULL; + {NULL, NULL} +}; + +struct PyModuleDef _PyIO_Module = { + PyModuleDef_HEAD_INIT, + "io", + module_doc, + sizeof(_PyIO_State), + module_methods, + NULL, + iomodule_traverse, + iomodule_clear, + (freefunc)iomodule_free, +}; + +PyMODINIT_FUNC +PyInit__io(void) +{ + PyObject *m = PyModule_Create(&_PyIO_Module); + _PyIO_State *state = NULL; + if (m == NULL) + return NULL; state = get_io_state(m); - state->initialized = 0; - + state->initialized = 0; + #define ADD_TYPE(type) \ if (PyModule_AddType(m, type) < 0) { \ - goto fail; \ - } - - /* DEFAULT_BUFFER_SIZE */ - if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0) - goto fail; - - /* 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) - goto fail; - Py_INCREF(state->unsupported_operation); - if (PyModule_AddObject(m, "UnsupportedOperation", - state->unsupported_operation) < 0) - goto fail; - - /* BlockingIOError, for compatibility */ - Py_INCREF(PyExc_BlockingIOError); - if (PyModule_AddObject(m, "BlockingIOError", - (PyObject *) PyExc_BlockingIOError) < 0) - goto fail; - - /* Concrete base types of the IO ABCs. - (the ABCs themselves are declared through inheritance in io.py) - */ + goto fail; \ + } + + /* DEFAULT_BUFFER_SIZE */ + if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0) + goto fail; + + /* 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) + goto fail; + Py_INCREF(state->unsupported_operation); + if (PyModule_AddObject(m, "UnsupportedOperation", + state->unsupported_operation) < 0) + goto fail; + + /* BlockingIOError, for compatibility */ + Py_INCREF(PyExc_BlockingIOError); + if (PyModule_AddObject(m, "BlockingIOError", + (PyObject *) PyExc_BlockingIOError) < 0) + goto fail; + + /* Concrete base types of the IO ABCs. + (the ABCs themselves are declared through inheritance in io.py) + */ ADD_TYPE(&PyIOBase_Type); ADD_TYPE(&PyRawIOBase_Type); ADD_TYPE(&PyBufferedIOBase_Type); ADD_TYPE(&PyTextIOBase_Type); - - /* Implementation of concrete IO objects. */ - /* FileIO */ - PyFileIO_Type.tp_base = &PyRawIOBase_Type; + + /* Implementation of concrete IO objects. */ + /* FileIO */ + PyFileIO_Type.tp_base = &PyRawIOBase_Type; ADD_TYPE(&PyFileIO_Type); - - /* BytesIO */ - PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; + + /* BytesIO */ + PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; ADD_TYPE(&PyBytesIO_Type); - if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) - goto fail; - - /* StringIO */ - PyStringIO_Type.tp_base = &PyTextIOBase_Type; + if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) + goto fail; + + /* StringIO */ + PyStringIO_Type.tp_base = &PyTextIOBase_Type; ADD_TYPE(&PyStringIO_Type); - -#ifdef MS_WINDOWS - /* WindowsConsoleIO */ - PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; + +#ifdef MS_WINDOWS + /* WindowsConsoleIO */ + PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; ADD_TYPE(&PyWindowsConsoleIO_Type); -#endif - - /* BufferedReader */ - PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; +#endif + + /* BufferedReader */ + PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; ADD_TYPE(&PyBufferedReader_Type); - - /* BufferedWriter */ - PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; + + /* BufferedWriter */ + PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; ADD_TYPE(&PyBufferedWriter_Type); - - /* BufferedRWPair */ - PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; + + /* BufferedRWPair */ + PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; ADD_TYPE(&PyBufferedRWPair_Type); - - /* BufferedRandom */ - PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; + + /* BufferedRandom */ + PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; ADD_TYPE(&PyBufferedRandom_Type); - - /* TextIOWrapper */ - PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; + + /* TextIOWrapper */ + PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; ADD_TYPE(&PyTextIOWrapper_Type); - - /* IncrementalNewlineDecoder */ + + /* IncrementalNewlineDecoder */ ADD_TYPE(&PyIncrementalNewlineDecoder_Type); - - /* Interned strings */ -#define ADD_INTERNED(name) \ - if (!_PyIO_str_ ## name && \ - !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \ - goto fail; - - ADD_INTERNED(close) - ADD_INTERNED(closed) - ADD_INTERNED(decode) - ADD_INTERNED(encode) - ADD_INTERNED(fileno) - ADD_INTERNED(flush) - ADD_INTERNED(getstate) - ADD_INTERNED(isatty) - ADD_INTERNED(newlines) - ADD_INTERNED(peek) - ADD_INTERNED(read) - ADD_INTERNED(read1) - ADD_INTERNED(readable) - ADD_INTERNED(readall) - ADD_INTERNED(readinto) - ADD_INTERNED(readline) - ADD_INTERNED(reset) - ADD_INTERNED(seek) - ADD_INTERNED(seekable) - ADD_INTERNED(setstate) - ADD_INTERNED(tell) - ADD_INTERNED(truncate) - ADD_INTERNED(write) - ADD_INTERNED(writable) - - if (!_PyIO_str_nl && - !(_PyIO_str_nl = PyUnicode_InternFromString("\n"))) - goto fail; - - if (!_PyIO_empty_str && - !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0))) - goto fail; - if (!_PyIO_empty_bytes && - !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) - goto fail; - - state->initialized = 1; - - return m; - - fail: - Py_XDECREF(state->unsupported_operation); - Py_DECREF(m); - return NULL; -} + + /* Interned strings */ +#define ADD_INTERNED(name) \ + if (!_PyIO_str_ ## name && \ + !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \ + goto fail; + + ADD_INTERNED(close) + ADD_INTERNED(closed) + ADD_INTERNED(decode) + ADD_INTERNED(encode) + ADD_INTERNED(fileno) + ADD_INTERNED(flush) + ADD_INTERNED(getstate) + ADD_INTERNED(isatty) + ADD_INTERNED(newlines) + ADD_INTERNED(peek) + ADD_INTERNED(read) + ADD_INTERNED(read1) + ADD_INTERNED(readable) + ADD_INTERNED(readall) + ADD_INTERNED(readinto) + ADD_INTERNED(readline) + ADD_INTERNED(reset) + ADD_INTERNED(seek) + ADD_INTERNED(seekable) + ADD_INTERNED(setstate) + ADD_INTERNED(tell) + ADD_INTERNED(truncate) + ADD_INTERNED(write) + ADD_INTERNED(writable) + + if (!_PyIO_str_nl && + !(_PyIO_str_nl = PyUnicode_InternFromString("\n"))) + goto fail; + + if (!_PyIO_empty_str && + !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0))) + goto fail; + if (!_PyIO_empty_bytes && + !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) + goto fail; + + state->initialized = 1; + + return m; + + fail: + Py_XDECREF(state->unsupported_operation); + Py_DECREF(m); + return NULL; +} diff --git a/contrib/tools/python3/src/Modules/_io/_iomodule.h b/contrib/tools/python3/src/Modules/_io/_iomodule.h index a8f3951e57f..947fbf5ac32 100644 --- a/contrib/tools/python3/src/Modules/_io/_iomodule.h +++ b/contrib/tools/python3/src/Modules/_io/_iomodule.h @@ -1,188 +1,188 @@ -/* - * Declarations shared between the different parts of the io module - */ - +/* + * Declarations shared between the different parts of the io module + */ + #include "exports.h" -/* ABCs */ -extern PyTypeObject PyIOBase_Type; -extern PyTypeObject PyRawIOBase_Type; -extern PyTypeObject PyBufferedIOBase_Type; -extern PyTypeObject PyTextIOBase_Type; - -/* Concrete classes */ -extern PyTypeObject PyFileIO_Type; -extern PyTypeObject PyBytesIO_Type; -extern PyTypeObject PyStringIO_Type; -extern PyTypeObject PyBufferedReader_Type; -extern PyTypeObject PyBufferedWriter_Type; -extern PyTypeObject PyBufferedRWPair_Type; -extern PyTypeObject PyBufferedRandom_Type; -extern PyTypeObject PyTextIOWrapper_Type; -extern PyTypeObject PyIncrementalNewlineDecoder_Type; - -#ifndef Py_LIMITED_API -#ifdef MS_WINDOWS -extern PyTypeObject PyWindowsConsoleIO_Type; -PyAPI_DATA(PyObject *) _PyWindowsConsoleIO_Type; -#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), (PyTypeObject*)_PyWindowsConsoleIO_Type)) -#endif /* MS_WINDOWS */ -#endif /* Py_LIMITED_API */ - -/* 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. - */ -extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args); -extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args); -extern PyObject* _PyIOBase_check_seekable(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; - -typedef struct { - int initialized; - PyObject *locale_module; - - PyObject *unsupported_operation; -} _PyIO_State; - -#define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod)) -#define IO_STATE() _PyIO_get_module_state() - -extern _PyIO_State *_PyIO_get_module_state(void); -extern PyObject *_PyIO_get_locale_module(_PyIO_State *); - -#ifdef MS_WINDOWS -extern char _PyIO_get_console_type(PyObject *); -#endif - -extern PyObject *_PyIO_str_close; -extern PyObject *_PyIO_str_closed; -extern PyObject *_PyIO_str_decode; -extern PyObject *_PyIO_str_encode; -extern PyObject *_PyIO_str_fileno; -extern PyObject *_PyIO_str_flush; -extern PyObject *_PyIO_str_getstate; -extern PyObject *_PyIO_str_isatty; -extern PyObject *_PyIO_str_newlines; -extern PyObject *_PyIO_str_nl; -extern PyObject *_PyIO_str_peek; -extern PyObject *_PyIO_str_read; -extern PyObject *_PyIO_str_read1; -extern PyObject *_PyIO_str_readable; -extern PyObject *_PyIO_str_readall; -extern PyObject *_PyIO_str_readinto; -extern PyObject *_PyIO_str_readline; -extern PyObject *_PyIO_str_reset; -extern PyObject *_PyIO_str_seek; -extern PyObject *_PyIO_str_seekable; -extern PyObject *_PyIO_str_setstate; -extern PyObject *_PyIO_str_tell; -extern PyObject *_PyIO_str_truncate; -extern PyObject *_PyIO_str_writable; -extern PyObject *_PyIO_str_write; - -extern PyObject *_PyIO_empty_str; -extern PyObject *_PyIO_empty_bytes; - +/* ABCs */ +extern PyTypeObject PyIOBase_Type; +extern PyTypeObject PyRawIOBase_Type; +extern PyTypeObject PyBufferedIOBase_Type; +extern PyTypeObject PyTextIOBase_Type; + +/* Concrete classes */ +extern PyTypeObject PyFileIO_Type; +extern PyTypeObject PyBytesIO_Type; +extern PyTypeObject PyStringIO_Type; +extern PyTypeObject PyBufferedReader_Type; +extern PyTypeObject PyBufferedWriter_Type; +extern PyTypeObject PyBufferedRWPair_Type; +extern PyTypeObject PyBufferedRandom_Type; +extern PyTypeObject PyTextIOWrapper_Type; +extern PyTypeObject PyIncrementalNewlineDecoder_Type; + +#ifndef Py_LIMITED_API +#ifdef MS_WINDOWS +extern PyTypeObject PyWindowsConsoleIO_Type; +PyAPI_DATA(PyObject *) _PyWindowsConsoleIO_Type; +#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), (PyTypeObject*)_PyWindowsConsoleIO_Type)) +#endif /* MS_WINDOWS */ +#endif /* Py_LIMITED_API */ + +/* 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. + */ +extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args); +extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args); +extern PyObject* _PyIOBase_check_seekable(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; + +typedef struct { + int initialized; + PyObject *locale_module; + + PyObject *unsupported_operation; +} _PyIO_State; + +#define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod)) +#define IO_STATE() _PyIO_get_module_state() + +extern _PyIO_State *_PyIO_get_module_state(void); +extern PyObject *_PyIO_get_locale_module(_PyIO_State *); + +#ifdef MS_WINDOWS +extern char _PyIO_get_console_type(PyObject *); +#endif + +extern PyObject *_PyIO_str_close; +extern PyObject *_PyIO_str_closed; +extern PyObject *_PyIO_str_decode; +extern PyObject *_PyIO_str_encode; +extern PyObject *_PyIO_str_fileno; +extern PyObject *_PyIO_str_flush; +extern PyObject *_PyIO_str_getstate; +extern PyObject *_PyIO_str_isatty; +extern PyObject *_PyIO_str_newlines; +extern PyObject *_PyIO_str_nl; +extern PyObject *_PyIO_str_peek; +extern PyObject *_PyIO_str_read; +extern PyObject *_PyIO_str_read1; +extern PyObject *_PyIO_str_readable; +extern PyObject *_PyIO_str_readall; +extern PyObject *_PyIO_str_readinto; +extern PyObject *_PyIO_str_readline; +extern PyObject *_PyIO_str_reset; +extern PyObject *_PyIO_str_seek; +extern PyObject *_PyIO_str_seekable; +extern PyObject *_PyIO_str_setstate; +extern PyObject *_PyIO_str_tell; +extern PyObject *_PyIO_str_truncate; +extern PyObject *_PyIO_str_writable; +extern PyObject *_PyIO_str_write; + +extern PyObject *_PyIO_empty_str; +extern PyObject *_PyIO_empty_bytes; + extern Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type; diff --git a/contrib/tools/python3/src/Modules/_io/bufferedio.c b/contrib/tools/python3/src/Modules/_io/bufferedio.c index b0fe9e45891..46da7b74c9a 100644 --- a/contrib/tools/python3/src/Modules/_io/bufferedio.c +++ b/contrib/tools/python3/src/Modules/_io/bufferedio.c @@ -1,1387 +1,1387 @@ -/* - 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" +/* + 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_object.h" #include "structmember.h" // PyMemberDef -#include "_iomodule.h" - -/*[clinic input] -module _io -class _io._BufferedIOBase "PyObject *" "&PyBufferedIOBase_Type" -class _io._Buffered "buffered *" "&PyBufferedIOBase_Type" -class _io.BufferedReader "buffered *" "&PyBufferedReader_Type" -class _io.BufferedWriter "buffered *" "&PyBufferedWriter_Type" -class _io.BufferedRWPair "rwpair *" "&PyBufferedRWPair_Type" -class _io.BufferedRandom "buffered *" "&PyBufferedRandom_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=59460b9c5639984d]*/ - -_Py_IDENTIFIER(close); -_Py_IDENTIFIER(_dealloc_warn); -_Py_IDENTIFIER(flush); -_Py_IDENTIFIER(isatty); -_Py_IDENTIFIER(mode); -_Py_IDENTIFIER(name); -_Py_IDENTIFIER(peek); -_Py_IDENTIFIER(read); -_Py_IDENTIFIER(read1); -_Py_IDENTIFIER(readable); -_Py_IDENTIFIER(readinto); -_Py_IDENTIFIER(readinto1); -_Py_IDENTIFIER(writable); -_Py_IDENTIFIER(write); - -/* - * 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; - - data = _PyObject_CallMethodId(self, - readinto1 ? &PyId_read1 : &PyId_read, - "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(const char *message) -{ - _PyIO_State *state = IO_STATE(); - if (state != NULL) - PyErr_SetString(state->unsupported_operation, message); - return NULL; -} - -/*[clinic input] -_io._BufferedIOBase.detach - -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) -/*[clinic end generated code: output=754977c8d10ed88c input=822427fb58fe4169]*/ -{ - return bufferediobase_unsupported("detach"); -} - -PyDoc_STRVAR(bufferediobase_read_doc, - "Read and return up to n bytes.\n" - "\n" - "If the argument is omitted, None, or negative, reads and\n" - "returns all data until EOF.\n" - "\n" - "If the 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). But for\n" - "interactive raw streams (as well as sockets and pipes), at most\n" - "one raw read will be issued, and a short result does not imply\n" - "that EOF is imminent.\n" - "\n" - "Returns an empty bytes object on EOF.\n" - "\n" - "Returns None if the underlying raw stream was open in non-blocking\n" - "mode and no data is available at the moment.\n"); - -static PyObject * -bufferediobase_read(PyObject *self, PyObject *args) -{ - return bufferediobase_unsupported("read"); -} - -PyDoc_STRVAR(bufferediobase_read1_doc, - "Read and return up to n bytes, with at most one read() call\n" - "to the underlying raw stream. A short result does not imply\n" - "that EOF is imminent.\n" - "\n" - "Returns an empty bytes object on EOF.\n"); - -static PyObject * -bufferediobase_read1(PyObject *self, PyObject *args) -{ - return bufferediobase_unsupported("read1"); -} - -PyDoc_STRVAR(bufferediobase_write_doc, - "Write the given buffer to the IO stream.\n" - "\n" - "Returns the number of bytes written, which is always the length of b\n" - "in bytes.\n" - "\n" - "Raises BlockingIOError if the buffer is full and the\n" - "underlying raw stream cannot accept more data at the moment.\n"); - -static PyObject * -bufferediobase_write(PyObject *self, PyObject *args) -{ - return bufferediobase_unsupported("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; - } - relax_locking = _Py_IsFinalizing(); - 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) { +#include "_iomodule.h" + +/*[clinic input] +module _io +class _io._BufferedIOBase "PyObject *" "&PyBufferedIOBase_Type" +class _io._Buffered "buffered *" "&PyBufferedIOBase_Type" +class _io.BufferedReader "buffered *" "&PyBufferedReader_Type" +class _io.BufferedWriter "buffered *" "&PyBufferedWriter_Type" +class _io.BufferedRWPair "rwpair *" "&PyBufferedRWPair_Type" +class _io.BufferedRandom "buffered *" "&PyBufferedRandom_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=59460b9c5639984d]*/ + +_Py_IDENTIFIER(close); +_Py_IDENTIFIER(_dealloc_warn); +_Py_IDENTIFIER(flush); +_Py_IDENTIFIER(isatty); +_Py_IDENTIFIER(mode); +_Py_IDENTIFIER(name); +_Py_IDENTIFIER(peek); +_Py_IDENTIFIER(read); +_Py_IDENTIFIER(read1); +_Py_IDENTIFIER(readable); +_Py_IDENTIFIER(readinto); +_Py_IDENTIFIER(readinto1); +_Py_IDENTIFIER(writable); +_Py_IDENTIFIER(write); + +/* + * 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; + + data = _PyObject_CallMethodId(self, + readinto1 ? &PyId_read1 : &PyId_read, + "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(const char *message) +{ + _PyIO_State *state = IO_STATE(); + if (state != NULL) + PyErr_SetString(state->unsupported_operation, message); + return NULL; +} + +/*[clinic input] +_io._BufferedIOBase.detach + +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) +/*[clinic end generated code: output=754977c8d10ed88c input=822427fb58fe4169]*/ +{ + return bufferediobase_unsupported("detach"); +} + +PyDoc_STRVAR(bufferediobase_read_doc, + "Read and return up to n bytes.\n" + "\n" + "If the argument is omitted, None, or negative, reads and\n" + "returns all data until EOF.\n" + "\n" + "If the 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). But for\n" + "interactive raw streams (as well as sockets and pipes), at most\n" + "one raw read will be issued, and a short result does not imply\n" + "that EOF is imminent.\n" + "\n" + "Returns an empty bytes object on EOF.\n" + "\n" + "Returns None if the underlying raw stream was open in non-blocking\n" + "mode and no data is available at the moment.\n"); + +static PyObject * +bufferediobase_read(PyObject *self, PyObject *args) +{ + return bufferediobase_unsupported("read"); +} + +PyDoc_STRVAR(bufferediobase_read1_doc, + "Read and return up to n bytes, with at most one read() call\n" + "to the underlying raw stream. A short result does not imply\n" + "that EOF is imminent.\n" + "\n" + "Returns an empty bytes object on EOF.\n"); + +static PyObject * +bufferediobase_read1(PyObject *self, PyObject *args) +{ + return bufferediobase_unsupported("read1"); +} + +PyDoc_STRVAR(bufferediobase_write_doc, + "Write the given buffer to the IO stream.\n" + "\n" + "Returns the number of bytes written, which is always the length of b\n" + "in bytes.\n" + "\n" + "Raises BlockingIOError if the buffer is full and the\n" + "underlying raw stream cannot accept more data at the moment.\n"); + +static PyObject * +bufferediobase_write(PyObject *self, PyObject *args) +{ + return bufferediobase_unsupported("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; + } + relax_locking = _Py_IsFinalizing(); + 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", + "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) \ + } + 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; \ + 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 void -buffered_dealloc(buffered *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); - Py_CLEAR(self->raw); - if (self->buffer) { - PyMem_Free(self->buffer); - self->buffer = NULL; - } - if (self->lock) { - PyThread_free_lock(self->lock); - self->lock = NULL; - } - Py_CLEAR(self->dict); - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static PyObject * + +#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 void +buffered_dealloc(buffered *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); + Py_CLEAR(self->raw); + if (self->buffer) { + PyMem_Free(self->buffer); + self->buffer = NULL; + } + if (self->lock) { + PyThread_free_lock(self->lock); + self->lock = NULL; + } + Py_CLEAR(self->dict); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyObject * buffered_sizeof(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - Py_ssize_t res; - - res = _PyObject_SIZE(Py_TYPE(self)); - if (self->buffer) - res += self->buffer_size; - return PyLong_FromSsize_t(res); -} - -static int -buffered_traverse(buffered *self, visitproc visit, void *arg) -{ - Py_VISIT(self->raw); - Py_VISIT(self->dict); - return 0; -} - -static int -buffered_clear(buffered *self) -{ - self->ok = 0; - Py_CLEAR(self->raw); - Py_CLEAR(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). */ -static PyObject * -buffered_dealloc_warn(buffered *self, PyObject *source) -{ - if (self->ok && self->raw) { - PyObject *r; +{ + Py_ssize_t res; + + res = _PyObject_SIZE(Py_TYPE(self)); + if (self->buffer) + res += self->buffer_size; + return PyLong_FromSsize_t(res); +} + +static int +buffered_traverse(buffered *self, visitproc visit, void *arg) +{ + Py_VISIT(self->raw); + Py_VISIT(self->dict); + return 0; +} + +static int +buffered_clear(buffered *self) +{ + self->ok = 0; + Py_CLEAR(self->raw); + Py_CLEAR(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). */ +static PyObject * +buffered_dealloc_warn(buffered *self, PyObject *source) +{ + if (self->ok && self->raw) { + PyObject *r; r = _PyObject_CallMethodIdOneArg(self->raw, &PyId__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 */ - -static PyObject * -buffered_simple_flush(buffered *self, PyObject *args) -{ - CHECK_INITIALIZED(self) + 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 */ + +static PyObject * +buffered_simple_flush(buffered *self, PyObject *args) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_flush); -} - -static int -buffered_closed(buffered *self) -{ - int closed; - PyObject *res; - CHECK_INITIALIZED_INT(self) - res = PyObject_GetAttr(self->raw, _PyIO_str_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, _PyIO_str_closed); -} - -static PyObject * -buffered_close(buffered *self, PyObject *args) -{ - PyObject *res = NULL, *exc = NULL, *val, *tb; - 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_None; - Py_INCREF(res); - goto end; - } - - if (self->finalizing) { - PyObject *r = 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) +} + +static int +buffered_closed(buffered *self) +{ + int closed; + PyObject *res; + CHECK_INITIALIZED_INT(self) + res = PyObject_GetAttr(self->raw, _PyIO_str_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, _PyIO_str_closed); +} + +static PyObject * +buffered_close(buffered *self, PyObject *args) +{ + PyObject *res = NULL, *exc = NULL, *val, *tb; + 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_None; + Py_INCREF(res); + goto end; + } + + if (self->finalizing) { + PyObject *r = 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, _PyIO_str_flush); - if (!ENTER_BUFFERED(self)) - return NULL; - if (res == NULL) - PyErr_Fetch(&exc, &val, &tb); - else - Py_DECREF(res); - + if (!ENTER_BUFFERED(self)) + return NULL; + if (res == NULL) + PyErr_Fetch(&exc, &val, &tb); + else + Py_DECREF(res); + res = PyObject_CallMethodNoArgs(self->raw, _PyIO_str_close); - - if (self->buffer) { - PyMem_Free(self->buffer); - self->buffer = NULL; - } - - if (exc != NULL) { - _PyErr_ChainExceptions(exc, val, tb); - Py_CLEAR(res); - } - + + if (self->buffer) { + PyMem_Free(self->buffer); + self->buffer = NULL; + } + + if (exc != NULL) { + _PyErr_ChainExceptions(exc, val, tb); + Py_CLEAR(res); + } + self->read_end = 0; self->pos = 0; -end: - LEAVE_BUFFERED(self) - return res; -} - -/* detach */ - -static PyObject * +end: + LEAVE_BUFFERED(self) + return res; +} + +/* detach */ + +static PyObject * buffered_detach(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - PyObject *raw, *res; - CHECK_INITIALIZED(self) +{ + PyObject *raw, *res; + CHECK_INITIALIZED(self) res = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_flush); - if (res == NULL) - return NULL; - Py_DECREF(res); - raw = self->raw; - self->raw = NULL; - self->detached = 1; - self->ok = 0; - return raw; -} - -/* Inquiries */ - -static PyObject * + if (res == NULL) + return NULL; + Py_DECREF(res); + raw = self->raw; + self->raw = NULL; + self->detached = 1; + self->ok = 0; + return raw; +} + +/* Inquiries */ + +static PyObject * buffered_seekable(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - CHECK_INITIALIZED(self) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_seekable); -} - -static PyObject * +} + +static PyObject * buffered_readable(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - CHECK_INITIALIZED(self) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_readable); -} - -static PyObject * +} + +static PyObject * buffered_writable(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - CHECK_INITIALIZED(self) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_writable); -} - -static PyObject * -buffered_name_get(buffered *self, void *context) -{ - CHECK_INITIALIZED(self) - return _PyObject_GetAttrId(self->raw, &PyId_name); -} - -static PyObject * -buffered_mode_get(buffered *self, void *context) -{ - CHECK_INITIALIZED(self) - return _PyObject_GetAttrId(self->raw, &PyId_mode); -} - -/* Lower-level APIs */ - -static PyObject * +} + +static PyObject * +buffered_name_get(buffered *self, void *context) +{ + CHECK_INITIALIZED(self) + return _PyObject_GetAttrId(self->raw, &PyId_name); +} + +static PyObject * +buffered_mode_get(buffered *self, void *context) +{ + CHECK_INITIALIZED(self) + return _PyObject_GetAttrId(self->raw, &PyId_mode); +} + +/* Lower-level APIs */ + +static PyObject * buffered_fileno(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - CHECK_INITIALIZED(self) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_fileno); -} - -static PyObject * +} + +static PyObject * buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - CHECK_INITIALIZED(self) +{ + CHECK_INITIALIZED(self) return PyObject_CallMethodNoArgs(self->raw, _PyIO_str_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 *t, *v, *tb; - PyOSErrorObject *err; - - PyErr_Fetch(&t, &v, &tb); - if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) { - PyErr_Restore(t, v, tb); - return NULL; - } - err = (PyOSErrorObject *) v; - /* TODO: sanity check (err->written >= 0) */ - PyErr_Restore(t, v, tb); - return &err->written; -} - -static Py_off_t -_buffered_raw_tell(buffered *self) -{ - Py_off_t n; - PyObject *res; +} + +/* 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 *t, *v, *tb; + PyOSErrorObject *err; + + PyErr_Fetch(&t, &v, &tb); + if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) { + PyErr_Restore(t, v, tb); + return NULL; + } + err = (PyOSErrorObject *) v; + /* TODO: sanity check (err->written >= 0) */ + PyErr_Restore(t, v, tb); + return &err->written; +} + +static Py_off_t +_buffered_raw_tell(buffered *self) +{ + Py_off_t n; + PyObject *res; res = PyObject_CallMethodNoArgs(self->raw, _PyIO_str_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, _PyIO_str_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) -{ - static PyObject *eintr_int = NULL; - PyObject *typ, *val, *tb; - PyOSErrorObject *env_err; - - if (eintr_int == NULL) { - eintr_int = PyLong_FromLong(EINTR); - assert(eintr_int != NULL); - } - if (!PyErr_ExceptionMatches(PyExc_OSError)) - return 0; - PyErr_Fetch(&typ, &val, &tb); - PyErr_NormalizeException(&typ, &val, &tb); - env_err = (PyOSErrorObject *) val; - assert(env_err != NULL); - if (env_err->myerrno != NULL && - PyObject_RichCompareBool(env_err->myerrno, eintr_int, Py_EQ) > 0) { - Py_DECREF(typ); - Py_DECREF(val); - Py_XDECREF(tb); - return 1; - } - /* This silences any error set by PyObject_RichCompareBool() */ - PyErr_Restore(typ, val, tb); - 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; -} - -static PyObject * -buffered_flush(buffered *self, PyObject *args) -{ - 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) + 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, _PyIO_str_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) +{ + static PyObject *eintr_int = NULL; + PyObject *typ, *val, *tb; + PyOSErrorObject *env_err; + + if (eintr_int == NULL) { + eintr_int = PyLong_FromLong(EINTR); + assert(eintr_int != NULL); + } + if (!PyErr_ExceptionMatches(PyExc_OSError)) + return 0; + PyErr_Fetch(&typ, &val, &tb); + PyErr_NormalizeException(&typ, &val, &tb); + env_err = (PyOSErrorObject *) val; + assert(env_err != NULL); + if (env_err->myerrno != NULL && + PyObject_RichCompareBool(env_err->myerrno, eintr_int, Py_EQ) > 0) { + Py_DECREF(typ); + Py_DECREF(val); + Py_XDECREF(tb); + return 1; + } + /* This silences any error set by PyObject_RichCompareBool() */ + PyErr_Restore(typ, val, tb); + 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; +} + +static PyObject * +buffered_flush(buffered *self, PyObject *args) +{ + 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) { - Py_INCREF(Py_None); - res = 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, written = 0; - 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); - written += n; - 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); - written += n; - if (limit >= 0) - limit -= n; - } -found: - if (res != NULL && PyList_Append(chunks, res) < 0) { - Py_CLEAR(res); - goto end; - } - Py_XSETREF(res, _PyBytes_Join(_PyIO_empty_bytes, 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); -} - - -static PyObject * + + 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) { + Py_INCREF(Py_None); + res = 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, written = 0; + 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); + written += n; + 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); + written += n; + if (limit >= 0) + limit -= n; + } +found: + if (res != NULL && PyList_Append(chunks, res) < 0) { + Py_CLEAR(res); + goto end; + } + Py_XSETREF(res, _PyBytes_Join(_PyIO_empty_bytes, 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); +} + + +static PyObject * buffered_tell(buffered *self, PyObject *Py_UNUSED(ignored)) -{ - 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") - - if (_PyIOBase_check_seekable(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 - pos: object = None - / -[clinic start generated code]*/ - -static PyObject * -_io__Buffered_truncate_impl(buffered *self, PyObject *pos) -/*[clinic end generated code: output=667ca03c60c270de input=8a1be34d57cca2d3]*/ -{ - PyObject *res = NULL; - - CHECK_INITIALIZED(self) +{ + 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") + + if (_PyIOBase_check_seekable(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 + pos: object = None + / +[clinic start generated code]*/ + +static PyObject * +_io__Buffered_truncate_impl(buffered *self, PyObject *pos) +/*[clinic end generated code: output=667ca03c60c270de input=8a1be34d57cca2d3]*/ +{ + PyObject *res = NULL; + + CHECK_INITIALIZED(self) CHECK_CLOSED(self, "truncate of closed file") if (!self->writable) { return bufferediobase_unsupported("truncate"); } - if (!ENTER_BUFFERED(self)) - return NULL; - + 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, _PyIO_str_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); - - tp = Py_TYPE(self); - if (tp == &PyBufferedReader_Type || - tp == &PyBufferedRandom_Type) { - /* Skip method call overhead for speed */ - line = _buffered_readline(self, -1); - } - else { + 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); + + tp = Py_TYPE(self); + if (tp == &PyBufferedReader_Type || + tp == &PyBufferedRandom_Type) { + /* Skip method call overhead for speed */ + line = _buffered_readline(self, -1); + } + else { line = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_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 (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_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { if (!PyErr_ExceptionMatches(PyExc_ValueError)) { return NULL; @@ -1389,1340 +1389,1340 @@ buffered_repr(buffered *self) /* 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; - - if (_PyIOBase_check_readable(raw, Py_True) == NULL) - return -1; - - Py_INCREF(raw); - Py_XSETREF(self->raw, raw); - self->buffer_size = buffer_size; - self->readable = 1; - self->writable = 0; - - if (_buffered_init(self) < 0) - return -1; - _bufferedreader_reset_buf(self); - + 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; + + if (_PyIOBase_check_readable(raw, Py_True) == NULL) + return -1; + + Py_INCREF(raw); + Py_XSETREF(self->raw, 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, &PyBufferedReader_Type) && Py_IS_TYPE(raw, &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 { + + 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, _PyIO_str_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 < 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, _PyIO_str_readall, &readall) < 0) { - goto cleanup; - } - if (readall) { - tmp = _PyObject_CallNoArg(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. */ + } 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 < 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, _PyIO_str_readall, &readall) < 0) { + goto cleanup; + } + if (readall) { + tmp = _PyObject_CallNoArg(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, _PyIO_str_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(_PyIO_empty_bytes, 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; - - if (_PyIOBase_check_writable(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; - + 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(_PyIO_empty_bytes, 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; + + if (_PyIOBase_check_writable(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, &PyBufferedWriter_Type) && Py_IS_TYPE(raw, &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; + + 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, _PyIO_str_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_ssize_t written = 0; - 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; - written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t); - /* 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]*/ -{ - if (_PyIOBase_check_readable(reader, Py_True) == NULL) - return -1; - if (_PyIOBase_check_writable(writer, Py_True) == NULL) - return -1; - - self->reader = (buffered *) PyObject_CallFunction( - (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size); - if (self->reader == NULL) - return -1; - - self->writer = (buffered *) PyObject_CallFunction( - (PyObject *) &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(self->dict); - 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) -{ - _PyObject_GC_UNTRACK(self); - if (self->weakreflist != NULL) - PyObject_ClearWeakRefs((PyObject *)self); - Py_CLEAR(self->reader); - Py_CLEAR(self->writer); - Py_CLEAR(self->dict); - Py_TYPE(self)->tp_free((PyObject *) self); -} - -static PyObject * -_forward_call(buffered *self, _Py_Identifier *name, PyObject *args) -{ - PyObject *func, *ret; - if (self == NULL) { - PyErr_SetString(PyExc_ValueError, - "I/O operation on uninitialized object"); - return NULL; - } - - func = _PyObject_GetAttrId((PyObject *)self, name); - if (func == NULL) { - PyErr_SetString(PyExc_AttributeError, name->string); - 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, &PyId_read, args); -} - -static PyObject * -bufferedrwpair_peek(rwpair *self, PyObject *args) -{ - return _forward_call(self->reader, &PyId_peek, args); -} - -static PyObject * -bufferedrwpair_read1(rwpair *self, PyObject *args) -{ - return _forward_call(self->reader, &PyId_read1, args); -} - -static PyObject * -bufferedrwpair_readinto(rwpair *self, PyObject *args) -{ - return _forward_call(self->reader, &PyId_readinto, args); -} - -static PyObject * -bufferedrwpair_readinto1(rwpair *self, PyObject *args) -{ - return _forward_call(self->reader, &PyId_readinto1, args); -} - -static PyObject * -bufferedrwpair_write(rwpair *self, PyObject *args) -{ - return _forward_call(self->writer, &PyId_write, args); -} - -static PyObject * + 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_ssize_t written = 0; + 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; + written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t); + /* 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]*/ +{ + if (_PyIOBase_check_readable(reader, Py_True) == NULL) + return -1; + if (_PyIOBase_check_writable(writer, Py_True) == NULL) + return -1; + + self->reader = (buffered *) PyObject_CallFunction( + (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size); + if (self->reader == NULL) + return -1; + + self->writer = (buffered *) PyObject_CallFunction( + (PyObject *) &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(self->dict); + 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) +{ + _PyObject_GC_UNTRACK(self); + if (self->weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *)self); + Py_CLEAR(self->reader); + Py_CLEAR(self->writer); + Py_CLEAR(self->dict); + Py_TYPE(self)->tp_free((PyObject *) self); +} + +static PyObject * +_forward_call(buffered *self, _Py_Identifier *name, PyObject *args) +{ + PyObject *func, *ret; + if (self == NULL) { + PyErr_SetString(PyExc_ValueError, + "I/O operation on uninitialized object"); + return NULL; + } + + func = _PyObject_GetAttrId((PyObject *)self, name); + if (func == NULL) { + PyErr_SetString(PyExc_AttributeError, name->string); + 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, &PyId_read, args); +} + +static PyObject * +bufferedrwpair_peek(rwpair *self, PyObject *args) +{ + return _forward_call(self->reader, &PyId_peek, args); +} + +static PyObject * +bufferedrwpair_read1(rwpair *self, PyObject *args) +{ + return _forward_call(self->reader, &PyId_read1, args); +} + +static PyObject * +bufferedrwpair_readinto(rwpair *self, PyObject *args) +{ + return _forward_call(self->reader, &PyId_readinto, args); +} + +static PyObject * +bufferedrwpair_readinto1(rwpair *self, PyObject *args) +{ + return _forward_call(self->reader, &PyId_readinto1, args); +} + +static PyObject * +bufferedrwpair_write(rwpair *self, PyObject *args) +{ + return _forward_call(self->writer, &PyId_write, args); +} + +static PyObject * bufferedrwpair_flush(rwpair *self, PyObject *Py_UNUSED(ignored)) -{ +{ return _forward_call(self->writer, &PyId_flush, NULL); -} - -static PyObject * +} + +static PyObject * bufferedrwpair_readable(rwpair *self, PyObject *Py_UNUSED(ignored)) -{ +{ return _forward_call(self->reader, &PyId_readable, NULL); -} - -static PyObject * +} + +static PyObject * bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored)) -{ +{ return _forward_call(self->writer, &PyId_writable, NULL); -} - -static PyObject * +} + +static PyObject * bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored)) -{ - PyObject *exc = NULL, *val, *tb; +{ + PyObject *exc = NULL, *val, *tb; PyObject *ret = _forward_call(self->writer, &PyId_close, NULL); - if (ret == NULL) - PyErr_Fetch(&exc, &val, &tb); - else - Py_DECREF(ret); + if (ret == NULL) + PyErr_Fetch(&exc, &val, &tb); + else + Py_DECREF(ret); ret = _forward_call(self->reader, &PyId_close, NULL); - if (exc != NULL) { - _PyErr_ChainExceptions(exc, val, tb); - Py_CLEAR(ret); - } - return ret; -} - -static PyObject * + if (exc != NULL) { + _PyErr_ChainExceptions(exc, val, tb); + Py_CLEAR(ret); + } + return ret; +} + +static PyObject * bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored)) -{ +{ PyObject *ret = _forward_call(self->writer, &PyId_isatty, NULL); - - if (ret != Py_False) { - /* either True or exception */ - return ret; - } - Py_DECREF(ret); - + + if (ret != Py_False) { + /* either True or exception */ + return ret; + } + Py_DECREF(ret); + return _forward_call(self->reader, &PyId_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, _PyIO_str_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; - - if (_PyIOBase_check_seekable(raw, Py_True) == NULL) - return -1; - if (_PyIOBase_check_readable(raw, Py_True) == NULL) - return -1; - if (_PyIOBase_check_writable(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; - +} + +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, _PyIO_str_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; + + if (_PyIOBase_check_seekable(raw, Py_True) == NULL) + return -1; + if (_PyIOBase_check_readable(raw, Py_True) == NULL) + return -1; + if (_PyIOBase_check_writable(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, &PyBufferedRandom_Type) && Py_IS_TYPE(raw, &PyFileIO_Type)); - - self->ok = 1; - return 0; -} - -#include "clinic/bufferedio.c.h" - - -static PyMethodDef bufferediobase_methods[] = { - _IO__BUFFEREDIOBASE_DETACH_METHODDEF - {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc}, - {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc}, - _IO__BUFFEREDIOBASE_READINTO_METHODDEF - _IO__BUFFEREDIOBASE_READINTO1_METHODDEF - {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc}, - {NULL, NULL} -}; - -PyTypeObject PyBufferedIOBase_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._BufferedIOBase", /*tp_name*/ - 0, /*tp_basicsize*/ - 0, /*tp_itemsize*/ - 0, /*tp_dealloc*/ + + self->ok = 1; + return 0; +} + +#include "clinic/bufferedio.c.h" + + +static PyMethodDef bufferediobase_methods[] = { + _IO__BUFFEREDIOBASE_DETACH_METHODDEF + {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc}, + {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc}, + _IO__BUFFEREDIOBASE_READINTO_METHODDEF + _IO__BUFFEREDIOBASE_READINTO1_METHODDEF + {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc}, + {NULL, NULL} +}; + +PyTypeObject PyBufferedIOBase_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io._BufferedIOBase", /*tp_name*/ + 0, /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - bufferediobase_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - bufferediobase_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - &PyIOBase_Type, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - 0, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - - -static PyMethodDef bufferedreader_methods[] = { - /* BufferedIOMixin methods */ - {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, - {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS}, - {"close", (PyCFunction)buffered_close, METH_NOARGS}, - {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, - {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, - {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, - {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, - {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, - - _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 - {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, - _IO__BUFFERED_TRUNCATE_METHODDEF - {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, - {NULL, NULL} -}; - -static PyMemberDef bufferedreader_members[] = { - {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, - {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, - {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} -}; - - -PyTypeObject PyBufferedReader_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.BufferedReader", /*tp_name*/ - sizeof(buffered), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)buffered_dealloc, /*tp_dealloc*/ + bufferediobase_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + bufferediobase_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + &PyIOBase_Type, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + + +static PyMethodDef bufferedreader_methods[] = { + /* BufferedIOMixin methods */ + {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, + {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS}, + {"close", (PyCFunction)buffered_close, METH_NOARGS}, + {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, + {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, + {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, + {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, + {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, + + _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 + {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, + _IO__BUFFERED_TRUNCATE_METHODDEF + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, + {NULL, NULL} +}; + +static PyMemberDef bufferedreader_members[] = { + {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, + {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, + {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} +}; + + +PyTypeObject PyBufferedReader_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.BufferedReader", /*tp_name*/ + sizeof(buffered), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)buffered_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - (reprfunc)buffered_repr, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)buffered_repr, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_BufferedReader___init____doc__, /* tp_doc */ - (traverseproc)buffered_traverse, /* tp_traverse */ - (inquiry)buffered_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ - 0, /* tp_iter */ - (iternextfunc)buffered_iternext, /* tp_iternext */ - bufferedreader_methods, /* tp_methods */ - bufferedreader_members, /* tp_members */ - bufferedreader_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(buffered, dict), /* tp_dictoffset */ - _io_BufferedReader___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - - -static PyMethodDef bufferedwriter_methods[] = { - /* BufferedIOMixin methods */ - {"close", (PyCFunction)buffered_close, METH_NOARGS}, - {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, - {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, - {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, - {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, - {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, - {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, - - _IO_BUFFEREDWRITER_WRITE_METHODDEF - _IO__BUFFERED_TRUNCATE_METHODDEF - {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, - _IO__BUFFERED_SEEK_METHODDEF - {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, - {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, - {NULL, NULL} -}; - -static PyMemberDef bufferedwriter_members[] = { - {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, - {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, - {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} -}; - - -PyTypeObject PyBufferedWriter_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.BufferedWriter", /*tp_name*/ - sizeof(buffered), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)buffered_dealloc, /*tp_dealloc*/ + _io_BufferedReader___init____doc__, /* tp_doc */ + (traverseproc)buffered_traverse, /* tp_traverse */ + (inquiry)buffered_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ + 0, /* tp_iter */ + (iternextfunc)buffered_iternext, /* tp_iternext */ + bufferedreader_methods, /* tp_methods */ + bufferedreader_members, /* tp_members */ + bufferedreader_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(buffered, dict), /* tp_dictoffset */ + _io_BufferedReader___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + + +static PyMethodDef bufferedwriter_methods[] = { + /* BufferedIOMixin methods */ + {"close", (PyCFunction)buffered_close, METH_NOARGS}, + {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, + {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, + {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, + {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, + {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, + {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, + + _IO_BUFFEREDWRITER_WRITE_METHODDEF + _IO__BUFFERED_TRUNCATE_METHODDEF + {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, + _IO__BUFFERED_SEEK_METHODDEF + {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, + {NULL, NULL} +}; + +static PyMemberDef bufferedwriter_members[] = { + {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, + {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, + {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} +}; + + +PyTypeObject PyBufferedWriter_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.BufferedWriter", /*tp_name*/ + sizeof(buffered), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)buffered_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - (reprfunc)buffered_repr, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)buffered_repr, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_BufferedWriter___init____doc__, /* tp_doc */ - (traverseproc)buffered_traverse, /* tp_traverse */ - (inquiry)buffered_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ - 0, /* tp_iter */ - 0, /* tp_iternext */ - bufferedwriter_methods, /* tp_methods */ - bufferedwriter_members, /* tp_members */ - bufferedwriter_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(buffered, dict), /* tp_dictoffset */ - _io_BufferedWriter___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - - -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 PyGetSetDef bufferedrwpair_getset[] = { - {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL}, - {NULL} -}; - -PyTypeObject PyBufferedRWPair_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.BufferedRWPair", /*tp_name*/ - sizeof(rwpair), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/ + _io_BufferedWriter___init____doc__, /* tp_doc */ + (traverseproc)buffered_traverse, /* tp_traverse */ + (inquiry)buffered_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ + 0, /* tp_iter */ + 0, /* tp_iternext */ + bufferedwriter_methods, /* tp_methods */ + bufferedwriter_members, /* tp_members */ + bufferedwriter_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(buffered, dict), /* tp_dictoffset */ + _io_BufferedWriter___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + + +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 PyGetSetDef bufferedrwpair_getset[] = { + {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL}, + {NULL} +}; + +PyTypeObject PyBufferedRWPair_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.BufferedRWPair", /*tp_name*/ + sizeof(rwpair), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - _io_BufferedRWPair___init____doc__, /* tp_doc */ - (traverseproc)bufferedrwpair_traverse, /* tp_traverse */ - (inquiry)bufferedrwpair_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/ - 0, /* tp_iter */ - 0, /* tp_iternext */ - bufferedrwpair_methods, /* tp_methods */ - 0, /* tp_members */ - bufferedrwpair_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(rwpair, dict), /* tp_dictoffset */ - _io_BufferedRWPair___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - - -static PyMethodDef bufferedrandom_methods[] = { - /* BufferedIOMixin methods */ - {"close", (PyCFunction)buffered_close, METH_NOARGS}, - {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, - {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, - {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, - {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, - {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, - {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, - {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, - - {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, - - _IO__BUFFERED_SEEK_METHODDEF - {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, - _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 - {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, - {NULL, NULL} -}; - -static PyMemberDef bufferedrandom_members[] = { - {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, - {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, - {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} -}; - - -PyTypeObject PyBufferedRandom_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.BufferedRandom", /*tp_name*/ - sizeof(buffered), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)buffered_dealloc, /*tp_dealloc*/ + _io_BufferedRWPair___init____doc__, /* tp_doc */ + (traverseproc)bufferedrwpair_traverse, /* tp_traverse */ + (inquiry)bufferedrwpair_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/ + 0, /* tp_iter */ + 0, /* tp_iternext */ + bufferedrwpair_methods, /* tp_methods */ + 0, /* tp_members */ + bufferedrwpair_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(rwpair, dict), /* tp_dictoffset */ + _io_BufferedRWPair___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + + +static PyMethodDef bufferedrandom_methods[] = { + /* BufferedIOMixin methods */ + {"close", (PyCFunction)buffered_close, METH_NOARGS}, + {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, + {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, + {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, + {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, + {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, + {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, + {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O}, + + {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, + + _IO__BUFFERED_SEEK_METHODDEF + {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, + _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 + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, + {NULL, NULL} +}; + +static PyMemberDef bufferedrandom_members[] = { + {"raw", T_OBJECT, offsetof(buffered, raw), READONLY}, + {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0}, + {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} +}; + + +PyTypeObject PyBufferedRandom_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.BufferedRandom", /*tp_name*/ + sizeof(buffered), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)buffered_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - (reprfunc)buffered_repr, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)buffered_repr, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_BufferedRandom___init____doc__, /* tp_doc */ - (traverseproc)buffered_traverse, /* tp_traverse */ - (inquiry)buffered_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ - 0, /* tp_iter */ - (iternextfunc)buffered_iternext, /* tp_iternext */ - bufferedrandom_methods, /* tp_methods */ - bufferedrandom_members, /* tp_members */ - bufferedrandom_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /*tp_dict*/ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(buffered, dict), /*tp_dictoffset*/ - _io_BufferedRandom___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; + _io_BufferedRandom___init____doc__, /* tp_doc */ + (traverseproc)buffered_traverse, /* tp_traverse */ + (inquiry)buffered_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ + 0, /* tp_iter */ + (iternextfunc)buffered_iternext, /* tp_iternext */ + bufferedrandom_methods, /* tp_methods */ + bufferedrandom_members, /* tp_members */ + bufferedrandom_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /*tp_dict*/ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(buffered, dict), /*tp_dictoffset*/ + _io_BufferedRandom___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; diff --git a/contrib/tools/python3/src/Modules/_io/bytesio.c b/contrib/tools/python3/src/Modules/_io/bytesio.c index 2468f45f941..47471efd436 100644 --- a/contrib/tools/python3/src/Modules/_io/bytesio.c +++ b/contrib/tools/python3/src/Modules/_io/bytesio.c @@ -1,36 +1,36 @@ -#include "Python.h" +#include "Python.h" #include "pycore_object.h" #include <stddef.h> // offsetof() -#include "_iomodule.h" - -/*[clinic input] -module _io -class _io.BytesIO "bytesio *" "&PyBytesIO_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7f50ec034f5c0b26]*/ - -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. -*/ - +#include "_iomodule.h" + +/*[clinic input] +module _io +class _io.BytesIO "bytesio *" "&PyBytesIO_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7f50ec034f5c0b26]*/ + +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) { @@ -52,133 +52,133 @@ check_exports(bytesio *self) return 0; } -#define CHECK_CLOSED(self) \ +#define CHECK_CLOSED(self) \ if (check_closed(self)) { \ - return NULL; \ - } - -#define CHECK_EXPORTS(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) -{ - /* 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); - - assert(self->buf != NULL); - - /* 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 + 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) +{ + /* 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); + + assert(self->buf != NULL); + + /* 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; } @@ -195,777 +195,777 @@ write_bytes(bytesio *self, PyObject *b) goto done; } - assert(self->pos >= 0); + assert(self->pos >= 0); size_t endpos = (size_t)self->pos + len; - if (endpos > (size_t)PyBytes_GET_SIZE(self->buf)) { + if (endpos > (size_t)PyBytes_GET_SIZE(self->buf)) { if (resize_buffer(self, endpos) < 0) { len = -1; goto done; } - } - else if (SHARED_BUF(self)) { + } + 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. */ + } + + 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; - } - + 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 - -Get a read-write view over the contents of the BytesIO object. -[clinic start generated code]*/ - -static PyObject * -_io_BytesIO_getbuffer_impl(bytesio *self) -/*[clinic end generated code: output=72cd7c6e13aa09ed input=8f738ef615865176]*/ -{ - PyTypeObject *type = &_PyBytesIOBuffer_Type; - bytesiobuf *buf; - PyObject *view; - - CHECK_CLOSED(self); - - buf = (bytesiobuf *) type->tp_alloc(type, 0); - if (buf == NULL) - return NULL; - Py_INCREF(self); - buf->source = 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; - } - } - Py_INCREF(self->buf); - return 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) -{ + 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 + +Get a read-write view over the contents of the BytesIO object. +[clinic start generated code]*/ + +static PyObject * +_io_BytesIO_getbuffer_impl(bytesio *self) +/*[clinic end generated code: output=72cd7c6e13aa09ed input=8f738ef615865176]*/ +{ + PyTypeObject *type = &_PyBytesIOBuffer_Type; + bytesiobuf *buf; + PyObject *view; + + CHECK_CLOSED(self); + + buf = (bytesiobuf *) type->tp_alloc(type, 0); + if (buf == NULL) + return NULL; + Py_INCREF(self); + buf->source = 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; + } + } + Py_INCREF(self->buf); + return 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; - Py_INCREF(self->buf); - return 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; + + 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; + Py_INCREF(self->buf); + return 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]*/ -{ + + 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) { + 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); + 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 * + 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) { - Py_INCREF(Py_None); - dict = 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 +{ + PyObject *initvalue = _io_BytesIO_getvalue_impl(self); + PyObject *dict; + PyObject *state; + + if (initvalue == NULL) + return NULL; + if (self->dict == NULL) { + Py_INCREF(Py_None); + dict = 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 { - Py_INCREF(dict); - self->dict = dict; - } - } - - Py_RETURN_NONE; -} - -static void -bytesio_dealloc(bytesio *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); - Py_TYPE(self)->tp_free(self); -} - -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_INCREF(initvalue); - Py_XSETREF(self->buf, 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) -{ - Py_ssize_t res; - - res = _PyObject_SIZE(Py_TYPE(self)); + 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 { + Py_INCREF(dict); + self->dict = dict; + } + } + + Py_RETURN_NONE; +} + +static void +bytesio_dealloc(bytesio *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); + Py_TYPE(self)->tp_free(self); +} + +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_INCREF(initvalue); + Py_XSETREF(self->buf, 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) +{ + Py_ssize_t res; + + res = _PyObject_SIZE(Py_TYPE(self)); if (self->buf && !SHARED_BUF(self)) { Py_ssize_t s = _PySys_GetSizeOf(self->buf); if (s == -1) { @@ -973,194 +973,194 @@ bytesio_sizeof(bytesio *self, void *unused) } res += s; } - return PyLong_FromSsize_t(res); -} - -static int -bytesio_traverse(bytesio *self, visitproc visit, void *arg) -{ - Py_VISIT(self->dict); - return 0; -} - -static int -bytesio_clear(bytesio *self) -{ - Py_CLEAR(self->dict); - return 0; -} - - -#include "clinic/bytesio.c.h" - -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 */ -}; - -PyTypeObject PyBytesIO_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.BytesIO", /*tp_name*/ - sizeof(bytesio), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)bytesio_dealloc, /*tp_dealloc*/ + return PyLong_FromSsize_t(res); +} + +static int +bytesio_traverse(bytesio *self, visitproc visit, void *arg) +{ + Py_VISIT(self->dict); + return 0; +} + +static int +bytesio_clear(bytesio *self) +{ + Py_CLEAR(self->dict); + return 0; +} + + +#include "clinic/bytesio.c.h" + +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 */ +}; + +PyTypeObject PyBytesIO_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.BytesIO", /*tp_name*/ + sizeof(bytesio), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)bytesio_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_BytesIO___init____doc__, /*tp_doc*/ - (traverseproc)bytesio_traverse, /*tp_traverse*/ - (inquiry)bytesio_clear, /*tp_clear*/ - 0, /*tp_richcompare*/ - offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/ - PyObject_SelfIter, /*tp_iter*/ - (iternextfunc)bytesio_iternext, /*tp_iternext*/ - bytesio_methods, /*tp_methods*/ - 0, /*tp_members*/ - bytesio_getsetlist, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - offsetof(bytesio, dict), /*tp_dictoffset*/ - _io_BytesIO___init__, /*tp_init*/ - 0, /*tp_alloc*/ - bytesio_new, /*tp_new*/ -}; - - -/* - * 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 (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(self->source); - return 0; -} - -static void -bytesiobuf_dealloc(bytesiobuf *self) -{ - /* bpo-31095: UnTrack is needed before calling any callbacks */ - PyObject_GC_UnTrack(self); - Py_CLEAR(self->source); - Py_TYPE(self)->tp_free(self); -} - -static PyBufferProcs bytesiobuf_as_buffer = { - (getbufferproc) bytesiobuf_getbuffer, - (releasebufferproc) bytesiobuf_releasebuffer, -}; - + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + _io_BytesIO___init____doc__, /*tp_doc*/ + (traverseproc)bytesio_traverse, /*tp_traverse*/ + (inquiry)bytesio_clear, /*tp_clear*/ + 0, /*tp_richcompare*/ + offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/ + PyObject_SelfIter, /*tp_iter*/ + (iternextfunc)bytesio_iternext, /*tp_iternext*/ + bytesio_methods, /*tp_methods*/ + 0, /*tp_members*/ + bytesio_getsetlist, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + offsetof(bytesio, dict), /*tp_dictoffset*/ + _io_BytesIO___init__, /*tp_init*/ + 0, /*tp_alloc*/ + bytesio_new, /*tp_new*/ +}; + + +/* + * 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 (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(self->source); + return 0; +} + +static void +bytesiobuf_dealloc(bytesiobuf *self) +{ + /* bpo-31095: UnTrack is needed before calling any callbacks */ + PyObject_GC_UnTrack(self); + Py_CLEAR(self->source); + Py_TYPE(self)->tp_free(self); +} + +static PyBufferProcs bytesiobuf_as_buffer = { + (getbufferproc) bytesiobuf_getbuffer, + (releasebufferproc) bytesiobuf_releasebuffer, +}; + Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._BytesIOBuffer", /*tp_name*/ - sizeof(bytesiobuf), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)bytesiobuf_dealloc, /*tp_dealloc*/ + PyVarObject_HEAD_INIT(NULL, 0) + "_io._BytesIOBuffer", /*tp_name*/ + sizeof(bytesiobuf), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)bytesiobuf_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - &bytesiobuf_as_buffer, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - 0, /*tp_doc*/ - (traverseproc)bytesiobuf_traverse, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - 0, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - 0, /*tp_init*/ - 0, /*tp_alloc*/ - 0, /*tp_new*/ -}; + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &bytesiobuf_as_buffer, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + 0, /*tp_doc*/ + (traverseproc)bytesiobuf_traverse, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + 0, /*tp_methods*/ + 0, /*tp_members*/ + 0, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + 0, /*tp_new*/ +}; diff --git a/contrib/tools/python3/src/Modules/_io/clinic/_iomodule.c.h b/contrib/tools/python3/src/Modules/_io/clinic/_iomodule.c.h index 1a9651d3408..3cab3356809 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/_iomodule.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/_iomodule.c.h @@ -1,160 +1,160 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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.getpreferredencoding(False) is called to get the\n" -"current locale encoding. (For reading and writing raw bytes use binary\n" -"mode and leave encoding 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" -"\'U\' universal newline mode (deprecated)\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" -"\'U\' mode is deprecated and will raise an exception in future versions\n" -"of Python. It has no effect in Python 3. Use newline to control\n" -"universal newlines mode.\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 \ +/*[clinic input] +preserve +[clinic start generated code]*/ + +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.getpreferredencoding(False) is called to get the\n" +"current locale encoding. (For reading and writing raw bytes use binary\n" +"mode and leave encoding 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" +"\'U\' universal newline mode (deprecated)\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" +"\'U\' mode is deprecated and will raise an exception in future versions\n" +"of Python. It has no effect in Python 3. Use newline to control\n" +"universal newlines mode.\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)(void(*)(void))_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; - static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL}; + +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; + static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "open", 0}; 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; - + 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; - } + goto exit; + } file = args[0]; if (!noptargs) { goto skip_optional_pos; @@ -276,11 +276,11 @@ _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kw } opener = args[7]; skip_optional_pos: - return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener); - -exit: - return return_value; -} + return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener); + +exit: + return return_value; +} PyDoc_STRVAR(_io_open_code__doc__, "open_code($module, /, path)\n" diff --git a/contrib/tools/python3/src/Modules/_io/clinic/bufferedio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/bufferedio.c.h index 56d6332a250..b5e677812f5 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/bufferedio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/bufferedio.c.h @@ -1,122 +1,122 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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}; - +/*[clinic input] +preserve +[clinic start generated code]*/ + +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; - } + 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}; - + 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; - } + 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)_io__BufferedIOBase_detach, METH_NOARGS, _io__BufferedIOBase_detach__doc__}, - -static PyObject * -_io__BufferedIOBase_detach_impl(PyObject *self); - -static PyObject * -_io__BufferedIOBase_detach(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - return _io__BufferedIOBase_detach_impl(self); -} - -PyDoc_STRVAR(_io__Buffered_peek__doc__, -"peek($self, size=0, /)\n" -"--\n" -"\n"); - -#define _IO__BUFFERED_PEEK_METHODDEF \ + 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)_io__BufferedIOBase_detach, METH_NOARGS, _io__BufferedIOBase_detach__doc__}, + +static PyObject * +_io__BufferedIOBase_detach_impl(PyObject *self); + +static PyObject * +_io__BufferedIOBase_detach(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return _io__BufferedIOBase_detach_impl(self); +} + +PyDoc_STRVAR(_io__Buffered_peek__doc__, +"peek($self, size=0, /)\n" +"--\n" +"\n"); + +#define _IO__BUFFERED_PEEK_METHODDEF \ {"peek", (PyCFunction)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -138,32 +138,32 @@ _io__Buffered_peek(buffered *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -171,32 +171,32 @@ _io__Buffered_read(buffered *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -218,106 +218,106 @@ _io__Buffered_read1(buffered *self, PyObject *const *args, Py_ssize_t nargs) 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}; - + 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; - } + 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}; - + 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; - } + 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -325,33 +325,33 @@ _io__Buffered_readline(buffered *self, PyObject *const *args, Py_ssize_t nargs) goto exit; } skip_optional: - return_value = _io__Buffered_readline_impl(self, size); - -exit: - return return_value; -} - -PyDoc_STRVAR(_io__Buffered_seek__doc__, -"seek($self, target, whence=0, /)\n" -"--\n" -"\n"); - -#define _IO__BUFFERED_SEEK_METHODDEF \ + return_value = _io__Buffered_readline_impl(self, size); + +exit: + return return_value; +} + +PyDoc_STRVAR(_io__Buffered_seek__doc__, +"seek($self, target, whence=0, /)\n" +"--\n" +"\n"); + +#define _IO__BUFFERED_SEEK_METHODDEF \ {"seek", (PyCFunction)(void(*)(void))_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; - + +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; - } + goto exit; + } targetobj = args[0]; if (nargs < 2) { goto skip_optional; @@ -366,70 +366,70 @@ _io__Buffered_seek(buffered *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_io__Buffered_truncate, METH_FASTCALL, _io__Buffered_truncate__doc__}, - -static PyObject * -_io__Buffered_truncate_impl(buffered *self, PyObject *pos); - -static PyObject * -_io__Buffered_truncate(buffered *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *pos = Py_None; - + +static PyObject * +_io__Buffered_truncate_impl(buffered *self, PyObject *pos); + +static PyObject * +_io__Buffered_truncate(buffered *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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } pos = args[0]; skip_optional: - return_value = _io__Buffered_truncate_impl(self, 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; - static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + return_value = _io__Buffered_truncate_impl(self, 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; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "BufferedReader", 0}; 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; - + 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; - } + goto exit; + } raw = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -452,43 +452,43 @@ _io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs) 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; - static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + 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; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "BufferedWriter", 0}; 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; - + 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; - } + goto exit; + } raw = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -511,80 +511,80 @@ _io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs) 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}; - + 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; - } + 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; - PyObject *reader; - PyObject *writer; - Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; - + 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; + PyObject *reader; + PyObject *writer; + Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; + if (Py_IS_TYPE(self, &PyBufferedRWPair_Type) && - !_PyArg_NoKeywords("BufferedRWPair", kwargs)) { - goto exit; - } + !_PyArg_NoKeywords("BufferedRWPair", kwargs)) { + goto exit; + } if (!_PyArg_CheckPositional("BufferedRWPair", PyTuple_GET_SIZE(args), 2, 3)) { - goto exit; - } + goto exit; + } reader = PyTuple_GET_ITEM(args, 0); writer = PyTuple_GET_ITEM(args, 1); if (PyTuple_GET_SIZE(args) < 3) { @@ -608,43 +608,43 @@ _io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs) 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; - static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + 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; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "BufferedRandom", 0}; 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; - + 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; - } + goto exit; + } raw = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -667,9 +667,9 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs) buffer_size = ival; } skip_optional_pos: - return_value = _io_BufferedRandom___init___impl((buffered *)self, raw, buffer_size); - -exit: - return return_value; -} + return_value = _io_BufferedRandom___init___impl((buffered *)self, raw, buffer_size); + +exit: + return return_value; +} /*[clinic end generated code: output=7d9ad40c95bdd808 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/bytesio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/bytesio.c.h index 83cd490dc59..af84feb8daa 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/bytesio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/bytesio.c.h @@ -1,177 +1,177 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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)_io_BytesIO_getbuffer, METH_NOARGS, _io_BytesIO_getbuffer__doc__}, - -static PyObject * -_io_BytesIO_getbuffer_impl(bytesio *self); - -static PyObject * -_io_BytesIO_getbuffer(bytesio *self, PyObject *Py_UNUSED(ignored)) -{ - return _io_BytesIO_getbuffer_impl(self); -} - -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 \ +/*[clinic input] +preserve +[clinic start generated code]*/ + +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)_io_BytesIO_getbuffer, METH_NOARGS, _io_BytesIO_getbuffer__doc__}, + +static PyObject * +_io_BytesIO_getbuffer_impl(bytesio *self); + +static PyObject * +_io_BytesIO_getbuffer(bytesio *self, PyObject *Py_UNUSED(ignored)) +{ + return _io_BytesIO_getbuffer_impl(self); +} + +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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -179,36 +179,36 @@ _io_BytesIO_read(bytesio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -216,37 +216,37 @@ _io_BytesIO_read1(bytesio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -254,113 +254,113 @@ _io_BytesIO_readline(bytesio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + 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}; - + 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; - } + 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -368,40 +368,40 @@ _io_BytesIO_truncate(bytesio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (PyFloat_Check(args[0])) { PyErr_SetString(PyExc_TypeError, "integer argument expected, got float" ); @@ -432,87 +432,87 @@ _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs) 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; - static const char * const _keywords[] = {"initial_bytes", NULL}; + 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; + static const char * const _keywords[] = {"initial_bytes", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "BytesIO", 0}; 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; - + PyObject *initvalue = NULL; + fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf); if (!fastargs) { - goto exit; - } + 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; -} + return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue); + +exit: + return return_value; +} /*[clinic end generated code: output=4ec2506def9c8eb9 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/fileio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/fileio.c.h index 53e7067cf7a..2b8819bc882 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/fileio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/fileio.c.h @@ -1,69 +1,69 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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)_io_FileIO_close, METH_NOARGS, _io_FileIO_close__doc__}, - -static PyObject * -_io_FileIO_close_impl(fileio *self); - -static PyObject * -_io_FileIO_close(fileio *self, PyObject *Py_UNUSED(ignored)) -{ - return _io_FileIO_close_impl(self); -} - -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; - static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL}; +/*[clinic input] +preserve +[clinic start generated code]*/ + +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)_io_FileIO_close, METH_NOARGS, _io_FileIO_close__doc__}, + +static PyObject * +_io_FileIO_close_impl(fileio *self); + +static PyObject * +_io_FileIO_close(fileio *self, PyObject *Py_UNUSED(ignored)) +{ + return _io_FileIO_close_impl(self); +} + +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; + static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "FileIO", 0}; 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; - + 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; - } + goto exit; + } nameobj = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -102,168 +102,168 @@ _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) } 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)_io_FileIO_readinto, METH_O, _io_FileIO_readinto__doc__}, - -static PyObject * -_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer); - -static PyObject * -_io_FileIO_readinto(fileio *self, PyObject *arg) -{ - PyObject *return_value = NULL; - Py_buffer buffer = {NULL, NULL}; - + 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)_io_FileIO_readinto, METH_O, _io_FileIO_readinto__doc__}, + +static PyObject * +_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer); + +static PyObject * +_io_FileIO_readinto(fileio *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; - } + goto exit; + } if (!PyBuffer_IsContiguous(&buffer, 'C')) { _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg); goto exit; } - return_value = _io_FileIO_readinto_impl(self, &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 \ + return_value = _io_FileIO_readinto_impl(self, &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)(void(*)(void))_io_FileIO_read, METH_FASTCALL, _io_FileIO_read__doc__}, - -static PyObject * -_io_FileIO_read_impl(fileio *self, Py_ssize_t size); - -static PyObject * -_io_FileIO_read(fileio *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - Py_ssize_t size = -1; - + +static PyObject * +_io_FileIO_read_impl(fileio *self, Py_ssize_t size); + +static PyObject * +_io_FileIO_read(fileio *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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -271,82 +271,82 @@ _io_FileIO_read(fileio *self, PyObject *const *args, Py_ssize_t nargs) goto exit; } skip_optional: - return_value = _io_FileIO_read_impl(self, 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)_io_FileIO_write, METH_O, _io_FileIO_write__doc__}, - -static PyObject * -_io_FileIO_write_impl(fileio *self, Py_buffer *b); - -static PyObject * -_io_FileIO_write(fileio *self, PyObject *arg) -{ - PyObject *return_value = NULL; - Py_buffer b = {NULL, NULL}; - + return_value = _io_FileIO_read_impl(self, 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)_io_FileIO_write, METH_O, _io_FileIO_write__doc__}, + +static PyObject * +_io_FileIO_write_impl(fileio *self, Py_buffer *b); + +static PyObject * +_io_FileIO_write(fileio *self, PyObject *arg) +{ + PyObject *return_value = NULL; + Py_buffer b = {NULL, NULL}; + if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) { - goto exit; - } + goto exit; + } if (!PyBuffer_IsContiguous(&b, 'C')) { _PyArg_BadArgument("write", "argument", "contiguous buffer", arg); goto exit; } - return_value = _io_FileIO_write_impl(self, &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 \ + return_value = _io_FileIO_write_impl(self, &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)(void(*)(void))_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; - + +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; - } + goto exit; + } pos = args[0]; if (nargs < 2) { goto skip_optional; @@ -361,90 +361,90 @@ _io_FileIO_seek(fileio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_io_FileIO_truncate, METH_FASTCALL, _io_FileIO_truncate__doc__}, - -static PyObject * -_io_FileIO_truncate_impl(fileio *self, PyObject *posobj); - -static PyObject * -_io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; + +static PyObject * +_io_FileIO_truncate_impl(fileio *self, PyObject *posobj); + +static PyObject * +_io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; PyObject *posobj = Py_None; - + if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) { - goto exit; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } posobj = args[0]; skip_optional: - return_value = _io_FileIO_truncate_impl(self, 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) */ + return_value = _io_FileIO_truncate_impl(self, 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=e7682d0a3264d284 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/iobase.c.h b/contrib/tools/python3/src/Modules/_io/clinic/iobase.c.h index ddaff7b5d13..65c92043cfa 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/iobase.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/iobase.c.h @@ -1,193 +1,193 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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_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" -"Returns underlying file descriptor if one exists.\n" -"\n" -"OSError is raised if the IO object does not use a file descriptor."); - -#define _IO__IOBASE_FILENO_METHODDEF \ - {"fileno", (PyCFunction)_io__IOBase_fileno, METH_NOARGS, _io__IOBase_fileno__doc__}, - -static PyObject * -_io__IOBase_fileno_impl(PyObject *self); - -static PyObject * -_io__IOBase_fileno(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - return _io__IOBase_fileno_impl(self); -} - -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 \ +/*[clinic input] +preserve +[clinic start generated code]*/ + +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_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" +"Returns underlying file descriptor if one exists.\n" +"\n" +"OSError is raised if the IO object does not use a file descriptor."); + +#define _IO__IOBASE_FILENO_METHODDEF \ + {"fileno", (PyCFunction)_io__IOBase_fileno, METH_NOARGS, _io__IOBase_fileno__doc__}, + +static PyObject * +_io__IOBase_fileno_impl(PyObject *self); + +static PyObject * +_io__IOBase_fileno(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return _io__IOBase_fileno_impl(self); +} + +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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -195,37 +195,37 @@ _io__IOBase_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -233,44 +233,44 @@ _io__IOBase_readlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs) 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" + 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 \ + +#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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -292,27 +292,27 @@ _io__RawIOBase_read(PyObject *self, PyObject *const *args, Py_ssize_t nargs) 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); -} + 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=61b6ea7153ef9940 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/stringio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/stringio.c.h index 77a720c2a6f..14d6cd6423c 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/stringio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/stringio.c.h @@ -1,67 +1,67 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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 \ +/*[clinic input] +preserve +[clinic start generated code]*/ + +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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -69,35 +69,35 @@ _io_StringIO_read(stringio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -105,37 +105,37 @@ _io_StringIO_readline(stringio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -143,40 +143,40 @@ _io_StringIO_truncate(stringio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (PyFloat_Check(args[0])) { PyErr_SetString(PyExc_TypeError, "integer argument expected, got float" ); @@ -207,77 +207,77 @@ _io_StringIO_seek(stringio *self, PyObject *const *args, Py_ssize_t nargs) 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; - static const char * const _keywords[] = {"initial_value", "newline", NULL}; + 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; + static const char * const _keywords[] = {"initial_value", "newline", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "StringIO", 0}; 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; - + 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; - } + goto exit; + } if (!noptargs) { goto skip_optional_pos; } @@ -289,63 +289,63 @@ _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) } 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); -} + 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=7aad5ab2e64a25b8 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/textio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/textio.c.h index b8b507543ea..72aea83dcc9 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/textio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/textio.c.h @@ -1,43 +1,43 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -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; - static const char * const _keywords[] = {"decoder", "translate", "errors", NULL}; +/*[clinic input] +preserve +[clinic start generated code]*/ + +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; + static const char * const _keywords[] = {"decoder", "translate", "errors", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "IncrementalNewlineDecoder", 0}; 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; - + 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; - } + goto exit; + } decoder = fastargs[0]; if (PyFloat_Check(fastargs[1])) { PyErr_SetString(PyExc_TypeError, @@ -53,39 +53,39 @@ _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject } 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 \ + 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)(void(*)(void))_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; - static const char * const _keywords[] = {"input", "final", NULL}; + +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; + static const char * const _keywords[] = {"input", "final", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0}; PyObject *argsbuf[2]; Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; - PyObject *input; - int final = 0; - + PyObject *input; + int final = 0; + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); if (!args) { - goto exit; - } + goto exit; + } input = args[0]; if (!noptargs) { goto skip_optional_pos; @@ -100,115 +100,115 @@ _io_IncrementalNewlineDecoder_decode(nldecoder_object *self, PyObject *const *ar 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.getpreferredencoding(False).\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; - static const char * const _keywords[] = {"buffer", "encoding", "errors", "newline", "line_buffering", "write_through", NULL}; + 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.getpreferredencoding(False).\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; + static const char * const _keywords[] = {"buffer", "encoding", "errors", "newline", "line_buffering", "write_through", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "TextIOWrapper", 0}; 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; - + 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; - } + goto exit; + } buffer = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -289,48 +289,48 @@ _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs) 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 \ + 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)(void(*)(void))_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; - static const char * const _keywords[] = {"encoding", "errors", "newline", "line_buffering", "write_through", NULL}; + +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; + static const char * const _keywords[] = {"encoding", "errors", "newline", "line_buffering", "write_through", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "reconfigure", 0}; 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; - + 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; - } + goto exit; + } if (!noptargs) { goto skip_optional_kwonly; } @@ -360,80 +360,80 @@ _io_TextIOWrapper_reconfigure(textio *self, PyObject *const *args, Py_ssize_t na } 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; - + 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; - } + 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -441,32 +441,32 @@ _io_TextIOWrapper_read(textio *self, PyObject *const *args, Py_ssize_t nargs) 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 \ + 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)(void(*)(void))_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; - + +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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -488,33 +488,33 @@ _io_TextIOWrapper_readline(textio *self, PyObject *const *args, Py_ssize_t nargs 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=0, /)\n" -"--\n" -"\n"); - -#define _IO_TEXTIOWRAPPER_SEEK_METHODDEF \ + return_value = _io_TextIOWrapper_readline_impl(self, size); + +exit: + return return_value; +} + +PyDoc_STRVAR(_io_TextIOWrapper_seek__doc__, +"seek($self, cookie, whence=0, /)\n" +"--\n" +"\n"); + +#define _IO_TEXTIOWRAPPER_SEEK_METHODDEF \ {"seek", (PyCFunction)(void(*)(void))_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; - + +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; - } + goto exit; + } cookieObj = args[0]; if (nargs < 2) { goto skip_optional; @@ -529,176 +529,176 @@ _io_TextIOWrapper_seek(textio *self, PyObject *const *args, Py_ssize_t nargs) 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"); - -#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 \ + return_value = _io_TextIOWrapper_seek_impl(self, cookieObj, whence); + +exit: + return return_value; +} + +PyDoc_STRVAR(_io_TextIOWrapper_tell__doc__, +"tell($self, /)\n" +"--\n" +"\n"); + +#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)(void(*)(void))_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; - + +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; - } + 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); -} + 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=b1bae4f4cdf6019e input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/clinic/winconsoleio.c.h b/contrib/tools/python3/src/Modules/_io/clinic/winconsoleio.c.h index 3e501a58537..9be1d5a0ace 100644 --- a/contrib/tools/python3/src/Modules/_io/clinic/winconsoleio.c.h +++ b/contrib/tools/python3/src/Modules/_io/clinic/winconsoleio.c.h @@ -1,68 +1,68 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(MS_WINDOWS) - -PyDoc_STRVAR(_io__WindowsConsoleIO_close__doc__, -"close($self, /)\n" -"--\n" -"\n" -"Close the handle.\n" -"\n" -"A closed handle cannot be used for further I/O operations. close() may be\n" -"called more than once without error."); - -#define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \ - {"close", (PyCFunction)_io__WindowsConsoleIO_close, METH_NOARGS, _io__WindowsConsoleIO_close__doc__}, - -static PyObject * -_io__WindowsConsoleIO_close_impl(winconsoleio *self); - -static PyObject * -_io__WindowsConsoleIO_close(winconsoleio *self, PyObject *Py_UNUSED(ignored)) -{ - return _io__WindowsConsoleIO_close_impl(self); -} - -#endif /* defined(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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; - static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL}; +/*[clinic input] +preserve +[clinic start generated code]*/ + +#if defined(MS_WINDOWS) + +PyDoc_STRVAR(_io__WindowsConsoleIO_close__doc__, +"close($self, /)\n" +"--\n" +"\n" +"Close the handle.\n" +"\n" +"A closed handle cannot be used for further I/O operations. close() may be\n" +"called more than once without error."); + +#define _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF \ + {"close", (PyCFunction)_io__WindowsConsoleIO_close, METH_NOARGS, _io__WindowsConsoleIO_close__doc__}, + +static PyObject * +_io__WindowsConsoleIO_close_impl(winconsoleio *self); + +static PyObject * +_io__WindowsConsoleIO_close(winconsoleio *self, PyObject *Py_UNUSED(ignored)) +{ + return _io__WindowsConsoleIO_close_impl(self); +} + +#endif /* defined(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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; + static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "_WindowsConsoleIO", 0}; 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; - + 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; - } + goto exit; + } nameobj = fastargs[0]; if (!noptargs) { goto skip_optional_pos; @@ -101,176 +101,176 @@ _io__WindowsConsoleIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) } opener = fastargs[3]; skip_optional_pos: - return_value = _io__WindowsConsoleIO___init___impl((winconsoleio *)self, nameobj, mode, closefd, opener); - -exit: - return return_value; -} - -#endif /* defined(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -PyDoc_STRVAR(_io__WindowsConsoleIO_fileno__doc__, -"fileno($self, /)\n" -"--\n" -"\n" -"Return the underlying file descriptor (an integer).\n" -"\n" -"fileno is only set when a file descriptor is used to open\n" -"one of the standard streams."); - -#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(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -PyDoc_STRVAR(_io__WindowsConsoleIO_readinto__doc__, -"readinto($self, buffer, /)\n" -"--\n" -"\n" -"Same as RawIOBase.readinto()."); - -#define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF \ - {"readinto", (PyCFunction)_io__WindowsConsoleIO_readinto, METH_O, _io__WindowsConsoleIO_readinto__doc__}, - -static PyObject * -_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer); - -static PyObject * -_io__WindowsConsoleIO_readinto(winconsoleio *self, PyObject *arg) -{ - PyObject *return_value = NULL; - Py_buffer buffer = {NULL, NULL}; - + return_value = _io__WindowsConsoleIO___init___impl((winconsoleio *)self, nameobj, mode, closefd, opener); + +exit: + return return_value; +} + +#endif /* defined(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +PyDoc_STRVAR(_io__WindowsConsoleIO_fileno__doc__, +"fileno($self, /)\n" +"--\n" +"\n" +"Return the underlying file descriptor (an integer).\n" +"\n" +"fileno is only set when a file descriptor is used to open\n" +"one of the standard streams."); + +#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(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +PyDoc_STRVAR(_io__WindowsConsoleIO_readinto__doc__, +"readinto($self, buffer, /)\n" +"--\n" +"\n" +"Same as RawIOBase.readinto()."); + +#define _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF \ + {"readinto", (PyCFunction)_io__WindowsConsoleIO_readinto, METH_O, _io__WindowsConsoleIO_readinto__doc__}, + +static PyObject * +_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer); + +static PyObject * +_io__WindowsConsoleIO_readinto(winconsoleio *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; - } + goto exit; + } if (!PyBuffer_IsContiguous(&buffer, 'C')) { _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg); goto exit; } - return_value = _io__WindowsConsoleIO_readinto_impl(self, &buffer); - -exit: - /* Cleanup for buffer */ - if (buffer.obj) { - PyBuffer_Release(&buffer); - } - - return return_value; -} - -#endif /* defined(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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 \ + return_value = _io__WindowsConsoleIO_readinto_impl(self, &buffer); + +exit: + /* Cleanup for buffer */ + if (buffer.obj) { + PyBuffer_Release(&buffer); + } + + return return_value; +} + +#endif /* defined(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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)(void(*)(void))_io__WindowsConsoleIO_read, METH_FASTCALL, _io__WindowsConsoleIO_read__doc__}, - -static PyObject * -_io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size); - -static PyObject * -_io__WindowsConsoleIO_read(winconsoleio *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - Py_ssize_t size = -1; - + +static PyObject * +_io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size); + +static PyObject * +_io__WindowsConsoleIO_read(winconsoleio *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; - } + goto exit; + } if (nargs < 1) { goto skip_optional; } @@ -278,112 +278,112 @@ _io__WindowsConsoleIO_read(winconsoleio *self, PyObject *const *args, Py_ssize_t goto exit; } skip_optional: - return_value = _io__WindowsConsoleIO_read_impl(self, size); - -exit: - return return_value; -} - -#endif /* defined(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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)_io__WindowsConsoleIO_write, METH_O, _io__WindowsConsoleIO_write__doc__}, - -static PyObject * -_io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b); - -static PyObject * -_io__WindowsConsoleIO_write(winconsoleio *self, PyObject *arg) -{ - PyObject *return_value = NULL; - Py_buffer b = {NULL, NULL}; - + return_value = _io__WindowsConsoleIO_read_impl(self, size); + +exit: + return return_value; +} + +#endif /* defined(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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)_io__WindowsConsoleIO_write, METH_O, _io__WindowsConsoleIO_write__doc__}, + +static PyObject * +_io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b); + +static PyObject * +_io__WindowsConsoleIO_write(winconsoleio *self, PyObject *arg) +{ + PyObject *return_value = NULL; + Py_buffer b = {NULL, NULL}; + if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) { - goto exit; - } + goto exit; + } if (!PyBuffer_IsContiguous(&b, 'C')) { _PyArg_BadArgument("write", "argument", "contiguous buffer", arg); goto exit; } - return_value = _io__WindowsConsoleIO_write_impl(self, &b); - -exit: - /* Cleanup for b */ - if (b.obj) { - PyBuffer_Release(&b); - } - - return return_value; -} - -#endif /* defined(MS_WINDOWS) */ - -#if defined(MS_WINDOWS) - -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(MS_WINDOWS) */ - -#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) */ + return_value = _io__WindowsConsoleIO_write_impl(self, &b); + +exit: + /* Cleanup for b */ + if (b.obj) { + PyBuffer_Release(&b); + } + + return return_value; +} + +#endif /* defined(MS_WINDOWS) */ + +#if defined(MS_WINDOWS) + +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(MS_WINDOWS) */ + +#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=f5b8860a658a001a input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_io/fileio.c b/contrib/tools/python3/src/Modules/_io/fileio.c index 048484bde54..6f9d32c334c 100644 --- a/contrib/tools/python3/src/Modules/_io/fileio.c +++ b/contrib/tools/python3/src/Modules/_io/fileio.c @@ -1,930 +1,930 @@ -/* Author: Daniel Stutzbach */ - -#define PY_SSIZE_T_CLEAN -#include "Python.h" +/* Author: Daniel Stutzbach */ + +#define PY_SSIZE_T_CLEAN +#include "Python.h" #include "pycore_object.h" #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 -#define WIN32_LEAN_AND_MEAN -#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 *" "&PyFileIO_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/ - -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; - -PyTypeObject PyFileIO_Type; - -_Py_IDENTIFIER(name); - -#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) - -/* Forward declarations */ +#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 +#define WIN32_LEAN_AND_MEAN +#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 *" "&PyFileIO_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c77708b41fda70c]*/ + +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; + +PyTypeObject PyFileIO_Type; + +_Py_IDENTIFIER(name); + +#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &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, *val, *tb; - PyErr_Fetch(&exc, &val, &tb); - 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_Restore(exc, val, tb); - } - 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 - -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) -/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/ -{ - PyObject *res; - PyObject *exc, *val, *tb; - int rc; - _Py_IDENTIFIER(close); + +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, *val, *tb; + PyErr_Fetch(&exc, &val, &tb); + 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_Restore(exc, val, tb); + } + 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 + +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) +/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/ +{ + PyObject *res; + PyObject *exc, *val, *tb; + int rc; + _Py_IDENTIFIER(close); res = _PyObject_CallMethodIdOneArg((PyObject*)&PyRawIOBase_Type, &PyId_close, (PyObject *)self); - if (!self->closefd) { - self->fd = -1; - return res; - } - if (res == NULL) - PyErr_Fetch(&exc, &val, &tb); - 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_ChainExceptions(exc, val, tb); - 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(accept={int}) = 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=1596c9157a042a39]*/ -{ -#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; - - assert(PyFileIO_Check(self)); - 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; - } - - if (PyFloat_Check(nameobj)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float"); - return -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; - } + if (!self->closefd) { + self->fd = -1; + return res; + } + if (res == NULL) + PyErr_Fetch(&exc, &val, &tb); + 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_ChainExceptions(exc, val, tb); + 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(accept={int}) = 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=1596c9157a042a39]*/ +{ +#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; + + assert(PyFileIO_Check(self)); + 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; + } + + if (PyFloat_Check(nameobj)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float"); + return -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; + } _Py_COMP_DIAG_PUSH _Py_COMP_DIAG_IGNORE_DEPR_DECLS - widename = PyUnicode_AsUnicode(stringobj); + widename = PyUnicode_AsUnicode(stringobj); _Py_COMP_DIAG_POP - 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 (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; - } - 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; - if (self->fd < 0) { - PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj); - goto error; - } - -#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_SetAttrId((PyObject *)self, &PyId_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()). */ + 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; + } + 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; + if (self->fd < 0) { + PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj); + goto error; + } + +#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_SetAttrId((PyObject *)self, &PyId_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) - internal_close(self); - - done: - Py_CLEAR(stringobj); - return ret; -} - -static int -fileio_traverse(fileio *self, visitproc visit, void *arg) -{ - Py_VISIT(self->dict); - return 0; -} - -static int -fileio_clear(fileio *self) -{ - Py_CLEAR(self->dict); - return 0; -} - -static void -fileio_dealloc(fileio *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); - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static PyObject * -err_closed(void) -{ - PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); - return NULL; -} - -static PyObject * -err_mode(const char *action) -{ - _PyIO_State *state = IO_STATE(); - if (state != NULL) - PyErr_Format(state->unsupported_operation, - "File not open for %s", action); - return NULL; -} - -/*[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 */ + if (pos == NULL) + goto error; + Py_DECREF(pos); + } + + goto done; + + error: + ret = -1; + if (!fd_is_own) + self->fd = -1; + if (self->fd >= 0) + internal_close(self); + + done: + Py_CLEAR(stringobj); + return ret; +} + +static int +fileio_traverse(fileio *self, visitproc visit, void *arg) +{ + Py_VISIT(self->dict); + return 0; +} + +static int +fileio_clear(fileio *self) +{ + Py_CLEAR(self->dict); + return 0; +} + +static void +fileio_dealloc(fileio *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); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyObject * +err_closed(void) +{ + PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); + return NULL; +} + +static PyObject * +err_mode(const char *action) +{ + _PyIO_State *state = IO_STATE(); + if (state != NULL) + PyErr_Format(state->unsupported_operation, + "File not open for %s", action); + return NULL; +} + +/*[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 - buffer: Py_buffer(accept={rwbuffer}) - / - -Same as RawIOBase.readinto(). -[clinic start generated code]*/ - -static PyObject * -_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer) -/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/ -{ - Py_ssize_t n; - int err; - - if (self->fd < 0) - return err_closed(); - if (!self->readable) - return err_mode("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 - 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, Py_ssize_t size) -/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/ -{ - char *ptr; - Py_ssize_t n; - PyObject *bytes; - - if (self->fd < 0) - return err_closed(); - if (!self->readable) - return err_mode("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 - 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, Py_buffer *b) -/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/ -{ - Py_ssize_t n; - int err; - - if (self->fd < 0) - return err_closed(); - if (!self->writable) - return err_mode("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 * + assert(self->seekable >= 0); + if (pos == NULL) { + PyErr_Clear(); + } + else { + Py_DECREF(pos); + } + } + return PyBool_FromLong((long) self->seekable); +} + +/*[clinic input] +_io.FileIO.readinto + buffer: Py_buffer(accept={rwbuffer}) + / + +Same as RawIOBase.readinto(). +[clinic start generated code]*/ + +static PyObject * +_io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer) +/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/ +{ + Py_ssize_t n; + int err; + + if (self->fd < 0) + return err_closed(); + if (!self->readable) + return err_mode("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 + 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, Py_ssize_t size) +/*[clinic end generated code: output=42528d39dd0ca641 input=bec9a2c704ddcbc9]*/ +{ + char *ptr; + Py_ssize_t n; + PyObject *bytes; + + if (self->fd < 0) + return err_closed(); + if (!self->readable) + return err_mode("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 + 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, Py_buffer *b) +/*[clinic end generated code: output=b4059db3d363a2f7 input=6e7908b36f0ce74f]*/ +{ + Py_ssize_t n; + int err; + + if (self->fd < 0) + return err_closed(); + if (!self->writable) + return err_mode("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(PyFloat_Check(posobj)) { - PyErr_SetString(PyExc_TypeError, "an integer is required"); - return NULL; - } -#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); - } - +{ + 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(PyFloat_Check(posobj)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } +#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; @@ -932,310 +932,310 @@ portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_er 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(); - + +#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(); - +} + +/*[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 +} + +#ifdef HAVE_FTRUNCATE +/*[clinic input] +_io.FileIO.truncate 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, PyObject *posobj) + / + +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, PyObject *posobj) /*[clinic end generated code: output=e49ca7a916c176fa input=b0ac133939823875]*/ -{ - Py_off_t pos; - int ret; - int fd; - - fd = self->fd; - if (fd < 0) - return err_closed(); - if (!self->writable) - return err_mode("writing"); - +{ + Py_off_t pos; + int ret; + int fd; + + fd = self->fd; + if (fd < 0) + return err_closed(); + if (!self->writable) + return err_mode("writing"); + if (posobj == Py_None) { - /* Get the current position. */ + /* 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) { - Py_DECREF(posobj); - PyErr_SetFromErrno(PyExc_OSError); - 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_LookupAttrId((PyObject *) self, &PyId_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}, - {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}, - {NULL} -}; - -PyTypeObject PyFileIO_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.FileIO", - sizeof(fileio), - 0, - (destructor)fileio_dealloc, /* tp_dealloc */ + 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) { + Py_DECREF(posobj); + PyErr_SetFromErrno(PyExc_OSError); + 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_LookupAttrId((PyObject *) self, &PyId_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}, + {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}, + {NULL} +}; + +PyTypeObject PyFileIO_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.FileIO", + sizeof(fileio), + 0, + (destructor)fileio_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc)fileio_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)fileio_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - _io_FileIO___init____doc__, /* tp_doc */ - (traverseproc)fileio_traverse, /* tp_traverse */ - (inquiry)fileio_clear, /* tp_clear */ - 0, /* tp_richcompare */ + _io_FileIO___init____doc__, /* tp_doc */ + (traverseproc)fileio_traverse, /* tp_traverse */ + (inquiry)fileio_clear, /* tp_clear */ + 0, /* tp_richcompare */ offsetof(fileio, weakreflist), /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - fileio_methods, /* tp_methods */ - fileio_members, /* tp_members */ - fileio_getsetlist, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + fileio_methods, /* tp_methods */ + fileio_members, /* tp_members */ + fileio_getsetlist, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ offsetof(fileio, dict), /* tp_dictoffset */ - _io_FileIO___init__, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - fileio_new, /* tp_new */ - PyObject_GC_Del, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; + _io_FileIO___init__, /* tp_init */ + PyType_GenericAlloc, /* tp_alloc */ + fileio_new, /* tp_new */ + PyObject_GC_Del, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; diff --git a/contrib/tools/python3/src/Modules/_io/iobase.c b/contrib/tools/python3/src/Modules/_io/iobase.c index a8e55c34799..dab8eda4f10 100644 --- a/contrib/tools/python3/src/Modules/_io/iobase.c +++ b/contrib/tools/python3/src/Modules/_io/iobase.c @@ -1,290 +1,290 @@ -/* - 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" +/* + 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_object.h" #include <stddef.h> // offsetof() -#include "_iomodule.h" - -/*[clinic input] -module _io -class _io._IOBase "PyObject *" "&PyIOBase_Type" -class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/ - -/* - * 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, acting on streams of\n" - "bytes. There is no public constructor.\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. */ - -_Py_IDENTIFIER(__IOBase_closed); -_Py_IDENTIFIER(read); - - -/* Internal methods */ -static PyObject * -iobase_unsupported(const char *message) -{ - _PyIO_State *state = IO_STATE(); - if (state != NULL) - PyErr_SetString(state->unsupported_operation, message); - return NULL; -} - -/* Positioning */ - -PyDoc_STRVAR(iobase_seek_doc, - "Change stream position.\n" - "\n" - "Change the stream position to the given byte offset. The offset is\n" - "interpreted relative to the position indicated by whence. Values\n" - "for whence are:\n" - "\n" - "* 0 -- start of stream (the default); offset should be zero or positive\n" - "* 1 -- current stream position; offset may be negative\n" - "* 2 -- end of stream; offset is usually negative\n" - "\n" - "Return the new absolute position."); - -static PyObject * -iobase_seek(PyObject *self, PyObject *args) -{ - return iobase_unsupported("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]*/ -{ - _Py_IDENTIFIER(seek); - - return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); -} - -PyDoc_STRVAR(iobase_truncate_doc, - "Truncate file to size bytes.\n" - "\n" - "File pointer is left unchanged. Size defaults to the current IO\n" - "position as reported by tell(). Returns the new size."); - -static PyObject * -iobase_truncate(PyObject *self, PyObject *args) -{ - return iobase_unsupported("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_LookupAttrId(self, &PyId___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, _PyIO_str_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; -} - -/* 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]*/ -{ - PyObject *res, *exc, *val, *tb; - int rc, closed = iobase_is_closed(self); - - if (closed < 0) { - return NULL; - } - if (closed) { - Py_RETURN_NONE; - } - +#include "_iomodule.h" + +/*[clinic input] +module _io +class _io._IOBase "PyObject *" "&PyIOBase_Type" +class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/ + +/* + * 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, acting on streams of\n" + "bytes. There is no public constructor.\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. */ + +_Py_IDENTIFIER(__IOBase_closed); +_Py_IDENTIFIER(read); + + +/* Internal methods */ +static PyObject * +iobase_unsupported(const char *message) +{ + _PyIO_State *state = IO_STATE(); + if (state != NULL) + PyErr_SetString(state->unsupported_operation, message); + return NULL; +} + +/* Positioning */ + +PyDoc_STRVAR(iobase_seek_doc, + "Change stream position.\n" + "\n" + "Change the stream position to the given byte offset. The offset is\n" + "interpreted relative to the position indicated by whence. Values\n" + "for whence are:\n" + "\n" + "* 0 -- start of stream (the default); offset should be zero or positive\n" + "* 1 -- current stream position; offset may be negative\n" + "* 2 -- end of stream; offset is usually negative\n" + "\n" + "Return the new absolute position."); + +static PyObject * +iobase_seek(PyObject *self, PyObject *args) +{ + return iobase_unsupported("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]*/ +{ + _Py_IDENTIFIER(seek); + + return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); +} + +PyDoc_STRVAR(iobase_truncate_doc, + "Truncate file to size bytes.\n" + "\n" + "File pointer is left unchanged. Size defaults to the current IO\n" + "position as reported by tell(). Returns the new size."); + +static PyObject * +iobase_truncate(PyObject *self, PyObject *args) +{ + return iobase_unsupported("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_LookupAttrId(self, &PyId___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, _PyIO_str_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; +} + +/* 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]*/ +{ + PyObject *res, *exc, *val, *tb; + int rc, closed = iobase_is_closed(self); + + if (closed < 0) { + return NULL; + } + if (closed) { + Py_RETURN_NONE; + } + res = PyObject_CallMethodNoArgs(self, _PyIO_str_flush); - - PyErr_Fetch(&exc, &val, &tb); - rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True); - _PyErr_ChainExceptions(exc, val, tb); - 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; - PyObject *error_type, *error_value, *error_traceback; - int closed; - _Py_IDENTIFIER(_finalizing); - - /* Save the current exception, if any. */ - PyErr_Fetch(&error_type, &error_value, &error_traceback); - - /* 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, _PyIO_str_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_SetAttrId(self, &PyId__finalizing, Py_True)) - PyErr_Clear(); + + PyErr_Fetch(&exc, &val, &tb); + rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True); + _PyErr_ChainExceptions(exc, val, tb); + 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; + PyObject *error_type, *error_value, *error_traceback; + int closed; + _Py_IDENTIFIER(_finalizing); + + /* Save the current exception, if any. */ + PyErr_Fetch(&error_type, &error_value, &error_traceback); + + /* 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, _PyIO_str_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_SetAttrId(self, &PyId__finalizing, Py_True)) + PyErr_Clear(); res = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_close); - /* Silencing I/O errors is bad, but printing spurious tracebacks is - equally as bad, and potentially more frequent (because of - shutdown issues). */ + /* 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) { @@ -298,784 +298,784 @@ iobase_finalize(PyObject *self) #endif } else { - Py_DECREF(res); + Py_DECREF(res); } - } - - /* Restore the saved exception. */ - PyErr_Restore(error_type, error_value, error_traceback); -} - -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(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; - } - _PyObject_GC_UNTRACK(self); - if (self->weakreflist != NULL) - PyObject_ClearWeakRefs((PyObject *) self); - Py_CLEAR(self->dict); - Py_TYPE(self)->tp_free((PyObject *) self); -} - -/* 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(PyObject *self, PyObject *args) -{ + } + + /* Restore the saved exception. */ + PyErr_Restore(error_type, error_value, error_traceback); +} + +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(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; + } + _PyObject_GC_UNTRACK(self); + if (self->weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *) self); + Py_CLEAR(self->dict); + Py_TYPE(self)->tp_free((PyObject *) self); +} + +/* 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(PyObject *self, PyObject *args) +{ PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_seekable); - if (res == NULL) - return NULL; - if (res != Py_True) { - Py_CLEAR(res); - iobase_unsupported("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(PyObject *self, PyObject *args) -{ + if (res == NULL) + return NULL; + if (res != Py_True) { + Py_CLEAR(res); + iobase_unsupported("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(PyObject *self, PyObject *args) +{ PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_readable); - if (res == NULL) - return NULL; - if (res != Py_True) { - Py_CLEAR(res); - iobase_unsupported("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(PyObject *self, PyObject *args) -{ + if (res == NULL) + return NULL; + if (res != Py_True) { + Py_CLEAR(res); + iobase_unsupported("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(PyObject *self, PyObject *args) +{ PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_writable); - if (res == NULL) - return NULL; - if (res != Py_True) { - Py_CLEAR(res); - iobase_unsupported("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; - - Py_INCREF(self); - return self; -} - -static PyObject * -iobase_exit(PyObject *self, PyObject *args) -{ + if (res == NULL) + return NULL; + if (res != Py_True) { + Py_CLEAR(res); + iobase_unsupported("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; + + Py_INCREF(self); + return self; +} + +static PyObject * +iobase_exit(PyObject *self, PyObject *args) +{ return PyObject_CallMethodNoArgs(self, _PyIO_str_close); -} - -/* Lower-level APIs */ - -/* XXX Should these be present even if unimplemented? */ - -/*[clinic input] -_io._IOBase.fileno - -Returns underlying file descriptor if one exists. - -OSError is raised if the IO object does not use a file descriptor. -[clinic start generated code]*/ - -static PyObject * -_io__IOBase_fileno_impl(PyObject *self) -/*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ -{ - return iobase_unsupported("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, _PyIO_str_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) { +} + +/* Lower-level APIs */ + +/* XXX Should these be present even if unimplemented? */ + +/*[clinic input] +_io._IOBase.fileno + +Returns underlying file descriptor if one exists. + +OSError is raised if the IO object does not use a file descriptor. +[clinic start generated code]*/ + +static PyObject * +_io__IOBase_fileno_impl(PyObject *self) +/*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ +{ + return iobase_unsupported("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, _PyIO_str_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_One); - 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_CallMethodId(self, &PyId_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; - - Py_INCREF(self); - return self; -} - -static PyObject * -iobase_iternext(PyObject *self) -{ + 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_CallMethodId(self, &PyId_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; + + Py_INCREF(self); + return self; +} + +static PyObject * +iobase_iternext(PyObject *self) +{ PyObject *line = PyObject_CallMethodNoArgs(self, _PyIO_str_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. */ - _Py_IDENTIFIER(extend); - PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_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 - / + + 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. */ + _Py_IDENTIFIER(extend); + PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_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 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, _PyIO_str_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; -} - -#include "clinic/iobase.c.h" - -static PyMethodDef iobase_methods[] = { - {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, - _IO__IOBASE_TELL_METHODDEF - {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, - _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", _PyIOBase_check_seekable, METH_NOARGS}, - {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, - {"_checkWritable", _PyIOBase_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} -}; - - -PyTypeObject PyIOBase_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._IOBase", /*tp_name*/ - sizeof(iobase), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)iobase_dealloc, /*tp_dealloc*/ +{ + 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, _PyIO_str_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; +} + +#include "clinic/iobase.c.h" + +static PyMethodDef iobase_methods[] = { + {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, + _IO__IOBASE_TELL_METHODDEF + {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, + _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", _PyIOBase_check_seekable, METH_NOARGS}, + {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, + {"_checkWritable", _PyIOBase_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} +}; + + +PyTypeObject PyIOBase_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io._IOBase", /*tp_name*/ + sizeof(iobase), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)iobase_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - iobase_doc, /* tp_doc */ - (traverseproc)iobase_traverse, /* tp_traverse */ - (inquiry)iobase_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(iobase, weakreflist), /* tp_weaklistoffset */ - iobase_iter, /* tp_iter */ - iobase_iternext, /* tp_iternext */ - iobase_methods, /* tp_methods */ - 0, /* tp_members */ - iobase_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(iobase, dict), /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - iobase_finalize, /* tp_finalize */ -}; - - -/* - * 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) { - _Py_IDENTIFIER(readall); - + iobase_doc, /* tp_doc */ + (traverseproc)iobase_traverse, /* tp_traverse */ + (inquiry)iobase_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(iobase, weakreflist), /* tp_weaklistoffset */ + iobase_iter, /* tp_iter */ + iobase_iternext, /* tp_iternext */ + iobase_methods, /* tp_methods */ + 0, /* tp_members */ + iobase_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(iobase, dict), /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + iobase_finalize, /* tp_finalize */ +}; + + +/* + * 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) { + _Py_IDENTIFIER(readall); + return _PyObject_CallMethodIdNoArgs(self, &PyId_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, _PyIO_str_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_CallMethodId(self, &PyId_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(_PyIO_empty_bytes, 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} -}; - -PyTypeObject PyRawIOBase_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._RawIOBase", /*tp_name*/ - 0, /*tp_basicsize*/ - 0, /*tp_itemsize*/ - 0, /*tp_dealloc*/ + } + + /* 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, _PyIO_str_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_CallMethodId(self, &PyId_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(_PyIO_empty_bytes, 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} +}; + +PyTypeObject PyRawIOBase_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io._RawIOBase", /*tp_name*/ + 0, /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - rawiobase_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - rawiobase_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - &PyIOBase_Type, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - 0, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; + rawiobase_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + rawiobase_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + &PyIOBase_Type, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; diff --git a/contrib/tools/python3/src/Modules/_io/stringio.c b/contrib/tools/python3/src/Modules/_io/stringio.c index e76152e617b..dedbc18bcca 100644 --- a/contrib/tools/python3/src/Modules/_io/stringio.c +++ b/contrib/tools/python3/src/Modules/_io/stringio.c @@ -1,1044 +1,1044 @@ -#define PY_SSIZE_T_CLEAN -#include "Python.h" +#define PY_SSIZE_T_CLEAN +#include "Python.h" #include <stddef.h> // offsetof() #include "pycore_accu.h" #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 *" "&PyStringIO_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c17bc0f42165cd7d]*/ - -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 _PyAccu structure. - In realized state, the internal buffer is meaningful and the - _PyAccu is destroyed. - */ - int state; - _PyAccu accu; - - char ok; /* initialized? */ - char closed; - char readuniversal; - char readtranslate; - PyObject *decoder; - PyObject *readnl; - PyObject *writenl; - - PyObject *dict; - PyObject *weakreflist; -} 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 = _PyAccu_Finish(&self->accu); - self->state = STATE_REALIZED; - if (intermediate == NULL) - return NULL; - if (_PyAccu_Init(&self->accu) || - _PyAccu_Accumulate(&self->accu, 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 = _PyAccu_Finish(&self->accu); - 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 = obj; - Py_INCREF(decoded); - } - if (self->writenl) { - PyObject *translated = PyUnicode_Replace( - decoded, _PyIO_str_nl, self->writenl, -1); - Py_DECREF(decoded); - 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 (_PyAccu_Accumulate(&self->accu, 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); - +#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 *" "&PyStringIO_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c17bc0f42165cd7d]*/ + +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 _PyAccu structure. + In realized state, the internal buffer is meaningful and the + _PyAccu is destroyed. + */ + int state; + _PyAccu accu; + + char ok; /* initialized? */ + char closed; + char readuniversal; + char readtranslate; + PyObject *decoder; + PyObject *readnl; + PyObject *writenl; + + PyObject *dict; + PyObject *weakreflist; +} 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 = _PyAccu_Finish(&self->accu); + self->state = STATE_REALIZED; + if (intermediate == NULL) + return NULL; + if (_PyAccu_Init(&self->accu) || + _PyAccu_Accumulate(&self->accu, 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 = _PyAccu_Finish(&self->accu); + 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 = obj; + Py_INCREF(decoded); + } + if (self->writenl) { + PyObject *translated = PyUnicode_Replace( + decoded, _PyIO_str_nl, self->writenl, -1); + Py_DECREF(decoded); + 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 (_PyAccu_Accumulate(&self->accu, 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, &PyStringIO_Type)) { - /* Skip method call overhead for speed */ - line = _stringio_readline(self, -1); - } - else { - /* XXX is subclassing StringIO really supported? */ + /* Skip method call overhead for speed */ + line = _stringio_readline(self, -1); + } + else { + /* XXX is subclassing StringIO really supported? */ line = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_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; - _PyAccu_Destroy(&self->accu); - 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(self->dict); - return 0; -} - -static int -stringio_clear(stringio *self) -{ - Py_CLEAR(self->dict); - return 0; -} - -static void -stringio_dealloc(stringio *self) -{ - _PyObject_GC_UNTRACK(self); - self->ok = 0; - if (self->buf) { - PyMem_Free(self->buf); - self->buf = NULL; - } - _PyAccu_Destroy(&self->accu); - Py_CLEAR(self->readnl); - Py_CLEAR(self->writenl); - Py_CLEAR(self->decoder); - Py_CLEAR(self->dict); - if (self->weakreflist != NULL) - PyObject_ClearWeakRefs((PyObject *) self); - Py_TYPE(self)->tp_free(self); -} - -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; - - _PyAccu_Destroy(&self->accu); - 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 = self->readnl; - Py_INCREF(self->writenl); - } - - if (self->readuniversal) { + 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; + _PyAccu_Destroy(&self->accu); + 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(self->dict); + return 0; +} + +static int +stringio_clear(stringio *self) +{ + Py_CLEAR(self->dict); + return 0; +} + +static void +stringio_dealloc(stringio *self) +{ + _PyObject_GC_UNTRACK(self); + self->ok = 0; + if (self->buf) { + PyMem_Free(self->buf); + self->buf = NULL; + } + _PyAccu_Destroy(&self->accu); + Py_CLEAR(self->readnl); + Py_CLEAR(self->writenl); + Py_CLEAR(self->decoder); + Py_CLEAR(self->dict); + if (self->weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *) self); + Py_TYPE(self)->tp_free(self); +} + +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; + + _PyAccu_Destroy(&self->accu); + 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 = self->readnl; + Py_INCREF(self->writenl); + } + + if (self->readuniversal) { self->decoder = PyObject_CallFunctionObjArgs( - (PyObject *)&PyIncrementalNewlineDecoder_Type, + (PyObject *)&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; - if (_PyAccu_Init(&self->accu)) - return -1; - self->state = STATE_ACCUMULATING; - } - self->pos = 0; - - 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 * + 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; + if (_PyAccu_Init(&self->accu)) + return -1; + self->state = STATE_ACCUMULATING; + } + self->pos = 0; + + 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) { - Py_INCREF(Py_None); - dict = 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 +{ + PyObject *initvalue = _io_StringIO_getvalue_impl(self); + PyObject *dict; + PyObject *state; + + if (initvalue == NULL) + return NULL; + if (self->dict == NULL) { + Py_INCREF(Py_None); + dict = 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 { - Py_INCREF(dict); - self->dict = 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, _PyIO_str_newlines); -} - -#include "clinic/stringio.c.h" - -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} -}; - -PyTypeObject PyStringIO_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.StringIO", /*tp_name*/ - sizeof(stringio), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)stringio_dealloc, /*tp_dealloc*/ + 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 { + Py_INCREF(dict); + self->dict = 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, _PyIO_str_newlines); +} + +#include "clinic/stringio.c.h" + +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} +}; + +PyTypeObject PyStringIO_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.StringIO", /*tp_name*/ + sizeof(stringio), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)stringio_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE - | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_StringIO___init____doc__, /*tp_doc*/ - (traverseproc)stringio_traverse, /*tp_traverse*/ - (inquiry)stringio_clear, /*tp_clear*/ - 0, /*tp_richcompare*/ - offsetof(stringio, weakreflist), /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - (iternextfunc)stringio_iternext, /*tp_iternext*/ - stringio_methods, /*tp_methods*/ - 0, /*tp_members*/ - stringio_getset, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - offsetof(stringio, dict), /*tp_dictoffset*/ - _io_StringIO___init__, /*tp_init*/ - 0, /*tp_alloc*/ - stringio_new, /*tp_new*/ -}; + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + _io_StringIO___init____doc__, /*tp_doc*/ + (traverseproc)stringio_traverse, /*tp_traverse*/ + (inquiry)stringio_clear, /*tp_clear*/ + 0, /*tp_richcompare*/ + offsetof(stringio, weakreflist), /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + (iternextfunc)stringio_iternext, /*tp_iternext*/ + stringio_methods, /*tp_methods*/ + 0, /*tp_members*/ + stringio_getset, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + offsetof(stringio, dict), /*tp_dictoffset*/ + _io_StringIO___init__, /*tp_init*/ + 0, /*tp_alloc*/ + stringio_new, /*tp_new*/ +}; diff --git a/contrib/tools/python3/src/Modules/_io/textio.c b/contrib/tools/python3/src/Modules/_io/textio.c index 966c532a0e3..57d669e9ff5 100644 --- a/contrib/tools/python3/src/Modules/_io/textio.c +++ b/contrib/tools/python3/src/Modules/_io/textio.c @@ -1,783 +1,783 @@ -/* - 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" +/* + 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_object.h" #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "structmember.h" // PyMemberDef -#include "_iomodule.h" - -/*[clinic input] -module _io -class _io.IncrementalNewlineDecoder "nldecoder_object *" "&PyIncrementalNewlineDecoder_Type" -class _io.TextIOWrapper "textio *" "&TextIOWrapper_TYpe" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2097a4fc85670c26]*/ - -_Py_IDENTIFIER(close); -_Py_IDENTIFIER(_dealloc_warn); -_Py_IDENTIFIER(decode); -_Py_IDENTIFIER(fileno); -_Py_IDENTIFIER(flush); -_Py_IDENTIFIER(getpreferredencoding); -_Py_IDENTIFIER(isatty); -_Py_IDENTIFIER(mode); -_Py_IDENTIFIER(name); -_Py_IDENTIFIER(raw); -_Py_IDENTIFIER(read); -_Py_IDENTIFIER(readable); -_Py_IDENTIFIER(replace); -_Py_IDENTIFIER(reset); -_Py_IDENTIFIER(seek); -_Py_IDENTIFIER(seekable); -_Py_IDENTIFIER(setstate); -_Py_IDENTIFIER(strict); -_Py_IDENTIFIER(tell); -_Py_IDENTIFIER(writable); - -/* 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. There is no public constructor.\n" - ); - -static PyObject * -_unsupported(const char *message) -{ - _PyIO_State *state = IO_STATE(); - if (state != NULL) - PyErr_SetString(state->unsupported_operation, message); - return NULL; -} - -PyDoc_STRVAR(textiobase_detach_doc, - "Separate the underlying buffer from the TextIOBase and return it.\n" - "\n" - "After the underlying buffer has been detached, the TextIO is in an\n" - "unusable state.\n" - ); - -static PyObject * +#include "_iomodule.h" + +/*[clinic input] +module _io +class _io.IncrementalNewlineDecoder "nldecoder_object *" "&PyIncrementalNewlineDecoder_Type" +class _io.TextIOWrapper "textio *" "&TextIOWrapper_TYpe" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2097a4fc85670c26]*/ + +_Py_IDENTIFIER(close); +_Py_IDENTIFIER(_dealloc_warn); +_Py_IDENTIFIER(decode); +_Py_IDENTIFIER(fileno); +_Py_IDENTIFIER(flush); +_Py_IDENTIFIER(getpreferredencoding); +_Py_IDENTIFIER(isatty); +_Py_IDENTIFIER(mode); +_Py_IDENTIFIER(name); +_Py_IDENTIFIER(raw); +_Py_IDENTIFIER(read); +_Py_IDENTIFIER(readable); +_Py_IDENTIFIER(replace); +_Py_IDENTIFIER(reset); +_Py_IDENTIFIER(seek); +_Py_IDENTIFIER(seekable); +_Py_IDENTIFIER(setstate); +_Py_IDENTIFIER(strict); +_Py_IDENTIFIER(tell); +_Py_IDENTIFIER(writable); + +/* 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. There is no public constructor.\n" + ); + +static PyObject * +_unsupported(const char *message) +{ + _PyIO_State *state = IO_STATE(); + if (state != NULL) + PyErr_SetString(state->unsupported_operation, message); + return NULL; +} + +PyDoc_STRVAR(textiobase_detach_doc, + "Separate the underlying buffer from the TextIOBase and return it.\n" + "\n" + "After the underlying buffer has been detached, the TextIO is in an\n" + "unusable state.\n" + ); + +static PyObject * textiobase_detach(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - return _unsupported("detach"); -} - -PyDoc_STRVAR(textiobase_read_doc, - "Read at most n characters from stream.\n" - "\n" - "Read from underlying buffer until we have n characters or we hit EOF.\n" - "If n is negative or omitted, read until EOF.\n" - ); - -static PyObject * -textiobase_read(PyObject *self, PyObject *args) -{ - return _unsupported("read"); -} - -PyDoc_STRVAR(textiobase_readline_doc, - "Read until newline or EOF.\n" - "\n" - "Returns an empty string if EOF is hit immediately.\n" - ); - -static PyObject * -textiobase_readline(PyObject *self, PyObject *args) -{ - return _unsupported("readline"); -} - -PyDoc_STRVAR(textiobase_write_doc, - "Write string to stream.\n" - "Returns the number of characters written (which is always equal to\n" - "the length of the string).\n" - ); - -static PyObject * -textiobase_write(PyObject *self, PyObject *args) -{ - return _unsupported("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[] = { +{ + return _unsupported("detach"); +} + +PyDoc_STRVAR(textiobase_read_doc, + "Read at most n characters from stream.\n" + "\n" + "Read from underlying buffer until we have n characters or we hit EOF.\n" + "If n is negative or omitted, read until EOF.\n" + ); + +static PyObject * +textiobase_read(PyObject *self, PyObject *args) +{ + return _unsupported("read"); +} + +PyDoc_STRVAR(textiobase_readline_doc, + "Read until newline or EOF.\n" + "\n" + "Returns an empty string if EOF is hit immediately.\n" + ); + +static PyObject * +textiobase_readline(PyObject *self, PyObject *args) +{ + return _unsupported("readline"); +} + +PyDoc_STRVAR(textiobase_write_doc, + "Write string to stream.\n" + "Returns the number of characters written (which is always equal to\n" + "the length of the string).\n" + ); + +static PyObject * +textiobase_write(PyObject *self, PyObject *args) +{ + return _unsupported("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[] = { {"detach", textiobase_detach, METH_NOARGS, textiobase_detach_doc}, - {"read", textiobase_read, METH_VARARGS, textiobase_read_doc}, - {"readline", textiobase_readline, METH_VARARGS, textiobase_readline_doc}, - {"write", textiobase_write, METH_VARARGS, textiobase_write_doc}, - {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} -}; - -PyTypeObject PyTextIOBase_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._TextIOBase", /*tp_name*/ - 0, /*tp_basicsize*/ - 0, /*tp_itemsize*/ - 0, /*tp_dealloc*/ + {"read", textiobase_read, METH_VARARGS, textiobase_read_doc}, + {"readline", textiobase_readline, METH_VARARGS, textiobase_readline_doc}, + {"write", textiobase_write, METH_VARARGS, textiobase_write_doc}, + {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} +}; + +PyTypeObject PyTextIOBase_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io._TextIOBase", /*tp_name*/ + 0, /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - textiobase_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - textiobase_methods, /* tp_methods */ - 0, /* tp_members */ - textiobase_getset, /* tp_getset */ - &PyIOBase_Type, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - 0, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - - -/* IncrementalNewlineDecoder */ - -typedef struct { - PyObject_HEAD - PyObject *decoder; - PyObject *errors; - unsigned int pendingcr: 1; - unsigned int translate: 1; - unsigned int seennl: 3; -} nldecoder_object; - -/*[clinic input] -_io.IncrementalNewlineDecoder.__init__ - decoder: object - translate: int - 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=89db6b19c6b126bf]*/ -{ - self->decoder = decoder; - Py_INCREF(decoder); - - if (errors == NULL) { - self->errors = _PyUnicode_FromId(&PyId_strict); - if (self->errors == NULL) - return -1; - } - else { - self->errors = errors; - } - Py_INCREF(self->errors); - - self->translate = translate ? 1 : 0; - self->seennl = 0; - self->pendingcr = 0; - - return 0; -} - -static void -incrementalnewlinedecoder_dealloc(nldecoder_object *self) -{ - Py_CLEAR(self->decoder); - Py_CLEAR(self->errors); - Py_TYPE(self)->tp_free((PyObject *)self); -} - -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 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; - - if (self->decoder == NULL) { - PyErr_SetString(PyExc_ValueError, - "IncrementalNewlineDecoder.__init__ not called"); - return NULL; - } - - /* decode input (with the eventual \r from a previous pass) */ - if (self->decoder != Py_None) { - output = PyObject_CallMethodObjArgs(self->decoder, - _PyIO_str_decode, input, final ? Py_True : Py_False, NULL); - } - else { - output = input; - Py_INCREF(output); - } - - 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); + textiobase_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + textiobase_methods, /* tp_methods */ + 0, /* tp_members */ + textiobase_getset, /* tp_getset */ + &PyIOBase_Type, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + + +/* IncrementalNewlineDecoder */ + +typedef struct { + PyObject_HEAD + PyObject *decoder; + PyObject *errors; + unsigned int pendingcr: 1; + unsigned int translate: 1; + unsigned int seennl: 3; +} nldecoder_object; + +/*[clinic input] +_io.IncrementalNewlineDecoder.__init__ + decoder: object + translate: int + 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=89db6b19c6b126bf]*/ +{ + self->decoder = decoder; + Py_INCREF(decoder); + + if (errors == NULL) { + self->errors = _PyUnicode_FromId(&PyId_strict); + if (self->errors == NULL) + return -1; + } + else { + self->errors = errors; + } + Py_INCREF(self->errors); + + self->translate = translate ? 1 : 0; + self->seennl = 0; + self->pendingcr = 0; + + return 0; +} + +static void +incrementalnewlinedecoder_dealloc(nldecoder_object *self) +{ + Py_CLEAR(self->decoder); + Py_CLEAR(self->errors); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +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 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; + + if (self->decoder == NULL) { + PyErr_SetString(PyExc_ValueError, + "IncrementalNewlineDecoder.__init__ not called"); + return NULL; + } + + /* decode input (with the eventual \r from a previous pass) */ + if (self->decoder != Py_None) { + output = PyObject_CallMethodObjArgs(self->decoder, + _PyIO_str_decode, input, final ? Py_True : Py_False, NULL); + } + else { + output = input; + Py_INCREF(output); + } + + 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_DECREF(output); - 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_DECREF(output); - output = modified; - self->pendingcr = 1; - } - } - - /* Record which newlines are read and do newline translation if desired, - all in one pass. */ - { + memcpy(out + kind, PyUnicode_DATA(output), kind * output_len); + Py_DECREF(output); + 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_DECREF(output); + 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); + 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(accept={int}) = 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=a4ea97f26372d866]*/ -{ - 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; - - if (self->decoder != Py_None) { + 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(accept={int}) = 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=a4ea97f26372d866]*/ +{ + 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; + + if (self->decoder != Py_None) { PyObject *state = PyObject_CallMethodNoArgs(self->decoder, _PyIO_str_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; - - 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_CallMethodId(self->decoder, - &PyId_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]*/ -{ - self->seennl = 0; - self->pendingcr = 0; - if (self->decoder != Py_None) + 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; + + 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_CallMethodId(self->decoder, + &PyId_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]*/ +{ + self->seennl = 0; + self->pendingcr = 0; + if (self->decoder != Py_None) return PyObject_CallMethodNoArgs(self->decoder, _PyIO_str_reset); - else - Py_RETURN_NONE; -} - -static PyObject * -incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context) -{ - 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 *); - -typedef struct -{ - 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() */ + else + Py_RETURN_NONE; +} + +static PyObject * +incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context) +{ + 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 *); + +typedef struct +{ + 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; -} textio; - -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)); -} - + 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; +} textio; + +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) @@ -787,209 +787,209 @@ is_asciicompat_encoding(encodefunc_t f) || 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; - +/* 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_CallMethodIdNoArgs(self->buffer, &PyId_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) { + 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) { PyObject *incrementalDecoder = PyObject_CallFunctionObjArgs( - (PyObject *)&PyIncrementalNewlineDecoder_Type, + (PyObject *)&PyIncrementalNewlineDecoder_Type, self->decoder, self->readtranslate ? Py_True : Py_False, NULL); - if (incrementalDecoder == NULL) - return -1; - Py_CLEAR(self->decoder); - self->decoder = incrementalDecoder; - } - - return 0; -} - -static PyObject* -_textiowrapper_decode(PyObject *decoder, PyObject *bytes, int eof) -{ - PyObject *chars; - + if (incrementalDecoder == NULL) + return -1; + Py_CLEAR(self->decoder); + self->decoder = incrementalDecoder; + } + + return 0; +} + +static PyObject* +_textiowrapper_decode(PyObject *decoder, PyObject *bytes, int eof) +{ + PyObject *chars; + if (Py_IS_TYPE(decoder, &PyIncrementalNewlineDecoder_Type)) - chars = _PyIncrementalNewlineDecoder_decode(decoder, bytes, eof); - else - chars = PyObject_CallMethodObjArgs(decoder, _PyIO_str_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; - + chars = _PyIncrementalNewlineDecoder_decode(decoder, bytes, eof); + else + chars = PyObject_CallMethodObjArgs(decoder, _PyIO_str_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_CallMethodIdNoArgs(self->buffer, &PyId_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_LookupAttrId(codec_info, &PyId_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; - + 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_LookupAttrId(codec_info, &PyId_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, _PyIO_str_tell); - if (cookieObj == NULL) { - return -1; - } - - int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); - Py_DECREF(cookieObj); - if (cmp < 0) { - return -1; - } - - if (cmp == 0) { - self->encoding_start_of_stream = 0; + if (cookieObj == NULL) { + return -1; + } + + int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, 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, _PyIO_str_setstate, _PyLong_Zero); - if (res == NULL) { - return -1; - } - Py_DECREF(res); - } - - return 0; -} - + if (res == NULL) { + return -1; + } + Py_DECREF(res); + } + + return 0; +} + static int io_check_errors(PyObject *errors) { @@ -1030,521 +1030,521 @@ io_check_errors(PyObject *errors) -/*[clinic input] -_io.TextIOWrapper.__init__ - buffer: object +/*[clinic input] +_io.TextIOWrapper.__init__ + buffer: object encoding: str(accept={str, NoneType}) = None - errors: object = None + errors: object = None newline: str(accept={str, NoneType}) = None - line_buffering: bool(accept={int}) = False - write_through: bool(accept={int}) = 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.getpreferredencoding(False). - -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) + line_buffering: bool(accept={int}) = False + write_through: bool(accept={int}) = 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.getpreferredencoding(False). + +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=77d8696d1a1f460b]*/ -{ - PyObject *raw, *codec_info = NULL; - _PyIO_State *state = NULL; - PyObject *res; - int r; - - self->ok = 0; - self->detached = 0; - - if (errors == Py_None) { - errors = _PyUnicode_FromId(&PyId_strict); /* borrowed */ - if (errors == NULL) { - return -1; - } - } - 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", +{ + PyObject *raw, *codec_info = NULL; + _PyIO_State *state = NULL; + PyObject *res; + int r; + + self->ok = 0; + self->detached = 0; + + if (errors == Py_None) { + errors = _PyUnicode_FromId(&PyId_strict); /* borrowed */ + if (errors == NULL) { + return -1; + } + } + 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; - } + return -1; + } else if (io_check_errors(errors)) { 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) { - /* Try os.device_encoding(fileno) */ - PyObject *fileno; - state = IO_STATE(); - if (state == NULL) - goto error; + + 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) { + /* Try os.device_encoding(fileno) */ + PyObject *fileno; + state = IO_STATE(); + if (state == NULL) + goto error; fileno = _PyObject_CallMethodIdNoArgs(buffer, &PyId_fileno); - /* Ignore only AttributeError and UnsupportedOperation */ - if (fileno == NULL) { - if (PyErr_ExceptionMatches(PyExc_AttributeError) || - PyErr_ExceptionMatches(state->unsupported_operation)) { - PyErr_Clear(); - } - else { - goto error; - } - } - else { - int fd = _PyLong_AsInt(fileno); - Py_DECREF(fileno); - if (fd == -1 && PyErr_Occurred()) { - goto error; - } - - self->encoding = _Py_device_encoding(fd); - if (self->encoding == NULL) - goto error; - else if (!PyUnicode_Check(self->encoding)) - Py_CLEAR(self->encoding); - } - } - if (encoding == NULL && self->encoding == NULL) { - PyObject *locale_module = _PyIO_get_locale_module(state); - if (locale_module == NULL) - goto catch_ImportError; + /* Ignore only AttributeError and UnsupportedOperation */ + if (fileno == NULL) { + if (PyErr_ExceptionMatches(PyExc_AttributeError) || + PyErr_ExceptionMatches(state->unsupported_operation)) { + PyErr_Clear(); + } + else { + goto error; + } + } + else { + int fd = _PyLong_AsInt(fileno); + Py_DECREF(fileno); + if (fd == -1 && PyErr_Occurred()) { + goto error; + } + + self->encoding = _Py_device_encoding(fd); + if (self->encoding == NULL) + goto error; + else if (!PyUnicode_Check(self->encoding)) + Py_CLEAR(self->encoding); + } + } + if (encoding == NULL && self->encoding == NULL) { + PyObject *locale_module = _PyIO_get_locale_module(state); + if (locale_module == NULL) + goto catch_ImportError; self->encoding = _PyObject_CallMethodIdOneArg( locale_module, &PyId_getpreferredencoding, Py_False); - Py_DECREF(locale_module); - if (self->encoding == NULL) { - catch_ImportError: - /* - Importing locale can raise an ImportError because of - _functools, and locale.getpreferredencoding can raise an - ImportError if _locale is not available. These will happen - during module building. - */ - if (PyErr_ExceptionMatches(PyExc_ImportError)) { - PyErr_Clear(); - self->encoding = PyUnicode_FromString("ascii"); - } - else - goto error; - } - else if (!PyUnicode_Check(self->encoding)) - Py_CLEAR(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) - */ - - Py_INCREF(errors); - self->errors = 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 = buffer; - Py_INCREF(buffer); - - /* Build the decoder object */ - if (_textiowrapper_set_decoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) - goto error; - - /* Build the encoder object */ - if (_textiowrapper_set_encoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) - goto error; - - /* Finished sorting out the codec details */ - Py_CLEAR(codec_info); - + Py_DECREF(locale_module); + if (self->encoding == NULL) { + catch_ImportError: + /* + Importing locale can raise an ImportError because of + _functools, and locale.getpreferredencoding can raise an + ImportError if _locale is not available. These will happen + during module building. + */ + if (PyErr_ExceptionMatches(PyExc_ImportError)) { + PyErr_Clear(); + self->encoding = PyUnicode_FromString("ascii"); + } + else + goto error; + } + else if (!PyUnicode_Check(self->encoding)) + Py_CLEAR(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) + */ + + Py_INCREF(errors); + self->errors = 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 = buffer; + Py_INCREF(buffer); + + /* Build the decoder object */ + if (_textiowrapper_set_decoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) + goto error; + + /* Build the encoder object */ + if (_textiowrapper_set_encoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) + goto error; + + /* Finished sorting out the codec details */ + Py_CLEAR(codec_info); + if (Py_IS_TYPE(buffer, &PyBufferedReader_Type) || Py_IS_TYPE(buffer, &PyBufferedWriter_Type) || Py_IS_TYPE(buffer, &PyBufferedRandom_Type)) - { - if (_PyObject_LookupAttrId(buffer, &PyId_raw, &raw) < 0) - goto error; - /* Cache the raw FileIO object to speed up 'closed' checks */ - if (raw != NULL) { + { + if (_PyObject_LookupAttrId(buffer, &PyId_raw, &raw) < 0) + goto error; + /* Cache the raw FileIO object to speed up 'closed' checks */ + if (raw != NULL) { if (Py_IS_TYPE(raw, &PyFileIO_Type)) - self->raw = raw; - else - Py_DECREF(raw); - } - } - + self->raw = raw; + else + Py_DECREF(raw); + } + } + res = _PyObject_CallMethodIdNoArgs(buffer, &PyId_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, _PyIO_str_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; - } - } - else if (errors == Py_None) { - errors = _PyUnicode_FromId(&PyId_strict); - if (errors == NULL) { - return -1; - } - } - - const char *c_errors = PyUnicode_AsUTF8(errors); - if (c_errors == NULL) { - return -1; - } - - // Create new encoder & decoder - PyObject *codec_info = _PyCodec_LookupTextEncoding( - PyUnicode_AsUTF8(encoding), "codecs.open()"); - if (codec_info == NULL) { - 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); - return -1; - } - Py_DECREF(codec_info); - - Py_INCREF(encoding); - Py_INCREF(errors); - 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; - - /* Check if something is in the read buffer */ - if (self->decoded_chars != NULL) { - if (encoding != Py_None || errors != Py_None || newline_obj != NULL) { - _unsupported("It is not possible to set the encoding or newline " - "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); - write_through = convert_optional_bool(write_through_obj, - self->write_through); - if (line_buffering < 0 || write_through < 0) { - return NULL; - } - + 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, _PyIO_str_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; + } + } + else if (errors == Py_None) { + errors = _PyUnicode_FromId(&PyId_strict); + if (errors == NULL) { + return -1; + } + } + + const char *c_errors = PyUnicode_AsUTF8(errors); + if (c_errors == NULL) { + return -1; + } + + // Create new encoder & decoder + PyObject *codec_info = _PyCodec_LookupTextEncoding( + PyUnicode_AsUTF8(encoding), "codecs.open()"); + if (codec_info == NULL) { + 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); + return -1; + } + Py_DECREF(codec_info); + + Py_INCREF(encoding); + Py_INCREF(errors); + 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; + + /* Check if something is in the read buffer */ + if (self->decoded_chars != NULL) { + if (encoding != Py_None || errors != Py_None || newline_obj != NULL) { + _unsupported("It is not possible to set the encoding or newline " + "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); + write_through = convert_optional_bool(write_through_obj, + self->write_through); + if (line_buffering < 0 || write_through < 0) { + return NULL; + } + PyObject *res = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_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) -{ - 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); - textiowrapper_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static int -textiowrapper_traverse(textio *self, visitproc visit, void *arg) -{ - 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 (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) +{ + 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); + textiowrapper_clear(self); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static int +textiowrapper_traverse(textio *self, visitproc visit, void *arg) +{ + 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, &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); + 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, _PyIO_str_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; - + 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; @@ -1593,74 +1593,74 @@ _textiowrapper_writeflush(textio *self) assert(pos == self->pending_bytes_count); } - self->pending_bytes_count = 0; + self->pending_bytes_count = 0; self->pending_bytes = NULL; Py_DECREF(pending); - + PyObject *ret; - do { + do { ret = PyObject_CallMethodOneArg(self->buffer, _PyIO_str_write, b); - } while (ret == NULL && _PyIO_trap_eintr()); - Py_DECREF(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("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_CallMethodId( - text, &PyId_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 (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("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_CallMethodId( + text, &PyId_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 && @@ -1671,23 +1671,23 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) else { b = (*self->encodefunc)((PyObject *) self, text); } - self->encoding_start_of_stream = 0; - } + self->encoding_start_of_stream = 0; + } else { b = PyObject_CallMethodOneArg(self->encoder, _PyIO_str_encode, text); } - Py_DECREF(text); - if (b == NULL) - return NULL; + 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; - } - + 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); @@ -1696,7 +1696,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) bytes_len = PyBytes_GET_SIZE(b); } - if (self->pending_bytes == NULL) { + if (self->pending_bytes == NULL) { self->pending_bytes_count = 0; self->pending_bytes = b; } @@ -1711,1630 +1711,1630 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) else if (!PyList_CheckExact(self->pending_bytes)) { PyObject *list = PyList_New(2); if (list == NULL) { - Py_DECREF(b); - return 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); - } + 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) { + text_needflush) { + if (_textiowrapper_writeflush(self) < 0) + return NULL; + } + + if (needflush) { ret = PyObject_CallMethodNoArgs(self->buffer, _PyIO_str_flush); - if (ret == NULL) - return NULL; - Py_DECREF(ret); - } - - textiowrapper_set_decoded_chars(self, NULL); - Py_CLEAR(self->snapshot); - - if (self->decoder) { + if (ret == NULL) + return NULL; + Py_DECREF(ret); + } + + textiowrapper_set_decoded_chars(self, NULL); + Py_CLEAR(self->snapshot); + + if (self->decoder) { ret = _PyObject_CallMethodIdNoArgs(self->decoder, &PyId_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 = self->decoded_chars; - Py_INCREF(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("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. - */ + 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 = self->decoded_chars; + Py_INCREF(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("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, _PyIO_str_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; - + 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 ? _PyIO_str_read1: _PyIO_str_read), + (self->has_read1 ? _PyIO_str_read1: _PyIO_str_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->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("not readable"); - - if (_textiowrapper_writeflush(self) < 0) - return NULL; - - if (n < 0) { - /* Read everything */ + 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->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("not readable"); + + if (_textiowrapper_writeflush(self) < 0) + return NULL; + + if (n < 0) { + /* Read everything */ PyObject *bytes = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_read); - PyObject *decoded; - if (bytes == NULL) - goto fail; - + PyObject *decoded; + if (bytes == NULL) + goto fail; + if (Py_IS_TYPE(self->decoder, &PyIncrementalNewlineDecoder_Type)) - decoded = _PyIncrementalNewlineDecoder_decode(self->decoder, - bytes, 1); - else - decoded = PyObject_CallMethodObjArgs( - self->decoder, _PyIO_str_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; - - 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_XSETREF(result, PyUnicode_Join(_PyIO_empty_str, 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); + decoded = _PyIncrementalNewlineDecoder_decode(self->decoder, + bytes, 1); + else + decoded = PyObject_CallMethodObjArgs( + self->decoder, _PyIO_str_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; + + 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_XSETREF(result, PyUnicode_Join(_PyIO_empty_str, 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) -{ + } + 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); + + 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) { + /* 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 = self->decoded_chars; - start = self->decoded_chars_used; - offset_to_buffer = 0; - Py_INCREF(line); - } - 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(_PyIO_empty_str, chunks); - if (line == NULL) - goto error; - Py_CLEAR(chunks); - } - if (line == NULL) { - Py_INCREF(_PyIO_empty_str); - line = _PyIO_empty_str; - } - - 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) + 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 = self->decoded_chars; + start = self->decoded_chars_used; + offset_to_buffer = 0; + Py_INCREF(line); + } + 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(_PyIO_empty_str, chunks); + if (line == NULL) + goto error; + Py_CLEAR(chunks); + } + if (line == NULL) { + Py_INCREF(_PyIO_empty_str); + line = _PyIO_empty_str; + } + + 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, _PyIO_str_reset); - else - res = _PyObject_CallMethodId(self->decoder, &PyId_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) { + else + res = _PyObject_CallMethodId(self->decoder, &PyId_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, _PyIO_str_reset); - self->encoding_start_of_stream = 1; - } - else { + self->encoding_start_of_stream = 1; + } + else { res = PyObject_CallMethodOneArg(self->encoder, _PyIO_str_setstate, _PyLong_Zero); - 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 - whence: int = 0 - / -[clinic start generated code]*/ - -static PyObject * -_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) -/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/ -{ - PyObject *posobj; - cookie_type cookie; - PyObject *res; - int cmp; - PyObject *snapshot; - - CHECK_ATTACHED(self); - CHECK_CLOSED(self); - - Py_INCREF(cookieObj); - - if (!self->seekable) { - _unsupported("underlying stream is not seekable"); - goto fail; - } - + 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 + whence: int = 0 + / +[clinic start generated code]*/ + +static PyObject * +_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) +/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/ +{ + PyObject *posobj; + cookie_type cookie; + PyObject *res; + int cmp; + PyObject *snapshot; + + CHECK_ATTACHED(self); + CHECK_CLOSED(self); + + Py_INCREF(cookieObj); + + if (!self->seekable) { + _unsupported("underlying stream is not seekable"); + goto fail; + } + switch (whence) { case SEEK_CUR: - /* seek relative to current position */ - cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); - if (cmp < 0) - goto fail; - - if (cmp == 0) { - _unsupported("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); + /* seek relative to current position */ + cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); + if (cmp < 0) + goto fail; + + if (cmp == 0) { + _unsupported("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_CallMethodIdNoArgs((PyObject *)self, &PyId_tell); - if (cookieObj == NULL) - goto fail; + if (cookieObj == NULL) + goto fail; break; case SEEK_END: - /* seek relative to end of file */ - cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); - if (cmp < 0) - goto fail; - - if (cmp == 0) { - _unsupported("can't do nonzero end-relative seeks"); - goto fail; - } - + /* seek relative to end of file */ + cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); + if (cmp < 0) + goto fail; + + if (cmp == 0) { + _unsupported("can't do nonzero end-relative seeks"); + goto fail; + } + res = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); - if (res == NULL) - goto fail; - Py_DECREF(res); - - textiowrapper_set_decoded_chars(self, NULL); - Py_CLEAR(self->snapshot); - if (self->decoder) { + if (res == NULL) + goto fail; + Py_DECREF(res); + + textiowrapper_set_decoded_chars(self, NULL); + Py_CLEAR(self->snapshot); + if (self->decoder) { res = _PyObject_CallMethodIdNoArgs(self->decoder, &PyId_reset); - if (res == NULL) - goto fail; - Py_DECREF(res); - } - - res = _PyObject_CallMethodId(self->buffer, &PyId_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, _PyLong_Zero, Py_EQ); - if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) { - Py_DECREF(res); - goto fail; - } - } - return res; + if (res == NULL) + goto fail; + Py_DECREF(res); + } + + res = _PyObject_CallMethodId(self->buffer, &PyId_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, _PyLong_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, + 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, _PyLong_Zero, Py_LT); - if (cmp < 0) - goto fail; - - if (cmp == 1) { - PyErr_Format(PyExc_ValueError, - "negative seek position %R", cookieObj); - goto fail; - } - + goto fail; + } + + cmp = PyObject_RichCompareBool(cookieObj, _PyLong_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, _PyIO_str_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; + 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, _PyIO_str_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_CallMethodId( - self->buffer, &PyId_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); - + 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_CallMethodId( + self->buffer, &PyId_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_CallMethodIdObjArgs(self->decoder, &PyId_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 -[clinic start generated code]*/ - -static PyObject * -_io_TextIOWrapper_tell_impl(textio *self) -/*[clinic end generated code: output=4f168c08bf34ad5f input=9a2caf88c24f9ddf]*/ -{ - 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; + + 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 +[clinic start generated code]*/ + +static PyObject * +_io_TextIOWrapper_tell_impl(textio *self) +/*[clinic end generated code: output=4f168c08bf34ad5f input=9a2caf88c24f9ddf]*/ +{ + 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("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; + Py_ssize_t dec_buffer_len; + int dec_flags; + + CHECK_ATTACHED(self); + CHECK_CLOSED(self); + + if (!self->seekable) { + _unsupported("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_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); - if (res == NULL) - goto fail; - Py_DECREF(res); - + if (res == NULL) + goto fail; + Py_DECREF(res); + posobj = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_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 */ + 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, _PyIO_str_getstate); - if (saved_state == NULL) - goto fail; - -#define DECODER_GETSTATE() do { \ - PyObject *dec_buffer; \ + if (saved_state == NULL) + goto fail; + +#define DECODER_GETSTATE() do { \ + PyObject *dec_buffer; \ PyObject *_state = PyObject_CallMethodNoArgs(self->decoder, \ _PyIO_str_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_CallMethodId( \ - self->decoder, &PyId_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_CallMethodId( + 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_CallMethodId( \ + self->decoder, &PyId_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_CallMethodId( self->decoder, &PyId_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: + 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_CallMethodIdOneArg(self->decoder, &PyId_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 *type, *value, *traceback; - PyErr_Fetch(&type, &value, &traceback); + 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 *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); res = _PyObject_CallMethodIdOneArg(self->decoder, &PyId_setstate, saved_state); - _PyErr_ChainExceptions(type, value, traceback); - 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) - + _PyErr_ChainExceptions(type, value, traceback); + 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, _PyIO_str_flush); - if (res == NULL) - return NULL; - Py_DECREF(res); - + if (res == NULL) + return NULL; + Py_DECREF(res); + return PyObject_CallMethodOneArg(self->buffer, _PyIO_str_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; - } +} + +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_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { if (!PyErr_ExceptionMatches(PyExc_ValueError)) { - goto error; + 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; - } + 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_LookupAttrId((PyObject *) self, &PyId_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); + 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_CallMethodIdNoArgs(self->buffer, &PyId_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); +} + +/*[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_CallMethodIdNoArgs(self->buffer, &PyId_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); +} + +/*[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_CallMethodIdNoArgs(self->buffer, &PyId_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); +} + +/*[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_CallMethodIdNoArgs(self->buffer, &PyId_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); +} + +/*[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_CallMethodIdNoArgs(self->buffer, &PyId_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; +} + +/*[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_CallMethodIdNoArgs(self->buffer, &PyId_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, *val, *tb; - if (self->finalizing) { +} + +/*[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, *val, *tb; + if (self->finalizing) { res = _PyObject_CallMethodIdOneArg(self->buffer, &PyId__dealloc_warn, (PyObject *)self); - if (res) - Py_DECREF(res); - else - PyErr_Clear(); - } + if (res) + Py_DECREF(res); + else + PyErr_Clear(); + } res = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); - if (res == NULL) - PyErr_Fetch(&exc, &val, &tb); - else - Py_DECREF(res); - + if (res == NULL) + PyErr_Fetch(&exc, &val, &tb); + else + Py_DECREF(res); + res = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_close); - if (exc != NULL) { - _PyErr_ChainExceptions(exc, val, tb); - Py_CLEAR(res); - } - return res; - } -} - -static PyObject * -textiowrapper_iternext(textio *self) -{ - PyObject *line; - - CHECK_ATTACHED(self); - - self->telling = 0; + if (exc != NULL) { + _PyErr_ChainExceptions(exc, val, tb); + Py_CLEAR(res); + } + return res; + } +} + +static PyObject * +textiowrapper_iternext(textio *self) +{ + PyObject *line; + + CHECK_ATTACHED(self); + + self->telling = 0; if (Py_IS_TYPE(self, &PyTextIOWrapper_Type)) { - /* Skip method call overhead for speed */ - line = _textiowrapper_readline(self, -1); - } - else { + /* Skip method call overhead for speed */ + line = _textiowrapper_readline(self, -1); + } + else { line = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_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_GetAttrId(self->buffer, &PyId_name); -} - -static PyObject * -textiowrapper_closed_get(textio *self, void *context) -{ - CHECK_ATTACHED(self); - return PyObject_GetAttr(self->buffer, _PyIO_str_closed); -} - -static PyObject * -textiowrapper_newlines_get(textio *self, void *context) -{ - PyObject *res; - CHECK_ATTACHED(self); - if (self->decoder == NULL || - _PyObject_LookupAttr(self->decoder, _PyIO_str_newlines, &res) == 0) - { - Py_RETURN_NONE; - } - return res; -} - -static PyObject * -textiowrapper_errors_get(textio *self, void *context) -{ - CHECK_INITIALIZED(self); - Py_INCREF(self->errors); - return 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; -} - -#include "clinic/textio.c.h" - -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} -}; - -PyTypeObject PyIncrementalNewlineDecoder_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.IncrementalNewlineDecoder", /*tp_name*/ - sizeof(nldecoder_object), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/ + 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_GetAttrId(self->buffer, &PyId_name); +} + +static PyObject * +textiowrapper_closed_get(textio *self, void *context) +{ + CHECK_ATTACHED(self); + return PyObject_GetAttr(self->buffer, _PyIO_str_closed); +} + +static PyObject * +textiowrapper_newlines_get(textio *self, void *context) +{ + PyObject *res; + CHECK_ATTACHED(self); + if (self->decoder == NULL || + _PyObject_LookupAttr(self->decoder, _PyIO_str_newlines, &res) == 0) + { + Py_RETURN_NONE; + } + return res; +} + +static PyObject * +textiowrapper_errors_get(textio *self, void *context) +{ + CHECK_INITIALIZED(self); + Py_INCREF(self->errors); + return 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; +} + +#include "clinic/textio.c.h" + +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} +}; + +PyTypeObject PyIncrementalNewlineDecoder_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.IncrementalNewlineDecoder", /*tp_name*/ + sizeof(nldecoder_object), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - _io_IncrementalNewlineDecoder___init____doc__, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /*tp_weaklistoffset*/ - 0, /* tp_iter */ - 0, /* tp_iternext */ - incrementalnewlinedecoder_methods, /* tp_methods */ - 0, /* tp_members */ - incrementalnewlinedecoder_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - _io_IncrementalNewlineDecoder___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ -}; - - -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 - {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}, - {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} -}; - -PyTypeObject PyTextIOWrapper_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io.TextIOWrapper", /*tp_name*/ - sizeof(textio), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)textiowrapper_dealloc, /*tp_dealloc*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + _io_IncrementalNewlineDecoder___init____doc__, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /*tp_weaklistoffset*/ + 0, /* tp_iter */ + 0, /* tp_iternext */ + incrementalnewlinedecoder_methods, /* tp_methods */ + 0, /* tp_members */ + incrementalnewlinedecoder_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + _io_IncrementalNewlineDecoder___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + + +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 + {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}, + {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} +}; + +PyTypeObject PyTextIOWrapper_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io.TextIOWrapper", /*tp_name*/ + sizeof(textio), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)textiowrapper_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tps_etattr*/ + 0, /*tp_getattr*/ + 0, /*tps_etattr*/ 0, /*tp_as_async*/ - (reprfunc)textiowrapper_repr,/*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)textiowrapper_repr,/*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - _io_TextIOWrapper___init____doc__, /* tp_doc */ - (traverseproc)textiowrapper_traverse, /* tp_traverse */ - (inquiry)textiowrapper_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(textio, weakreflist), /*tp_weaklistoffset*/ - 0, /* tp_iter */ - (iternextfunc)textiowrapper_iternext, /* tp_iternext */ - textiowrapper_methods, /* tp_methods */ - textiowrapper_members, /* tp_members */ - textiowrapper_getset, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(textio, dict), /*tp_dictoffset*/ - _io_TextIOWrapper___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; + _io_TextIOWrapper___init____doc__, /* tp_doc */ + (traverseproc)textiowrapper_traverse, /* tp_traverse */ + (inquiry)textiowrapper_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(textio, weakreflist), /*tp_weaklistoffset*/ + 0, /* tp_iter */ + (iternextfunc)textiowrapper_iternext, /* tp_iternext */ + textiowrapper_methods, /* tp_methods */ + textiowrapper_members, /* tp_members */ + textiowrapper_getset, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(textio, dict), /*tp_dictoffset*/ + _io_TextIOWrapper___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; diff --git a/contrib/tools/python3/src/Modules/_io/winconsoleio.c b/contrib/tools/python3/src/Modules/_io/winconsoleio.c index a83ef37a1fc..0e223cd8724 100644 --- a/contrib/tools/python3/src/Modules/_io/winconsoleio.c +++ b/contrib/tools/python3/src/Modules/_io/winconsoleio.c @@ -1,1169 +1,1169 @@ -/* - An implementation of Windows console I/O - - Classes defined here: _WindowsConsoleIO - - Written by Steve Dower -*/ - -#define PY_SSIZE_T_CLEAN -#include "Python.h" +/* + 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_object.h" - -#ifdef MS_WINDOWS - + +#ifdef MS_WINDOWS + #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 */ - -#define WIN32_LEAN_AND_MEAN -#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_BEGIN_SUPPRESS_IPH - handle = (HANDLE)_get_osfhandle(fd); - _Py_END_SUPPRESS_IPH - 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; -} - - -/*[clinic input] -module _io -class _io._WindowsConsoleIO "winconsoleio *" "&PyWindowsConsoleIO_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e897fdc1fba4e131]*/ - -typedef struct { - PyObject_HEAD - HANDLE handle; - int fd; - unsigned int created : 1; - unsigned int readable : 1; - unsigned int writable : 1; - unsigned int closehandle : 1; - char finalizing; - unsigned int blksize; - PyObject *weakreflist; - PyObject *dict; - char buf[SMALLBUF]; - wchar_t wbuf; -} winconsoleio; - -PyTypeObject PyWindowsConsoleIO_Type; - -_Py_IDENTIFIER(name); - -int -_PyWindowsConsoleIO_closed(PyObject *self) -{ - return ((winconsoleio *)self)->handle == INVALID_HANDLE_VALUE; -} - - -/* Returns 0 on success, -1 with exception set on failure. */ -static int -internal_close(winconsoleio *self) -{ - if (self->handle != INVALID_HANDLE_VALUE) { - if (self->closehandle) { - if (self->fd >= 0) { - _Py_BEGIN_SUPPRESS_IPH - close(self->fd); - _Py_END_SUPPRESS_IPH - } - CloseHandle(self->handle); - } - self->handle = INVALID_HANDLE_VALUE; - self->fd = -1; - } - return 0; -} - -/*[clinic input] -_io._WindowsConsoleIO.close - -Close the handle. - -A closed handle 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) -/*[clinic end generated code: output=27ef95b66c29057b input=185617e349ae4c7b]*/ -{ - PyObject *res; - PyObject *exc, *val, *tb; - int rc; - _Py_IDENTIFIER(close); +#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 */ + +#define WIN32_LEAN_AND_MEAN +#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_BEGIN_SUPPRESS_IPH + handle = (HANDLE)_get_osfhandle(fd); + _Py_END_SUPPRESS_IPH + 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; +} + + +/*[clinic input] +module _io +class _io._WindowsConsoleIO "winconsoleio *" "&PyWindowsConsoleIO_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e897fdc1fba4e131]*/ + +typedef struct { + PyObject_HEAD + HANDLE handle; + int fd; + unsigned int created : 1; + unsigned int readable : 1; + unsigned int writable : 1; + unsigned int closehandle : 1; + char finalizing; + unsigned int blksize; + PyObject *weakreflist; + PyObject *dict; + char buf[SMALLBUF]; + wchar_t wbuf; +} winconsoleio; + +PyTypeObject PyWindowsConsoleIO_Type; + +_Py_IDENTIFIER(name); + +int +_PyWindowsConsoleIO_closed(PyObject *self) +{ + return ((winconsoleio *)self)->handle == INVALID_HANDLE_VALUE; +} + + +/* Returns 0 on success, -1 with exception set on failure. */ +static int +internal_close(winconsoleio *self) +{ + if (self->handle != INVALID_HANDLE_VALUE) { + if (self->closehandle) { + if (self->fd >= 0) { + _Py_BEGIN_SUPPRESS_IPH + close(self->fd); + _Py_END_SUPPRESS_IPH + } + CloseHandle(self->handle); + } + self->handle = INVALID_HANDLE_VALUE; + self->fd = -1; + } + return 0; +} + +/*[clinic input] +_io._WindowsConsoleIO.close + +Close the handle. + +A closed handle 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) +/*[clinic end generated code: output=27ef95b66c29057b input=185617e349ae4c7b]*/ +{ + PyObject *res; + PyObject *exc, *val, *tb; + int rc; + _Py_IDENTIFIER(close); res = _PyObject_CallMethodIdOneArg((PyObject*)&PyRawIOBase_Type, &PyId_close, (PyObject*)self); - if (!self->closehandle) { - self->handle = INVALID_HANDLE_VALUE; - return res; - } - if (res == NULL) - PyErr_Fetch(&exc, &val, &tb); - rc = internal_close(self); - if (res == NULL) - _PyErr_ChainExceptions(exc, val, tb); - 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->handle = INVALID_HANDLE_VALUE; - self->fd = -1; - self->created = 0; - self->readable = 0; - self->writable = 0; - self->closehandle = 0; - self->blksize = 0; - self->weakreflist = NULL; - } - - return (PyObject *) self; -} - -/*[clinic input] -_io._WindowsConsoleIO.__init__ - file as nameobj: object - mode: str = "r" - closefd: bool(accept={int}) = 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=06ae4b863c63244b]*/ -{ - 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; - - assert(PyWindowsConsoleIO_Check(self)); - if (self->handle >= 0) { - if (self->closehandle) { - /* Have to close the existing file first. */ - if (internal_close(self) < 0) - return -1; - } - else - self->handle = INVALID_HANDLE_VALUE; - } - - if (PyFloat_Check(nameobj)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float"); - return -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) { - _Py_BEGIN_SUPPRESS_IPH - self->handle = (HANDLE)_get_osfhandle(fd); - _Py_END_SUPPRESS_IPH - self->closehandle = 0; - } else { - DWORD access = GENERIC_READ; - - self->closehandle = 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. */ - self->handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - if (self->handle == INVALID_HANDLE_VALUE) - self->handle = CreateFileW(name, access, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - Py_END_ALLOW_THREADS - - if (self->handle == INVALID_HANDLE_VALUE) { - PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, GetLastError(), nameobj); - goto error; - } - } - - if (console_type == '\0') - console_type = _get_console_type(self->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_SetAttrId((PyObject *)self, &PyId_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(self->dict); - return 0; -} - -static int -winconsoleio_clear(winconsoleio *self) -{ - Py_CLEAR(self->dict); - return 0; -} - -static void -winconsoleio_dealloc(winconsoleio *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); - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static PyObject * -err_closed(void) -{ - PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); - return NULL; -} - -static PyObject * -err_mode(const char *action) -{ - _PyIO_State *state = IO_STATE(); - if (state != NULL) - PyErr_Format(state->unsupported_operation, - "Console buffer does not support %s", action); - return NULL; -} - -/*[clinic input] -_io._WindowsConsoleIO.fileno - -Return the underlying file descriptor (an integer). - -fileno is only set when a file descriptor is used to open -one of the standard streams. - -[clinic start generated code]*/ - -static PyObject * -_io__WindowsConsoleIO_fileno_impl(winconsoleio *self) -/*[clinic end generated code: output=006fa74ce3b5cfbf input=079adc330ddaabe6]*/ -{ - if (self->fd < 0 && self->handle != INVALID_HANDLE_VALUE) { - _Py_BEGIN_SUPPRESS_IPH - if (self->writable) - self->fd = _open_osfhandle((intptr_t)self->handle, _O_WRONLY | _O_BINARY); - else - self->fd = _open_osfhandle((intptr_t)self->handle, _O_RDONLY | _O_BINARY); - _Py_END_SUPPRESS_IPH - } - if (self->fd < 0) - return err_mode("fileno"); - 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->handle == INVALID_HANDLE_VALUE) - 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->handle == INVALID_HANDLE_VALUE) - 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) { + if (!self->closehandle) { + self->handle = INVALID_HANDLE_VALUE; + return res; + } + if (res == NULL) + PyErr_Fetch(&exc, &val, &tb); + rc = internal_close(self); + if (res == NULL) + _PyErr_ChainExceptions(exc, val, tb); + 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->handle = INVALID_HANDLE_VALUE; + self->fd = -1; + self->created = 0; + self->readable = 0; + self->writable = 0; + self->closehandle = 0; + self->blksize = 0; + self->weakreflist = NULL; + } + + return (PyObject *) self; +} + +/*[clinic input] +_io._WindowsConsoleIO.__init__ + file as nameobj: object + mode: str = "r" + closefd: bool(accept={int}) = 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=06ae4b863c63244b]*/ +{ + 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; + + assert(PyWindowsConsoleIO_Check(self)); + if (self->handle >= 0) { + if (self->closehandle) { + /* Have to close the existing file first. */ + if (internal_close(self) < 0) + return -1; + } + else + self->handle = INVALID_HANDLE_VALUE; + } + + if (PyFloat_Check(nameobj)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float"); + return -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) { + _Py_BEGIN_SUPPRESS_IPH + self->handle = (HANDLE)_get_osfhandle(fd); + _Py_END_SUPPRESS_IPH + self->closehandle = 0; + } else { + DWORD access = GENERIC_READ; + + self->closehandle = 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. */ + self->handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + if (self->handle == INVALID_HANDLE_VALUE) + self->handle = CreateFileW(name, access, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + Py_END_ALLOW_THREADS + + if (self->handle == INVALID_HANDLE_VALUE) { + PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, GetLastError(), nameobj); + goto error; + } + } + + if (console_type == '\0') + console_type = _get_console_type(self->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_SetAttrId((PyObject *)self, &PyId_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(self->dict); + return 0; +} + +static int +winconsoleio_clear(winconsoleio *self) +{ + Py_CLEAR(self->dict); + return 0; +} + +static void +winconsoleio_dealloc(winconsoleio *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); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyObject * +err_closed(void) +{ + PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); + return NULL; +} + +static PyObject * +err_mode(const char *action) +{ + _PyIO_State *state = IO_STATE(); + if (state != NULL) + PyErr_Format(state->unsupported_operation, + "Console buffer does not support %s", action); + return NULL; +} + +/*[clinic input] +_io._WindowsConsoleIO.fileno + +Return the underlying file descriptor (an integer). + +fileno is only set when a file descriptor is used to open +one of the standard streams. + +[clinic start generated code]*/ + +static PyObject * +_io__WindowsConsoleIO_fileno_impl(winconsoleio *self) +/*[clinic end generated code: output=006fa74ce3b5cfbf input=079adc330ddaabe6]*/ +{ + if (self->fd < 0 && self->handle != INVALID_HANDLE_VALUE) { + _Py_BEGIN_SUPPRESS_IPH + if (self->writable) + self->fd = _open_osfhandle((intptr_t)self->handle, _O_WRONLY | _O_BINARY); + else + self->fd = _open_osfhandle((intptr_t)self->handle, _O_RDONLY | _O_BINARY); + _Py_END_SUPPRESS_IPH + } + if (self->fd < 0) + return err_mode("fileno"); + 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->handle == INVALID_HANDLE_VALUE) + 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->handle == INVALID_HANDLE_VALUE) + 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(winconsoleio *self, char *buf, Py_ssize_t len) -{ - if (self->handle == INVALID_HANDLE_VALUE) { - err_closed(); - return -1; - } - if (!self->readable) { - err_mode("reading"); - return -1; - } - if (len == 0) - return 0; - if (len > BUFMAX) { - PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX); - 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(self->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, + 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(winconsoleio *self, char *buf, Py_ssize_t len) +{ + if (self->handle == INVALID_HANDLE_VALUE) { + err_closed(); + return -1; + } + if (!self->readable) { + err_mode("reading"); + return -1; + } + if (len == 0) + return 0; + if (len > BUFMAX) { + PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX); + 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(self->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 - buffer: Py_buffer(accept={rwbuffer}) - / - -Same as RawIOBase.readinto(). -[clinic start generated code]*/ - -static PyObject * -_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer) -/*[clinic end generated code: output=66d1bdfa3f20af39 input=4ed68da48a6baffe]*/ -{ - Py_ssize_t len = readinto(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; - - if (self->handle == INVALID_HANDLE_VALUE) - return err_closed(); - - 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(self->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 - 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, Py_ssize_t size) -/*[clinic end generated code: output=57df68af9f4b22d0 input=8bc73bc15d0fa072]*/ -{ - PyObject *bytes; - Py_ssize_t bytes_size; - - if (self->handle == INVALID_HANDLE_VALUE) - return err_closed(); - if (!self->readable) - return err_mode("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; - - bytes_size = readinto(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 - 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, Py_buffer *b) -/*[clinic end generated code: output=775bdb16fbf9137b input=be35fb624f97c941]*/ -{ - BOOL res = TRUE; - wchar_t *wbuf; - DWORD len, wlen, n = 0; - - if (self->handle == INVALID_HANDLE_VALUE) - return err_closed(); - if (!self->writable) - return err_mode("writing"); - - 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; - 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(self->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->handle == INVALID_HANDLE_VALUE) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>"); - - if (self->readable) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>", - self->closehandle ? "True" : "False"); - if (self->writable) - return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>", - self->closehandle ? "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->handle == INVALID_HANDLE_VALUE) - return err_closed(); - - Py_RETURN_TRUE; -} - -#include "clinic/winconsoleio.c.h" - -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->handle == INVALID_HANDLE_VALUE)); -} - -static PyObject * -get_closefd(winconsoleio *self, void *closure) -{ - return PyBool_FromLong((long)(self->closehandle)); -} - -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}, - {NULL} -}; - -PyTypeObject PyWindowsConsoleIO_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_io._WindowsConsoleIO", - sizeof(winconsoleio), - 0, - (destructor)winconsoleio_dealloc, /* tp_dealloc */ + len, u8n); + return -1; + } + if (err) { + PyErr_SetFromWindowsErr(err); + return -1; + } + + return read_len; +} + +/*[clinic input] +_io._WindowsConsoleIO.readinto + buffer: Py_buffer(accept={rwbuffer}) + / + +Same as RawIOBase.readinto(). +[clinic start generated code]*/ + +static PyObject * +_io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer) +/*[clinic end generated code: output=66d1bdfa3f20af39 input=4ed68da48a6baffe]*/ +{ + Py_ssize_t len = readinto(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; + + if (self->handle == INVALID_HANDLE_VALUE) + return err_closed(); + + 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(self->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 + 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, Py_ssize_t size) +/*[clinic end generated code: output=57df68af9f4b22d0 input=8bc73bc15d0fa072]*/ +{ + PyObject *bytes; + Py_ssize_t bytes_size; + + if (self->handle == INVALID_HANDLE_VALUE) + return err_closed(); + if (!self->readable) + return err_mode("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; + + bytes_size = readinto(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 + 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, Py_buffer *b) +/*[clinic end generated code: output=775bdb16fbf9137b input=be35fb624f97c941]*/ +{ + BOOL res = TRUE; + wchar_t *wbuf; + DWORD len, wlen, n = 0; + + if (self->handle == INVALID_HANDLE_VALUE) + return err_closed(); + if (!self->writable) + return err_mode("writing"); + + 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; + 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(self->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->handle == INVALID_HANDLE_VALUE) + return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>"); + + if (self->readable) + return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>", + self->closehandle ? "True" : "False"); + if (self->writable) + return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>", + self->closehandle ? "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->handle == INVALID_HANDLE_VALUE) + return err_closed(); + + Py_RETURN_TRUE; +} + +#include "clinic/winconsoleio.c.h" + +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->handle == INVALID_HANDLE_VALUE)); +} + +static PyObject * +get_closefd(winconsoleio *self, void *closure) +{ + return PyBool_FromLong((long)(self->closehandle)); +} + +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}, + {NULL} +}; + +PyTypeObject PyWindowsConsoleIO_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_io._WindowsConsoleIO", + sizeof(winconsoleio), + 0, + (destructor)winconsoleio_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc)winconsoleio_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE + (reprfunc)winconsoleio_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - _io__WindowsConsoleIO___init____doc__, /* tp_doc */ - (traverseproc)winconsoleio_traverse, /* tp_traverse */ - (inquiry)winconsoleio_clear, /* tp_clear */ - 0, /* tp_richcompare */ - offsetof(winconsoleio, weakreflist), /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - winconsoleio_methods, /* tp_methods */ - winconsoleio_members, /* tp_members */ - winconsoleio_getsetlist, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(winconsoleio, dict), /* tp_dictoffset */ - _io__WindowsConsoleIO___init__, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - winconsoleio_new, /* tp_new */ - PyObject_GC_Del, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0, /* tp_weaklist */ - 0, /* tp_del */ - 0, /* tp_version_tag */ - 0, /* tp_finalize */ -}; - + _io__WindowsConsoleIO___init____doc__, /* tp_doc */ + (traverseproc)winconsoleio_traverse, /* tp_traverse */ + (inquiry)winconsoleio_clear, /* tp_clear */ + 0, /* tp_richcompare */ + offsetof(winconsoleio, weakreflist), /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + winconsoleio_methods, /* tp_methods */ + winconsoleio_members, /* tp_members */ + winconsoleio_getsetlist, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + offsetof(winconsoleio, dict), /* tp_dictoffset */ + _io__WindowsConsoleIO___init__, /* tp_init */ + PyType_GenericAlloc, /* tp_alloc */ + winconsoleio_new, /* tp_new */ + PyObject_GC_Del, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0, /* tp_version_tag */ + 0, /* tp_finalize */ +}; + PyObject * _PyWindowsConsoleIO_Type = (PyObject*)&PyWindowsConsoleIO_Type; - -#endif /* MS_WINDOWS */ + +#endif /* MS_WINDOWS */ |
