blob: 5a2de147576f3d2e14fc1c671ac2e7c0ddeef247 [file] [log] [blame]
Georg Brandl014197c2008-04-09 18:40:51 +00001: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 Peterson4fa88fa2009-03-04 00:14:51 +00009.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net>
10.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com>
Benjamin Petersonef9f2bd2009-05-01 20:45:43 +000011.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson058e31e2009-01-16 03:54:08 +000012.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Georg Brandl014197c2008-04-09 18:40:51 +000013
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000014.. _io-overview:
15
Antoine Pitroub530e142010-08-30 12:41:00 +000016Overview
17--------
Georg Brandl014197c2008-04-09 18:40:51 +000018
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000019The :mod:`io` module provides Python's main facilities for dealing for various
20types of I/O. There are three main types of I/O: *text I/O*, *binary I/O*, *raw
21I/O*. These are generic categories, and various backing stores can be used for
22each of them. Concrete objects belonging to any of these categories will often
23be called *streams*; another common term is *file-like objects*.
Georg Brandl014197c2008-04-09 18:40:51 +000024
Antoine Pitroub530e142010-08-30 12:41:00 +000025Independently of its category, each concrete stream object will also have
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000026various capabilities: it can be read-only, write-only, or read-write. It can
27also allow arbitrary random access (seeking forwards or backwards to any
28location), or only sequential access (for example in the case of a socket or
29pipe).
Georg Brandl014197c2008-04-09 18:40:51 +000030
Antoine Pitroub530e142010-08-30 12:41:00 +000031All streams are careful about the type of data you give to them. For example
32giving a :class:`str` object to the ``write()`` method of a binary stream
33will raise a ``TypeError``. So will giving a :class:`bytes` object to the
34``write()`` method of a text stream.
Georg Brandl014197c2008-04-09 18:40:51 +000035
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000036
Antoine Pitroub530e142010-08-30 12:41:00 +000037Text I/O
38^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +000039
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000040Text I/O expects and produces :class:`str` objects. This means that whenever
41the backing store is natively made of bytes (such as in the case of a file),
42encoding and decoding of data is made transparently as well as optional
43translation of platform-specific newline characters.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000044
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000045The easiest way to create a text stream is with :meth:`open()`, optionally
46specifying an encoding::
Antoine Pitroub530e142010-08-30 12:41:00 +000047
48 f = open("myfile.txt", "r", encoding="utf-8")
49
50In-memory text streams are also available as :class:`StringIO` objects::
51
52 f = io.StringIO("some initial text data")
53
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000054The text stream API is described in detail in the documentation for the
55:class:`TextIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000056
57.. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000058
59 Text I/O over a binary storage (such as a file) is significantly slower than
60 binary I/O over the same storage. This can become noticeable if you handle
61 huge amounts of text data (for example very large log files).
62
Antoine Pitroub530e142010-08-30 12:41:00 +000063
64Binary I/O
65^^^^^^^^^^
66
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000067Binary I/O (also called *buffered I/O*) expects and produces :class:`bytes`
68objects. No encoding, decoding, or newline translation is performed. This
69category of streams can be used for all kinds of non-text data, and also when
70manual control over the handling of text data is desired.
Antoine Pitroub530e142010-08-30 12:41:00 +000071
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000072The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in
73the mode string::
Antoine Pitroub530e142010-08-30 12:41:00 +000074
75 f = open("myfile.jpg", "rb")
76
77In-memory binary streams are also available as :class:`BytesIO` objects::
78
79 f = io.BytesIO(b"some initial binary data: \x00\x01")
80
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000081The binary stream API is described in detail in the docs of
82:class:`BufferedIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000083
84Other library modules may provide additional ways to create text or binary
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000085streams. See :meth:`socket.socket.makefile` for example.
86
Antoine Pitroub530e142010-08-30 12:41:00 +000087
88Raw I/O
89^^^^^^^
90
91Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
92building-block for binary and text streams; it is rarely useful to directly
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000093manipulate a raw stream from user code. Nevertheless, you can create a raw
94stream by opening a file in binary mode with buffering disabled::
Antoine Pitroub530e142010-08-30 12:41:00 +000095
96 f = open("myfile.jpg", "rb", buffering=0)
97
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000098The raw stream API is described in detail in the docs of :class:`RawIOBase`.
Benjamin Petersoncc12e1b2010-02-19 00:58:13 +000099
Georg Brandl014197c2008-04-09 18:40:51 +0000100
Antoine Pitroub530e142010-08-30 12:41:00 +0000101High-level Module Interface
102---------------------------
Georg Brandl014197c2008-04-09 18:40:51 +0000103
104.. data:: DEFAULT_BUFFER_SIZE
105
106 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000107 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000108 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +0000109
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000110
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000111.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Georg Brandl014197c2008-04-09 18:40:51 +0000112
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000113 This is an alias for the builtin :func:`open` function.
Georg Brandl014197c2008-04-09 18:40:51 +0000114
Georg Brandl014197c2008-04-09 18:40:51 +0000115
116.. exception:: BlockingIOError
117
118 Error raised when blocking would occur on a non-blocking stream. It inherits
119 :exc:`IOError`.
120
121 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
122 attribute:
123
124 .. attribute:: characters_written
125
126 An integer containing the number of characters written to the stream
127 before it blocked.
128
129
130.. exception:: UnsupportedOperation
131
132 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
133 when an unsupported operation is called on a stream.
134
135
Antoine Pitroub530e142010-08-30 12:41:00 +0000136In-memory streams
137^^^^^^^^^^^^^^^^^
138
139It is also possible to use a :class:`str` or :class:`bytes`-like object as a
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000140file for both reading and writing. For strings :class:`StringIO` can be used
141like a file opened in text mode. :class:`BytesIO` can be used like a file
142opened in binary mode. Both provide full read-write capabilities with random
143access.
Antoine Pitroub530e142010-08-30 12:41:00 +0000144
145
146.. seealso::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000147
Antoine Pitroub530e142010-08-30 12:41:00 +0000148 :mod:`sys`
149 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
150 and :data:`sys.stderr`.
151
152
153Class hierarchy
154---------------
155
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000156The implementation of I/O streams is organized as a hierarchy of classes. First
157:term:`abstract base classes <abstract base class>` (ABCs), which are used to
158specify the various categories of streams, then concrete classes providing the
159standard stream implementations.
Antoine Pitroub530e142010-08-30 12:41:00 +0000160
161 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000162
163 The abstract base classes also provide default implementations of some
164 methods in order to help implementation of concrete stream classes. For
165 example, :class:`BufferedIOBase` provides unoptimized implementations of
166 ``readinto()`` and ``readline()``.
Antoine Pitroub530e142010-08-30 12:41:00 +0000167
168At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
169defines the basic interface to a stream. Note, however, that there is no
170separation between reading and writing to streams; implementations are allowed
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000171to raise :exc:`UnsupportedOperation` if they do not support a given operation.
Antoine Pitroub530e142010-08-30 12:41:00 +0000172
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000173The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading
174and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase`
175to provide an interface to files in the machine's file system.
Antoine Pitroub530e142010-08-30 12:41:00 +0000176
177The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream
178(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
179:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000180readable, writable, and both readable and writable. :class:`BufferedRandom`
181provides a buffered interface to random access streams. Another
Georg Brandl682d7e02010-10-06 10:26:05 +0000182:class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000183bytes.
Antoine Pitroub530e142010-08-30 12:41:00 +0000184
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000185The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with
186streams whose bytes represent text, and handles encoding and decoding to and
187from strings. :class:`TextIOWrapper`, which extends it, is a buffered text
188interface to a buffered raw stream (:class:`BufferedIOBase`). Finally,
189:class:`StringIO` is an in-memory stream for text.
Antoine Pitroub530e142010-08-30 12:41:00 +0000190
191Argument names are not part of the specification, and only the arguments of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000192:func:`open` are intended to be used as keyword arguments.
Antoine Pitroub530e142010-08-30 12:41:00 +0000193
194
Georg Brandl014197c2008-04-09 18:40:51 +0000195I/O Base Classes
Antoine Pitroub530e142010-08-30 12:41:00 +0000196^^^^^^^^^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000197
198.. class:: IOBase
199
200 The abstract base class for all I/O classes, acting on streams of bytes.
201 There is no public constructor.
202
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000203 This class provides empty abstract implementations for many methods
204 that derived classes can override selectively; the default
205 implementations represent a file that cannot be read, written or
206 seeked.
Georg Brandl014197c2008-04-09 18:40:51 +0000207
208 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000209 or :meth:`write` because their signatures will vary, implementations and
210 clients should consider those methods part of the interface. Also,
211 implementations may raise a :exc:`IOError` when operations they do not
212 support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000213
214 The basic type used for binary data read from or written to a file is
215 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000216 (such as :class:`readinto`) required. Text I/O classes work with
217 :class:`str` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000218
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000219 Note that calling any method (even inquiries) on a closed stream is
220 undefined. Implementations may raise :exc:`IOError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000221
222 IOBase (and its subclasses) support the iterator protocol, meaning that an
223 :class:`IOBase` object can be iterated over yielding the lines in a stream.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000224 Lines are defined slightly differently depending on whether the stream is
225 a binary stream (yielding bytes), or a text stream (yielding character
226 strings). See :meth:`readline` below.
Georg Brandl014197c2008-04-09 18:40:51 +0000227
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000228 IOBase is also a context manager and therefore supports the
229 :keyword:`with` statement. In this example, *file* is closed after the
230 :keyword:`with` statement's suite is finished---even if an exception occurs::
Georg Brandl014197c2008-04-09 18:40:51 +0000231
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000232 with open('spam.txt', 'w') as file:
233 file.write('Spam and eggs!')
Georg Brandl014197c2008-04-09 18:40:51 +0000234
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000235 :class:`IOBase` provides these data attributes and methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000236
237 .. method:: close()
238
Christian Heimesecc42a22008-11-05 19:30:32 +0000239 Flush and close this stream. This method has no effect if the file is
Georg Brandl48310cd2009-01-03 21:18:54 +0000240 already closed. Once the file is closed, any operation on the file
Georg Brandl8569e582010-05-19 20:57:08 +0000241 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrouf9fc08f2010-04-28 19:59:32 +0000242
243 As a convenience, it is allowed to call this method more than once;
244 only the first call, however, will have an effect.
Georg Brandl014197c2008-04-09 18:40:51 +0000245
246 .. attribute:: closed
247
248 True if the stream is closed.
249
250 .. method:: fileno()
251
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000252 Return the underlying file descriptor (an integer) of the stream if it
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000253 exists. An :exc:`IOError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000254 descriptor.
255
256 .. method:: flush()
257
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000258 Flush the write buffers of the stream if applicable. This does nothing
259 for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000260
261 .. method:: isatty()
262
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000263 Return ``True`` if the stream is interactive (i.e., connected to
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000264 a terminal/tty device).
Georg Brandl014197c2008-04-09 18:40:51 +0000265
266 .. method:: readable()
267
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000268 Return ``True`` if the stream can be read from. If False, :meth:`read`
269 will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000270
Georg Brandl3dd33882009-06-01 17:35:27 +0000271 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000272
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000273 Read and return one line from the stream. If *limit* is specified, at
274 most *limit* bytes will be read.
Georg Brandl014197c2008-04-09 18:40:51 +0000275
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000276 The line terminator is always ``b'\n'`` for binary files; for text files,
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000277 the *newlines* argument to :func:`open` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000278 terminator(s) recognized.
279
Georg Brandl3dd33882009-06-01 17:35:27 +0000280 .. method:: readlines(hint=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000281
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000282 Read and return a list of lines from the stream. *hint* can be specified
283 to control the number of lines read: no more lines will be read if the
284 total size (in bytes/characters) of all lines so far exceeds *hint*.
Georg Brandl014197c2008-04-09 18:40:51 +0000285
Georg Brandl3dd33882009-06-01 17:35:27 +0000286 .. method:: seek(offset, whence=SEEK_SET)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000287
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000288 Change the stream position to the given byte *offset*. *offset* is
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000289 interpreted relative to the position indicated by *whence*. Values for
290 *whence* are:
291
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000292 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
293 *offset* should be zero or positive
294 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
295 be negative
296 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
297 negative
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000298
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000299 Return the new absolute position.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000300
Raymond Hettinger35a88362009-04-09 00:08:24 +0000301 .. versionadded:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000302 The ``SEEK_*`` constants.
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000303
Georg Brandl014197c2008-04-09 18:40:51 +0000304 .. method:: seekable()
305
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000306 Return ``True`` if the stream supports random access. If ``False``,
307 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000308
309 .. method:: tell()
310
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000311 Return the current stream position.
Georg Brandl014197c2008-04-09 18:40:51 +0000312
Georg Brandl3dd33882009-06-01 17:35:27 +0000313 .. method:: truncate(size=None)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000314
Antoine Pitrou2016dc92010-05-29 12:08:25 +0000315 Resize the stream to the given *size* in bytes (or the current position
316 if *size* is not specified). The current stream position isn't changed.
317 This resizing can extend or reduce the current file size. In case of
318 extension, the contents of the new file area depend on the platform
319 (on most systems, additional bytes are zero-filled, on Windows they're
320 undetermined). The new file size is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000321
Georg Brandl014197c2008-04-09 18:40:51 +0000322 .. method:: writable()
323
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000324 Return ``True`` if the stream supports writing. If ``False``,
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000325 :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000326
327 .. method:: writelines(lines)
328
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000329 Write a list of lines to the stream. Line separators are not added, so it
330 is usual for each of the lines provided to have a line separator at the
331 end.
Georg Brandl014197c2008-04-09 18:40:51 +0000332
333
334.. class:: RawIOBase
335
336 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
337 public constructor.
338
Antoine Pitrou497a7672009-09-17 17:18:01 +0000339 Raw binary I/O typically provides low-level access to an underlying OS
340 device or API, and does not try to encapsulate it in high-level primitives
341 (this is left to Buffered I/O and Text I/O, described later in this page).
342
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000343 In addition to the attributes and methods from :class:`IOBase`,
344 RawIOBase provides the following methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000345
Georg Brandl3dd33882009-06-01 17:35:27 +0000346 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000347
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000348 Read up to *n* bytes from the object and return them. As a convenience,
349 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise,
350 only one system call is ever made. Fewer than *n* bytes may be
351 returned if the operating system call returns fewer than *n* bytes.
352
353 If 0 bytes are returned, and *n* was not 0, this indicates end of file.
354 If the object is in non-blocking mode and no bytes are available,
355 ``None`` is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000356
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000357 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000358
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000359 Read and return all the bytes from the stream until EOF, using multiple
360 calls to the stream if necessary.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000361
362 .. method:: readinto(b)
363
Antoine Pitrou328ec742010-09-14 18:37:24 +0000364 Read up to len(b) bytes into bytearray *b* and return the number ofbytes
365 read. If the object is in non-blocking mode and no bytes are available,
366 ``None`` is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000367
368 .. method:: write(b)
369
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000370 Write the given bytes or bytearray object, *b*, to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000371 stream and return the number of bytes written. This can be less than
372 ``len(b)``, depending on specifics of the underlying raw stream, and
373 especially if it is in non-blocking mode. ``None`` is returned if the
374 raw stream is set not to block and no single byte could be readily
375 written to it.
Georg Brandl014197c2008-04-09 18:40:51 +0000376
377
Georg Brandl014197c2008-04-09 18:40:51 +0000378.. class:: BufferedIOBase
379
Antoine Pitrou497a7672009-09-17 17:18:01 +0000380 Base class for binary streams that support some kind of buffering.
381 It inherits :class:`IOBase`. There is no public constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000382
Antoine Pitrou497a7672009-09-17 17:18:01 +0000383 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
384 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
385 input as requested or to consume all given output, at the expense of
386 making perhaps more than one system call.
387
388 In addition, those methods can raise :exc:`BlockingIOError` if the
389 underlying raw stream is in non-blocking mode and cannot take or give
390 enough data; unlike their :class:`RawIOBase` counterparts, they will
391 never return ``None``.
392
393 Besides, the :meth:`read` method does not have a default
Georg Brandl014197c2008-04-09 18:40:51 +0000394 implementation that defers to :meth:`readinto`.
395
Antoine Pitrou497a7672009-09-17 17:18:01 +0000396 A typical :class:`BufferedIOBase` implementation should not inherit from a
397 :class:`RawIOBase` implementation, but wrap one, like
398 :class:`BufferedWriter` and :class:`BufferedReader` do.
Georg Brandl014197c2008-04-09 18:40:51 +0000399
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000400 :class:`BufferedIOBase` provides or overrides these members in addition to
Georg Brandl014197c2008-04-09 18:40:51 +0000401 those from :class:`IOBase`:
402
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000403 .. attribute:: raw
404
405 The underlying raw stream (a :class:`RawIOBase` instance) that
406 :class:`BufferedIOBase` deals with. This is not part of the
407 :class:`BufferedIOBase` API and may not exist on some implementations.
408
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000409 .. method:: detach()
410
411 Separate the underlying raw stream from the buffer and return it.
412
413 After the raw stream has been detached, the buffer is in an unusable
414 state.
415
416 Some buffers, like :class:`BytesIO`, do not have the concept of a single
417 raw stream to return from this method. They raise
418 :exc:`UnsupportedOperation`.
419
Benjamin Petersonedc36472009-05-01 20:48:14 +0000420 .. versionadded:: 3.1
421
Georg Brandl3dd33882009-06-01 17:35:27 +0000422 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000423
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000424 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
Georg Brandl014197c2008-04-09 18:40:51 +0000425 negative, data is read and returned until EOF is reached. An empty bytes
426 object is returned if the stream is already at EOF.
427
428 If the argument is positive, and the underlying raw stream is not
429 interactive, multiple raw reads may be issued to satisfy the byte count
430 (unless EOF is reached first). But for interactive raw streams, at most
431 one raw read will be issued, and a short result does not imply that EOF is
432 imminent.
433
Antoine Pitrou497a7672009-09-17 17:18:01 +0000434 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
435 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000436
Georg Brandl3dd33882009-06-01 17:35:27 +0000437 .. method:: read1(n=-1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000438
439 Read and return up to *n* bytes, with at most one call to the underlying
Antoine Pitrou497a7672009-09-17 17:18:01 +0000440 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
441 are implementing your own buffering on top of a :class:`BufferedIOBase`
442 object.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000443
Georg Brandl014197c2008-04-09 18:40:51 +0000444 .. method:: readinto(b)
445
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000446 Read up to len(b) bytes into bytearray *b* and return the number of bytes
Georg Brandl014197c2008-04-09 18:40:51 +0000447 read.
448
449 Like :meth:`read`, multiple reads may be issued to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000450 stream, unless the latter is 'interactive'.
Georg Brandl014197c2008-04-09 18:40:51 +0000451
Antoine Pitrou497a7672009-09-17 17:18:01 +0000452 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
453 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000454
Georg Brandl014197c2008-04-09 18:40:51 +0000455 .. method:: write(b)
456
Antoine Pitrou497a7672009-09-17 17:18:01 +0000457 Write the given bytes or bytearray object, *b* and return the number
458 of bytes written (never less than ``len(b)``, since if the write fails
459 an :exc:`IOError` will be raised). Depending on the actual
460 implementation, these bytes may be readily written to the underlying
461 stream, or held in a buffer for performance and latency reasons.
Georg Brandl014197c2008-04-09 18:40:51 +0000462
Antoine Pitrou497a7672009-09-17 17:18:01 +0000463 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
464 data needed to be written to the raw stream but it couldn't accept
465 all the data without blocking.
Georg Brandl014197c2008-04-09 18:40:51 +0000466
467
Benjamin Petersonaa069002009-01-23 03:26:36 +0000468Raw File I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000469^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000470
Georg Brandl3dd33882009-06-01 17:35:27 +0000471.. class:: FileIO(name, mode='r', closefd=True)
Benjamin Petersonaa069002009-01-23 03:26:36 +0000472
Antoine Pitrou497a7672009-09-17 17:18:01 +0000473 :class:`FileIO` represents an OS-level file containing bytes data.
474 It implements the :class:`RawIOBase` interface (and therefore the
475 :class:`IOBase` interface, too).
476
477 The *name* can be one of two things:
478
479 * a character string or bytes object representing the path to the file
480 which will be opened;
481 * an integer representing the number of an existing OS-level file descriptor
482 to which the resulting :class:`FileIO` object will give access.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000483
484 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
485 or appending. The file will be created if it doesn't exist when opened for
486 writing or appending; it will be truncated when opened for writing. Add a
487 ``'+'`` to the mode to allow simultaneous reading and writing.
488
Antoine Pitrou497a7672009-09-17 17:18:01 +0000489 The :meth:`read` (when called with a positive argument), :meth:`readinto`
490 and :meth:`write` methods on this class will only make one system call.
491
Benjamin Petersonaa069002009-01-23 03:26:36 +0000492 In addition to the attributes and methods from :class:`IOBase` and
493 :class:`RawIOBase`, :class:`FileIO` provides the following data
494 attributes and methods:
495
496 .. attribute:: mode
497
498 The mode as given in the constructor.
499
500 .. attribute:: name
501
502 The file name. This is the file descriptor of the file when no name is
503 given in the constructor.
504
Benjamin Petersonaa069002009-01-23 03:26:36 +0000505
506Buffered Streams
Antoine Pitroub530e142010-08-30 12:41:00 +0000507^^^^^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000508
Antoine Pitrou497a7672009-09-17 17:18:01 +0000509In many situations, buffered I/O streams will provide higher performance
510(bandwidth and latency) than raw I/O streams. Their API is also more usable.
511
Georg Brandl014197c2008-04-09 18:40:51 +0000512.. class:: BytesIO([initial_bytes])
513
514 A stream implementation using an in-memory bytes buffer. It inherits
515 :class:`BufferedIOBase`.
516
Antoine Pitroub530e142010-08-30 12:41:00 +0000517 The argument *initial_bytes* contains optional initial :class:`bytes` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000518
519 :class:`BytesIO` provides or overrides these methods in addition to those
520 from :class:`BufferedIOBase` and :class:`IOBase`:
521
Antoine Pitrou972ee132010-09-06 18:48:21 +0000522 .. method:: getbuffer()
523
524 Return a readable and writable view over the contents of the buffer
525 without copying them. Also, mutating the view will transparently
526 update the contents of the buffer::
527
528 >>> b = io.BytesIO(b"abcdef")
529 >>> view = b.getbuffer()
530 >>> view[2:4] = b"56"
531 >>> b.getvalue()
532 b'ab56ef'
533
534 .. note::
535 As long as the view exists, the :class:`BytesIO` object cannot be
536 resized.
537
538 .. versionadded:: 3.2
539
Georg Brandl014197c2008-04-09 18:40:51 +0000540 .. method:: getvalue()
541
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000542 Return ``bytes`` containing the entire contents of the buffer.
Georg Brandl014197c2008-04-09 18:40:51 +0000543
544 .. method:: read1()
545
Benjamin Peterson9efcc4b2008-04-14 21:30:21 +0000546 In :class:`BytesIO`, this is the same as :meth:`read`.
Georg Brandl014197c2008-04-09 18:40:51 +0000547
Georg Brandl014197c2008-04-09 18:40:51 +0000548
Georg Brandl3dd33882009-06-01 17:35:27 +0000549.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000550
Antoine Pitrou497a7672009-09-17 17:18:01 +0000551 A buffer providing higher-level access to a readable, sequential
552 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
553 When reading data from this object, a larger amount of data may be
554 requested from the underlying raw stream, and kept in an internal buffer.
555 The buffered data can then be returned directly on subsequent reads.
Georg Brandl014197c2008-04-09 18:40:51 +0000556
557 The constructor creates a :class:`BufferedReader` for the given readable
558 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
559 :data:`DEFAULT_BUFFER_SIZE` is used.
560
561 :class:`BufferedReader` provides or overrides these methods in addition to
562 those from :class:`BufferedIOBase` and :class:`IOBase`:
563
564 .. method:: peek([n])
565
Benjamin Petersonc43a26d2009-06-16 23:09:24 +0000566 Return bytes from the stream without advancing the position. At most one
Benjamin Peterson2a8b54d2009-06-14 14:37:23 +0000567 single read on the raw stream is done to satisfy the call. The number of
568 bytes returned may be less or more than requested.
Georg Brandl014197c2008-04-09 18:40:51 +0000569
570 .. method:: read([n])
571
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000572 Read and return *n* bytes, or if *n* is not given or negative, until EOF
Georg Brandl014197c2008-04-09 18:40:51 +0000573 or if the read call would block in non-blocking mode.
574
575 .. method:: read1(n)
576
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000577 Read and return up to *n* bytes with only one call on the raw stream. If
Georg Brandl014197c2008-04-09 18:40:51 +0000578 at least one byte is buffered, only buffered bytes are returned.
579 Otherwise, one raw stream read call is made.
580
581
Georg Brandl3dd33882009-06-01 17:35:27 +0000582.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000583
Antoine Pitrou497a7672009-09-17 17:18:01 +0000584 A buffer providing higher-level access to a writeable, sequential
585 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
586 When writing to this object, data is normally held into an internal
587 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
588 object under various conditions, including:
589
590 * when the buffer gets too small for all pending data;
591 * when :meth:`flush()` is called;
592 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
593 * when the :class:`BufferedWriter` object is closed or destroyed.
Georg Brandl014197c2008-04-09 18:40:51 +0000594
595 The constructor creates a :class:`BufferedWriter` for the given writeable
596 *raw* stream. If the *buffer_size* is not given, it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000597 :data:`DEFAULT_BUFFER_SIZE`.
598
Georg Brandl3dd33882009-06-01 17:35:27 +0000599 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000600
601 :class:`BufferedWriter` provides or overrides these methods in addition to
602 those from :class:`BufferedIOBase` and :class:`IOBase`:
603
604 .. method:: flush()
605
606 Force bytes held in the buffer into the raw stream. A
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000607 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000608
609 .. method:: write(b)
610
Antoine Pitrou497a7672009-09-17 17:18:01 +0000611 Write the bytes or bytearray object, *b* and return the number of bytes
612 written. When in non-blocking mode, a :exc:`BlockingIOError` is raised
613 if the buffer needs to be written out but the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000614
615
Antoine Pitrou497a7672009-09-17 17:18:01 +0000616.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000617
Antoine Pitrou497a7672009-09-17 17:18:01 +0000618 A buffered I/O object giving a combined, higher-level access to two
619 sequential :class:`RawIOBase` objects: one readable, the other writeable.
620 It is useful for pairs of unidirectional communication channels
621 (pipes, for instance). It inherits :class:`BufferedIOBase`.
Georg Brandl014197c2008-04-09 18:40:51 +0000622
623 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
624 writeable respectively. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000625 :data:`DEFAULT_BUFFER_SIZE`.
626
Georg Brandl3dd33882009-06-01 17:35:27 +0000627 A fourth argument, *max_buffer_size*, is supported, but unused and
628 deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000629
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000630 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
631 except for :meth:`~BufferedIOBase.detach`, which raises
632 :exc:`UnsupportedOperation`.
Georg Brandl014197c2008-04-09 18:40:51 +0000633
634
Georg Brandl3dd33882009-06-01 17:35:27 +0000635.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000636
637 A buffered interface to random access streams. It inherits
Antoine Pitrou497a7672009-09-17 17:18:01 +0000638 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
639 :meth:`seek` and :meth:`tell` functionality.
Georg Brandl014197c2008-04-09 18:40:51 +0000640
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000641 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000642 in the first argument. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000643 :data:`DEFAULT_BUFFER_SIZE`.
644
Georg Brandl3dd33882009-06-01 17:35:27 +0000645 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000646
647 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
648 :class:`BufferedWriter` can do.
649
650
651Text I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000652^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000653
654.. class:: TextIOBase
655
656 Base class for text streams. This class provides a character and line based
657 interface to stream I/O. There is no :meth:`readinto` method because
658 Python's character strings are immutable. It inherits :class:`IOBase`.
659 There is no public constructor.
660
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000661 :class:`TextIOBase` provides or overrides these data attributes and
662 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000663
664 .. attribute:: encoding
665
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000666 The name of the encoding used to decode the stream's bytes into
Georg Brandl014197c2008-04-09 18:40:51 +0000667 strings, and to encode strings into bytes.
668
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000669 .. attribute:: errors
670
671 The error setting of the decoder or encoder.
672
Georg Brandl014197c2008-04-09 18:40:51 +0000673 .. attribute:: newlines
674
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000675 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrou497a7672009-09-17 17:18:01 +0000676 translated so far. Depending on the implementation and the initial
677 constructor flags, this may not be available.
Georg Brandl014197c2008-04-09 18:40:51 +0000678
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000679 .. attribute:: buffer
680
681 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
682 :class:`TextIOBase` deals with. This is not part of the
683 :class:`TextIOBase` API and may not exist on some implementations.
684
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000685 .. method:: detach()
686
Antoine Pitrou497a7672009-09-17 17:18:01 +0000687 Separate the underlying binary buffer from the :class:`TextIOBase` and
688 return it.
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000689
690 After the underlying buffer has been detached, the :class:`TextIOBase` is
691 in an unusable state.
692
693 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
694 have the concept of an underlying buffer and calling this method will
695 raise :exc:`UnsupportedOperation`.
696
Benjamin Petersonedc36472009-05-01 20:48:14 +0000697 .. versionadded:: 3.1
698
Georg Brandl014197c2008-04-09 18:40:51 +0000699 .. method:: read(n)
700
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000701 Read and return at most *n* characters from the stream as a single
Antoine Pitrou497a7672009-09-17 17:18:01 +0000702 :class:`str`. If *n* is negative or ``None``, reads until EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000703
704 .. method:: readline()
705
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000706 Read until newline or EOF and return a single ``str``. If the stream is
707 already at EOF, an empty string is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000708
Georg Brandl014197c2008-04-09 18:40:51 +0000709 .. method:: write(s)
710
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000711 Write the string *s* to the stream and return the number of characters
712 written.
Georg Brandl014197c2008-04-09 18:40:51 +0000713
714
Georg Brandl3dd33882009-06-01 17:35:27 +0000715.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
Georg Brandl014197c2008-04-09 18:40:51 +0000716
Antoine Pitrou497a7672009-09-17 17:18:01 +0000717 A buffered text stream over a :class:`BufferedIOBase` binary stream.
Georg Brandl014197c2008-04-09 18:40:51 +0000718 It inherits :class:`TextIOBase`.
719
720 *encoding* gives the name of the encoding that the stream will be decoded or
721 encoded with. It defaults to :func:`locale.getpreferredencoding`.
722
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000723 *errors* is an optional string that specifies how encoding and decoding
724 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
725 exception if there is an encoding error (the default of ``None`` has the same
726 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
727 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Christian Heimesa342c012008-04-20 21:01:16 +0000728 (such as ``'?'``) to be inserted where there is malformed data. When
729 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
730 reference) or ``'backslashreplace'`` (replace with backslashed escape
731 sequences) can be used. Any other error handling name that has been
732 registered with :func:`codecs.register_error` is also valid.
Georg Brandl014197c2008-04-09 18:40:51 +0000733
734 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It
735 controls the handling of line endings. If it is ``None``, universal newlines
736 is enabled. With this enabled, on input, the lines endings ``'\n'``,
737 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
738 the caller. Conversely, on output, ``'\n'`` is translated to the system
Mark Dickinson934896d2009-02-21 20:59:32 +0000739 default line separator, :data:`os.linesep`. If *newline* is any other of its
Georg Brandl014197c2008-04-09 18:40:51 +0000740 legal values, that newline becomes the newline when the file is read and it
741 is returned untranslated. On output, ``'\n'`` is converted to the *newline*.
742
743 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
744 write contains a newline character.
745
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000746 :class:`TextIOWrapper` provides one attribute in addition to those of
Georg Brandl014197c2008-04-09 18:40:51 +0000747 :class:`TextIOBase` and its parents:
748
Georg Brandl014197c2008-04-09 18:40:51 +0000749 .. attribute:: line_buffering
750
751 Whether line buffering is enabled.
Georg Brandl48310cd2009-01-03 21:18:54 +0000752
Georg Brandl014197c2008-04-09 18:40:51 +0000753
Georg Brandl3dd33882009-06-01 17:35:27 +0000754.. class:: StringIO(initial_value='', newline=None)
Georg Brandl014197c2008-04-09 18:40:51 +0000755
Antoine Pitroub530e142010-08-30 12:41:00 +0000756 An in-memory stream for text I/O.
Georg Brandl014197c2008-04-09 18:40:51 +0000757
Benjamin Petersonaa1c8d82009-03-09 02:02:23 +0000758 The initial value of the buffer (an empty string by default) can be set by
759 providing *initial_value*. The *newline* argument works like that of
760 :class:`TextIOWrapper`. The default is to do no newline translation.
Georg Brandl014197c2008-04-09 18:40:51 +0000761
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000762 :class:`StringIO` provides this method in addition to those from
Antoine Pitroub530e142010-08-30 12:41:00 +0000763 :class:`TextIOBase` and its parents:
Georg Brandl014197c2008-04-09 18:40:51 +0000764
765 .. method:: getvalue()
766
Georg Brandl2932d932008-05-30 06:27:09 +0000767 Return a ``str`` containing the entire contents of the buffer at any
768 time before the :class:`StringIO` object's :meth:`close` method is
769 called.
Georg Brandl014197c2008-04-09 18:40:51 +0000770
Georg Brandl2932d932008-05-30 06:27:09 +0000771 Example usage::
772
773 import io
774
775 output = io.StringIO()
776 output.write('First line.\n')
777 print('Second line.', file=output)
778
779 # Retrieve file contents -- this will be
780 # 'First line.\nSecond line.\n'
781 contents = output.getvalue()
782
Georg Brandl48310cd2009-01-03 21:18:54 +0000783 # Close object and discard memory buffer --
Georg Brandl2932d932008-05-30 06:27:09 +0000784 # .getvalue() will now raise an exception.
785 output.close()
Georg Brandl014197c2008-04-09 18:40:51 +0000786
Antoine Pitroub530e142010-08-30 12:41:00 +0000787 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000788
789 :class:`StringIO` uses a native text storage and doesn't suffer from the
790 performance issues of other text streams, such as those based on
Antoine Pitroub530e142010-08-30 12:41:00 +0000791 :class:`TextIOWrapper`.
792
Georg Brandl014197c2008-04-09 18:40:51 +0000793.. class:: IncrementalNewlineDecoder
794
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000795 A helper codec that decodes newlines for universal newlines mode. It
796 inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +0000797