Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/io.rst

    r2 r391  
    77.. moduleauthor:: Mike Verdone <mike.verdone@gmail.com>
    88.. moduleauthor:: Mark Russell <mark.russell@zen.co.uk>
     9.. moduleauthor:: Antoine Pitrou <solipsis@pitrou.net>
     10.. moduleauthor:: Amaury Forgeot d'Arc <amauryfa@gmail.com>
     11.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
    912.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
     13
    1014.. versionadded:: 2.6
    1115
    12 The :mod:`io` module provides the Python interfaces to stream handling.  The
    13 built-in :func:`open` function is defined in this module.
     16The :mod:`io` module provides the Python interfaces to stream handling.
     17Under Python 2.x, this is proposed as an alternative to the built-in
     18:class:`file` object, but in Python 3.x it is the default interface to
     19access files and streams.
     20
     21.. note::
     22
     23   Since this module has been designed primarily for Python 3.x, you have to
     24   be aware that all uses of "bytes" in this document refer to the
     25   :class:`str` type (of which :class:`bytes` is an alias), and all uses
     26   of "text" refer to the :class:`unicode` type.  Furthermore, those two
     27   types are not interchangeable in the :mod:`io` APIs.
    1428
    1529At the top of the I/O hierarchy is the abstract base class :class:`IOBase`.  It
    1630defines the basic interface to a stream.  Note, however, that there is no
    1731separation between reading and writing to streams; implementations are allowed
    18 to throw an :exc:`IOError` if they do not support a given operation.
     32to raise an :exc:`IOError` if they do not support a given operation.
    1933
    2034Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the
     
    3246Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with
    3347streams whose bytes represent text, and handles encoding and decoding
    34 from and to strings. :class:`TextIOWrapper`, which extends it, is a
    35 buffered text interface to a buffered raw stream
     48from and to :class:`unicode` strings.  :class:`TextIOWrapper`, which extends
     49it, is a buffered text interface to a buffered raw stream
    3650(:class:`BufferedIOBase`). Finally, :class:`StringIO` is an in-memory
    37 stream for text.
     51stream for unicode text.
    3852
    3953Argument names are not part of the specification, and only the arguments of
     
    5064   :func:`os.stat`) if possible.
    5165
    52 .. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
    53 
    54    Open *file* and return a stream.  If the file cannot be opened, an
    55    :exc:`IOError` is raised.
    56 
    57    *file* is either a string giving the name (and the path if the file isn't in
    58    the current working directory) of the file to be opened or a file
    59    descriptor of the file to be opened.  (If a file descriptor is given,
    60    for example, from :func:`os.fdopen`, it is closed when the returned
    61    I/O object is closed, unless *closefd* is set to ``False``.)
     66.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
     67
     68   Open *file* and return a corresponding stream.  If the file cannot be opened,
     69   an :exc:`IOError` is raised.
     70
     71   *file* is either a string giving the pathname (absolute or
     72   relative to the current working directory) of the file to be opened or
     73   an integer file descriptor of the file to be wrapped.  (If a file descriptor
     74   is given, it is closed when the returned I/O object is closed, unless
     75   *closefd* is set to ``False``.)
    6276
    6377   *mode* is an optional string that specifies the mode in which the file is
     
    7993   ``'t'``   text mode (default)
    8094   ``'+'``   open a disk file for updating (reading and writing)
    81    ``'U'``   universal newline mode (for backwards compatibility; should
     95   ``'U'``   universal newlines mode (for backwards compatibility; should
    8296             not be used in new code)
    8397   ========= ===============================================================
     
    89103   Python distinguishes between files opened in binary and text modes, even when
    90104   the underlying operating system doesn't.  Files opened in binary mode
    91    (including ``'b'`` in the *mode* argument) return contents as ``bytes``
     105   (including ``'b'`` in the *mode* argument) return contents as :class:`bytes`
    92106   objects without any decoding.  In text mode (the default, or when ``'t'`` is
    93107   included in the *mode* argument), the contents of the file are returned as
    94    strings, the bytes having been first decoded using a platform-dependent
    95    encoding or using the specified *encoding* if given.
     108   :class:`unicode` strings, the bytes having been first decoded using a
     109   platform-dependent encoding or using the specified *encoding* if given.
    96110
    97111   *buffering* is an optional integer used to set the buffering policy.
     
    112126   *encoding* is the name of the encoding used to decode or encode the file.
    113127   This should only be used in text mode.  The default encoding is platform
    114    dependent, but any encoding supported by Python can be used.  See the
    115    :mod:`codecs` module for the list of supported encodings.
     128   dependent (whatever :func:`locale.getpreferredencoding` returns), but any
     129   encoding supported by Python can be used.  See the :mod:`codecs` module for
     130   the list of supported encodings.
     131
     132   *errors* is an optional string that specifies how encoding and decoding
     133   errors are to be handled--this cannot be used in binary mode.  Pass
     134   ``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding
     135   error (the default of ``None`` has the same effect), or pass ``'ignore'`` to
     136   ignore errors.  (Note that ignoring encoding errors can lead to data loss.)
     137   ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted
     138   where there is malformed data.  When writing, ``'xmlcharrefreplace'``
     139   (replace with the appropriate XML character reference) or
     140   ``'backslashreplace'`` (replace with backslashed escape sequences) can be
     141   used.  Any other error handling name that has been registered with
     142   :func:`codecs.register_error` is also valid.
     143
     144   .. index::
     145      single: universal newlines; open() (in module io)
     146
     147   *newline* controls how :term:`universal newlines` works (it only applies to
     148   text mode).  It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``.
     149   It works as follows:
     150
     151   * On input, if *newline* is ``None``, universal newlines mode is enabled.
     152     Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
     153     are translated into ``'\n'`` before being returned to the caller.  If it is
     154     ``''``, universal newlines mode is enabled, but line endings are returned to
     155     the caller untranslated.  If it has any of the other legal values, input
     156     lines are only terminated by the given string, and the line ending is
     157     returned to the caller untranslated.
     158
     159   * On output, if *newline* is ``None``, any ``'\n'`` characters written are
     160     translated to the system default line separator, :data:`os.linesep`.  If
     161     *newline* is ``''``, no translation takes place.  If *newline* is any of
     162     the other legal values, any ``'\n'`` characters written are translated to
     163     the given string.
     164
     165   If *closefd* is ``False`` and a file descriptor rather than a filename was
     166   given, the underlying file descriptor will be kept open when the file is
     167   closed.  If a filename is given *closefd* has no effect and must be ``True``
     168   (the default).
     169
     170   The type of file object returned by the :func:`.open` function depends on the
     171   mode.  When :func:`.open` is used to open a file in a text mode (``'w'``,
     172   ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a subclass of
     173   :class:`TextIOBase` (specifically :class:`TextIOWrapper`).  When used to open
     174   a file in a binary mode with buffering, the returned class is a subclass of
     175   :class:`BufferedIOBase`.  The exact class varies: in read binary mode, it
     176   returns a :class:`BufferedReader`; in write binary and append binary modes,
     177   it returns a :class:`BufferedWriter`, and in read/write mode, it returns a
     178   :class:`BufferedRandom`.  When buffering is disabled, the raw stream, a
     179   subclass of :class:`RawIOBase`, :class:`FileIO`, is returned.
     180
     181   It is also possible to use an :class:`unicode` or :class:`bytes` string
     182   as a file for both reading and writing.  For :class:`unicode` strings
     183   :class:`StringIO` can be used like a file opened in text mode,
     184   and for :class:`bytes` a :class:`BytesIO` can be used like a
     185   file opened in a binary mode.
     186
     187
     188.. exception:: BlockingIOError
     189
     190   Error raised when blocking would occur on a non-blocking stream.  It inherits
     191   :exc:`IOError`.
     192
     193   In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
     194   attribute:
     195
     196   .. attribute:: characters_written
     197
     198      An integer containing the number of characters written to the stream
     199      before it blocked.
     200
     201
     202.. exception:: UnsupportedOperation
     203
     204   An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
     205   when an unsupported operation is called on a stream.
     206
     207
     208I/O Base Classes
     209----------------
     210
     211.. class:: IOBase
     212
     213   The abstract base class for all I/O classes, acting on streams of bytes.
     214   There is no public constructor.
     215
     216   This class provides empty abstract implementations for many methods
     217   that derived classes can override selectively; the default
     218   implementations represent a file that cannot be read, written or
     219   seeked.
     220
     221   Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
     222   or :meth:`write` because their signatures will vary, implementations and
     223   clients should consider those methods part of the interface.  Also,
     224   implementations may raise a :exc:`IOError` when operations they do not
     225   support are called.
     226
     227   The basic type used for binary data read from or written to a file is
     228   :class:`bytes` (also known as :class:`str`).  :class:`bytearray`\s are
     229   accepted too, and in some cases (such as :class:`readinto`) required.
     230   Text I/O classes work with :class:`unicode` data.
     231
     232   Note that calling any method (even inquiries) on a closed stream is
     233   undefined.  Implementations may raise :exc:`IOError` in this case.
     234
     235   IOBase (and its subclasses) support the iterator protocol, meaning that an
     236   :class:`IOBase` object can be iterated over yielding the lines in a stream.
     237   Lines are defined slightly differently depending on whether the stream is
     238   a binary stream (yielding :class:`bytes`), or a text stream (yielding
     239   :class:`unicode` strings).  See :meth:`~IOBase.readline` below.
     240
     241   IOBase is also a context manager and therefore supports the
     242   :keyword:`with` statement.  In this example, *file* is closed after the
     243   :keyword:`with` statement's suite is finished---even if an exception occurs::
     244
     245      with io.open('spam.txt', 'w') as file:
     246          file.write(u'Spam and eggs!')
     247
     248   :class:`IOBase` provides these data attributes and methods:
     249
     250   .. method:: close()
     251
     252      Flush and close this stream. This method has no effect if the file is
     253      already closed. Once the file is closed, any operation on the file
     254      (e.g. reading or writing) will raise a :exc:`ValueError`.
     255
     256      As a convenience, it is allowed to call this method more than once;
     257      only the first call, however, will have an effect.
     258
     259   .. attribute:: closed
     260
     261      True if the stream is closed.
     262
     263   .. method:: fileno()
     264
     265      Return the underlying file descriptor (an integer) of the stream if it
     266      exists.  An :exc:`IOError` is raised if the IO object does not use a file
     267      descriptor.
     268
     269   .. method:: flush()
     270
     271      Flush the write buffers of the stream if applicable.  This does nothing
     272      for read-only and non-blocking streams.
     273
     274   .. method:: isatty()
     275
     276      Return ``True`` if the stream is interactive (i.e., connected to
     277      a terminal/tty device).
     278
     279   .. method:: readable()
     280
     281      Return ``True`` if the stream can be read from.  If False, :meth:`read`
     282      will raise :exc:`IOError`.
     283
     284   .. method:: readline(limit=-1)
     285
     286      Read and return one line from the stream.  If *limit* is specified, at
     287      most *limit* bytes will be read.
     288
     289      The line terminator is always ``b'\n'`` for binary files; for text files,
     290      the *newlines* argument to :func:`.open` can be used to select the line
     291      terminator(s) recognized.
     292
     293   .. method:: readlines(hint=-1)
     294
     295      Read and return a list of lines from the stream.  *hint* can be specified
     296      to control the number of lines read: no more lines will be read if the
     297      total size (in bytes/characters) of all lines so far exceeds *hint*.
     298
     299      Note that it's already possible to iterate on file objects using ``for
     300      line in file: ...`` without calling ``file.readlines()``.
     301
     302   .. method:: seek(offset, whence=SEEK_SET)
     303
     304      Change the stream position to the given byte *offset*.  *offset* is
     305      interpreted relative to the position indicated by *whence*.  Values for
     306      *whence* are:
     307
     308      * :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
     309        *offset* should be zero or positive
     310      * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
     311        be negative
     312      * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
     313        negative
     314
     315      Return the new absolute position.
     316
     317      .. versionadded:: 2.7
     318         The ``SEEK_*`` constants
     319
     320   .. method:: seekable()
     321
     322      Return ``True`` if the stream supports random access.  If ``False``,
     323      :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
     324
     325   .. method:: tell()
     326
     327      Return the current stream position.
     328
     329   .. method:: truncate(size=None)
     330
     331      Resize the stream to the given *size* in bytes (or the current position
     332      if *size* is not specified).  The current stream position isn't changed.
     333      This resizing can extend or reduce the current file size.  In case of
     334      extension, the contents of the new file area depend on the platform
     335      (on most systems, additional bytes are zero-filled, on Windows they're
     336      undetermined).  The new file size is returned.
     337
     338   .. method:: writable()
     339
     340      Return ``True`` if the stream supports writing.  If ``False``,
     341      :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
     342
     343   .. method:: writelines(lines)
     344
     345      Write a list of lines to the stream.  Line separators are not added, so it
     346      is usual for each of the lines provided to have a line separator at the
     347      end.
     348
     349
     350.. class:: RawIOBase
     351
     352   Base class for raw binary I/O.  It inherits :class:`IOBase`.  There is no
     353   public constructor.
     354
     355   Raw binary I/O typically provides low-level access to an underlying OS
     356   device or API, and does not try to encapsulate it in high-level primitives
     357   (this is left to Buffered I/O and Text I/O, described later in this page).
     358
     359   In addition to the attributes and methods from :class:`IOBase`,
     360   RawIOBase provides the following methods:
     361
     362   .. method:: read(n=-1)
     363
     364      Read up to *n* bytes from the object and return them.  As a convenience,
     365      if *n* is unspecified or -1, :meth:`readall` is called.  Otherwise,
     366      only one system call is ever made.  Fewer than *n* bytes may be
     367      returned if the operating system call returns fewer than *n* bytes.
     368
     369      If 0 bytes are returned, and *n* was not 0, this indicates end of file.
     370      If the object is in non-blocking mode and no bytes are available,
     371      ``None`` is returned.
     372
     373   .. method:: readall()
     374
     375      Read and return all the bytes from the stream until EOF, using multiple
     376      calls to the stream if necessary.
     377
     378   .. method:: readinto(b)
     379
     380      Read up to len(b) bytes into bytearray *b* and return the number
     381      of bytes read.  If the object is in non-blocking mode and no
     382      bytes are available, ``None`` is returned.
     383
     384   .. method:: write(b)
     385
     386      Write the given bytes or bytearray object, *b*, to the underlying raw
     387      stream and return the number of bytes written.  This can be less than
     388      ``len(b)``, depending on specifics of the underlying raw stream, and
     389      especially if it is in non-blocking mode.  ``None`` is returned if the
     390      raw stream is set not to block and no single byte could be readily
     391      written to it.
     392
     393
     394.. class:: BufferedIOBase
     395
     396   Base class for binary streams that support some kind of buffering.
     397   It inherits :class:`IOBase`. There is no public constructor.
     398
     399   The main difference with :class:`RawIOBase` is that methods :meth:`read`,
     400   :meth:`readinto` and :meth:`write` will try (respectively) to read as much
     401   input as requested or to consume all given output, at the expense of
     402   making perhaps more than one system call.
     403
     404   In addition, those methods can raise :exc:`BlockingIOError` if the
     405   underlying raw stream is in non-blocking mode and cannot take or give
     406   enough data; unlike their :class:`RawIOBase` counterparts, they will
     407   never return ``None``.
     408
     409   Besides, the :meth:`read` method does not have a default
     410   implementation that defers to :meth:`readinto`.
     411
     412   A typical :class:`BufferedIOBase` implementation should not inherit from a
     413   :class:`RawIOBase` implementation, but wrap one, like
     414   :class:`BufferedWriter` and :class:`BufferedReader` do.
     415
     416   :class:`BufferedIOBase` provides or overrides these methods and attribute in
     417   addition to those from :class:`IOBase`:
     418
     419   .. attribute:: raw
     420
     421      The underlying raw stream (a :class:`RawIOBase` instance) that
     422      :class:`BufferedIOBase` deals with.  This is not part of the
     423      :class:`BufferedIOBase` API and may not exist on some implementations.
     424
     425   .. method:: detach()
     426
     427      Separate the underlying raw stream from the buffer and return it.
     428
     429      After the raw stream has been detached, the buffer is in an unusable
     430      state.
     431
     432      Some buffers, like :class:`BytesIO`, do not have the concept of a single
     433      raw stream to return from this method.  They raise
     434      :exc:`UnsupportedOperation`.
     435
     436      .. versionadded:: 2.7
     437
     438   .. method:: read(n=-1)
     439
     440      Read and return up to *n* bytes.  If the argument is omitted, ``None``, or
     441      negative, data is read and returned until EOF is reached.  An empty bytes
     442      object is returned if the stream is already at EOF.
     443
     444      If the argument is positive, and the underlying raw stream is not
     445      interactive, multiple raw reads may be issued to satisfy the byte count
     446      (unless EOF is reached first).  But for interactive raw streams, at most
     447      one raw read will be issued, and a short result does not imply that EOF is
     448      imminent.
     449
     450      A :exc:`BlockingIOError` is raised if the underlying raw stream is in
     451      non blocking-mode, and has no data available at the moment.
     452
     453   .. method:: read1(n=-1)
     454
     455      Read and return up to *n* bytes, with at most one call to the underlying
     456      raw stream's :meth:`~RawIOBase.read` method.  This can be useful if you
     457      are implementing your own buffering on top of a :class:`BufferedIOBase`
     458      object.
     459
     460   .. method:: readinto(b)
     461
     462      Read up to len(b) bytes into bytearray *b* and return the number of bytes
     463      read.
     464
     465      Like :meth:`read`, multiple reads may be issued to the underlying raw
     466      stream, unless the latter is 'interactive'.
     467
     468      A :exc:`BlockingIOError` is raised if the underlying raw stream is in
     469      non blocking-mode, and has no data available at the moment.
     470
     471   .. method:: write(b)
     472
     473      Write the given bytes or bytearray object, *b* and return the number
     474      of bytes written (never less than ``len(b)``, since if the write fails
     475      an :exc:`IOError` will be raised).  Depending on the actual
     476      implementation, these bytes may be readily written to the underlying
     477      stream, or held in a buffer for performance and latency reasons.
     478
     479      When in non-blocking mode, a :exc:`BlockingIOError` is raised if the
     480      data needed to be written to the raw stream but it couldn't accept
     481      all the data without blocking.
     482
     483
     484Raw File I/O
     485------------
     486
     487.. class:: FileIO(name, mode='r', closefd=True)
     488
     489   :class:`FileIO` represents an OS-level file containing bytes data.
     490   It implements the :class:`RawIOBase` interface (and therefore the
     491   :class:`IOBase` interface, too).
     492
     493   The *name* can be one of two things:
     494
     495   * a string representing the path to the file which will be opened;
     496   * an integer representing the number of an existing OS-level file descriptor
     497     to which the resulting :class:`FileIO` object will give access.
     498
     499   The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
     500   or appending.  The file will be created if it doesn't exist when opened for
     501   writing or appending; it will be truncated when opened for writing.  Add a
     502   ``'+'`` to the mode to allow simultaneous reading and writing.
     503
     504   The :meth:`read` (when called with a positive argument), :meth:`readinto`
     505   and :meth:`write` methods on this class will only make one system call.
     506
     507   In addition to the attributes and methods from :class:`IOBase` and
     508   :class:`RawIOBase`, :class:`FileIO` provides the following data
     509   attributes and methods:
     510
     511   .. attribute:: mode
     512
     513      The mode as given in the constructor.
     514
     515   .. attribute:: name
     516
     517      The file name.  This is the file descriptor of the file when no name is
     518      given in the constructor.
     519
     520
     521Buffered Streams
     522----------------
     523
     524Buffered I/O streams provide a higher-level interface to an I/O device
     525than raw I/O does.
     526
     527.. class:: BytesIO([initial_bytes])
     528
     529   A stream implementation using an in-memory bytes buffer.  It inherits
     530   :class:`BufferedIOBase`.
     531
     532   The argument *initial_bytes* is an optional initial :class:`bytes`.
     533
     534   :class:`BytesIO` provides or overrides these methods in addition to those
     535   from :class:`BufferedIOBase` and :class:`IOBase`:
     536
     537   .. method:: getvalue()
     538
     539      Return ``bytes`` containing the entire contents of the buffer.
     540
     541   .. method:: read1()
     542
     543      In :class:`BytesIO`, this is the same as :meth:`read`.
     544
     545
     546.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
     547
     548   A buffer providing higher-level access to a readable, sequential
     549   :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`.
     550   When reading data from this object, a larger amount of data may be
     551   requested from the underlying raw stream, and kept in an internal buffer.
     552   The buffered data can then be returned directly on subsequent reads.
     553
     554   The constructor creates a :class:`BufferedReader` for the given readable
     555   *raw* stream and *buffer_size*.  If *buffer_size* is omitted,
     556   :data:`DEFAULT_BUFFER_SIZE` is used.
     557
     558   :class:`BufferedReader` provides or overrides these methods in addition to
     559   those from :class:`BufferedIOBase` and :class:`IOBase`:
     560
     561   .. method:: peek([n])
     562
     563      Return bytes from the stream without advancing the position.  At most one
     564      single read on the raw stream is done to satisfy the call. The number of
     565      bytes returned may be less or more than requested.
     566
     567   .. method:: read([n])
     568
     569      Read and return *n* bytes, or if *n* is not given or negative, until EOF
     570      or if the read call would block in non-blocking mode.
     571
     572   .. method:: read1(n)
     573
     574      Read and return up to *n* bytes with only one call on the raw stream.  If
     575      at least one byte is buffered, only buffered bytes are returned.
     576      Otherwise, one raw stream read call is made.
     577
     578
     579.. class:: BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
     580
     581   A buffer providing higher-level access to a writeable, sequential
     582   :class:`RawIOBase` object.  It inherits :class:`BufferedIOBase`.
     583   When writing to this object, data is normally held into an internal
     584   buffer.  The buffer will be written out to the underlying :class:`RawIOBase`
     585   object under various conditions, including:
     586
     587   * when the buffer gets too small for all pending data;
     588   * when :meth:`flush()` is called;
     589   * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects);
     590   * when the :class:`BufferedWriter` object is closed or destroyed.
     591
     592   The constructor creates a :class:`BufferedWriter` for the given writeable
     593   *raw* stream.  If the *buffer_size* is not given, it defaults to
     594   :data:`DEFAULT_BUFFER_SIZE`.
     595
     596   A third argument, *max_buffer_size*, is supported, but unused and deprecated.
     597
     598   :class:`BufferedWriter` provides or overrides these methods in addition to
     599   those from :class:`BufferedIOBase` and :class:`IOBase`:
     600
     601   .. method:: flush()
     602
     603      Force bytes held in the buffer into the raw stream.  A
     604      :exc:`BlockingIOError` should be raised if the raw stream blocks.
     605
     606   .. method:: write(b)
     607
     608      Write the bytes or bytearray object, *b* and return the number of bytes
     609      written.  When in non-blocking mode, a :exc:`BlockingIOError` is raised
     610      if the buffer needs to be written out but the raw stream blocks.
     611
     612
     613.. class:: BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
     614
     615   A buffered interface to random access streams.  It inherits
     616   :class:`BufferedReader` and :class:`BufferedWriter`, and further supports
     617   :meth:`seek` and :meth:`tell` functionality.
     618
     619   The constructor creates a reader and writer for a seekable raw stream, given
     620   in the first argument.  If the *buffer_size* is omitted it defaults to
     621   :data:`DEFAULT_BUFFER_SIZE`.
     622
     623   A third argument, *max_buffer_size*, is supported, but unused and deprecated.
     624
     625   :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
     626   :class:`BufferedWriter` can do.
     627
     628
     629.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
     630
     631   A buffered I/O object combining two unidirectional :class:`RawIOBase`
     632   objects -- one readable, the other writeable -- into a single bidirectional
     633   endpoint.  It inherits :class:`BufferedIOBase`.
     634
     635   *reader* and *writer* are :class:`RawIOBase` objects that are readable and
     636   writeable respectively.  If the *buffer_size* is omitted it defaults to
     637   :data:`DEFAULT_BUFFER_SIZE`.
     638
     639   A fourth argument, *max_buffer_size*, is supported, but unused and
     640   deprecated.
     641
     642   :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods
     643   except for :meth:`~BufferedIOBase.detach`, which raises
     644   :exc:`UnsupportedOperation`.
     645
     646   .. warning::
     647      :class:`BufferedRWPair` does not attempt to synchronize accesses to
     648      its underlying raw streams.  You should not pass it the same object
     649      as reader and writer; use :class:`BufferedRandom` instead.
     650
     651
     652Text I/O
     653--------
     654
     655.. class:: TextIOBase
     656
     657   Base class for text streams.  This class provides an unicode character
     658   and line based interface to stream I/O.  There is no :meth:`readinto`
     659   method because Python's :class:`unicode` strings are immutable.
     660   It inherits :class:`IOBase`.  There is no public constructor.
     661
     662   :class:`TextIOBase` provides or overrides these data attributes and
     663   methods in addition to those from :class:`IOBase`:
     664
     665   .. attribute:: encoding
     666
     667      The name of the encoding used to decode the stream's bytes into
     668      strings, and to encode strings into bytes.
     669
     670   .. attribute:: errors
     671
     672      The error setting of the decoder or encoder.
     673
     674   .. attribute:: newlines
     675
     676      A string, a tuple of strings, or ``None``, indicating the newlines
     677      translated so far.  Depending on the implementation and the initial
     678      constructor flags, this may not be available.
     679
     680   .. attribute:: buffer
     681
     682      The underlying binary buffer (a :class:`BufferedIOBase` instance) that
     683      :class:`TextIOBase` deals with.  This is not part of the
     684      :class:`TextIOBase` API and may not exist on some implementations.
     685
     686   .. method:: detach()
     687
     688      Separate the underlying binary buffer from the :class:`TextIOBase` and
     689      return it.
     690
     691      After the underlying buffer has been detached, the :class:`TextIOBase` is
     692      in an unusable state.
     693
     694      Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not
     695      have the concept of an underlying buffer and calling this method will
     696      raise :exc:`UnsupportedOperation`.
     697
     698      .. versionadded:: 2.7
     699
     700   .. method:: read(n)
     701
     702      Read and return at most *n* characters from the stream as a single
     703      :class:`unicode`.  If *n* is negative or ``None``, reads until EOF.
     704
     705   .. method:: readline(limit=-1)
     706
     707      Read until newline or EOF and return a single ``unicode``.  If the
     708      stream is already at EOF, an empty string is returned.
     709
     710      If *limit* is specified, at most *limit* characters will be read.
     711
     712   .. method:: seek(offset, whence=SEEK_SET)
     713
     714      Change the stream position to the given *offset*.  Behaviour depends
     715      on the *whence* parameter:
     716
     717      * :data:`SEEK_SET` or ``0``: seek from the start of the stream
     718        (the default); *offset* must either be a number returned by
     719        :meth:`TextIOBase.tell`, or zero.  Any other *offset* value
     720        produces undefined behaviour.
     721      * :data:`SEEK_CUR` or ``1``: "seek" to the current position;
     722        *offset* must be zero, which is a no-operation (all other values
     723        are unsupported).
     724      * :data:`SEEK_END` or ``2``: seek to the end of the stream;
     725        *offset* must be zero (all other values are unsupported).
     726
     727      Return the new absolute position as an opaque number.
     728
     729      .. versionadded:: 2.7
     730         The ``SEEK_*`` constants.
     731
     732   .. method:: tell()
     733
     734      Return the current stream position as an opaque number.  The number
     735      does not usually represent a number of bytes in the underlying
     736      binary storage.
     737
     738   .. method:: write(s)
     739
     740      Write the :class:`unicode` string *s* to the stream and return the
     741      number of characters written.
     742
     743
     744.. class:: TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
     745
     746   A buffered text stream over a :class:`BufferedIOBase` binary stream.
     747   It inherits :class:`TextIOBase`.
     748
     749   *encoding* gives the name of the encoding that the stream will be decoded or
     750   encoded with.  It defaults to :func:`locale.getpreferredencoding`.
    116751
    117752   *errors* is an optional string that specifies how encoding and decoding
     
    126761   registered with :func:`codecs.register_error` is also valid.
    127762
    128    *newline* controls how universal newlines works (it only applies to text
    129    mode).  It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``.  It
    130    works as follows:
    131 
    132    * On input, if *newline* is ``None``, universal newlines mode is enabled.
    133      Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
    134      are translated into ``'\n'`` before being returned to the caller.  If it is
    135      ``''``, universal newline mode is enabled, but line endings are returned to
    136      the caller untranslated.  If it has any of the other legal values, input
    137      lines are only terminated by the given string, and the line ending is
    138      returned to the caller untranslated.
     763   .. index::
     764      single: universal newlines; io.TextIOWrapper class
     765
     766   *newline* controls how line endings are handled.  It can be ``None``,
     767   ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``.  It works as follows:
     768
     769   * On input, if *newline* is ``None``, :term:`universal newlines` mode is
     770     enabled.  Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``,
     771     and these are translated into ``'\n'`` before being returned to the
     772     caller.  If it is ``''``, universal newlines mode is enabled, but line
     773     endings are returned to the caller untranslated.  If it has any of the
     774     other legal values, input lines are only terminated by the given string,
     775     and the line ending is returned to the caller untranslated.
    139776
    140777   * On output, if *newline* is ``None``, any ``'\n'`` characters written are
     
    144781     the given string.
    145782
    146    If *closefd* is ``False`` and a file descriptor rather than a
    147    filename was given, the underlying file descriptor will be kept open
    148    when the file is closed.  If a filename is given *closefd* has no
    149    effect but must be ``True`` (the default).
    150 
    151    The type of file object returned by the :func:`.open` function depends
    152    on the mode.  When :func:`.open` is used to open a file in a text mode
    153    (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a
    154    :class:`TextIOWrapper`. When used to open a file in a binary mode,
    155    the returned class varies: in read binary mode, it returns a
    156    :class:`BufferedReader`; in write binary and append binary modes, it
    157    returns a :class:`BufferedWriter`, and in read/write mode, it returns
    158    a :class:`BufferedRandom`.
    159 
    160    It is also possible to use a string or bytearray as a file for both reading
    161    and writing.  For strings :class:`StringIO` can be used like a file opened in
    162    a text mode, and for bytearrays a :class:`BytesIO` can be used like a
    163    file opened in a binary mode.
    164 
    165 
    166 .. exception:: BlockingIOError
    167 
    168    Error raised when blocking would occur on a non-blocking stream.  It inherits
    169    :exc:`IOError`.
    170 
    171    In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
    172    attribute:
    173 
    174    .. attribute:: characters_written
    175 
    176       An integer containing the number of characters written to the stream
    177       before it blocked.
    178 
    179 
    180 .. exception:: UnsupportedOperation
    181 
    182    An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
    183    when an unsupported operation is called on a stream.
    184 
    185 
    186 I/O Base Classes
    187 ----------------
    188 
    189 .. class:: IOBase
    190 
    191    The abstract base class for all I/O classes, acting on streams of bytes.
    192    There is no public constructor.
    193 
    194    This class provides empty abstract implementations for many methods
    195    that derived classes can override selectively; the default
    196    implementations represent a file that cannot be read, written or
    197    seeked.
    198 
    199    Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
    200    or :meth:`write` because their signatures will vary, implementations and
    201    clients should consider those methods part of the interface.  Also,
    202    implementations may raise a :exc:`IOError` when operations they do not
    203    support are called.
    204 
    205    The basic type used for binary data read from or written to a file is
    206    :class:`bytes`.  :class:`bytearray`\s are accepted too, and in some cases
    207    (such as :class:`readinto`) required.  Text I/O classes work with
    208    :class:`str` data.
    209 
    210    Note that calling any method (even inquiries) on a closed stream is
    211    undefined.  Implementations may raise :exc:`IOError` in this case.
    212 
    213    IOBase (and its subclasses) support the iterator protocol, meaning that an
    214    :class:`IOBase` object can be iterated over yielding the lines in a stream.
    215 
    216    IOBase is also a context manager and therefore supports the
    217    :keyword:`with` statement.  In this example, *file* is closed after the
    218    :keyword:`with` statement's suite is finished---even if an exception occurs::
    219 
    220       with open('spam.txt', 'w') as file:
    221           file.write('Spam and eggs!')
    222 
    223    :class:`IOBase` provides these data attributes and methods:
    224 
    225    .. method:: close()
    226 
    227       Flush and close this stream. This method has no effect if the file is
    228       already closed. Once the file is closed, any operation on the file
    229       (e.g. reading or writing) will raise an :exc:`IOError`. The internal
    230       file descriptor isn't closed if *closefd* was False.
    231 
    232    .. attribute:: closed
    233 
    234       True if the stream is closed.
    235 
    236    .. method:: fileno()
    237 
    238       Return the underlying file descriptor (an integer) of the stream if it
    239       exists.  An :exc:`IOError` is raised if the IO object does not use a file
    240       descriptor.
    241 
    242    .. method:: flush()
    243 
    244       Flush the write buffers of the stream if applicable.  This does nothing
    245       for read-only and non-blocking streams.
    246 
    247    .. method:: isatty()
    248 
    249       Return ``True`` if the stream is interactive (i.e., connected to
    250       a terminal/tty device).
    251 
    252    .. method:: readable()
    253 
    254       Return ``True`` if the stream can be read from.  If False, :meth:`read`
    255       will raise :exc:`IOError`.
    256 
    257    .. method:: readline([limit])
    258 
    259       Read and return one line from the stream.  If *limit* is specified, at
    260       most *limit* bytes will be read.
    261 
    262       The line terminator is always ``b'\n'`` for binary files; for text files,
    263       the *newlines* argument to :func:`.open` can be used to select the line
    264       terminator(s) recognized.
    265 
    266    .. method:: readlines([hint])
    267 
    268       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*.
    271 
    272    .. method:: seek(offset[, whence])
    273 
    274       Change the stream position to the given byte *offset*.  *offset* is
    275       interpreted relative to the position indicated by *whence*.  Values for
    276       *whence* are:
    277 
    278       * ``0`` -- start of the stream (the default); *offset* should be zero or positive
    279       * ``1`` -- current stream position; *offset* may be negative
    280       * ``2`` -- end of the stream; *offset* is usually negative
    281 
    282       Return the new absolute position.
    283 
    284    .. method:: seekable()
    285 
    286       Return ``True`` if the stream supports random access.  If ``False``,
    287       :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
    288 
    289    .. method:: tell()
    290 
    291       Return the current stream position.
    292 
    293    .. method:: truncate([size])
    294 
    295       Truncate the file to at most *size* bytes.  *size* defaults to the current
    296       file position, as returned by :meth:`tell`.
    297 
    298    .. method:: writable()
    299 
    300       Return ``True`` if the stream supports writing.  If ``False``,
    301       :meth:`write` and :meth:`truncate` will raise :exc:`IOError`.
    302 
    303    .. method:: writelines(lines)
    304 
    305       Write a list of lines to the stream.  Line separators are not added, so it
    306       is usual for each of the lines provided to have a line separator at the
    307       end.
    308 
    309 
    310 .. class:: RawIOBase
    311 
    312    Base class for raw binary I/O.  It inherits :class:`IOBase`.  There is no
    313    public constructor.
    314 
    315    In addition to the attributes and methods from :class:`IOBase`,
    316    RawIOBase provides the following methods:
    317 
    318    .. method:: read([n])
    319 
    320       Read and return all the bytes from the stream until EOF, or if *n* is
    321       specified, up to *n* bytes.  Only one system call is ever made.  An empty
    322       bytes object is returned on EOF; ``None`` is returned if the object is set
    323       not to block and has no data to read.
    324 
    325    .. method:: readall()
    326 
    327       Read and return all the bytes from the stream until EOF, using multiple
    328       calls to the stream if necessary.
    329 
    330    .. method:: readinto(b)
    331 
    332       Read up to len(b) bytes into bytearray *b* and return the number of bytes
    333       read.
    334 
    335    .. method:: write(b)
    336 
    337       Write the given bytes or bytearray object, *b*, to the underlying raw
    338       stream and return the number of bytes written (This is never less than
    339       ``len(b)``, since if the write fails, an :exc:`IOError` will be raised).
    340 
    341 
    342 .. class:: BufferedIOBase
    343 
    344    Base class for streams that support buffering.  It inherits :class:`IOBase`.
    345    There is no public constructor.
    346 
    347    The main difference with :class:`RawIOBase` is that the :meth:`read` method
    348    supports omitting the *size* argument, and does not have a default
    349    implementation that defers to :meth:`readinto`.
    350 
    351    In addition, :meth:`read`, :meth:`readinto`, and :meth:`write` may raise
    352    :exc:`BlockingIOError` if the underlying raw stream is in non-blocking mode
    353    and not ready; unlike their raw counterparts, they will never return
    354    ``None``.
    355 
    356    A typical implementation should not inherit from a :class:`RawIOBase`
    357    implementation, but wrap one like :class:`BufferedWriter` and
    358    :class:`BufferedReader`.
    359 
    360    :class:`BufferedIOBase` provides or overrides these methods in addition to
    361    those from :class:`IOBase`:
    362 
    363    .. method:: read([n])
    364 
    365       Read and return up to *n* bytes.  If the argument is omitted, ``None``, or
    366       negative, data is read and returned until EOF is reached.  An empty bytes
    367       object is returned if the stream is already at EOF.
    368 
    369       If the argument is positive, and the underlying raw stream is not
    370       interactive, multiple raw reads may be issued to satisfy the byte count
    371       (unless EOF is reached first).  But for interactive raw streams, at most
    372       one raw read will be issued, and a short result does not imply that EOF is
    373       imminent.
    374 
    375       A :exc:`BlockingIOError` is raised if the underlying raw stream has no
    376       data at the moment.
    377 
    378    .. method:: readinto(b)
    379 
    380       Read up to len(b) bytes into bytearray *b* and return the number of bytes
    381       read.
    382 
    383       Like :meth:`read`, multiple reads may be issued to the underlying raw
    384       stream, unless the latter is 'interactive.'
    385 
    386       A :exc:`BlockingIOError` is raised if the underlying raw stream has no
    387       data at the moment.
    388 
    389    .. method:: write(b)
    390 
    391       Write the given bytes or bytearray object, *b*, to the underlying raw
    392       stream and return the number of bytes written (never less than ``len(b)``,
    393       since if the write fails an :exc:`IOError` will be raised).
    394 
    395       A :exc:`BlockingIOError` is raised if the buffer is full, and the
    396       underlying raw stream cannot accept more data at the moment.
    397 
    398 
    399 Raw File I/O
    400 ------------
    401 
    402 .. class:: FileIO(name[, mode])
    403 
    404    :class:`FileIO` represents a file containing bytes data.  It implements
    405    the :class:`RawIOBase` interface (and therefore the :class:`IOBase`
    406    interface, too).
    407 
    408    The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
    409    or appending.  The file will be created if it doesn't exist when opened for
    410    writing or appending; it will be truncated when opened for writing.  Add a
    411    ``'+'`` to the mode to allow simultaneous reading and writing.
    412 
    413    In addition to the attributes and methods from :class:`IOBase` and
    414    :class:`RawIOBase`, :class:`FileIO` provides the following data
    415    attributes and methods:
    416 
    417    .. attribute:: mode
    418 
    419       The mode as given in the constructor.
    420 
    421    .. attribute:: name
    422 
    423       The file name.  This is the file descriptor of the file when no name is
    424       given in the constructor.
    425 
    426    .. method:: read([n])
    427 
    428       Read and return at most *n* bytes.  Only one system call is made, so it is
    429       possible that less data than was requested is returned.  Use :func:`len`
    430       on the returned bytes object to see how many bytes were actually returned.
    431       (In non-blocking mode, ``None`` is returned when no data is available.)
    432 
    433    .. method:: readall()
    434 
    435       Read and return the entire file's contents in a single bytes object.  As
    436       much as immediately available is returned in non-blocking mode.  If the
    437       EOF has been reached, ``b''`` is returned.
    438 
    439    .. method:: write(b)
    440 
    441       Write the bytes or bytearray object, *b*, to the file, and return
    442       the number actually written. Only one system call is made, so it
    443       is possible that only some of the data is written.
    444 
    445    Note that the inherited ``readinto()`` method should not be used on
    446    :class:`FileIO` objects.
    447 
    448 
    449 Buffered Streams
    450 ----------------
    451 
    452 .. class:: BytesIO([initial_bytes])
    453 
    454    A stream implementation using an in-memory bytes buffer.  It inherits
    455    :class:`BufferedIOBase`.
    456 
    457    The argument *initial_bytes* is an optional initial bytearray.
    458 
    459    :class:`BytesIO` provides or overrides these methods in addition to those
    460    from :class:`BufferedIOBase` and :class:`IOBase`:
    461 
    462    .. method:: getvalue()
    463 
    464       Return ``bytes`` containing the entire contents of the buffer.
    465 
    466    .. method:: read1()
    467 
    468       In :class:`BytesIO`, this is the same as :meth:`read`.
    469 
    470    .. method:: truncate([size])
    471 
    472       Truncate the buffer to at most *size* bytes.  *size* defaults to the
    473       current stream position, as returned by :meth:`tell`.
    474 
    475 
    476 .. class:: BufferedReader(raw[, buffer_size])
    477 
    478    A buffer for a readable, sequential :class:`RawIOBase` object.  It inherits
    479    :class:`BufferedIOBase`.
    480 
    481    The constructor creates a :class:`BufferedReader` for the given readable
    482    *raw* stream and *buffer_size*.  If *buffer_size* is omitted,
    483    :data:`DEFAULT_BUFFER_SIZE` is used.
    484 
    485    :class:`BufferedReader` provides or overrides these methods in addition to
    486    those from :class:`BufferedIOBase` and :class:`IOBase`:
    487 
    488    .. method:: peek([n])
    489 
    490       Return 1 (or *n* if specified) bytes from a buffer without advancing the
    491       position.  Only a single read on the raw stream is done to satisfy the
    492       call. The number of bytes returned may be less than requested since at
    493       most all the buffer's bytes from the current position to the end are
    494       returned.
    495 
    496    .. method:: read([n])
    497 
    498       Read and return *n* bytes, or if *n* is not given or negative, until EOF
    499       or if the read call would block in non-blocking mode.
    500 
    501    .. method:: read1(n)
    502 
    503       Read and return up to *n* bytes with only one call on the raw stream.  If
    504       at least one byte is buffered, only buffered bytes are returned.
    505       Otherwise, one raw stream read call is made.
    506 
    507 
    508 .. class:: BufferedWriter(raw[, buffer_size[, max_buffer_size]])
    509 
    510    A buffer for a writeable sequential RawIO object.  It inherits
    511    :class:`BufferedIOBase`.
    512 
    513    The constructor creates a :class:`BufferedWriter` for the given writeable
    514    *raw* stream.  If the *buffer_size* is not given, it defaults to
    515    :data:`DEAFULT_BUFFER_SIZE`.  If *max_buffer_size* is omitted, it defaults to
    516    twice the buffer size.
    517 
    518    :class:`BufferedWriter` provides or overrides these methods in addition to
    519    those from :class:`BufferedIOBase` and :class:`IOBase`:
    520 
    521    .. method:: flush()
    522 
    523       Force bytes held in the buffer into the raw stream.  A
    524       :exc:`BlockingIOError` should be raised if the raw stream blocks.
    525 
    526    .. method:: write(b)
    527 
    528       Write the bytes or bytearray object, *b*, onto the raw stream and return
    529       the number of bytes written.  A :exc:`BlockingIOError` is raised when the
    530       raw stream blocks.
    531 
    532 
    533 .. class:: BufferedRWPair(reader, writer[, buffer_size[, max_buffer_size]])
    534 
    535    A combined buffered writer and reader object for a raw stream that can be
    536    written to and read from.  It has and supports both :meth:`read`, :meth:`write`,
    537    and their variants.  This is useful for sockets and two-way pipes.
    538    It inherits :class:`BufferedIOBase`.
    539 
    540    *reader* and *writer* are :class:`RawIOBase` objects that are readable and
    541    writeable respectively.  If the *buffer_size* is omitted it defaults to
    542    :data:`DEFAULT_BUFFER_SIZE`.  The *max_buffer_size* (for the buffered writer)
    543    defaults to twice the buffer size.
    544 
    545    :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods.
    546 
    547 
    548 .. class:: BufferedRandom(raw[, buffer_size[, max_buffer_size]])
    549 
    550    A buffered interface to random access streams.  It inherits
    551    :class:`BufferedReader` and :class:`BufferedWriter`.
    552 
    553    The constructor creates a reader and writer for a seekable raw stream, given
    554    in the first argument.  If the *buffer_size* is omitted it defaults to
    555    :data:`DEFAULT_BUFFER_SIZE`.  The *max_buffer_size* (for the buffered writer)
    556    defaults to twice the buffer size.
    557 
    558    :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
    559    :class:`BufferedWriter` can do.
    560 
    561 
    562 Text I/O
    563 --------
    564 
    565 .. class:: TextIOBase
    566 
    567    Base class for text streams.  This class provides a character and line based
    568    interface to stream I/O.  There is no :meth:`readinto` method because
    569    Python's character strings are immutable.  It inherits :class:`IOBase`.
    570    There is no public constructor.
    571 
    572    :class:`TextIOBase` provides or overrides these data attributes and
    573    methods in addition to those from :class:`IOBase`:
    574 
    575    .. attribute:: encoding
    576 
    577       The name of the encoding used to decode the stream's bytes into
    578       strings, and to encode strings into bytes.
    579 
    580    .. attribute:: newlines
    581 
    582       A string, a tuple of strings, or ``None``, indicating the newlines
    583       translated so far.
    584 
    585    .. method:: read(n)
    586 
    587       Read and return at most *n* characters from the stream as a single
    588       :class:`str`.  If *n* is negative or ``None``, reads to EOF.
    589 
    590    .. method:: readline()
    591 
    592       Read until newline or EOF and return a single ``str``.  If the stream is
    593       already at EOF, an empty string is returned.
    594 
    595    .. method:: write(s)
    596 
    597       Write the string *s* to the stream and return the number of characters
    598       written.
    599 
    600 
    601 .. class:: TextIOWrapper(buffer[, encoding[, errors[, newline[, line_buffering]]]])
    602 
    603    A buffered text stream over a :class:`BufferedIOBase` raw stream, *buffer*.
    604    It inherits :class:`TextIOBase`.
    605 
    606    *encoding* gives the name of the encoding that the stream will be decoded or
    607    encoded with.  It defaults to :func:`locale.getpreferredencoding`.
    608 
    609    *errors* is an optional string that specifies how encoding and decoding
    610    errors are to be handled.  Pass ``'strict'`` to raise a :exc:`ValueError`
    611    exception if there is an encoding error (the default of ``None`` has the same
    612    effect), or pass ``'ignore'`` to ignore errors.  (Note that ignoring encoding
    613    errors can lead to data loss.)  ``'replace'`` causes a replacement marker
    614    (such as ``'?'``) to be inserted where there is malformed data.  When
    615    writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
    616    reference) or ``'backslashreplace'`` (replace with backslashed escape
    617    sequences) can be used.  Any other error handling name that has been
    618    registered with :func:`codecs.register_error` is also valid.
    619 
    620    *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``.  It
    621    controls the handling of line endings.  If it is ``None``, universal newlines
    622    is enabled.  With this enabled, on input, the lines endings ``'\n'``,
    623    ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
    624    the caller.  Conversely, on output, ``'\n'`` is translated to the system
    625    default line separator, :data:`os.linesep`.  If *newline* is any other of its
    626    legal values, that newline becomes the newline when the file is read and it
    627    is returned untranslated.  On output, ``'\n'`` is converted to the *newline*.
    628 
    629783   If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
    630784   write contains a newline character.
    631785
    632    :class:`TextIOWrapper` provides these data attributes in addition to those of
     786   :class:`TextIOWrapper` provides one attribute in addition to those of
    633787   :class:`TextIOBase` and its parents:
    634788
    635    .. attribute:: errors
    636 
    637       The encoding and decoding error setting.
    638 
    639789   .. attribute:: line_buffering
    640790
     
    642792
    643793
    644 .. class:: StringIO([initial_value[, encoding[, errors[, newline]]]])
    645 
    646    An in-memory stream for text.  It inherits :class:`TextIOWrapper`.
    647 
    648    Create a new StringIO stream with an inital value, encoding, error handling,
    649    and newline setting.  See :class:`TextIOWrapper`\'s constructor for more
    650    information.
     794.. class:: StringIO(initial_value=u'', newline=None)
     795
     796   An in-memory stream for unicode text.  It inherits :class:`TextIOWrapper`.
     797
     798   The initial value of the buffer (an empty unicode string by default) can
     799   be set by providing *initial_value*.  The *newline* argument works like
     800   that of :class:`TextIOWrapper`.  The default is to do no newline
     801   translation.
    651802
    652803   :class:`StringIO` provides this method in addition to those from
     
    655806   .. method:: getvalue()
    656807
    657       Return a ``str`` containing the entire contents of the buffer.
    658 
     808      Return a ``unicode`` containing the entire contents of the buffer at any
     809      time before the :class:`StringIO` object's :meth:`close` method is
     810      called.
     811
     812   Example usage::
     813
     814      import io
     815
     816      output = io.StringIO()
     817      output.write(u'First line.\n')
     818      output.write(u'Second line.\n')
     819
     820      # Retrieve file contents -- this will be
     821      # u'First line.\nSecond line.\n'
     822      contents = output.getvalue()
     823
     824      # Close object and discard memory buffer --
     825      # .getvalue() will now raise an exception.
     826      output.close()
     827
     828
     829.. index::
     830   single: universal newlines; io.IncrementalNewlineDecoder class
    659831
    660832.. class:: IncrementalNewlineDecoder
    661833
    662    A helper codec that decodes newlines for universal newlines mode.  It
    663    inherits :class:`codecs.IncrementalDecoder`.
    664 
     834   A helper codec that decodes newlines for :term:`universal newlines` mode.
     835   It inherits :class:`codecs.IncrementalDecoder`.
     836
     837
     838Advanced topics
     839---------------
     840
     841Here we will discuss several advanced topics pertaining to the concrete
     842I/O implementations described above.
     843
     844Performance
     845^^^^^^^^^^^
     846
     847Binary I/O
     848""""""""""
     849
     850By reading and writing only large chunks of data even when the user asks
     851for a single byte, buffered I/O is designed to hide any inefficiency in
     852calling and executing the operating system's unbuffered I/O routines.  The
     853gain will vary very much depending on the OS and the kind of I/O which is
     854performed (for example, on some contemporary OSes such as Linux, unbuffered
     855disk I/O can be as fast as buffered I/O).  The bottom line, however, is
     856that buffered I/O will offer you predictable performance regardless of the
     857platform and the backing device.  Therefore, it is most always preferable to
     858use buffered I/O rather than unbuffered I/O.
     859
     860Text I/O
     861""""""""
     862
     863Text I/O over a binary storage (such as a file) is significantly slower than
     864binary I/O over the same storage, because it implies conversions from
     865unicode to binary data using a character codec.  This can become noticeable
     866if you handle huge amounts of text data (for example very large log files).
     867Also, :meth:`TextIOWrapper.tell` and :meth:`TextIOWrapper.seek` are both
     868quite slow due to the reconstruction algorithm used.
     869
     870:class:`StringIO`, however, is a native in-memory unicode container and will
     871exhibit similar speed to :class:`BytesIO`.
     872
     873Multi-threading
     874^^^^^^^^^^^^^^^
     875
     876:class:`FileIO` objects are thread-safe to the extent that the operating
     877system calls (such as ``read(2)`` under Unix) they are wrapping are thread-safe
     878too.
     879
     880Binary buffered objects (instances of :class:`BufferedReader`,
     881:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
     882protect their internal structures using a lock; it is therefore safe to call
     883them from multiple threads at once.
     884
     885:class:`TextIOWrapper` objects are not thread-safe.
     886
     887Reentrancy
     888^^^^^^^^^^
     889
     890Binary buffered objects (instances of :class:`BufferedReader`,
     891:class:`BufferedWriter`, :class:`BufferedRandom` and :class:`BufferedRWPair`)
     892are not reentrant.  While reentrant calls will not happen in normal situations,
     893they can arise if you are doing I/O in a :mod:`signal` handler.  If it is
     894attempted to enter a buffered object again while already being accessed
     895*from the same thread*, then a :exc:`RuntimeError` is raised.
     896
     897The above implicitly extends to text files, since the :func:`open()`
     898function will wrap a buffered object inside a :class:`TextIOWrapper`.  This
     899includes standard streams and therefore affects the built-in function
     900:func:`print()` as well.
     901
Note: See TracChangeset for help on using the changeset viewer.