blob: 97f354a032334acf33243bff8f0be5e87a7353fd [file] [log] [blame]
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +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>
9.. sectionauthor:: Benjamin Peterson
10.. versionadded:: 2.6
11
12The :mod:`io` module provides the Python interfaces to stream handling. The
13builtin :func:`open` function is defined in this module.
14
15At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
16defines the basic interface to a stream. Note, however, that there is no
17seperation between reading and writing to streams; implementations are allowed
18to throw an :exc:`IOError` if they do not support a given operation.
19
20Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the
21reading and writing of raw bytes to a stream. :class:`FileIO` subclasses
22:class:`RawIOBase` to provide an interface to OS files.
23
24:class:`BufferedIOBase` deals with buffering on a raw byte stream
25(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
26:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
27readable, writable, and both respectively. :class:`BufferedRandom` provides a
28buffered interface to random access streams. :class:`BytesIO` is a simple
29stream of in-memory bytes.
30
31Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with the encoding
32and decoding of streams into text. :class:`TextIOWrapper`, which extends it, is
33a buffered text interface to a buffered raw stream (:class:`BufferedIOBase`).
34Finally, :class:`StringIO` is a in-memory stream for text.
35
36Argument names are not part of the specification, and only the arguments of
Benjamin Peterson53be57e2008-04-19 19:34:05 +000037:func:`open` are intended to be used as keyword arguments.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000038
39
40Module Interface
41----------------
42
43.. data:: DEFAULT_BUFFER_SIZE
44
45 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson53be57e2008-04-19 19:34:05 +000046 classes. :func:`open` uses the file's blksize (as obtained by
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000047 :func:`os.stat`) if possible.
48
49.. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
50
51 Open *file* and return a stream. If the file cannot be opened, an
52 :exc:`IOError` is raised.
53
54 *file* is either a string giving the name (and the path if the file isn't in
55 the current working directory) of the file to be opened or an integer file
56 descriptor of the file to be wrapped. (If a file descriptor is given, it is
57 closed when the returned I/O object is closed, unless *closefd* is set to
58 ``False``.)
59
60 *mode* is an optional string that specifies the mode in which the file is
61 opened. It defaults to ``'r'`` which means open for reading in text mode.
62 Other common values are ``'w'`` for writing (truncating the file if it
63 already exists), and ``'a'`` for appending (which on *some* Unix systems,
64 means that *all* writes append to the end of the file regardless of the
65 current seek position). In text mode, if *encoding* is not specified the
66 encoding used is platform dependent. (For reading and writing raw bytes use
67 binary mode and leave *encoding* unspecified.) The available modes are:
68
69 ========= ===============================================================
70 Character Meaning
71 --------- ---------------------------------------------------------------
72 ``'r'`` open for reading (default)
73 ``'w'`` open for writing, truncating the file first
74 ``'a'`` open for writing, appending to the end of the file if it exists
75 ``'b'`` binary mode
76 ``'t'`` text mode (default)
77 ``'+'`` open a disk file for updating (reading and writing)
78 ``'U'`` universal newline mode (for backwards compatibility; unneeded
79 for new code)
80 ========= ===============================================================
81
82 The default mode is ``'rt'`` (open for reading text). For binary random
83 access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
84 ``'r+b'`` opens the file without truncation.
85
86 Python distinguishes between files opened in binary and text modes, even when
87 the underlying operating system doesn't. Files opened in binary mode
88 (appending ``'b'`` to the *mode* argument) return contents as ``bytes``
89 objects without any decoding. In text mode (the default, or when ``'t'`` is
90 appended to the *mode* argument), the contents of the file are returned as
91 strings, the bytes having been first decoded using a platform-dependent
92 encoding or using the specified *encoding* if given.
93
94 *buffering* is an optional integer used to set the buffering policy. By
95 default full buffering is on. Pass 0 to switch buffering off (only allowed
96 in binary mode), 1 to set line buffering, and an integer > 1 for full
97 buffering.
98
99 *encoding* is the name of the encoding used to decode or encode the file.
100 This should only be used in text mode. The default encoding is platform
101 dependent, but any encoding supported by Python can be passed. See the
102 :mod:`codecs` module for the list of supported encodings.
103
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000104 *errors* is an optional string that specifies how encoding and decoding
Benjamin Petersona7d09032008-04-19 19:47:34 +0000105 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
106 exception if there is an encoding error (the default of ``None`` has the same
107 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
108 errors can lead to data loss.) ``'replace'`` causes a replacement marker
109 (such as ``'?'``) to be inserted where there is malformed data. When
110 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
111 reference) or ``'backslashreplace'`` (replace with backslashed escape
112 sequences) can be used. Any other error handling name that has been
113 registered with :func:`codecs.register_error` is also valid.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000114
115 *newline* controls how universal newlines works (it only applies to text
116 mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
117 works as follows:
118
119 * On input, if *newline* is ``None``, universal newlines mode is enabled.
120 Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
121 are translated into ``'\n'`` before being returned to the caller. If it is
122 ``''``, universal newline mode is enabled, but line endings are returned to
123 the caller untranslated. If it has any of the other legal values, input
124 lines are only terminated by the given string, and the line ending is
125 returned to the caller untranslated.
126
127 * On output, if *newline* is ``None``, any ``'\n'`` characters written are
128 translated to the system default line separator, :data:`os.linesep`. If
129 *newline* is ``''``, no translation takes place. If *newline* is any of
130 the other legal values, any ``'\n'`` characters written are translated to
131 the given string.
132
133 If *closefd* is ``False``, the underlying file descriptor will be kept open
134 when the file is closed. This does not work when a file name is given and
135 must be ``True`` in that case.
136
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000137 :func:`open` returns a file object whose type depends on the mode, and
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000138 through which the standard file operations such as reading and writing are
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000139 performed. When :func:`open` is used to open a file in a text mode (``'w'``,
140 ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a :class:`TextIOWrapper`.
141 When used to open a file in a binary mode, the returned class varies: in read
142 binary mode, it returns a :class:`BufferedReader`; in write binary and append
143 binary modes, it returns a :class:`BufferedWriter`, and in read/write mode,
144 it returns a :class:`BufferedRandom`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000145
146 It is also possible to use a string or bytearray as a file for both reading
147 and writing. For strings :class:`StringIO` can be used like a file opened in
148 a text mode, and for bytes a :class:`BytesIO` can be used like a file opened
149 in a binary mode.
150
151
152.. exception:: BlockingIOError
153
154 Error raised when blocking would occur on a non-blocking stream. It inherits
155 :exc:`IOError`.
156
157 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
158 attribute:
159
160 .. attribute:: characters_written
161
162 An integer containing the number of characters written to the stream
163 before it blocked.
164
165
166.. exception:: UnsupportedOperation
167
168 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
169 when an unsupported operation is called on a stream.
170
171
172I/O Base Classes
173----------------
174
175.. class:: IOBase
176
177 The abstract base class for all I/O classes, acting on streams of bytes.
178 There is no public constructor.
179
180 This class provides dummy implementations for many methods that derived
181 classes can override selectively; the default implementations represent a
182 file that cannot be read, written or seeked.
183
184 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
185 or :meth:`write` because their signatures will vary, implementations and
186 clients should consider those methods part of the interface. Also,
187 implementations may raise a :exc:`IOError` when operations they do not
188 support are called.
189
190 The basic type used for binary data read from or written to a file is
191 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
192 (such as :class:`readinto`) needed. Text I/O classes work with :class:`str`
193 data.
194
195 Note that calling any method (even inquiries) on a closed stream is
196 undefined. Implementations may raise :exc:`IOError` in this case.
197
198 IOBase (and its subclasses) support the iterator protocol, meaning that an
199 :class:`IOBase` object can be iterated over yielding the lines in a stream.
200
201 IOBase also supports the :keyword:`with` statement. In this example, *fp* is
202 closed after the suite of the with statment is complete::
203
204 with open('spam.txt', 'r') as fp:
205 fp.write('Spam and eggs!')
206
207 :class:`IOBase` provides these methods:
208
209 .. method:: close()
210
211 Flush and close this stream. This method has no effect if the file is
212 already closed.
213
214 .. attribute:: closed
215
216 True if the stream is closed.
217
218 .. method:: fileno()
219
220 Return the underlying file descriptor (an integer) of the stream, if it
221 exists. An :exc:`IOError` is raised if the IO object does not use a file
222 descriptor.
223
224 .. method:: flush()
225
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000226 Flush the write buffers of the stream if applicable. This does nothing
227 for read-only and non-blocking streams.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000228
229 .. method:: isatty()
230
231 Tell if a stream is interactive (connected to a terminal/tty device).
232
233 .. method:: readable()
234
235 Tell if a stream can be read from. If False, :meth:`read` will raise
236 :exc:`IOError`.
237
238 .. method:: readline([limit])
239
240 Read and return a line from the stream. If *limit* is specified, at most
241 *limit* bytes will be read.
242
243 The line terminator is always ``b'\n'`` for binary files; for text files,
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000244 the *newlines* argument to :func:`open` can be used to select the line
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000245 terminator(s) recognized.
246
247 .. method:: readlines([hint])
248
249 Return a list of lines from the stream. *hint* can be specified to
250 control the number of lines read: no more lines will be read if the total
251 size (in bytes/characters) of all lines so far exceeds *hint*.
252
253 .. method:: seek(offset[, whence])
254
255 Change the stream position to byte offset *offset*. *offset* is
256 interpreted relative to the position indicated by *whence*. Values for
257 *whence* are:
258
259 * ``0`` -- start of stream (the default); *pos* should be zero or positive
260 * ``1`` -- current stream position; *pos* may be negative
261 * ``2`` -- end of stream; *pos* is usually negative
262
263 Return the new absolute position.
264
265 .. method:: seekable()
266
267 Tell if a stream supports random IO access. If ``False``, :meth:`seek`,
268 :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
269
270 .. method:: tell()
271
272 Return an integer indicating the current stream position.
273
274 .. method:: truncate([pos])
275
276 Truncate the file to at most *pos* bytes. *pos* defaults to the current
277 file position, as returned by :meth:`tell`.
278
279 .. method:: writable()
280
281 Tell if a stream supports writing. If ``False``, :meth:`write` and
282 :meth:`truncate` will raise :exc:`IOError`.
283
284 .. method:: writelines(lines)
285
286 Write a list of lines to the stream. The lines will not be altered; they
287 must contain line separators.
288
289
290.. class:: RawIOBase
291
292 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
293 public constructor.
294
295 RawIOBase provides or overrides these methods in addition to those from
296 :class:`IOBase`:
297
298 .. method:: read([n])
299
300 Read and return all bytes from the stream until EOF, or if *n* is
301 specified, up to *n* bytes. An empty bytes object is returned on EOF;
302 ``None`` is returned if the object is set not to block and has no data to
303 read.
304
305 .. method:: readall()
306
307 Read and return all bytes from the stream until EOF, using multiple calls
308 to the stream.
309
310 .. method:: readinto(b)
311
312 Read up to len(b) bytes into bytearray *b* and return the number of bytes
313 read.
314
315 .. method:: write(b)
316
317 Write the given bytes, *b*, to the underlying raw stream and return the
318 number of bytes written (never less than ``len(b)``).
319
320
321Raw File I/O
322------------
323
324.. class:: FileIO(name[, mode])
325
326 :class:`FileIO` represents an OS file containing bytes data. It implements
327 the :class:`RawIOBase` interface (and therefore the :class:`IOBase`
328 interface, too).
329
330 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
331 or appending. The file will be created if it doesn't exist when opened for
332 writing or appending; it will be truncated when opened for writing. Add a
333 ``'+'`` to the mode to allow simultaneous reading and writing.
334
335 :class:`FileIO` provides or overrides these methods in addition to those from
336 :class:`RawIOBase` and :class:`IOBase`:
337
338 .. attribute:: mode
339
340 The mode as given in the constructor.
341
342 .. attribute:: name
343
344 The file name.
345
346 .. method:: read([n])
347
348 Read and return bytes at most *n* bytes. Only one system call is made, so
349 less data than requested may be returned. In non-blocking mode, ``None``
350 is returned when no data is available.
351
352 .. method:: readall()
353
354 Read and return as bytes all the data from the file. As much as
355 immediately available is returned in non-blocking mode. If the EOF has
356 been reached, ``b''`` is returned.
357
358 .. method:: readinto(bytearray)
359
360 This method should not be used on :class:`FileIO` objects.
361
362 .. method:: write(b)
363
364 Write the bytes *b* to the file, and return the number actually written.
365 Only one system call is made, so not all of the data may be written.
366
367
368Buffered Streams
369----------------
370
371.. class:: BufferedIOBase
372
373 Base class for streams that support buffering. It inherits :class:`IOBase`.
374 There is no public constructor.
375
376 The main difference with :class:`RawIOBase` is that the :meth:`read` method
377 supports omitting the *size* argument, and does not have a default
378 implementation that defers to :meth:`readinto`.
379
380 In addition, :meth:`read`, :meth:`readinto`, and :meth:`write` may raise
381 :exc:`BlockingIOError` if the underlying raw stream is in non-blocking mode
382 and not ready; unlike their raw counterparts, they will never return
383 ``None``.
384
385 A typical implementation should not inherit from a :class:`RawIOBase`
386 implementation, but wrap one like :class:`BufferedWriter` and
387 :class:`BufferedReader`.
388
389 :class:`BufferedIOBase` provides or overrides these methods in addition to
390 those from :class:`IOBase`:
391
392 .. method:: read([n])
393
394 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
395 negative, data is read and returned until EOF is reached. An empty bytes
396 object is returned if the stream is already at EOF.
397
398 If the argument is positive, and the underlying raw stream is not
399 interactive, multiple raw reads may be issued to satisfy the byte count
400 (unless EOF is reached first). But for interactive raw streams, at most
401 one raw read will be issued, and a short result does not imply that EOF is
402 imminent.
403
404 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
405 data at the moment.
406
407 .. method:: readinto(b)
408
409 Read up to len(b) bytes into bytearray *b* and return the number of bytes
410 read.
411
412 Like :meth:`read`, multiple reads may be issued to the underlying raw
413 stream, unless the latter is 'interactive.'
414
415 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
416 data at the moment.
417
418 .. method:: write(b)
419
420 Write the given bytes, *b*, to the underlying raw stream and return the
421 number of bytes written (never less than ``len(b)``).
422
423 A :exc:`BlockingIOError` is raised if the buffer is full, and the
424 underlying raw stream cannot accept more data at the moment.
425
426
427.. class:: BytesIO([initial_bytes])
428
429 A stream implementation using an in-memory bytes buffer. It inherits
430 :class:`BufferedIOBase`.
431
432 The argument *initial_bytes* is an optional initial bytearray.
433
434 :class:`BytesIO` provides or overrides these methods in addition to those
435 from :class:`BufferedIOBase` and :class:`IOBase`:
436
437 .. method:: getvalue()
438
439 Return the bytes value of the buffer.
440
441 .. method:: read1()
442
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000443 In :class:`BytesIO`, this is the same as :meth:`read`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000444
445 .. method:: truncate([pos])
446
447 Truncate the file to at most *pos* bytes. *pos* defaults to the current
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000448 stream position, as returned by :meth:`tell`.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000449
450
451.. class:: BufferedReader(raw[, buffer_size])
452
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000453 A buffer for a readable, sequential :class:`RawIOBase` object. It inherits
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000454 :class:`BufferedIOBase`.
455
456 The constructor creates a :class:`BufferedReader` for the given readable
457 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
458 :data:`DEFAULT_BUFFER_SIZE` is used.
459
460 :class:`BufferedReader` provides or overrides these methods in addition to
461 those from :class:`BufferedIOBase` and :class:`IOBase`:
462
463 .. method:: peek([n])
464
465 Return bytes from a buffer without advancing the position. The argument
466 indicates a desired minimal number of bytes; only one read on the raw
467 stream is done to satisfy it. More than the buffer's size is never
468 returned.
469
470 .. method:: read([n])
471
472 Read and return *n* bytes, or if *n* is not given or negative, until EOF
473 or if the read call would block in non-blocking mode.
474
475 .. method:: read1(n)
476
477 Read and return up to *n* bytes with only one call on the raw stream. If
478 at least one byte is buffered, only buffered bytes are returned.
479 Otherwise, one raw stream read call is made.
480
481
482.. class:: BufferedWriter(raw[, buffer_size[, max_buffer_size]])
483
484 A buffer for a writeable sequential RawIO object. It inherits
485 :class:`BufferedIOBase`.
486
487 The constructor creates a :class:`BufferedWriter` for the given writeable
488 *raw* stream. If the *buffer_size* is not given, it defaults to
489 :data:`DEAFULT_BUFFER_SIZE`. If *max_buffer_size* is omitted, it defaults to
490 twice the buffer size.
491
492 :class:`BufferedWriter` provides or overrides these methods in addition to
493 those from :class:`BufferedIOBase` and :class:`IOBase`:
494
495 .. method:: flush()
496
497 Force bytes held in the buffer into the raw stream. A
498 :exc:`BlockingIOError` is be raised if the raw stream blocks.
499
500 .. method:: write(b)
501
502 Write bytes *b* onto the raw stream and return the number written. A
503 :exc:`BlockingIOError` is raised when the raw stream blocks.
504
505
506.. class:: BufferedRWPair(reader, writer[, buffer_size[, max_buffer_size]])
507
508 A buffered writer and reader object together for a raw stream that can be
509 written and read from. It has and supports both :meth:`read`, :meth:`write`,
510 and their variants. This is useful for such applications such as sockets and
511 two-way pipes. It inherits :class:`BufferedIOBase`.
512
513 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
514 writeable respectively. If the *buffer_size* is omitted it defaults to
515 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
516 defaults to twice the buffer size.
517
518 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods.
519
520
521.. class:: BufferedRandom(raw[, buffer_size[, max_buffer_size]])
522
523 A buffered interface to random access streams. It inherits
524 :class:`BufferedReader` and :class:`BufferedWriter`.
525
526 The constructor creates a reader and writer for a seekable raw stream, given
527 in the first argument. If the *buffer_size* is omitted it defaults to
528 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
529 defaults to twice the buffer size.
530
531 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
532 :class:`BufferedWriter` can do.
533
534
535Text I/O
536--------
537
538.. class:: TextIOBase
539
540 Base class for text streams. This class provides a character and line based
541 interface to stream I/O. There is no :meth:`readinto` method because
542 Python's character strings are immutable. It inherits :class:`IOBase`.
543 There is no public constructor.
544
545 :class:`TextIOBase` provides or overrides these methods in addition to those
546 from :class:`IOBase`:
547
548 .. attribute:: encoding
549
550 Return the name of the encoding used to decode the stream's bytes into
551 strings, and to encode strings into bytes.
552
553 .. attribute:: newlines
554
555 Return a string, tuple of strings, or ``None`` indicating the newlines
556 translated so far.
557
558 .. method:: read(n)
559
560 Read and return at most *n* characters from the stream. If *n* is
561 negative or ``None``, read to EOF.
562
563 .. method:: readline()
564
565 Read until newline or EOF and return. If the stream is already at EOF, an
566 empty stream is returned.
567
568 .. method:: write(s)
569
570 Write string *s* to the stream and return the number of characters
571 written.
572
573
574.. class:: TextIOWrapper(buffer[, encoding[, errors[, newline[, line_buffering]]]])
575
576 A buffered text stream over a :class:`BufferedIOBase` raw stream, *buffer*.
577 It inherits :class:`TextIOBase`.
578
579 *encoding* gives the name of the encoding that the stream will be decoded or
580 encoded with. It defaults to :func:`locale.getpreferredencoding`.
581
Benjamin Peterson53be57e2008-04-19 19:34:05 +0000582 *errors* is an optional string that specifies how encoding and decoding
583 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
584 exception if there is an encoding error (the default of ``None`` has the same
585 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
586 errors can lead to data loss.) ``'replace'`` causes a replacement marker
Benjamin Petersona7d09032008-04-19 19:47:34 +0000587 (such as ``'?'``) to be inserted where there is malformed data. When
588 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
589 reference) or ``'backslashreplace'`` (replace with backslashed escape
590 sequences) can be used. Any other error handling name that has been
591 registered with :func:`codecs.register_error` is also valid.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +0000592
593 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It
594 controls the handling of line endings. If it is ``None``, universal newlines
595 is enabled. With this enabled, on input, the lines endings ``'\n'``,
596 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
597 the caller. Conversely, on output, ``'\n'`` is translated to the system
598 default line seperator, :data:`os.linesep`. If *newline* is any other of its
599 legal values, that newline becomes the newline when the file is read and it
600 is returned untranslated. On output, ``'\n'`` is converted to the *newline*.
601
602 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
603 write contains a newline character.
604
605 :class:`TextIOWrapper` provides these methods in addition to those of
606 :class:`TextIOBase` and its parents:
607
608 .. attribute:: errors
609
610 The encoding and decoding error setting.
611
612 .. attribute:: line_buffering
613
614 Whether line buffering is enabled.
615
616
617.. class:: StringIO([initial_value[, encoding[, errors[, newline]]]])
618
619 An in-memory stream for text. It in inherits :class:`TextIOWrapper`.
620
621 Create a new StringIO stream with an inital value, encoding, error handling,
622 and newline setting. See :class:`TextIOWrapper`\'s constructor for more
623 information.
624
625 :class:`StringIO` provides these methods in addition to those from
626 :class:`TextIOWrapper` and its parents:
627
628 .. method:: getvalue()
629
630 Return a str representation of the contents of the internal buffer.
631
632
633.. class:: IncrementalNewlineDecoder
634
635 A helper codec that decodes newlines for universal newlines mode. It
636 inherits :class:`codecs.IncrementalDecoder`.
637