| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 1 | :mod:`io` --- Core tools for working with streams | 
 | 2 | ================================================= | 
 | 3 |  | 
 | 4 | .. module:: io | 
 | 5 |    :synopsis: Core tools for working with streams. | 
 | 6 | .. moduleauthor:: Guido van Rossum <guido@python.org> | 
 | 7 | .. moduleauthor:: Mike Verdone <mike.verdone@gmail.com> | 
 | 8 | .. moduleauthor:: Mark Russell <mark.russell@zen.co.uk> | 
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 9 | .. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net> | 
 | 10 | .. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com> | 
| Benjamin Peterson | ef9f2bd | 2009-05-01 20:45:43 +0000 | [diff] [blame] | 11 | .. moduleauthor:: Benjamin Peterson <benjamin@python.org> | 
| Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 12 | .. sectionauthor:: Benjamin Peterson <benjamin@python.org> | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 13 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 14 | .. _io-overview: | 
 | 15 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 16 | Overview | 
 | 17 | -------- | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 18 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 19 | The :mod:`io` module provides Python's main facilities for dealing for various | 
 | 20 | types of I/O.  There are three main types of I/O: *text I/O*, *binary I/O*, *raw | 
 | 21 | I/O*.  These are generic categories, and various backing stores can be used for | 
 | 22 | each of them.  Concrete objects belonging to any of these categories will often | 
 | 23 | be called *streams*; another common term is *file-like objects*. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 24 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 25 | Independently of its category, each concrete stream object will also have | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 26 | various capabilities: it can be read-only, write-only, or read-write. It can | 
 | 27 | also allow arbitrary random access (seeking forwards or backwards to any | 
 | 28 | location), or only sequential access (for example in the case of a socket or | 
 | 29 | pipe). | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 30 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 31 | All streams are careful about the type of data you give to them.  For example | 
 | 32 | giving a :class:`str` object to the ``write()`` method of a binary stream | 
 | 33 | will raise a ``TypeError``.  So will giving a :class:`bytes` object to the | 
 | 34 | ``write()`` method of a text stream. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 35 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 36 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 37 | Text I/O | 
 | 38 | ^^^^^^^^ | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 39 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 40 | Text I/O expects and produces :class:`str` objects.  This means that whenever | 
 | 41 | the backing store is natively made of bytes (such as in the case of a file), | 
 | 42 | encoding and decoding of data is made transparently as well as optional | 
 | 43 | translation of platform-specific newline characters. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 44 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 45 | The easiest way to create a text stream is with :meth:`open()`, optionally | 
 | 46 | specifying an encoding:: | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 47 |  | 
 | 48 |    f = open("myfile.txt", "r", encoding="utf-8") | 
 | 49 |  | 
 | 50 | In-memory text streams are also available as :class:`StringIO` objects:: | 
 | 51 |  | 
 | 52 |    f = io.StringIO("some initial text data") | 
 | 53 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 54 | The text stream API is described in detail in the documentation for the | 
 | 55 | :class:`TextIOBase`. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 56 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 57 |  | 
 | 58 | Binary I/O | 
 | 59 | ^^^^^^^^^^ | 
 | 60 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 61 | Binary I/O (also called *buffered I/O*) expects and produces :class:`bytes` | 
 | 62 | objects.  No encoding, decoding, or newline translation is performed.  This | 
 | 63 | category of streams can be used for all kinds of non-text data, and also when | 
 | 64 | manual control over the handling of text data is desired. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 65 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 66 | The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in | 
 | 67 | the mode string:: | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 68 |  | 
 | 69 |    f = open("myfile.jpg", "rb") | 
 | 70 |  | 
 | 71 | In-memory binary streams are also available as :class:`BytesIO` objects:: | 
 | 72 |  | 
 | 73 |    f = io.BytesIO(b"some initial binary data: \x00\x01") | 
 | 74 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 75 | The binary stream API is described in detail in the docs of | 
 | 76 | :class:`BufferedIOBase`. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 77 |  | 
 | 78 | Other library modules may provide additional ways to create text or binary | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 79 | streams.  See :meth:`socket.socket.makefile` for example. | 
 | 80 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 81 |  | 
 | 82 | Raw I/O | 
 | 83 | ^^^^^^^ | 
 | 84 |  | 
 | 85 | Raw I/O (also called *unbuffered I/O*) is generally used as a low-level | 
 | 86 | building-block for binary and text streams; it is rarely useful to directly | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 87 | manipulate a raw stream from user code.  Nevertheless, you can create a raw | 
 | 88 | stream by opening a file in binary mode with buffering disabled:: | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 89 |  | 
 | 90 |    f = open("myfile.jpg", "rb", buffering=0) | 
 | 91 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 92 | The raw stream API is described in detail in the docs of :class:`RawIOBase`. | 
| Benjamin Peterson | cc12e1b | 2010-02-19 00:58:13 +0000 | [diff] [blame] | 93 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 94 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 95 | High-level Module Interface | 
 | 96 | --------------------------- | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 97 |  | 
 | 98 | .. data:: DEFAULT_BUFFER_SIZE | 
 | 99 |  | 
 | 100 |    An int containing the default buffer size used by the module's buffered I/O | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 101 |    classes.  :func:`open` uses the file's blksize (as obtained by | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 102 |    :func:`os.stat`) if possible. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 103 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 104 |  | 
