Index: Doc/library/allos.rst =================================================================== --- Doc/library/allos.rst (revision 62183) +++ Doc/library/allos.rst (working copy) @@ -14,6 +14,7 @@ .. toctree:: os.rst + io.rst time.rst optparse.rst getopt.rst Index: Doc/library/io.rst =================================================================== --- Doc/library/io.rst (revision 0) +++ Doc/library/io.rst (revision 0) @@ -0,0 +1,586 @@ +:mod:`io` -- Core tools for working with streams +================================================ + +.. module:: io + :synopsis: Core tools for working with streams +.. moduleauthor:: Guido van Rossum +.. moduleauthor:: Mike Verdone +.. moduleauthor:: Mark Russell = 0.) A value + of 1 causes *offset* to move from the current position. (*offset* can be + negative or positive.) Passing 2 sets the relative marker to the end of the + file. (*offset* is usually negative, although many platforms allow seeking + beyond the end of a file.) + +.. method:: FileIO.truncate(size) + + Truncate the file to at most *size* bytes. Size defaults to the current file + position, as returned by :meth:`tell()`. + +.. method:: FileIO.write(b) + + Write bytes, *b*, to the file and return the number actually written. Only + one system call is made, so not all of the data may be written. + + +:class:`BufferedIOBase` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Base class for streams that support buffering. It inherits :class:`IOBase`. + +The main difference with RawIOBase is that the read() method supports omitting +the size argument, and does not have a default implementation that defers to +readinto(). + +In addition, read(), readinto(), and write() may raise BlockingIOError if the +underlying raw stream is in non-blocking mode and not ready; unlike their raw +counterparts, they will never return :keyword:`None`. + +A typical implementation should not inherit from a RawIOBase implementation, but +wrap one like :class:`BufferedWriter` and :class:`BufferedReader`. + +.. class:: BufferedIOBase + + No public constructor. + +:class:`BufferedIOBase` provides or overrides these methods in addition to those +from :class:`IOBase`: + + +.. method:: BufferedIOBase.read([n]) + + Read and return up to *n* bytes. If the argument is omitted, + :keyword:`None`, or negative, data is read and returned until EOF is + reached. An empty byte array is returned if the stream is already at EOF. + + If the argument is positive, and the underlying raw stream is not + interactive, multiple raw reads may be issued to satisfy the byte count + (unless EOF is reached first). But for interactive raw streams, at most one + raw read will be issued, and a short result does not imply that EOF is + imminent. + + A :exc:`BlockingIOError` is raised if the underlying raw stream has no data + at the moment. + +.. method:: BufferedIOBase.readinto(b) + + Read up to len(b) bytes into bytearray *b* and return the number of bytes + read. + + Like :meth:`read()`, multiple reads may be issued to the underlying raw + stream, unless the latter is 'iteractive.' + + A :exc:`BlockingIOError` is raised if the underlying raw stream has no data + at the moment. + +.. method:: BufferedIOBase.seek(pos, [whence]) + + Move the position of the raw stream by *pos*. The optional argument, + *whence*, dictates the position the *pos* offset will move by. If *whence* + is 0, the default, the *pos* is moved from the start of the stream. If + *whence* is 1, *pos* is from the current position in the stream, and if + *whence* is 2, *pos* is from the end of the stream. + +.. method:: BufferedIOBase.write(b) + + Write the given bytes to the underlying raw stream and return the number of + bytes written (never less than len(b)). + + A :exc:`BlockingIOError` is raised if the buffer is full, and the underlying + raw stream cannot accept more data at the moment. + + +:class:`BytesIO` objects +^^^^^^^^^^^^^^^^^^^^^^^^ + +A stream implementation using an in-memory bytes buffer. It inherits +:class:`BufferedIOBase`. + +.. class:: BytesIO([initial_bytes]) + + Create a BytesIO object with an optional initial bytearray. + +:class:`BytesIO` provides or overrides these methods in addition to those from +:class:`BufferedIOBase` and :class:`IOBase`: + + +.. method:: BytesIO.getvalue() + + Returns the bytes value of the buffer. + +.. method:: BytesIO.read1() + + In :class:`BytesIO`, this is the same as :meth:`read()`. + +.. method:: BytesIO.truncate([size]) + + Truncates the stream to at most *size*. If *size* is not given, the stream + is truncated to the current position. + + +:class:`BufferedReader` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A buffer for a readable sequential RawIO object. It inherits +:class:`BufferedIOBase`. + +.. class:: BufferedReader(raw, [buffer_size]) + + Create a :class:`BufferedReader` for the given readable raw stream and buffer + size. If *buffer_size* is omitted, :data:`DEFAULT_BUFFER_SIZE` is used. + +:class:`BufferedReader` provides or overrides these methods in addition to those +from :class:`BufferedIOBase` and :class:`IOBase`: + + +.. method:: BufferedReader.peek([n]) + + Return bytes from a buffer without advancing the position. The argument + indicates a desired minimal number of bytes; only one read on the raw stream + is done to satisfy it. More than the buffer's size is never returned. + +.. method:: BufferedReader.read([n]) + + Read and return *n* bytes, or if *n* is not given or negative, until EOF or + if the read call would block in non-blocking mode. + +.. method:: BufferedReader.read1(n) + + Read and return up to *n* bytes with only one call on the raw stream. If at + least one byte is buffered, only buffered bytes are returned. Otherwise, one + raw stream read call is made. + + +:class:`BufferedWriter` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A buffer for a writeable sequential RawIO object. It inherits +:class:`BufferedIOBase`. + +.. class:: BufferedWriter(raw, [buffer_size, [max_buffer_size]]) + + Create a :class:`BufferedWriter` for the given writeable raw stream. If the + buffer size is not given, it defaults to :data:`DEAFULT_BUFFER_SIZE`. If the + maximum buffer size is omitted, it defaults to twice the buffer size. + +:class:`BufferedWriter` provides or overrides these methods in addition to those +from :class:`BufferedIOBase` and :class:`IOBase`: + + +.. method:: BufferedWriter.flush() + + Force bytes held in the buffer into the raw stream. A :exc:`BlockingIOError` + is be raised if the raw stream blocks. + +.. method:: BufferedWriter.write(b) + + Write bytes *b* onto the raw stream and return the number written. A + :exc:`BlockingIOError` is raised when the raw stream blocks. + + +:class:`BufferedRWPair` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A buffered writer and reader object together for a raw stream that can be +written and read from. It has and supports both :meth:`read`, :meth:`write`, +and their variants. This is useful for such applications such as sockets and +two-way pipes. It inherits :class:`BufferedIOBase`. + +.. class:: BufferedRWPair(reader, writer, [buffer_size, [max_buffer_size]]) + + Create a :class:`BufferedRWPair`. reader and writer and :class:`RawIOBase` + objects that are readable and writeable respectively. If a *buffer_size* is + omitted it defaults to :data:`DEFAULT_BUFFER_SIZE`. The maximum buffer size + (for the buffered writer) defaults to twice the buffer size. + +:class:`BufferedRWPair` implements all of :class:`BufferedIOBase`'s methods. + + +:class:`BufferedRandom` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A buffered interface to random access streams. It inherits +:class:`BufferedReader` and :class:`BufferedWriter`. + +.. class:: BufferedRandom(raw, [buffer_size, [max_buffer_size]]) + + Create a new :class:`BufferedRandom` reader and writer for a seekable raw + stream named in the first argument. If the *buffer_size* is omitted it + defaults to :data:`DEFAULT_BUFFER_SIZE`. The maximum buffer size (for the + buffered writer) defaults to twice the buffer size. + +:class:`BufferedRandom` is capable of anything :class:`BufferedReader` or +:class:`BufferedWriter` can do. + + +:class:`TextIOBase` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Base class for text streams. This class provides a character and line based +interface to stream I/O. There is not readinto() because character string as +immutable. It inherits :class:`IOBase`. + +.. class:: TextIOBase + + No public constructor + +:class:`TextIOBase` provides or overrides these methods in addition to those +from :class:`IOBase`: + + +.. method:: TextIOBase.encoding() + + Return the name of the encoding used to decode the stream into text. + +.. method:: TextIOBase.newlines() + + Return a str, tuple of strs, or :keyword:`None` containing the newlines + translated so far. + +.. method:: TextIOBase.read(n) + + Read and return at most *n* characters from the stream. If *n* is negative + or :keyword:`None`, read to EOF. + +.. method:: TextIOBase.readline() + + Read until newline of EOF and return. If the stream is already at EOF, an + empty stream is returned. + +.. method:: TextIOBase.truncate([pos]) + + Truncate size to *pos*. If *pos* is not specified, it is assumed to be the + current position. + +.. method:: TextIOBase.write(s) + + Write str *s* to the stream and return the number of characters written. + + +:class:`TextIOWrapper` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A buffered text stream over a :class:`BufferedIOBase` raw stream. It inherits +:class:`TextIOBase`. + +.. class:: TextIOWrapper(buffer, [encoding, [errors, [newline, +.. [line_buffering]]]]) + + Create a :class:`TextIOWrapper` for the :class:`BufferIOBase` stream, *buffer*. + + *encoding* gives the name of the encoding that the stream will be decoded or + encoded with. It defaults to :func:`locale.getpreferredencoding()`. + + *errors* determines the strictness of encoding and decoding (see the errors + argument of :func:`codecs.open`) and defaults to "strict". + + *newline* can be :keyword:`None`, '', '\n', '\r', or '\r\n'. It controls the + handling of line endings. If it is none, universal newlines is enabled. + With this enabled, on input, the lines endings "\n", "\r", or "\r\n" are + translated to "\n" before being returned to the caller. Conversly, on + output, "\n" is translated to the system default line seperator, + :data:`os.linsep`. If *newline* is any other of its legal values, that + newline becomes the newline when the file is read and it is returned + untranslated. On ouput, "\n" is converted to the *newline*. + + If *line_buffering* is :keyword:`True`, :meth:`flush` is implied when a call + to write contains a newline character. + +:class:`TextIOWrapper` provides these methods in addition to those of +:class:`TextIOBase` and its parents: + + +.. method:: TextIOWrapper.errors() + + Return the encoding and decoding error setting. + +.. method:: TextIOWrapper.line_buffering() + + Return whether line buffering is enabled. + + +:class:`StringIO` objects +^^^^^^^^^^^^^^^^^^^^^^^^^ + +An in-memory stream for text. It in inherits :class:`TextIOWrapper`. + +.. class:: StringIO([initial_value, [encoding, [errors, [newline]]]]) + + Create a new StringIO stream with an inital value, encoding, error handling, + and newline setting. See :class:`TextIOWrapper`'s constructor for more + information. + +:class:`StringIO` provides these methods in addition to those from +:class:`TextIOWrapper` and its parents: + + +.. method:: StringIO.getvalue() + + Return a str representation of the contents of the internal buffer. + + +:class:`IncrementalNewlineDecoder` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A helper codec that decodes newlines. It inherits +:class:`codecs.IncrementalDecoder`. +