blob: 86faa5827bb346e68d191cb8b0feb9c96095e182 [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
Antoine Pitroub530e142010-08-30 12:41:00 +000057
58Binary I/O
59^^^^^^^^^^
60
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000061Binary I/O (also called *buffered I/O*) expects and produces :class:`bytes`
62objects. No encoding, decoding, or newline translation is performed. This
63category of streams can be used for all kinds of non-text data, and also when
64manual control over the handling of text data is desired.
Antoine Pitroub530e142010-08-30 12:41:00 +000065
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000066The easiest way to create a binary stream is with :meth:`open()` with ``'b'`` in
67the mode string::
Antoine Pitroub530e142010-08-30 12:41:00 +000068
69 f = open("myfile.jpg", "rb")
70
71In-memory binary streams are also available as :class:`BytesIO` objects::
72
73 f = io.BytesIO(b"some initial binary data: \x00\x01")
74
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000075The binary stream API is described in detail in the docs of
76:class:`BufferedIOBase`.
Antoine Pitroub530e142010-08-30 12:41:00 +000077
78Other library modules may provide additional ways to create text or binary
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000079streams. See :meth:`socket.socket.makefile` for example.
80
Antoine Pitroub530e142010-08-30 12:41:00 +000081
82Raw I/O
83^^^^^^^
84
85Raw I/O (also called *unbuffered I/O*) is generally used as a low-level
86building-block for binary and text streams; it is rarely useful to directly
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000087manipulate a raw stream from user code. Nevertheless, you can create a raw
88stream by opening a file in binary mode with buffering disabled::
Antoine Pitroub530e142010-08-30 12:41:00 +000089
90 f = open("myfile.jpg", "rb", buffering=0)
91
Benjamin Peterson6b4fa772010-08-30 13:19:53 +000092The raw stream API is described in detail in the docs of :class:`RawIOBase`.
Benjamin Petersoncc12e1b2010-02-19 00:58:13 +000093
Georg Brandl014197c2008-04-09 18:40:51 +000094
Antoine Pitroub530e142010-08-30 12:41:00 +000095High-level Module Interface
96---------------------------
Georg Brandl014197c2008-04-09 18:40:51 +000097
98.. data:: DEFAULT_BUFFER_SIZE
99
100 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000101 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000102 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +0000103
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000104
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000105.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Georg Brandl014197c2008-04-09 18:40:51 +0000106
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000107 This is an alias for the builtin :func:`open` function.
Georg Brandl014197c2008-04-09 18:40:51 +0000108
Georg Brandl014197c2008-04-09 18:40:51 +0000109
110.. exception:: BlockingIOError
111
Antoine Pitrouf55011f2011-10-12 18:57:23 +0200112 This is a compatibility alias for the builtin :exc:`BlockingIOError`
113 exception.
Georg Brandl014197c2008-04-09 18:40:51 +0000114
115
116.. exception:: UnsupportedOperation
117
118 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
119 when an unsupported operation is called on a stream.
120
121
Antoine Pitroub530e142010-08-30 12:41:00 +0000122In-memory streams
123^^^^^^^^^^^^^^^^^
124
125It is also possible to use a :class:`str` or :class:`bytes`-like object as a
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000126file for both reading and writing. For strings :class:`StringIO` can be used
127like a file opened in text mode. :class:`BytesIO` can be used like a file
128opened in binary mode. Both provide full read-write capabilities with random
129access.
Antoine Pitroub530e142010-08-30 12:41:00 +0000130
131
132.. seealso::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000133
Antoine Pitroub530e142010-08-30 12:41:00 +0000134 :mod:`sys`
135 contains the standard IO streams: :data:`sys.stdin`, :data:`sys.stdout`,
136 and :data:`sys.stderr`.
137
138
139Class hierarchy
140---------------
141
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000142The implementation of I/O streams is organized as a hierarchy of classes. First
143:term:`abstract base classes <abstract base class>` (ABCs), which are used to
144specify the various categories of streams, then concrete classes providing the
145standard stream implementations.
Antoine Pitroub530e142010-08-30 12:41:00 +0000146
147 .. note::
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000148
149 The abstract base classes also provide default implementations of some
150 methods in order to help implementation of concrete stream classes. For
151 example, :class:`BufferedIOBase` provides unoptimized implementations of
152 ``readinto()`` and ``readline()``.
Antoine Pitroub530e142010-08-30 12:41:00 +0000153
154At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
155defines the basic interface to a stream. Note, however, that there is no
156separation between reading and writing to streams; implementations are allowed
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000157to raise :exc:`UnsupportedOperation` if they do not support a given operation.
Antoine Pitroub530e142010-08-30 12:41:00 +0000158
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000159The :class:`RawIOBase` ABC extends :class:`IOBase`. It deals with the reading
160and writing of bytes to a stream. :class:`FileIO` subclasses :class:`RawIOBase`
161to provide an interface to files in the machine's file system.
Antoine Pitroub530e142010-08-30 12:41:00 +0000162
163The :class:`BufferedIOBase` ABC deals with buffering on a raw byte stream
164(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
165:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000166readable, writable, and both readable and writable. :class:`BufferedRandom`
167provides a buffered interface to random access streams. Another
Georg Brandl682d7e02010-10-06 10:26:05 +0000168:class:`BufferedIOBase` subclass, :class:`BytesIO`, is a stream of in-memory
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000169bytes.
Antoine Pitroub530e142010-08-30 12:41:00 +0000170
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000171The :class:`TextIOBase` ABC, another subclass of :class:`IOBase`, deals with
172streams whose bytes represent text, and handles encoding and decoding to and
173from strings. :class:`TextIOWrapper`, which extends it, is a buffered text
174interface to a buffered raw stream (:class:`BufferedIOBase`). Finally,
175:class:`StringIO` is an in-memory stream for text.
Antoine Pitroub530e142010-08-30 12:41:00 +0000176
177Argument names are not part of the specification, and only the arguments of
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000178:func:`open` are intended to be used as keyword arguments.
Antoine Pitroub530e142010-08-30 12:41:00 +0000179
180
Georg Brandl014197c2008-04-09 18:40:51 +0000181I/O Base Classes
Antoine Pitroub530e142010-08-30 12:41:00 +0000182^^^^^^^^^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000183
184.. class:: IOBase
185
186 The abstract base class for all I/O classes, acting on streams of bytes.
187 There is no public constructor.
188
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000189 This class provides empty abstract implementations for many methods
190 that derived classes can override selectively; the default
191 implementations represent a file that cannot be read, written or
192 seeked.
Georg Brandl014197c2008-04-09 18:40:51 +0000193
194 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000195 or :meth:`write` because their signatures will vary, implementations and
196 clients should consider those methods part of the interface. Also,
197 implementations may raise a :exc:`IOError` when operations they do not
198 support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000199
200 The basic type used for binary data read from or written to a file is
201 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000202 (such as :class:`readinto`) required. Text I/O classes work with
203 :class:`str` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000204
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000205 Note that calling any method (even inquiries) on a closed stream is
206 undefined. Implementations may raise :exc:`IOError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000207
208 IOBase (and its subclasses) support the iterator protocol, meaning that an
209 :class:`IOBase` object can be iterated over yielding the lines in a stream.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000210 Lines are defined slightly differently depending on whether the stream is
211 a binary stream (yielding bytes), or a text stream (yielding character
212 strings). See :meth:`readline` below.
Georg Brandl014197c2008-04-09 18:40:51 +0000213
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000214 IOBase is also a context manager and therefore supports the
215 :keyword:`with` statement. In this example, *file* is closed after the
216 :keyword:`with` statement's suite is finished---even if an exception occurs::
Georg Brandl014197c2008-04-09 18:40:51 +0000217
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000218 with open('spam.txt', 'w') as file:
219 file.write('Spam and eggs!')
Georg Brandl014197c2008-04-09 18:40:51 +0000220
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000221 :class:`IOBase` provides these data attributes and methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000222
223 .. method:: close()
224
Christian Heimesecc42a22008-11-05 19:30:32 +0000225 Flush and close this stream. This method has no effect if the file is
Georg Brandl48310cd2009-01-03 21:18:54 +0000226 already closed. Once the file is closed, any operation on the file
Georg Brandl8569e582010-05-19 20:57:08 +0000227 (e.g. reading or writing) will raise a :exc:`ValueError`.
Antoine Pitrouf9fc08f2010-04-28 19:59:32 +0000228
229 As a convenience, it is allowed to call this method more than once;
230 only the first call, however, will have an effect.
Georg Brandl014197c2008-04-09 18:40:51 +0000231
232 .. attribute:: closed
233
234 True if the stream is closed.
235
236 .. method:: fileno()
237
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000238 Return the underlying file descriptor (an integer) of the stream if it
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000239 exists. An :exc:`IOError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000240 descriptor.
241
242 .. method:: flush()
243
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000244 Flush the write buffers of the stream if applicable. This does nothing
245 for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000246
247 .. method:: isatty()
248
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000249 Return ``True`` if the stream is interactive (i.e., connected to
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000250 a terminal/tty device).
Georg Brandl014197c2008-04-09 18:40:51 +0000251
252 .. method:: readable()
253
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000254 Return ``True`` if the stream can be read from. If False, :meth:`read`
255 will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000256
Georg Brandl3dd33882009-06-01 17:35:27 +0000257 .. method:: readline(limit=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000258
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000259 Read and return one line from the stream. If *limit* is specified, at
260 most *limit* bytes will be read.
Georg Brandl014197c2008-04-09 18:40:51 +0000261
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000262 The line terminator is always ``b'\n'`` for binary files; for text files,
Benjamin Peterson6b4fa772010-08-30 13:19:53 +0000263 the *newlines* argument to :func:`open` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000264 terminator(s) recognized.
265
Georg Brandl3dd33882009-06-01 17:35:27 +0000266 .. method:: readlines(hint=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000267
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000268 Read and return a list of lines from the stream. *hint* can be specified
269 to control the number of lines read: no more lines will be read if the
270 total size (in bytes/characters) of all lines so far exceeds *hint*.
Georg Brandl014197c2008-04-09 18:40:51 +0000271
Georg Brandl3dd33882009-06-01 17:35:27 +0000272 .. method:: seek(offset, whence=SEEK_SET)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000273
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000274 Change the stream position to the given byte *offset*. *offset* is
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000275 interpreted relative to the position indicated by *whence*. Values for
276 *whence* are:
277
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000278 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
279 *offset* should be zero or positive
280 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
281 be negative
282 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
283 negative
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000284
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000285 Return the new absolute position.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000286
Raymond Hettinger35a88362009-04-09 00:08:24 +0000287 .. versionadded:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000288 The ``SEEK_*`` constants.
Benjamin Peterson0e4caf42009-04-01 21:22:20 +0000289
Georg Brandl014197c2008-04-09 18:40:51 +0000290 .. method:: seekable()
291
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000292 Return ``True`` if the stream supports random access. If ``False``,
293 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000294
295 .. method:: tell()
296
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000297 Return the current stream position.
Georg Brandl014197c2008-04-09 18:40:51 +0000298
Georg Brandl3dd33882009-06-01 17:35:27 +0000299 .. method:: truncate(size=None)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000300
Antoine Pitrou2016dc92010-05-29 12:08:25 +0000301 Resize the stream to the given *size* in bytes (or the current position
302 if *size* is not specified). The current stream position isn't changed.
303 This resizing can extend or reduce the current file size. In case of
304 extension, the contents of the new file area depend on the platform
305 (on most systems, additional bytes are zero-filled, on Windows they're
306 undetermined). The new file size is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000307
Georg Brandl014197c2008-04-09 18:40:51 +0000308 .. method:: writable()
309
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000310 Return ``True`` if the stream supports writing. If ``False``,
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000311 :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
Georg Brandl014197c2008-04-09 18:40:51 +0000312
313 .. method:: writelines(lines)
314
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000315 Write a list of lines to the stream. Line separators are not added, so it
316 is usual for each of the lines provided to have a line separator at the
317 end.
Georg Brandl014197c2008-04-09 18:40:51 +0000318
319
320.. class:: RawIOBase
321
322 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
323 public constructor.
324
Antoine Pitrou497a7672009-09-17 17:18:01 +0000325 Raw binary I/O typically provides low-level access to an underlying OS
326 device or API, and does not try to encapsulate it in high-level primitives
327 (this is left to Buffered I/O and Text I/O, described later in this page).
328
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000329 In addition to the attributes and methods from :class:`IOBase`,
330 RawIOBase provides the following methods:
Georg Brandl014197c2008-04-09 18:40:51 +0000331
Georg Brandl3dd33882009-06-01 17:35:27 +0000332 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000333
Antoine Pitrou78ddbe62009-10-01 16:24:45 +0000334 Read up to *n* bytes from the object and return them. As a convenience,
335 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise,
336 only one system call is ever made. Fewer than *n* bytes may be
337 returned if the operating system call returns fewer than *n* bytes.
338
339 If 0 bytes are returned, and *n* was not 0, this indicates end of file.
340 If the object is in non-blocking mode and no bytes are available,
341 ``None`` is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000342
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000343 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000344
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000345 Read and return all the bytes from the stream until EOF, using multiple
346 calls to the stream if necessary.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000347
348 .. method:: readinto(b)
349
Daniel Stutzbachd01df462010-11-30 17:49:53 +0000350 Read up to len(b) bytes into bytearray *b* and return the number
351 of bytes read. If the object is in non-blocking mode and no
352 bytes are available, ``None`` is returned.
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000353
354 .. method:: write(b)
355
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000356 Write the given bytes or bytearray object, *b*, to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000357 stream and return the number of bytes written. This can be less than
358 ``len(b)``, depending on specifics of the underlying raw stream, and
359 especially if it is in non-blocking mode. ``None`` is returned if the
360 raw stream is set not to block and no single byte could be readily
361 written to it.
Georg Brandl014197c2008-04-09 18:40:51 +0000362
363
Georg Brandl014197c2008-04-09 18:40:51 +0000364.. class:: BufferedIOBase
365
Antoine Pitrou497a7672009-09-17 17:18:01 +0000366 Base class for binary streams that support some kind of buffering.
367 It inherits :class:`IOBase`. There is no public constructor.
Georg Brandl014197c2008-04-09 18:40:51 +0000368
Antoine Pitrou497a7672009-09-17 17:18:01 +0000369 The main difference with :class:`RawIOBase` is that methods :meth:`read`,
370 :meth:`readinto` and :meth:`write` will try (respectively) to read as much
371 input as requested or to consume all given output, at the expense of
372 making perhaps more than one system call.
373
374 In addition, those methods can raise :exc:`BlockingIOError` if the
375 underlying raw stream is in non-blocking mode and cannot take or give
376 enough data; unlike their :class:`RawIOBase` counterparts, they will
377 never return ``None``.
378
379 Besides, the :meth:`read` method does not have a default
Georg Brandl014197c2008-04-09 18:40:51 +0000380 implementation that defers to :meth:`readinto`.
381
Antoine Pitrou497a7672009-09-17 17:18:01 +0000382 A typical :class:`BufferedIOBase` implementation should not inherit from a
383 :class:`RawIOBase` implementation, but wrap one, like
384 :class:`BufferedWriter` and :class:`BufferedReader` do.
Georg Brandl014197c2008-04-09 18:40:51 +0000385
Senthil Kumarana6bac952011-07-04 11:28:30 -0700386 :class:`BufferedIOBase` provides or overrides these methods and attribute in
387 addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000388
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000389 .. attribute:: raw
390
391 The underlying raw stream (a :class:`RawIOBase` instance) that
392 :class:`BufferedIOBase` deals with. This is not part of the
393 :class:`BufferedIOBase` API and may not exist on some implementations.
394
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000395 .. method:: detach()
396
397 Separate the underlying raw stream from the buffer and return it.
398
399 After the raw stream has been detached, the buffer is in an unusable
400 state.
401
402 Some buffers, like :class:`BytesIO`, do not have the concept of a single
403 raw stream to return from this method. They raise
404 :exc:`UnsupportedOperation`.
405
Benjamin Petersonedc36472009-05-01 20:48:14 +0000406 .. versionadded:: 3.1
407
Georg Brandl3dd33882009-06-01 17:35:27 +0000408 .. method:: read(n=-1)
Georg Brandl014197c2008-04-09 18:40:51 +0000409
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000410 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
Georg Brandl014197c2008-04-09 18:40:51 +0000411 negative, data is read and returned until EOF is reached. An empty bytes
412 object is returned if the stream is already at EOF.
413
414 If the argument is positive, and the underlying raw stream is not
415 interactive, multiple raw reads may be issued to satisfy the byte count
416 (unless EOF is reached first). But for interactive raw streams, at most
417 one raw read will be issued, and a short result does not imply that EOF is
418 imminent.
419
Antoine Pitrou497a7672009-09-17 17:18:01 +0000420 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
421 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000422
Georg Brandl3dd33882009-06-01 17:35:27 +0000423 .. method:: read1(n=-1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000424
425 Read and return up to *n* bytes, with at most one call to the underlying
Antoine Pitrou497a7672009-09-17 17:18:01 +0000426 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
427 are implementing your own buffering on top of a :class:`BufferedIOBase`
428 object.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000429
Georg Brandl014197c2008-04-09 18:40:51 +0000430 .. method:: readinto(b)
431
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000432 Read up to len(b) bytes into bytearray *b* and return the number of bytes
Georg Brandl014197c2008-04-09 18:40:51 +0000433 read.
434
435 Like :meth:`read`, multiple reads may be issued to the underlying raw
Antoine Pitrou497a7672009-09-17 17:18:01 +0000436 stream, unless the latter is 'interactive'.
Georg Brandl014197c2008-04-09 18:40:51 +0000437
Antoine Pitrou497a7672009-09-17 17:18:01 +0000438 A :exc:`BlockingIOError` is raised if the underlying raw stream is in
439 non blocking-mode, and has no data available at the moment.
Georg Brandl014197c2008-04-09 18:40:51 +0000440
Georg Brandl014197c2008-04-09 18:40:51 +0000441 .. method:: write(b)
442
Antoine Pitrou497a7672009-09-17 17:18:01 +0000443 Write the given bytes or bytearray object, *b* and return the number
444 of bytes written (never less than ``len(b)``, since if the write fails
445 an :exc:`IOError` will be raised). Depending on the actual
446 implementation, these bytes may be readily written to the underlying
447 stream, or held in a buffer for performance and latency reasons.
Georg Brandl014197c2008-04-09 18:40:51 +0000448
Antoine Pitrou497a7672009-09-17 17:18:01 +0000449 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
450 data needed to be written to the raw stream but it couldn't accept
451 all the data without blocking.
Georg Brandl014197c2008-04-09 18:40:51 +0000452
453
Benjamin Petersonaa069002009-01-23 03:26:36 +0000454Raw File I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000455^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000456
Georg Brandl3dd33882009-06-01 17:35:27 +0000457.. class:: FileIO(name, mode='r', closefd=True)
Benjamin Petersonaa069002009-01-23 03:26:36 +0000458
Antoine Pitrou497a7672009-09-17 17:18:01 +0000459 :class:`FileIO` represents an OS-level file containing bytes data.
460 It implements the :class:`RawIOBase` interface (and therefore the
461 :class:`IOBase` interface, too).
462
463 The *name* can be one of two things:
464
465 * a character string or bytes object representing the path to the file
466 which will be opened;
467 * an integer representing the number of an existing OS-level file descriptor
468 to which the resulting :class:`FileIO` object will give access.
Benjamin Petersonaa069002009-01-23 03:26:36 +0000469
470 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
471 or appending. The file will be created if it doesn't exist when opened for
472 writing or appending; it will be truncated when opened for writing. Add a
473 ``'+'`` to the mode to allow simultaneous reading and writing.
474
Antoine Pitrou497a7672009-09-17 17:18:01 +0000475 The :meth:`read` (when called with a positive argument), :meth:`readinto`
476 and :meth:`write` methods on this class will only make one system call.
477
Benjamin Petersonaa069002009-01-23 03:26:36 +0000478 In addition to the attributes and methods from :class:`IOBase` and
479 :class:`RawIOBase`, :class:`FileIO` provides the following data
480 attributes and methods:
481
482 .. attribute:: mode
483
484 The mode as given in the constructor.
485
486 .. attribute:: name
487
488 The file name. This is the file descriptor of the file when no name is
489 given in the constructor.
490
Benjamin Petersonaa069002009-01-23 03:26:36 +0000491
492Buffered Streams
Antoine Pitroub530e142010-08-30 12:41:00 +0000493^^^^^^^^^^^^^^^^
Benjamin Petersonaa069002009-01-23 03:26:36 +0000494
Antoine Pitroubed81c82010-12-03 19:14:17 +0000495Buffered I/O streams provide a higher-level interface to an I/O device
496than raw I/O does.
Antoine Pitrou497a7672009-09-17 17:18:01 +0000497
Georg Brandl014197c2008-04-09 18:40:51 +0000498.. class:: BytesIO([initial_bytes])
499
500 A stream implementation using an in-memory bytes buffer. It inherits
501 :class:`BufferedIOBase`.
502
Antoine Pitroub530e142010-08-30 12:41:00 +0000503 The argument *initial_bytes* contains optional initial :class:`bytes` data.
Georg Brandl014197c2008-04-09 18:40:51 +0000504
505 :class:`BytesIO` provides or overrides these methods in addition to those
506 from :class:`BufferedIOBase` and :class:`IOBase`:
507
Antoine Pitrou972ee132010-09-06 18:48:21 +0000508 .. method:: getbuffer()
509
510 Return a readable and writable view over the contents of the buffer
511 without copying them. Also, mutating the view will transparently
512 update the contents of the buffer::
513
514 >>> b = io.BytesIO(b"abcdef")
515 >>> view = b.getbuffer()
516 >>> view[2:4] = b"56"
517 >>> b.getvalue()
518 b'ab56ef'
519
520 .. note::
521 As long as the view exists, the :class:`BytesIO` object cannot be
522 resized.
523
524 .. versionadded:: 3.2
525
Georg Brandl014197c2008-04-09 18:40:51 +0000526 .. method:: getvalue()
527
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000528 Return ``bytes`` containing the entire contents of the buffer.
Georg Brandl014197c2008-04-09 18:40:51 +0000529
530 .. method:: read1()
531
Benjamin Peterson9efcc4b2008-04-14 21:30:21 +0000532 In :class:`BytesIO`, this is the same as :meth:`read`.
Georg Brandl014197c2008-04-09 18:40:51 +0000533
Georg Brandl014197c2008-04-09 18:40:51 +0000534
Georg Brandl3dd33882009-06-01 17:35:27 +0000535.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000536
Antoine Pitrou497a7672009-09-17 17:18:01 +0000537 A buffer providing higher-level access to a readable, sequential
538 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
539 When reading data from this object, a larger amount of data may be
540 requested from the underlying raw stream, and kept in an internal buffer.
541 The buffered data can then be returned directly on subsequent reads.
Georg Brandl014197c2008-04-09 18:40:51 +0000542
543 The constructor creates a :class:`BufferedReader` for the given readable
544 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
545 :data:`DEFAULT_BUFFER_SIZE` is used.
546
547 :class:`BufferedReader` provides or overrides these methods in addition to
548 those from :class:`BufferedIOBase` and :class:`IOBase`:
549
550 .. method:: peek([n])
551
Benjamin Petersonc43a26d2009-06-16 23:09:24 +0000552 Return bytes from the stream without advancing the position. At most one
Benjamin Peterson2a8b54d2009-06-14 14:37:23 +0000553 single read on the raw stream is done to satisfy the call. The number of
554 bytes returned may be less or more than requested.
Georg Brandl014197c2008-04-09 18:40:51 +0000555
556 .. method:: read([n])
557
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000558 Read and return *n* bytes, or if *n* is not given or negative, until EOF
Georg Brandl014197c2008-04-09 18:40:51 +0000559 or if the read call would block in non-blocking mode.
560
561 .. method:: read1(n)
562
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000563 Read and return up to *n* bytes with only one call on the raw stream. If
Georg Brandl014197c2008-04-09 18:40:51 +0000564 at least one byte is buffered, only buffered bytes are returned.
565 Otherwise, one raw stream read call is made.
566
567
Georg Brandl3dd33882009-06-01 17:35:27 +0000568.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000569
Antoine Pitrou497a7672009-09-17 17:18:01 +0000570 A buffer providing higher-level access to a writeable, sequential
571 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`.
572 When writing to this object, data is normally held into an internal
573 buffer. The buffer will be written out to the underlying :class:`RawIOBase`
574 object under various conditions, including:
575
576 * when the buffer gets too small for all pending data;
577 * when :meth:`flush()` is called;
578 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
579 * when the :class:`BufferedWriter` object is closed or destroyed.
Georg Brandl014197c2008-04-09 18:40:51 +0000580
581 The constructor creates a :class:`BufferedWriter` for the given writeable
582 *raw* stream. If the *buffer_size* is not given, it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000583 :data:`DEFAULT_BUFFER_SIZE`.
584
Georg Brandl3dd33882009-06-01 17:35:27 +0000585 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000586
587 :class:`BufferedWriter` provides or overrides these methods in addition to
588 those from :class:`BufferedIOBase` and :class:`IOBase`:
589
590 .. method:: flush()
591
592 Force bytes held in the buffer into the raw stream. A
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000593 :exc:`BlockingIOError` should be raised if the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000594
595 .. method:: write(b)
596
Antoine Pitrou497a7672009-09-17 17:18:01 +0000597 Write the bytes or bytearray object, *b* and return the number of bytes
598 written. When in non-blocking mode, a :exc:`BlockingIOError` is raised
599 if the buffer needs to be written out but the raw stream blocks.
Georg Brandl014197c2008-04-09 18:40:51 +0000600
601
Georg Brandl3dd33882009-06-01 17:35:27 +0000602.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
Georg Brandl014197c2008-04-09 18:40:51 +0000603
604 A buffered interface to random access streams. It inherits
Antoine Pitrou497a7672009-09-17 17:18:01 +0000605 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
606 :meth:`seek` and :meth:`tell` functionality.
Georg Brandl014197c2008-04-09 18:40:51 +0000607
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000608 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000609 in the first argument. If the *buffer_size* is omitted it defaults to
Benjamin Peterson394ee002009-03-05 22:33:59 +0000610 :data:`DEFAULT_BUFFER_SIZE`.
611
Georg Brandl3dd33882009-06-01 17:35:27 +0000612 A third argument, *max_buffer_size*, is supported, but unused and deprecated.
Georg Brandl014197c2008-04-09 18:40:51 +0000613
614 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
615 :class:`BufferedWriter` can do.
616
617
Antoine Pitrou13d28952011-08-20 19:48:43 +0200618.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
619
620 A buffered I/O object combining two unidirectional :class:`RawIOBase`
621 objects -- one readable, the other writeable -- into a single bidirectional
622 endpoint. It inherits :class:`BufferedIOBase`.
623
624 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
625 writeable respectively. If the *buffer_size* is omitted it defaults to
626 :data:`DEFAULT_BUFFER_SIZE`.
627
628 A fourth argument, *max_buffer_size*, is supported, but unused and
629 deprecated.
630
631 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
632 except for :meth:`~BufferedIOBase.detach`, which raises
633 :exc:`UnsupportedOperation`.
634
635 .. warning::
636 :class:`BufferedRWPair` does not attempt to synchronize accesses to
637 its underlying raw streams. You should not pass it the same object
638 as reader and writer; use :class:`BufferedRandom` instead.
639
640
Georg Brandl014197c2008-04-09 18:40:51 +0000641Text I/O
Antoine Pitroub530e142010-08-30 12:41:00 +0000642^^^^^^^^
Georg Brandl014197c2008-04-09 18:40:51 +0000643
644.. class:: TextIOBase
645
646 Base class for text streams. This class provides a character and line based
647 interface to stream I/O. There is no :meth:`readinto` method because
648 Python's character strings are immutable. It inherits :class:`IOBase`.
649 There is no public constructor.
650
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000651 :class:`TextIOBase` provides or overrides these data attributes and
652 methods in addition to those from :class:`IOBase`:
Georg Brandl014197c2008-04-09 18:40:51 +0000653
654 .. attribute:: encoding
655
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000656 The name of the encoding used to decode the stream's bytes into
Georg Brandl014197c2008-04-09 18:40:51 +0000657 strings, and to encode strings into bytes.
658
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000659 .. attribute:: errors
660
661 The error setting of the decoder or encoder.
662
Georg Brandl014197c2008-04-09 18:40:51 +0000663 .. attribute:: newlines
664
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000665 A string, a tuple of strings, or ``None``, indicating the newlines
Antoine Pitrou497a7672009-09-17 17:18:01 +0000666 translated so far. Depending on the implementation and the initial
667 constructor flags, this may not be available.
Georg Brandl014197c2008-04-09 18:40:51 +0000668
Benjamin Petersonc609b6b2009-06-28 17:32:20 +0000669 .. attribute:: buffer
670
671 The underlying binary buffer (a :class:`BufferedIOBase` instance) that
672 :class:`TextIOBase` deals with. This is not part of the
673 :class:`TextIOBase` API and may not exist on some implementations.
674
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000675 .. method:: detach()
676
Antoine Pitrou497a7672009-09-17 17:18:01 +0000677 Separate the underlying binary buffer from the :class:`TextIOBase` and
678 return it.
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000679
680 After the underlying buffer has been detached, the :class:`TextIOBase` is
681 in an unusable state.
682
683 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
684 have the concept of an underlying buffer and calling this method will
685 raise :exc:`UnsupportedOperation`.
686
Benjamin Petersonedc36472009-05-01 20:48:14 +0000687 .. versionadded:: 3.1
688
Georg Brandl014197c2008-04-09 18:40:51 +0000689 .. method:: read(n)
690
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000691 Read and return at most *n* characters from the stream as a single
Antoine Pitrou497a7672009-09-17 17:18:01 +0000692 :class:`str`. If *n* is negative or ``None``, reads until EOF.
Georg Brandl014197c2008-04-09 18:40:51 +0000693
694 .. method:: readline()
695
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000696 Read until newline or EOF and return a single ``str``. If the stream is
697 already at EOF, an empty string is returned.
Georg Brandl014197c2008-04-09 18:40:51 +0000698
Georg Brandl014197c2008-04-09 18:40:51 +0000699 .. method:: write(s)
700
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000701 Write the string *s* to the stream and return the number of characters
702 written.
Georg Brandl014197c2008-04-09 18:40:51 +0000703
704
Antoine Pitrou664091b2011-07-23 22:00:03 +0200705.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, \
706 line_buffering=False, write_through=False)
Georg Brandl014197c2008-04-09 18:40:51 +0000707
Antoine Pitrou497a7672009-09-17 17:18:01 +0000708 A buffered text stream over a :class:`BufferedIOBase` binary stream.
Georg Brandl014197c2008-04-09 18:40:51 +0000709 It inherits :class:`TextIOBase`.
710
711 *encoding* gives the name of the encoding that the stream will be decoded or
712 encoded with. It defaults to :func:`locale.getpreferredencoding`.
713
Benjamin Petersonb85a5842008-04-13 21:39:58 +0000714 *errors* is an optional string that specifies how encoding and decoding
715 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
716 exception if there is an encoding error (the default of ``None`` has the same
717 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
718 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Christian Heimesa342c012008-04-20 21:01:16 +0000719 (such as ``'?'``) to be inserted where there is malformed data. When
720 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
721 reference) or ``'backslashreplace'`` (replace with backslashed escape
722 sequences) can be used. Any other error handling name that has been
723 registered with :func:`codecs.register_error` is also valid.
Georg Brandl014197c2008-04-09 18:40:51 +0000724
725 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It
726 controls the handling of line endings. If it is ``None``, universal newlines
727 is enabled. With this enabled, on input, the lines endings ``'\n'``,
728 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
729 the caller. Conversely, on output, ``'\n'`` is translated to the system
Mark Dickinson934896d2009-02-21 20:59:32 +0000730 default line separator, :data:`os.linesep`. If *newline* is any other of its
Georg Brandl014197c2008-04-09 18:40:51 +0000731 legal values, that newline becomes the newline when the file is read and it
732 is returned untranslated. On output, ``'\n'`` is converted to the *newline*.
733
734 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
735 write contains a newline character.
736
Antoine Pitrou664091b2011-07-23 22:00:03 +0200737 If *write_through* is ``True``, calls to :meth:`write` are guaranteed
738 not to be buffered: any data written on the :class:`TextIOWrapper`
739 object is immediately handled to its underlying binary *buffer*.
740
741 .. versionchanged:: 3.3
742 The *write_through* argument has been added.
743
Benjamin Peterson0926ad12009-06-06 18:02:12 +0000744 :class:`TextIOWrapper` provides one attribute in addition to those of
Georg Brandl014197c2008-04-09 18:40:51 +0000745 :class:`TextIOBase` and its parents:
746
Georg Brandl014197c2008-04-09 18:40:51 +0000747 .. attribute:: line_buffering
748
749 Whether line buffering is enabled.
Georg Brandl48310cd2009-01-03 21:18:54 +0000750
Georg Brandl014197c2008-04-09 18:40:51 +0000751
Georg Brandl3dd33882009-06-01 17:35:27 +0000752.. class:: StringIO(initial_value='', newline=None)
Georg Brandl014197c2008-04-09 18:40:51 +0000753
Antoine Pitroub530e142010-08-30 12:41:00 +0000754 An in-memory stream for text I/O.
Georg Brandl014197c2008-04-09 18:40:51 +0000755
Benjamin Petersonaa1c8d82009-03-09 02:02:23 +0000756 The initial value of the buffer (an empty string by default) can be set by
757 providing *initial_value*. The *newline* argument works like that of
758 :class:`TextIOWrapper`. The default is to do no newline translation.
Georg Brandl014197c2008-04-09 18:40:51 +0000759
Mark Summerfielde6d5f302008-04-21 10:29:45 +0000760 :class:`StringIO` provides this method in addition to those from
Antoine Pitroub530e142010-08-30 12:41:00 +0000761 :class:`TextIOBase` and its parents:
Georg Brandl014197c2008-04-09 18:40:51 +0000762
763 .. method:: getvalue()
764
Georg Brandl2932d932008-05-30 06:27:09 +0000765 Return a ``str`` containing the entire contents of the buffer at any
766 time before the :class:`StringIO` object's :meth:`close` method is
767 called.
Georg Brandl014197c2008-04-09 18:40:51 +0000768
Georg Brandl2932d932008-05-30 06:27:09 +0000769 Example usage::
770
771 import io
772
773 output = io.StringIO()
774 output.write('First line.\n')
775 print('Second line.', file=output)
776
777 # Retrieve file contents -- this will be
778 # 'First line.\nSecond line.\n'
779 contents = output.getvalue()
780
Georg Brandl48310cd2009-01-03 21:18:54 +0000781 # Close object and discard memory buffer --
Georg Brandl2932d932008-05-30 06:27:09 +0000782 # .getvalue() will now raise an exception.
783 output.close()
Georg Brandl014197c2008-04-09 18:40:51 +0000784
Antoine Pitroub530e142010-08-30 12:41:00 +0000785
Georg Brandl014197c2008-04-09 18:40:51 +0000786.. class:: IncrementalNewlineDecoder
787
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000788 A helper codec that decodes newlines for universal newlines mode. It
789 inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +0000790
Antoine Pitroubed81c82010-12-03 19:14:17 +0000791
Antoine Pitroubed81c82010-12-03 19:14:17 +0000792Performance
Benjamin Petersonedf51322011-02-24 03:03:46 +0000793-----------
794
795This section discusses the performance of the provided concrete I/O
796implementations.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000797
798Binary I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000799^^^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000800
Benjamin Petersonedf51322011-02-24 03:03:46 +0000801By reading and writing only large chunks of data even when the user asks for a
802single byte, buffered I/O hides any inefficiency in calling and executing the
803operating system's unbuffered I/O routines. The gain depends on the OS and the
804kind of I/O which is performed. For example, on some modern OSes such as Linux,
805unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however,
806is that buffered I/O offers predictable performance regardless of the platform
807and the backing device. Therefore, it is most always preferable to use buffered
808I/O rather than unbuffered I/O for binary datal
Antoine Pitroubed81c82010-12-03 19:14:17 +0000809
810Text I/O
Benjamin Petersonedf51322011-02-24 03:03:46 +0000811^^^^^^^^
Antoine Pitroubed81c82010-12-03 19:14:17 +0000812
813Text I/O over a binary storage (such as a file) is significantly slower than
Benjamin Petersonedf51322011-02-24 03:03:46 +0000814binary I/O over the same storage, because it requires conversions between
815unicode and binary data using a character codec. This can become noticeable
816handling huge amounts of text data like large log files. Also,
817:meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both quite slow
818due to the reconstruction algorithm used.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000819
820:class:`StringIO`, however, is a native in-memory unicode container and will
821exhibit similar speed to :class:`BytesIO`.
822
823Multi-threading
824^^^^^^^^^^^^^^^
825
Benjamin Petersonedf51322011-02-24 03:03:46 +0000826:class:`FileIO` objects are thread-safe to the extent that the operating system
827calls (such as ``read(2)`` under Unix) they wrap are thread-safe too.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000828
829Binary buffered objects (instances of :class:`BufferedReader`,
830:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
831protect their internal structures using a lock; it is therefore safe to call
832them from multiple threads at once.
833
834:class:`TextIOWrapper` objects are not thread-safe.
835
836Reentrancy
837^^^^^^^^^^
838
839Binary buffered objects (instances of :class:`BufferedReader`,
840:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
841are not reentrant. While reentrant calls will not happen in normal situations,
Benjamin Petersonedf51322011-02-24 03:03:46 +0000842they can arise from doing I/O in a :mod:`signal` handler. If a thread tries to
843renter a buffered object which it is already accessing, a :exc:`RuntimeError` is
844raised. Note this doesn't prohibit a different thread from entering the
845buffered object.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000846
Benjamin Petersonedf51322011-02-24 03:03:46 +0000847The above implicitly extends to text files, since the :func:`open()` function
848will wrap a buffered object inside a :class:`TextIOWrapper`. This includes
849standard streams and therefore affects the built-in function :func:`print()` as
850well.
Antoine Pitroubed81c82010-12-03 19:14:17 +0000851