| Benjamin Peterson | 95e392c | 2010-04-27 21:07:21 +0000 | [diff] [blame] | 105 | .. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 106 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 107 |    This is an alias for the builtin :func:`open` function. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 108 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 109 |  | 
 | 110 | .. exception:: BlockingIOError | 
 | 111 |  | 
 | 112 |    Error raised when blocking would occur on a non-blocking stream.  It inherits | 
 | 113 |    :exc:`IOError`. | 
 | 114 |  | 
 | 115 |    In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one | 
 | 116 |    attribute: | 
 | 117 |  | 
 | 118 |    .. attribute:: characters_written | 
 | 119 |  | 
 | 120 |       An integer containing the number of characters written to the stream | 
 | 121 |       before it blocked. | 
 | 122 |  | 
 | 123 |  | 
 | 124 | .. exception:: UnsupportedOperation | 
 | 125 |  | 
 | 126 |    An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised | 
 | 127 |    when an unsupported operation is called on a stream. | 
 | 128 |  | 
 | 129 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 130 | In-memory streams | 
 | 131 | ^^^^^^^^^^^^^^^^^ | 
 | 132 |  | 
 | 133 | It is also possible to use a :class:`str` or :class:`bytes`-like object as a | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 134 | file for both reading and writing.  For strings :class:`StringIO` can be used | 
 | 135 | like a file opened in text mode.  :class:`BytesIO` can be used like a file | 
 | 136 | opened in binary mode.  Both provide full read-write capabilities with random | 
 | 137 | access. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 138 |  | 
 | 139 |  | 
 | 140 | .. seealso:: | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 141 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 142 |    :mod:`sys` | 
 | 143 |        contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`, | 
 | 144 |        and :data:`sys.stderr`. | 
 | 145 |  | 
 | 146 |  | 
 | 147 | Class hierarchy | 
 | 148 | --------------- | 
 | 149 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 150 | The implementation of I/O streams is organized as a hierarchy of classes.  First | 
 | 151 | :term:`abstract base classes <abstract base class>` (ABCs), which are used to | 
 | 152 | specify the various categories of streams, then concrete classes providing the | 
 | 153 | standard stream implementations. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 154 |  | 
 | 155 |    .. note:: | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 156 |  | 
 | 157 |       The abstract base classes also provide default implementations of some | 
 | 158 |       methods in order to help implementation of concrete stream classes.  For | 
 | 159 |       example, :class:`BufferedIOBase` provides unoptimized implementations of | 
 | 160 |       ``readinto()`` and ``readline()``. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 161 |  | 
 | 162 | At the top of the I/O hierarchy is the abstract base class :class:`IOBase`.  It | 
 | 163 | defines the basic interface to a stream.  Note, however, that there is no | 
 | 164 | separation between reading and writing to streams; implementations are allowed | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 165 | to raise :exc:`UnsupportedOperation` if they do not support a given operation. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 166 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 167 | The :class:`RawIOBase` ABC extends :class:`IOBase`.  It deals with the reading | 
 | 168 | and writing of bytes to a stream.  :class:`FileIO` subclasses :class:`RawIOBase` | 
 | 169 | to provide an interface to files in the machine's file system. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 170 |  | 
 | 171 | The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream | 
 | 172 | (:class:`RawIOBase`).  Its subclasses, :class:`BufferedWriter`, | 
 | 173 | :class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 174 | readable, writable, and both readable and writable.  :class:`BufferedRandom` | 
 | 175 | provides a buffered interface to random access streams.  Another | 
| Georg Brandl | 682d7e0 | 2010-10-06 10:26:05 +0000 | [diff] [blame] | 176 | :class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 177 | bytes. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 178 |  | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 179 | The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with | 
 | 180 | streams whose bytes represent text, and handles encoding and decoding to and | 
 | 181 | from strings. :class:`TextIOWrapper`, which extends it, is a buffered text | 
 | 182 | interface to a buffered raw stream (:class:`BufferedIOBase`). Finally, | 
 | 183 | :class:`StringIO` is an in-memory stream for text. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 184 |  | 
 | 185 | Argument names are not part of the specification, and only the arguments of | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 186 | :func:`open` are intended to be used as keyword arguments. | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 187 |  | 
 | 188 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 189 | I/O Base Classes | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 190 | ^^^^^^^^^^^^^^^^ | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 191 |  | 
 | 192 | .. class:: IOBase | 
 | 193 |  | 
 | 194 |    The abstract base class for all I/O classes, acting on streams of bytes. | 
 | 195 |    There is no public constructor. | 
 | 196 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 197 |    This class provides empty abstract implementations for many methods | 
 | 198 |    that derived classes can override selectively; the default | 
 | 199 |    implementations represent a file that cannot be read, written or | 
 | 200 |    seeked. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 201 |  | 
 | 202 |    Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`, | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 203 |    or :meth:`write` because their signatures will vary, implementations and | 
 | 204 |    clients should consider those methods part of the interface.  Also, | 
 | 205 |    implementations may raise a :exc:`IOError` when operations they do not | 
 | 206 |    support are called. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 207 |  | 
 | 208 |    The basic type used for binary data read from or written to a file is | 
 | 209 |    :class:`bytes`.  :class:`bytearray`\s are accepted too, and in some cases | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 210 |    (such as :class:`readinto`) required.  Text I/O classes work with | 
 | 211 |    :class:`str` data. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 212 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 213 |    Note that calling any method (even inquiries) on a closed stream is | 
 | 214 |    undefined.  Implementations may raise :exc:`IOError` in this case. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 215 |  | 
 | 216 |    IOBase (and its subclasses) support the iterator protocol, meaning that an | 
 | 217 |    :class:`IOBase` object can be iterated over yielding the lines in a stream. | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 218 |    Lines are defined slightly differently depending on whether the stream is | 
 | 219 |    a binary stream (yielding bytes), or a text stream (yielding character | 
 | 220 |    strings).  See :meth:`readline` below. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 221 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 222 |    IOBase is also a context manager and therefore supports the | 
 | 223 |    :keyword:`with` statement.  In this example, *file* is closed after the | 
 | 224 |    :keyword:`with` statement's suite is finished---even if an exception occurs:: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 225 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 226 |       with open('spam.txt', 'w') as file: | 
 | 227 |           file.write('Spam and eggs!') | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 228 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 229 |    :class:`IOBase` provides these data attributes and methods: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 230 |  | 
 | 231 |    .. method:: close() | 
 | 232 |  | 
| Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 233 |       Flush and close this stream. This method has no effect if the file is | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 234 |       already closed. Once the file is closed, any operation on the file | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 235 |       (e.g. reading or writing) will raise a :exc:`ValueError`. | 
| Antoine Pitrou | f9fc08f | 2010-04-28 19:59:32 +0000 | [diff] [blame] | 236 |  | 
 | 237 |       As a convenience, it is allowed to call this method more than once; | 
 | 238 |       only the first call, however, will have an effect. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 239 |  | 
 | 240 |    .. attribute:: closed | 
 | 241 |  | 
 | 242 |       True if the stream is closed. | 
 | 243 |  | 
 | 244 |    .. method:: fileno() | 
 | 245 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 246 |       Return the underlying file descriptor (an integer) of the stream if it | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 247 |       exists.  An :exc:`IOError` is raised if the IO object does not use a file | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 248 |       descriptor. | 
 | 249 |  | 
 | 250 |    .. method:: flush() | 
 | 251 |  | 
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 252 |       Flush the write buffers of the stream if applicable.  This does nothing | 
 | 253 |       for read-only and non-blocking streams. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 254 |  | 
 | 255 |    .. method:: isatty() | 
 | 256 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 257 |       Return ``True`` if the stream is interactive (i.e., connected to | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 258 |       a terminal/tty device). | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 259 |  | 
 | 260 |    .. method:: readable() | 
 | 261 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 262 |       Return ``True`` if the stream can be read from.  If False, :meth:`read` | 
 | 263 |       will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 264 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 265 |    .. method:: readline(limit=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 266 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 267 |       Read and return one line from the stream.  If *limit* is specified, at | 
 | 268 |       most *limit* bytes will be read. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 269 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 270 |       The line terminator is always ``b'\n'`` for binary files; for text files, | 
| Benjamin Peterson | 6b4fa77 | 2010-08-30 13:19:53 +0000 | [diff] [blame] | 271 |       the *newlines* argument to :func:`open` can be used to select the line | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 272 |       terminator(s) recognized. | 
 | 273 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 274 |    .. method:: readlines(hint=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 275 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 276 |       Read and return a list of lines from the stream.  *hint* can be specified | 
 | 277 |       to control the number of lines read: no more lines will be read if the | 
 | 278 |       total size (in bytes/characters) of all lines so far exceeds *hint*. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 279 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 280 |    .. method:: seek(offset, whence=SEEK_SET) | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 281 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 282 |       Change the stream position to the given byte *offset*.  *offset* is | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 283 |       interpreted relative to the position indicated by *whence*.  Values for | 
 | 284 |       *whence* are: | 
 | 285 |  | 
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 286 |       * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); | 
 | 287 |         *offset* should be zero or positive | 
 | 288 |       * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may | 
 | 289 |         be negative | 
 | 290 |       * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually | 
 | 291 |         negative | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 292 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 293 |       Return the new absolute position. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 294 |  | 
| Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 295 |       .. versionadded:: 3.1 | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 296 |          The ``SEEK_*`` constants. | 
| Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 297 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 298 |    .. method:: seekable() | 
 | 299 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 300 |       Return ``True`` if the stream supports random access.  If ``False``, | 
 | 301 |       :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 302 |  | 
 | 303 |    .. method:: tell() | 
 | 304 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 305 |       Return the current stream position. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 306 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 307 |    .. method:: truncate(size=None) | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 308 |  | 
| Antoine Pitrou | 2016dc9 | 2010-05-29 12:08:25 +0000 | [diff] [blame] | 309 |       Resize the stream to the given *size* in bytes (or the current position | 
 | 310 |       if *size* is not specified).  The current stream position isn't changed. | 
 | 311 |       This resizing can extend or reduce the current file size.  In case of | 
 | 312 |       extension, the contents of the new file area depend on the platform | 
 | 313 |       (on most systems, additional bytes are zero-filled, on Windows they're | 
 | 314 |       undetermined).  The new file size is returned. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 315 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 316 |    .. method:: writable() | 
 | 317 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 318 |       Return ``True`` if the stream supports writing.  If ``False``, | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 319 |       :meth:`write` and :meth:`truncate` will raise :exc:`IOError`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 320 |  | 
 | 321 |    .. method:: writelines(lines) | 
 | 322 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 323 |       Write a list of lines to the stream.  Line separators are not added, so it | 
 | 324 |       is usual for each of the lines provided to have a line separator at the | 
 | 325 |       end. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 326 |  | 
 | 327 |  | 
 | 328 | .. class:: RawIOBase | 
 | 329 |  | 
 | 330 |    Base class for raw binary I/O.  It inherits :class:`IOBase`.  There is no | 
 | 331 |    public constructor. | 
 | 332 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 333 |    Raw binary I/O typically provides low-level access to an underlying OS | 
 | 334 |    device or API, and does not try to encapsulate it in high-level primitives | 
 | 335 |    (this is left to Buffered I/O and Text I/O, described later in this page). | 
 | 336 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 337 |    In addition to the attributes and methods from :class:`IOBase`, | 
 | 338 |    RawIOBase provides the following methods: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 339 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 340 |    .. method:: read(n=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 341 |  | 
| Antoine Pitrou | 78ddbe6 | 2009-10-01 16:24:45 +0000 | [diff] [blame] | 342 |       Read up to *n* bytes from the object and return them.  As a convenience, | 
 | 343 |       if *n* is unspecified or -1, :meth:`readall` is called.  Otherwise, | 
 | 344 |       only one system call is ever made.  Fewer than *n* bytes may be | 
 | 345 |       returned if the operating system call returns fewer than *n* bytes. | 
 | 346 |  | 
 | 347 |       If 0 bytes are returned, and *n* was not 0, this indicates end of file. | 
 | 348 |       If the object is in non-blocking mode and no bytes are available, | 
 | 349 |       ``None`` is returned. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 350 |  | 
| Benjamin Peterson | b47aace | 2008-04-09 21:38:38 +0000 | [diff] [blame] | 351 |    .. method:: readall() | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 352 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 353 |       Read and return all the bytes from the stream until EOF, using multiple | 
 | 354 |       calls to the stream if necessary. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 355 |  | 
 | 356 |    .. method:: readinto(b) | 
 | 357 |  | 
| Daniel Stutzbach | d01df46 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 358 |       Read up to len(b) bytes into bytearray *b* and return the number | 
 | 359 |       of bytes read.  If the object is in non-blocking mode and no | 
 | 360 |       bytes are available, ``None`` is returned. | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 361 |  | 
 | 362 |    .. method:: write(b) | 
 | 363 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 364 |       Write the given bytes or bytearray object, *b*, to the underlying raw | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 365 |       stream and return the number of bytes written.  This can be less than | 
 | 366 |       ``len(b)``, depending on specifics of the underlying raw stream, and | 
 | 367 |       especially if it is in non-blocking mode.  ``None`` is returned if the | 
 | 368 |       raw stream is set not to block and no single byte could be readily | 
 | 369 |       written to it. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 370 |  | 
 | 371 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 372 | .. class:: BufferedIOBase | 
 | 373 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 374 |    Base class for binary streams that support some kind of buffering. | 
 | 375 |    It inherits :class:`IOBase`. There is no public constructor. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 376 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 377 |    The main difference with :class:`RawIOBase` is that methods :meth:`read`, | 
 | 378 |    :meth:`readinto` and :meth:`write` will try (respectively) to read as much | 
 | 379 |    input as requested or to consume all given output, at the expense of | 
 | 380 |    making perhaps more than one system call. | 
 | 381 |  | 
 | 382 |    In addition, those methods can raise :exc:`BlockingIOError` if the | 
 | 383 |    underlying raw stream is in non-blocking mode and cannot take or give | 
 | 384 |    enough data; unlike their :class:`RawIOBase` counterparts, they will | 
 | 385 |    never return ``None``. | 
 | 386 |  | 
 | 387 |    Besides, the :meth:`read` method does not have a default | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 388 |    implementation that defers to :meth:`readinto`. | 
 | 389 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 390 |    A typical :class:`BufferedIOBase` implementation should not inherit from a | 
 | 391 |    :class:`RawIOBase` implementation, but wrap one, like | 
 | 392 |    :class:`BufferedWriter` and :class:`BufferedReader` do. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 393 |  | 
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 394 |    :class:`BufferedIOBase` provides or overrides these members in addition to | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 395 |    those from :class:`IOBase`: | 
 | 396 |  | 
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 397 |    .. attribute:: raw | 
 | 398 |  | 
 | 399 |       The underlying raw stream (a :class:`RawIOBase` instance) that | 
 | 400 |       :class:`BufferedIOBase` deals with.  This is not part of the | 
 | 401 |       :class:`BufferedIOBase` API and may not exist on some implementations. | 
 | 402 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 403 |    .. method:: detach() | 
 | 404 |  | 
 | 405 |       Separate the underlying raw stream from the buffer and return it. | 
 | 406 |  | 
 | 407 |       After the raw stream has been detached, the buffer is in an unusable | 
 | 408 |       state. | 
 | 409 |  | 
 | 410 |       Some buffers, like :class:`BytesIO`, do not have the concept of a single | 
 | 411 |       raw stream to return from this method.  They raise | 
 | 412 |       :exc:`UnsupportedOperation`. | 
 | 413 |  | 
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 414 |       .. versionadded:: 3.1 | 
 | 415 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 416 |    .. method:: read(n=-1) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 417 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 418 |       Read and return up to *n* bytes.  If the argument is omitted, ``None``, or | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 419 |       negative, data is read and returned until EOF is reached.  An empty bytes | 
 | 420 |       object is returned if the stream is already at EOF. | 
 | 421 |  | 
 | 422 |       If the argument is positive, and the underlying raw stream is not | 
 | 423 |       interactive, multiple raw reads may be issued to satisfy the byte count | 
 | 424 |       (unless EOF is reached first).  But for interactive raw streams, at most | 
 | 425 |       one raw read will be issued, and a short result does not imply that EOF is | 
 | 426 |       imminent. | 
 | 427 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 428 |       A :exc:`BlockingIOError` is raised if the underlying raw stream is in | 
 | 429 |       non blocking-mode, and has no data available at the moment. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 430 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 431 |    .. method:: read1(n=-1) | 
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 432 |  | 
 | 433 |       Read and return up to *n* bytes, with at most one call to the underlying | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 434 |       raw stream's :meth:`~RawIOBase.read` method.  This can be useful if you | 
 | 435 |       are implementing your own buffering on top of a :class:`BufferedIOBase` | 
 | 436 |       object. | 
| Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 437 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 438 |    .. method:: readinto(b) | 
 | 439 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 440 |       Read up to len(b) bytes into bytearray *b* and return the number of bytes | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 441 |       read. | 
 | 442 |  | 
 | 443 |       Like :meth:`read`, multiple reads may be issued to the underlying raw | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 444 |       stream, unless the latter is 'interactive'. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 445 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 446 |       A :exc:`BlockingIOError` is raised if the underlying raw stream is in | 
 | 447 |       non blocking-mode, and has no data available at the moment. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 448 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 449 |    .. method:: write(b) | 
 | 450 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 451 |       Write the given bytes or bytearray object, *b* and return the number | 
 | 452 |       of bytes written (never less than ``len(b)``, since if the write fails | 
 | 453 |       an :exc:`IOError` will be raised).  Depending on the actual | 
 | 454 |       implementation, these bytes may be readily written to the underlying | 
 | 455 |       stream, or held in a buffer for performance and latency reasons. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 456 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 457 |       When in non-blocking mode, a :exc:`BlockingIOError` is raised if the | 
 | 458 |       data needed to be written to the raw stream but it couldn't accept | 
 | 459 |       all the data without blocking. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 460 |  | 
 | 461 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 462 | Raw File I/O | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 463 | ^^^^^^^^^^^^ | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 464 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 465 | .. class:: FileIO(name, mode='r', closefd=True) | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 466 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 467 |    :class:`FileIO` represents an OS-level file containing bytes data. | 
 | 468 |    It implements the :class:`RawIOBase` interface (and therefore the | 
 | 469 |    :class:`IOBase` interface, too). | 
 | 470 |  | 
 | 471 |    The *name* can be one of two things: | 
 | 472 |  | 
 | 473 |    * a character string or bytes object representing the path to the file | 
 | 474 |      which will be opened; | 
 | 475 |    * an integer representing the number of an existing OS-level file descriptor | 
 | 476 |      to which the resulting :class:`FileIO` object will give access. | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 477 |  | 
 | 478 |    The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing, | 
 | 479 |    or appending.  The file will be created if it doesn't exist when opened for | 
 | 480 |    writing or appending; it will be truncated when opened for writing.  Add a | 
 | 481 |    ``'+'`` to the mode to allow simultaneous reading and writing. | 
 | 482 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 483 |    The :meth:`read` (when called with a positive argument), :meth:`readinto` | 
 | 484 |    and :meth:`write` methods on this class will only make one system call. | 
 | 485 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 486 |    In addition to the attributes and methods from :class:`IOBase` and | 
 | 487 |    :class:`RawIOBase`, :class:`FileIO` provides the following data | 
 | 488 |    attributes and methods: | 
 | 489 |  | 
 | 490 |    .. attribute:: mode | 
 | 491 |  | 
 | 492 |       The mode as given in the constructor. | 
 | 493 |  | 
 | 494 |    .. attribute:: name | 
 | 495 |  | 
 | 496 |       The file name.  This is the file descriptor of the file when no name is | 
 | 497 |       given in the constructor. | 
 | 498 |  | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 499 |  | 
 | 500 | Buffered Streams | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 501 | ^^^^^^^^^^^^^^^^ | 
| Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 502 |  | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 503 | Buffered I/O streams provide a higher-level interface to an I/O device | 
 | 504 | than raw I/O does. | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 505 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 506 | .. class:: BytesIO([initial_bytes]) | 
 | 507 |  | 
 | 508 |    A stream implementation using an in-memory bytes buffer.  It inherits | 
 | 509 |    :class:`BufferedIOBase`. | 
 | 510 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 511 |    The argument *initial_bytes* contains optional initial :class:`bytes` data. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 512 |  | 
 | 513 |    :class:`BytesIO` provides or overrides these methods in addition to those | 
 | 514 |    from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 515 |  | 
| Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 516 |    .. method:: getbuffer() | 
 | 517 |  | 
 | 518 |       Return a readable and writable view over the contents of the buffer | 
 | 519 |       without copying them.  Also, mutating the view will transparently | 
 | 520 |       update the contents of the buffer:: | 
 | 521 |  | 
 | 522 |          >>> b = io.BytesIO(b"abcdef") | 
 | 523 |          >>> view = b.getbuffer() | 
 | 524 |          >>> view[2:4] = b"56" | 
 | 525 |          >>> b.getvalue() | 
 | 526 |          b'ab56ef' | 
 | 527 |  | 
 | 528 |       .. note:: | 
 | 529 |          As long as the view exists, the :class:`BytesIO` object cannot be | 
 | 530 |          resized. | 
 | 531 |  | 
 | 532 |       .. versionadded:: 3.2 | 
 | 533 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 534 |    .. method:: getvalue() | 
 | 535 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 536 |       Return ``bytes`` containing the entire contents of the buffer. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 537 |  | 
 | 538 |    .. method:: read1() | 
 | 539 |  | 
| Benjamin Peterson | 9efcc4b | 2008-04-14 21:30:21 +0000 | [diff] [blame] | 540 |       In :class:`BytesIO`, this is the same as :meth:`read`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 541 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 542 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 543 | .. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 544 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 545 |    A buffer providing higher-level access to a readable, sequential | 
 | 546 |    :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`. | 
 | 547 |    When reading data from this object, a larger amount of data may be | 
 | 548 |    requested from the underlying raw stream, and kept in an internal buffer. | 
 | 549 |    The buffered data can then be returned directly on subsequent reads. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 550 |  | 
 | 551 |    The constructor creates a :class:`BufferedReader` for the given readable | 
 | 552 |    *raw* stream and *buffer_size*.  If *buffer_size* is omitted, | 
 | 553 |    :data:`DEFAULT_BUFFER_SIZE` is used. | 
 | 554 |  | 
 | 555 |    :class:`BufferedReader` provides or overrides these methods in addition to | 
 | 556 |    those from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 557 |  | 
 | 558 |    .. method:: peek([n]) | 
 | 559 |  | 
| Benjamin Peterson | c43a26d | 2009-06-16 23:09:24 +0000 | [diff] [blame] | 560 |       Return bytes from the stream without advancing the position.  At most one | 
| Benjamin Peterson | 2a8b54d | 2009-06-14 14:37:23 +0000 | [diff] [blame] | 561 |       single read on the raw stream is done to satisfy the call. The number of | 
 | 562 |       bytes returned may be less or more than requested. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 563 |  | 
 | 564 |    .. method:: read([n]) | 
 | 565 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 566 |       Read and return *n* bytes, or if *n* is not given or negative, until EOF | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 567 |       or if the read call would block in non-blocking mode. | 
 | 568 |  | 
 | 569 |    .. method:: read1(n) | 
 | 570 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 571 |       Read and return up to *n* bytes with only one call on the raw stream.  If | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 572 |       at least one byte is buffered, only buffered bytes are returned. | 
 | 573 |       Otherwise, one raw stream read call is made. | 
 | 574 |  | 
 | 575 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 576 | .. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 577 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 578 |    A buffer providing higher-level access to a writeable, sequential | 
 | 579 |    :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`. | 
 | 580 |    When writing to this object, data is normally held into an internal | 
 | 581 |    buffer.  The buffer will be written out to the underlying :class:`RawIOBase` | 
 | 582 |    object under various conditions, including: | 
 | 583 |  | 
 | 584 |    * when the buffer gets too small for all pending data; | 
 | 585 |    * when :meth:`flush()` is called; | 
 | 586 |    * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); | 
 | 587 |    * when the :class:`BufferedWriter` object is closed or destroyed. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 588 |  | 
 | 589 |    The constructor creates a :class:`BufferedWriter` for the given writeable | 
 | 590 |    *raw* stream.  If the *buffer_size* is not given, it defaults to | 
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 591 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 592 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 593 |    A third argument, *max_buffer_size*, is supported, but unused and deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 594 |  | 
 | 595 |    :class:`BufferedWriter` provides or overrides these methods in addition to | 
 | 596 |    those from :class:`BufferedIOBase` and :class:`IOBase`: | 
 | 597 |  | 
 | 598 |    .. method:: flush() | 
 | 599 |  | 
 | 600 |       Force bytes held in the buffer into the raw stream.  A | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 601 |       :exc:`BlockingIOError` should be raised if the raw stream blocks. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 602 |  | 
 | 603 |    .. method:: write(b) | 
 | 604 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 605 |       Write the bytes or bytearray object, *b* and return the number of bytes | 
 | 606 |       written.  When in non-blocking mode, a :exc:`BlockingIOError` is raised | 
 | 607 |       if the buffer needs to be written out but the raw stream blocks. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 608 |  | 
 | 609 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 610 | .. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 611 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 612 |    A buffered I/O object giving a combined, higher-level access to two | 
 | 613 |    sequential :class:`RawIOBase` objects: one readable, the other writeable. | 
 | 614 |    It is useful for pairs of unidirectional communication channels | 
 | 615 |    (pipes, for instance).  It inherits :class:`BufferedIOBase`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 616 |  | 
 | 617 |    *reader* and *writer* are :class:`RawIOBase` objects that are readable and | 
 | 618 |    writeable respectively.  If the *buffer_size* is omitted it defaults to | 
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 619 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 620 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 621 |    A fourth argument, *max_buffer_size*, is supported, but unused and | 
 | 622 |    deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 623 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 624 |    :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods | 
 | 625 |    except for :meth:`~BufferedIOBase.detach`, which raises | 
 | 626 |    :exc:`UnsupportedOperation`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 627 |  | 
 | 628 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 629 | .. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 630 |  | 
 | 631 |    A buffered interface to random access streams.  It inherits | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 632 |    :class:`BufferedReader` and :class:`BufferedWriter`, and further supports | 
 | 633 |    :meth:`seek` and :meth:`tell` functionality. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 634 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 635 |    The constructor creates a reader and writer for a seekable raw stream, given | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 636 |    in the first argument.  If the *buffer_size* is omitted it defaults to | 
| Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 637 |    :data:`DEFAULT_BUFFER_SIZE`. | 
 | 638 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 639 |    A third argument, *max_buffer_size*, is supported, but unused and deprecated. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 640 |  | 
 | 641 |    :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or | 
 | 642 |    :class:`BufferedWriter` can do. | 
 | 643 |  | 
 | 644 |  | 
 | 645 | Text I/O | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 646 | ^^^^^^^^ | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 647 |  | 
 | 648 | .. class:: TextIOBase | 
 | 649 |  | 
 | 650 |    Base class for text streams.  This class provides a character and line based | 
 | 651 |    interface to stream I/O.  There is no :meth:`readinto` method because | 
 | 652 |    Python's character strings are immutable.  It inherits :class:`IOBase`. | 
 | 653 |    There is no public constructor. | 
 | 654 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 655 |    :class:`TextIOBase` provides or overrides these data attributes and | 
 | 656 |    methods in addition to those from :class:`IOBase`: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 657 |  | 
 | 658 |    .. attribute:: encoding | 
 | 659 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 660 |       The name of the encoding used to decode the stream's bytes into | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 661 |       strings, and to encode strings into bytes. | 
 | 662 |  | 
| Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 663 |    .. attribute:: errors | 
 | 664 |  | 
 | 665 |       The error setting of the decoder or encoder. | 
 | 666 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 667 |    .. attribute:: newlines | 
 | 668 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 669 |       A string, a tuple of strings, or ``None``, indicating the newlines | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 670 |       translated so far.  Depending on the implementation and the initial | 
 | 671 |       constructor flags, this may not be available. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 672 |  | 
| Benjamin Peterson | c609b6b | 2009-06-28 17:32:20 +0000 | [diff] [blame] | 673 |    .. attribute:: buffer | 
 | 674 |  | 
 | 675 |       The underlying binary buffer (a :class:`BufferedIOBase` instance) that | 
 | 676 |       :class:`TextIOBase` deals with.  This is not part of the | 
 | 677 |       :class:`TextIOBase` API and may not exist on some implementations. | 
 | 678 |  | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 679 |    .. method:: detach() | 
 | 680 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 681 |       Separate the underlying binary buffer from the :class:`TextIOBase` and | 
 | 682 |       return it. | 
| Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 683 |  | 
 | 684 |       After the underlying buffer has been detached, the :class:`TextIOBase` is | 
 | 685 |       in an unusable state. | 
 | 686 |  | 
 | 687 |       Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not | 
 | 688 |       have the concept of an underlying buffer and calling this method will | 
 | 689 |       raise :exc:`UnsupportedOperation`. | 
 | 690 |  | 
| Benjamin Peterson | edc3647 | 2009-05-01 20:48:14 +0000 | [diff] [blame] | 691 |       .. versionadded:: 3.1 | 
 | 692 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 693 |    .. method:: read(n) | 
 | 694 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 695 |       Read and return at most *n* characters from the stream as a single | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 696 |       :class:`str`.  If *n* is negative or ``None``, reads until EOF. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 697 |  | 
 | 698 |    .. method:: readline() | 
 | 699 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 700 |       Read until newline or EOF and return a single ``str``.  If the stream is | 
 | 701 |       already at EOF, an empty string is returned. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 702 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 703 |    .. method:: write(s) | 
 | 704 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 705 |       Write the string *s* to the stream and return the number of characters | 
 | 706 |       written. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 707 |  | 
 | 708 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 709 | .. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 710 |  | 
| Antoine Pitrou | 497a767 | 2009-09-17 17:18:01 +0000 | [diff] [blame] | 711 |    A buffered text stream over a :class:`BufferedIOBase` binary stream. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 712 |    It inherits :class:`TextIOBase`. | 
 | 713 |  | 
 | 714 |    *encoding* gives the name of the encoding that the stream will be decoded or | 
 | 715 |    encoded with.  It defaults to :func:`locale.getpreferredencoding`. | 
 | 716 |  | 
| Benjamin Peterson | b85a584 | 2008-04-13 21:39:58 +0000 | [diff] [blame] | 717 |    *errors* is an optional string that specifies how encoding and decoding | 
 | 718 |    errors are to be handled.  Pass ``'strict'`` to raise a :exc:`ValueError` | 
 | 719 |    exception if there is an encoding error (the default of ``None`` has the same | 
 | 720 |    effect), or pass ``'ignore'`` to ignore errors.  (Note that ignoring encoding | 
 | 721 |    errors can lead to data loss.)  ``'replace'`` causes a replacement marker | 
| Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 722 |    (such as ``'?'``) to be inserted where there is malformed data.  When | 
 | 723 |    writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character | 
 | 724 |    reference) or ``'backslashreplace'`` (replace with backslashed escape | 
 | 725 |    sequences) can be used.  Any other error handling name that has been | 
 | 726 |    registered with :func:`codecs.register_error` is also valid. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 727 |  | 
 | 728 |    *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``.  It | 
 | 729 |    controls the handling of line endings.  If it is ``None``, universal newlines | 
 | 730 |    is enabled.  With this enabled, on input, the lines endings ``'\n'``, | 
 | 731 |    ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to | 
 | 732 |    the caller.  Conversely, on output, ``'\n'`` is translated to the system | 
| Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 733 |    default line separator, :data:`os.linesep`.  If *newline* is any other of its | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 734 |    legal values, that newline becomes the newline when the file is read and it | 
 | 735 |    is returned untranslated.  On output, ``'\n'`` is converted to the *newline*. | 
 | 736 |  | 
 | 737 |    If *line_buffering* is ``True``, :meth:`flush` is implied when a call to | 
 | 738 |    write contains a newline character. | 
 | 739 |  | 
| Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 740 |    :class:`TextIOWrapper` provides one attribute in addition to those of | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 741 |    :class:`TextIOBase` and its parents: | 
 | 742 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 743 |    .. attribute:: line_buffering | 
 | 744 |  | 
 | 745 |       Whether line buffering is enabled. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 746 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 747 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 748 | .. class:: StringIO(initial_value='', newline=None) | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 749 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 750 |    An in-memory stream for text I/O. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 751 |  | 
| Benjamin Peterson | aa1c8d8 | 2009-03-09 02:02:23 +0000 | [diff] [blame] | 752 |    The initial value of the buffer (an empty string by default) can be set by | 
 | 753 |    providing *initial_value*.  The *newline* argument works like that of | 
 | 754 |    :class:`TextIOWrapper`.  The default is to do no newline translation. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 755 |  | 
| Mark Summerfield | e6d5f30 | 2008-04-21 10:29:45 +0000 | [diff] [blame] | 756 |    :class:`StringIO` provides this method in addition to those from | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 757 |    :class:`TextIOBase` and its parents: | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 758 |  | 
 | 759 |    .. method:: getvalue() | 
 | 760 |  | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 761 |       Return a ``str`` containing the entire contents of the buffer at any | 
 | 762 |       time before the :class:`StringIO` object's :meth:`close` method is | 
 | 763 |       called. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 764 |  | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 765 |    Example usage:: | 
 | 766 |  | 
 | 767 |       import io | 
 | 768 |  | 
 | 769 |       output = io.StringIO() | 
 | 770 |       output.write('First line.\n') | 
 | 771 |       print('Second line.', file=output) | 
 | 772 |  | 
 | 773 |       # Retrieve file contents -- this will be | 
 | 774 |       # 'First line.\nSecond line.\n' | 
 | 775 |       contents = output.getvalue() | 
 | 776 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 777 |       # Close object and discard memory buffer -- | 
| Georg Brandl | 2932d93 | 2008-05-30 06:27:09 +0000 | [diff] [blame] | 778 |       # .getvalue() will now raise an exception. | 
 | 779 |       output.close() | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 780 |  | 
| Antoine Pitrou | b530e14 | 2010-08-30 12:41:00 +0000 | [diff] [blame] | 781 |  | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 782 | .. class:: IncrementalNewlineDecoder | 
 | 783 |  | 
| Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 784 |    A helper codec that decodes newlines for universal newlines mode.  It | 
 | 785 |    inherits :class:`codecs.IncrementalDecoder`. | 
| Georg Brandl | 014197c | 2008-04-09 18:40:51 +0000 | [diff] [blame] | 786 |  | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 787 |  | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 788 | Performance | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 789 | ----------- | 
 | 790 |  | 
 | 791 | This section discusses the performance of the provided concrete I/O | 
 | 792 | implementations. | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 793 |  | 
 | 794 | Binary I/O | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 795 | ^^^^^^^^^^ | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 796 |  | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 797 | By reading and writing only large chunks of data even when the user asks for a | 
 | 798 | single byte, buffered I/O hides any inefficiency in calling and executing the | 
 | 799 | operating system's unbuffered I/O routines.  The gain depends on the OS and the | 
 | 800 | kind of I/O which is performed.  For example, on some modern OSes such as Linux, | 
 | 801 | unbuffered disk I/O can be as fast as buffered I/O.  The bottom line, however, | 
 | 802 | is that buffered I/O offers predictable performance regardless of the platform | 
 | 803 | and the backing device.  Therefore, it is most always preferable to use buffered | 
 | 804 | I/O rather than unbuffered I/O for binary datal | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 805 |  | 
 | 806 | Text I/O | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 807 | ^^^^^^^^ | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 808 |  | 
 | 809 | Text I/O over a binary storage (such as a file) is significantly slower than | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 810 | binary I/O over the same storage, because it requires conversions between | 
 | 811 | unicode and binary data using a character codec.  This can become noticeable | 
 | 812 | handling huge amounts of text data like large log files.  Also, | 
 | 813 | :meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow | 
 | 814 | due to the reconstruction algorithm used. | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 815 |  | 
 | 816 | :class:`StringIO`, however, is a native in-memory unicode container and will | 
 | 817 | exhibit similar speed to :class:`BytesIO`. | 
 | 818 |  | 
 | 819 | Multi-threading | 
 | 820 | ^^^^^^^^^^^^^^^ | 
 | 821 |  | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 822 | :class:`FileIO` objects are thread-safe to the extent that the operating system | 
 | 823 | calls (such as ``read(2)`` under Unix) they wrap are thread-safe too. | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 824 |  | 
 | 825 | Binary buffered objects (instances of :class:`BufferedReader`, | 
 | 826 | :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) | 
 | 827 | protect their internal structures using a lock; it is therefore safe to call | 
 | 828 | them from multiple threads at once. | 
 | 829 |  | 
 | 830 | :class:`TextIOWrapper` objects are not thread-safe. | 
 | 831 |  | 
 | 832 | Reentrancy | 
 | 833 | ^^^^^^^^^^ | 
 | 834 |  | 
 | 835 | Binary buffered objects (instances of :class:`BufferedReader`, | 
 | 836 | :class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`) | 
 | 837 | are not reentrant.  While reentrant calls will not happen in normal situations, | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 838 | they can arise from doing I/O in a :mod:`signal` handler.  If a thread tries to | 
 | 839 | renter a buffered object which it is already accessing, a :exc:`RuntimeError` is | 
 | 840 | raised.  Note this doesn't prohibit a different thread from entering the | 
 | 841 | buffered object. | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 842 |  | 
| Benjamin Peterson | edf5132 | 2011-02-24 03:03:46 +0000 | [diff] [blame] | 843 | The above implicitly extends to text files, since the :func:`open()` function | 
 | 844 | will wrap a buffered object inside a :class:`TextIOWrapper`.  This includes | 
 | 845 | standard streams and therefore affects the built-in function :func:`print()` as | 
 | 846 | well. | 
| Antoine Pitrou | bed81c8 | 2010-12-03 19:14:17 +0000 | [diff] [blame] | 847 |  |