2 Python implementation of the io module.
5 from __future__
import print_function
6 from __future__
import unicode_literals
12 # Import _thread instead of threading to reduce startup cost
14 from thread
import allocate_lock
as Lock
16 from dummy_thread
import allocate_lock
as Lock
19 from io
import __all__
20 from io
import SEEK_SET
, SEEK_CUR
, SEEK_END
24 # open() uses st_blksize whenever we can
25 DEFAULT_BUFFER_SIZE
= 8 * 1024 # bytes
27 # NOTE: Base classes defined here are registered with the "official" ABCs
28 # defined in io.py. We don't use real inheritance though, because we don't
29 # want to inherit the C implementations.
32 class BlockingIOError(IOError):
34 """Exception raised when I/O would block on a non-blocking I/O stream."""
36 def __init__(self
, errno
, strerror
, characters_written
=0):
37 super(IOError, self
).__init
__(errno
, strerror
)
38 if not isinstance(characters_written
, (int, long)):
39 raise TypeError("characters_written must be a integer")
40 self
.characters_written
= characters_written
43 def open(file, mode
="r", buffering
=None,
44 encoding
=None, errors
=None,
45 newline
=None, closefd
=True):
47 r
"""Open file and return a stream. Raise IOError upon failure.
49 file is either a text or byte string giving the name (and the path
50 if the file isn't in the current working directory) of the file to
51 be opened or an integer file descriptor of the file to be
52 wrapped. (If a file descriptor is given, it is closed when the
53 returned I/O object is closed, unless closefd is set to False.)
55 mode is an optional string that specifies the mode in which the file
56 is opened. It defaults to 'r' which means open for reading in text
57 mode. Other common values are 'w' for writing (truncating the file if
58 it already exists), and 'a' for appending (which on some Unix systems,
59 means that all writes append to the end of the file regardless of the
60 current seek position). In text mode, if encoding is not specified the
61 encoding used is platform dependent. (For reading and writing raw
62 bytes use binary mode and leave encoding unspecified.) The available
65 ========= ===============================================================
67 --------- ---------------------------------------------------------------
68 'r' open for reading (default)
69 'w' open for writing, truncating the file first
70 'a' open for writing, appending to the end of the file if it exists
72 't' text mode (default)
73 '+' open a disk file for updating (reading and writing)
74 'U' universal newline mode (for backwards compatibility; unneeded
76 ========= ===============================================================
78 The default mode is 'rt' (open for reading text). For binary random
79 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
80 'r+b' opens the file without truncation.
82 Python distinguishes between files opened in binary and text modes,
83 even when the underlying operating system doesn't. Files opened in
84 binary mode (appending 'b' to the mode argument) return contents as
85 bytes objects without any decoding. In text mode (the default, or when
86 't' is appended to the mode argument), the contents of the file are
87 returned as strings, the bytes having been first decoded using a
88 platform-dependent encoding or using the specified encoding if given.
90 buffering is an optional integer used to set the buffering policy. By
91 default full buffering is on. Pass 0 to switch buffering off (only
92 allowed in binary mode), 1 to set line buffering, and an integer > 1
95 encoding is the name of the encoding used to decode or encode the
96 file. This should only be used in text mode. The default encoding is
97 platform dependent, but any encoding supported by Python can be
98 passed. See the codecs module for the list of supported encodings.
100 errors is an optional string that specifies how encoding errors are to
101 be handled---this argument should not be used in binary mode. Pass
102 'strict' to raise a ValueError exception if there is an encoding error
103 (the default of None has the same effect), or pass 'ignore' to ignore
104 errors. (Note that ignoring encoding errors can lead to data loss.)
105 See the documentation for codecs.register for a list of the permitted
106 encoding error strings.
108 newline controls how universal newlines works (it only applies to text
109 mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
112 * On input, if newline is None, universal newlines mode is
113 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
114 these are translated into '\n' before being returned to the
115 caller. If it is '', universal newline mode is enabled, but line
116 endings are returned to the caller untranslated. If it has any of
117 the other legal values, input lines are only terminated by the given
118 string, and the line ending is returned to the caller untranslated.
120 * On output, if newline is None, any '\n' characters written are
121 translated to the system default line separator, os.linesep. If
122 newline is '', no translation takes place. If newline is any of the
123 other legal values, any '\n' characters written are translated to
126 If closefd is False, the underlying file descriptor will be kept open
127 when the file is closed. This does not work when a file name is given
128 and must be True in that case.
130 open() returns a file object whose type depends on the mode, and
131 through which the standard file operations such as reading and writing
132 are performed. When open() is used to open a file in a text mode ('w',
133 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
134 a file in a binary mode, the returned class varies: in read binary
135 mode, it returns a BufferedReader; in write binary and append binary
136 modes, it returns a BufferedWriter, and in read/write mode, it returns
139 It is also possible to use a string or bytearray as a file for both
140 reading and writing. For strings StringIO can be used like a file
141 opened in a text mode, and for bytes a BytesIO can be used like a file
142 opened in a binary mode.
144 if not isinstance(file, (basestring
, int, long)):
145 raise TypeError("invalid file: %r" % file)
146 if not isinstance(mode
, basestring
):
147 raise TypeError("invalid mode: %r" % mode
)
148 if buffering
is not None and not isinstance(buffering
, (int, long)):
149 raise TypeError("invalid buffering: %r" % buffering
)
150 if encoding
is not None and not isinstance(encoding
, basestring
):
151 raise TypeError("invalid encoding: %r" % encoding
)
152 if errors
is not None and not isinstance(errors
, basestring
):
153 raise TypeError("invalid errors: %r" % errors
)
155 if modes
- set("arwb+tU") or len(mode
) > len(modes
):
156 raise ValueError("invalid mode: %r" % mode
)
157 reading
= "r" in modes
158 writing
= "w" in modes
159 appending
= "a" in modes
160 updating
= "+" in modes
162 binary
= "b" in modes
164 if writing
or appending
:
165 raise ValueError("can't use U and writing mode at once")
168 raise ValueError("can't have text and binary mode at once")
169 if reading
+ writing
+ appending
> 1:
170 raise ValueError("can't have read/write/append mode at once")
171 if not (reading
or writing
or appending
):
172 raise ValueError("must have exactly one of read/write/append mode")
173 if binary
and encoding
is not None:
174 raise ValueError("binary mode doesn't take an encoding argument")
175 if binary
and errors
is not None:
176 raise ValueError("binary mode doesn't take an errors argument")
177 if binary
and newline
is not None:
178 raise ValueError("binary mode doesn't take a newline argument")
180 (reading
and "r" or "") +
181 (writing
and "w" or "") +
182 (appending
and "a" or "") +
183 (updating
and "+" or ""),
185 if buffering
is None:
187 line_buffering
= False
188 if buffering
== 1 or buffering
< 0 and raw
.isatty():
190 line_buffering
= True
192 buffering
= DEFAULT_BUFFER_SIZE
194 bs
= os
.fstat(raw
.fileno()).st_blksize
195 except (os
.error
, AttributeError):
201 raise ValueError("invalid buffering size")
205 raise ValueError("can't have unbuffered text I/O")
207 buffer = BufferedRandom(raw
, buffering
)
208 elif writing
or appending
:
209 buffer = BufferedWriter(raw
, buffering
)
211 buffer = BufferedReader(raw
, buffering
)
213 raise ValueError("unknown mode: %r" % mode
)
216 text
= TextIOWrapper(buffer, encoding
, errors
, newline
, line_buffering
)
222 """Helper for builtins.open.__doc__
224 def __get__(self
, obj
, typ
):
226 "open(file, mode='r', buffering=None, encoding=None, "
227 "errors=None, newline=None, closefd=True)\n\n" +
231 """Wrapper for builtins.open
233 Trick so that open won't become a bound method when stored
234 as a class variable (as dbm.dumb does).
236 See initstdio() in Python/pythonrun.c.
238 __doc__
= DocDescriptor()
240 def __new__(cls
, *args
, **kwargs
):
241 return open(*args
, **kwargs
)
244 class UnsupportedOperation(ValueError, IOError):
249 __metaclass__
= abc
.ABCMeta
251 """The abstract base class for all I/O classes, acting on streams of
252 bytes. There is no public constructor.
254 This class provides dummy implementations for many methods that
255 derived classes can override selectively; the default implementations
256 represent a file that cannot be read, written or seeked.
258 Even though IOBase does not declare read, readinto, or write because
259 their signatures will vary, implementations and clients should
260 consider those methods part of the interface. Also, implementations
261 may raise a IOError when operations they do not support are called.
263 The basic type used for binary data read from or written to a file is
264 bytes. bytearrays are accepted too, and in some cases (such as
265 readinto) needed. Text I/O classes work with str data.
267 Note that calling any method (even inquiries) on a closed stream is
268 undefined. Implementations may raise IOError in this case.
270 IOBase (and its subclasses) support the iterator protocol, meaning
271 that an IOBase object can be iterated over yielding the lines in a
274 IOBase also supports the :keyword:`with` statement. In this example,
275 fp is closed after the suite of the with statement is complete:
277 with open('spam.txt', 'r') as fp:
278 fp.write('Spam and eggs!')
283 def _unsupported(self
, name
):
284 """Internal: raise an exception for unsupported operations."""
285 raise UnsupportedOperation("%s.%s() not supported" %
286 (self
.__class
__.__name
__, name
))
290 def seek(self
, pos
, whence
=0):
291 """Change stream position.
293 Change the stream position to byte offset offset. offset is
294 interpreted relative to the position indicated by whence. Values
297 * 0 -- start of stream (the default); offset should be zero or positive
298 * 1 -- current stream position; offset may be negative
299 * 2 -- end of stream; offset is usually negative
301 Return the new absolute position.
303 self
._unsupported
("seek")
306 """Return current stream position."""
307 return self
.seek(0, 1)
309 def truncate(self
, pos
=None):
310 """Truncate file to size bytes.
312 Size defaults to the current IO position as reported by tell(). Return
315 self
._unsupported
("truncate")
317 ### Flush and close ###
320 """Flush write buffers, if applicable.
322 This is not implemented for read-only and non-blocking streams.
324 # XXX Should this return the number of bytes written???
329 """Flush and close the IO object.
331 This method has no effect if the file is already closed.
333 if not self
.__closed
:
337 pass # If flush() fails, just give up
341 """Destructor. Calls close()."""
342 # The try/except block is in case this is called at program
343 # exit time, when it's possible that globals have already been
344 # deleted, and then the close() call might fail. Since
345 # there's nothing we can do about such failures and they annoy
346 # the end users, we suppress the traceback.
355 """Return whether object supports random access.
357 If False, seek(), tell() and truncate() will raise IOError.
358 This method may need to do a test seek().
362 def _checkSeekable(self
, msg
=None):
363 """Internal: raise an IOError if file is not seekable
365 if not self
.seekable():
366 raise IOError("File or stream is not seekable."
367 if msg
is None else msg
)
371 """Return whether object was opened for reading.
373 If False, read() will raise IOError.
377 def _checkReadable(self
, msg
=None):
378 """Internal: raise an IOError if file is not readable
380 if not self
.readable():
381 raise IOError("File or stream is not readable."
382 if msg
is None else msg
)
385 """Return whether object was opened for writing.
387 If False, write() and truncate() will raise IOError.
391 def _checkWritable(self
, msg
=None):
392 """Internal: raise an IOError if file is not writable
394 if not self
.writable():
395 raise IOError("File or stream is not writable."
396 if msg
is None else msg
)
400 """closed: bool. True iff the file has been closed.
402 For backwards compatibility, this is a property, not a predicate.
406 def _checkClosed(self
, msg
=None):
407 """Internal: raise an ValueError if file is closed
410 raise ValueError("I/O operation on closed file."
411 if msg
is None else msg
)
413 ### Context manager ###
416 """Context management protocol. Returns self."""
420 def __exit__(self
, *args
):
421 """Context management protocol. Calls close()"""
424 ### Lower-level APIs ###
426 # XXX Should these be present even if unimplemented?
429 """Returns underlying file descriptor if one exists.
431 An IOError is raised if the IO object does not use a file descriptor.
433 self
._unsupported
("fileno")
436 """Return whether this is an 'interactive' stream.
438 Return False if it can't be determined.
443 ### Readline[s] and writelines ###
445 def readline(self
, limit
=-1):
446 r
"""Read and return a line from the stream.
448 If limit is specified, at most limit bytes will be read.
450 The line terminator is always b'\n' for binary files; for text
451 files, the newlines argument to open can be used to select the line
452 terminator(s) recognized.
454 # For backwards compatibility, a (slowish) readline().
455 if hasattr(self
, "peek"):
457 readahead
= self
.peek(1)
460 n
= (readahead
.find(b
"\n") + 1) or len(readahead
)
469 elif not isinstance(limit
, (int, long)):
470 raise TypeError("limit must be an integer")
472 while limit
< 0 or len(res
) < limit
:
473 b
= self
.read(nreadahead())
477 if res
.endswith(b
"\n"):
486 line
= self
.readline()
491 def readlines(self
, hint
=None):
492 """Return a list of lines from the stream.
494 hint can be specified to control the number of lines read: no more
495 lines will be read if the total size (in bytes/characters) of all
496 lines so far exceeds hint.
498 if hint
is not None and not isinstance(hint
, (int, long)):
499 raise TypeError("integer or None expected")
500 if hint
is None or hint
<= 0:
511 def writelines(self
, lines
):
516 io
.IOBase
.register(IOBase
)
519 class RawIOBase(IOBase
):
521 """Base class for raw binary I/O."""
523 # The read() method is implemented by calling readinto(); derived
524 # classes that want to support read() only need to implement
525 # readinto() as a primitive operation. In general, readinto() can be
526 # more efficient than read().
528 # (It would be tempting to also provide an implementation of
529 # readinto() in terms of read(), in case the latter is a more suitable
530 # primitive operation, but that would lead to nasty recursion in case
531 # a subclass doesn't implement either.)
533 def read(self
, n
=-1):
534 """Read and return up to n bytes.
536 Returns an empty bytes object on EOF, or None if the object is
537 set not to block and has no data to read.
542 return self
.readall()
543 b
= bytearray(n
.__index
__())
549 """Read until EOF, using multiple read() call."""
552 data
= self
.read(DEFAULT_BUFFER_SIZE
)
558 def readinto(self
, b
):
559 """Read up to len(b) bytes into b.
561 Returns number of bytes read (0 for EOF), or None if the object
562 is set not to block as has no data to read.
564 self
._unsupported
("readinto")
567 """Write the given buffer to the IO stream.
569 Returns the number of bytes written, which may be less than len(b).
571 self
._unsupported
("write")
573 io
.RawIOBase
.register(RawIOBase
)
574 from _io
import FileIO
575 RawIOBase
.register(FileIO
)
578 class BufferedIOBase(IOBase
):
580 """Base class for buffered IO objects.
582 The main difference with RawIOBase is that the read() method
583 supports omitting the size argument, and does not have a default
584 implementation that defers to readinto().
586 In addition, read(), readinto() and write() may raise
587 BlockingIOError if the underlying raw stream is in non-blocking
588 mode and not ready; unlike their raw counterparts, they will never
591 A typical implementation should not inherit from a RawIOBase
592 implementation, but wrap one.
595 def read(self
, n
=None):
596 """Read and return up to n bytes.
598 If the argument is omitted, None, or negative, reads and
599 returns all data until EOF.
601 If the argument is positive, and the underlying raw stream is
602 not 'interactive', multiple raw reads may be issued to satisfy
603 the byte count (unless EOF is reached first). But for
604 interactive raw streams (XXX and for pipes?), at most one raw
605 read will be issued, and a short result does not imply that
608 Returns an empty bytes array on EOF.
610 Raises BlockingIOError if the underlying raw stream has no
613 self
._unsupported
("read")
615 def read1(self
, n
=None):
616 """Read up to n bytes with at most one read() system call."""
617 self
._unsupported
("read1")
619 def readinto(self
, b
):
620 """Read up to len(b) bytes into b.
622 Like read(), this may issue multiple reads to the underlying raw
623 stream, unless the latter is 'interactive'.
625 Returns the number of bytes read (0 for EOF).
627 Raises BlockingIOError if the underlying raw stream has no
630 # XXX This ought to work with anything that supports the buffer API
631 data
= self
.read(len(b
))
635 except TypeError as err
:
637 if not isinstance(b
, array
.array
):
639 b
[:n
] = array
.array(b
'b', data
)
643 """Write the given buffer to the IO stream.
645 Return the number of bytes written, which is never less than
648 Raises BlockingIOError if the buffer is full and the
649 underlying raw stream cannot accept more data at the moment.
651 self
._unsupported
("write")
655 Separate the underlying raw stream from the buffer and return it.
657 After the raw stream has been detached, the buffer is in an unusable
660 self
._unsupported
("detach")
662 io
.BufferedIOBase
.register(BufferedIOBase
)
665 class _BufferedIOMixin(BufferedIOBase
):
667 """A mixin implementation of BufferedIOBase with an underlying raw stream.
669 This passes most requests on to the underlying raw stream. It
670 does *not* provide implementations of read(), readinto() or
674 def __init__(self
, raw
):
679 def seek(self
, pos
, whence
=0):
680 new_position
= self
.raw
.seek(pos
, whence
)
682 raise IOError("seek() returned an invalid position")
686 pos
= self
.raw
.tell()
688 raise IOError("tell() returned an invalid position")
691 def truncate(self
, pos
=None):
692 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
693 # and a flush may be necessary to synch both views of the current
699 # XXX: Should seek() be used, instead of passing the position
700 # XXX directly to truncate?
701 return self
.raw
.truncate(pos
)
703 ### Flush and close ###
709 if not self
.closed
and self
.raw
is not None:
713 pass # If flush() fails, just give up
718 raise ValueError("raw stream already detached")
727 return self
.raw
.seekable()
730 return self
.raw
.readable()
733 return self
.raw
.writable()
737 return self
.raw
.closed
748 clsname
= self
.__class
__.__name
__
751 except AttributeError:
752 return "<_pyio.{0}>".format(clsname
)
754 return "<_pyio.{0} name={1!r}>".format(clsname
, name
)
756 ### Lower-level APIs ###
759 return self
.raw
.fileno()
762 return self
.raw
.isatty()
765 class BytesIO(BufferedIOBase
):
767 """Buffered I/O implementation using an in-memory bytes buffer."""
769 def __init__(self
, initial_bytes
=None):
771 if initial_bytes
is not None:
772 buf
.extend(initial_bytes
)
776 def __getstate__(self
):
778 raise ValueError("__getstate__ on closed file")
779 return self
.__dict
__.copy()
782 """Return the bytes value (contents) of the buffer
785 raise ValueError("getvalue on closed file")
786 return bytes(self
._buffer
)
788 def read(self
, n
=None):
790 raise ValueError("read from closed file")
793 if not isinstance(n
, (int, long)):
794 raise TypeError("integer argument expected, got {0!r}".format(
797 n
= len(self
._buffer
)
798 if len(self
._buffer
) <= self
._pos
:
800 newpos
= min(len(self
._buffer
), self
._pos
+ n
)
801 b
= self
._buffer
[self
._pos
: newpos
]
806 """This is the same as read.
812 raise ValueError("write to closed file")
813 if isinstance(b
, unicode):
814 raise TypeError("can't write unicode to binary stream")
819 if pos
> len(self
._buffer
):
820 # Inserts null bytes between the current end of the file
821 # and the new write position.
822 padding
= b
'\x00' * (pos
- len(self
._buffer
))
823 self
._buffer
+= padding
824 self
._buffer
[pos
:pos
+ n
] = b
828 def seek(self
, pos
, whence
=0):
830 raise ValueError("seek on closed file")
832 pos
= pos
.__index
__()
833 except AttributeError as err
:
834 raise TypeError("an integer is required")
837 raise ValueError("negative seek position %r" % (pos
,))
840 self
._pos
= max(0, self
._pos
+ pos
)
842 self
._pos
= max(0, len(self
._buffer
) + pos
)
844 raise ValueError("invalid whence value")
849 raise ValueError("tell on closed file")
852 def truncate(self
, pos
=None):
854 raise ValueError("truncate on closed file")
858 raise ValueError("negative truncate position %r" % (pos
,))
859 del self
._buffer
[pos
:]
860 return self
.seek(pos
)
872 class BufferedReader(_BufferedIOMixin
):
874 """BufferedReader(raw[, buffer_size])
876 A buffer for a readable, sequential BaseRawIO object.
878 The constructor creates a BufferedReader for the given readable raw
879 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
883 def __init__(self
, raw
, buffer_size
=DEFAULT_BUFFER_SIZE
):
884 """Create a new buffered reader using the given readable raw IO object.
886 if not raw
.readable():
887 raise IOError('"raw" argument must be readable.')
889 _BufferedIOMixin
.__init
__(self
, raw
)
891 raise ValueError("invalid buffer size")
892 self
.buffer_size
= buffer_size
893 self
._reset
_read
_buf
()
894 self
._read
_lock
= Lock()
896 def _reset_read_buf(self
):
900 def read(self
, n
=None):
903 Returns exactly n bytes of data unless the underlying raw IO
904 stream reaches EOF or if the call would block in non-blocking
905 mode. If n is negative, read until EOF or until read() would
908 if n
is not None and n
< -1:
909 raise ValueError("invalid number of bytes to read")
910 with self
._read
_lock
:
911 return self
._read
_unlocked
(n
)
913 def _read_unlocked(self
, n
=None):
915 empty_values
= (b
"", None)
919 # Special case for when the number of bytes to read is unspecified.
920 if n
is None or n
== -1:
921 self
._reset
_read
_buf
()
922 chunks
= [buf
[pos
:]] # Strip the consumed bytes.
925 # Read until EOF or until read() would block.
926 chunk
= self
.raw
.read()
927 if chunk
in empty_values
:
930 current_size
+= len(chunk
)
932 return b
"".join(chunks
) or nodata_val
934 # The number of bytes to read is specified, return at most n bytes.
935 avail
= len(buf
) - pos
# Length of the available buffered data.
937 # Fast path: the data to read is fully buffered.
939 return buf
[pos
:pos
+n
]
940 # Slow path: read from the stream until enough bytes are read,
941 # or until an EOF occurs or until read() would block.
943 wanted
= max(self
.buffer_size
, n
)
945 chunk
= self
.raw
.read(wanted
)
946 if chunk
in empty_values
:
951 # n is more then avail only when an EOF occurred or when
952 # read() would have blocked.
954 out
= b
"".join(chunks
)
955 self
._read
_buf
= out
[n
:] # Save the extra data in the buffer.
957 return out
[:n
] if out
else nodata_val
960 """Returns buffered bytes without advancing the position.
962 The argument indicates a desired minimal number of bytes; we
963 do at most one raw read to satisfy it. We never return more
964 than self.buffer_size.
966 with self
._read
_lock
:
967 return self
._peek
_unlocked
(n
)
969 def _peek_unlocked(self
, n
=0):
970 want
= min(n
, self
.buffer_size
)
971 have
= len(self
._read
_buf
) - self
._read
_pos
972 if have
< want
or have
<= 0:
973 to_read
= self
.buffer_size
- have
974 current
= self
.raw
.read(to_read
)
976 self
._read
_buf
= self
._read
_buf
[self
._read
_pos
:] + current
978 return self
._read
_buf
[self
._read
_pos
:]
981 """Reads up to n bytes, with at most one read() system call."""
982 # Returns up to n bytes. If at least one byte is buffered, we
983 # only return buffered bytes. Otherwise, we do one raw read.
985 raise ValueError("number of bytes to read must be positive")
988 with self
._read
_lock
:
989 self
._peek
_unlocked
(1)
990 return self
._read
_unlocked
(
991 min(n
, len(self
._read
_buf
) - self
._read
_pos
))
994 return _BufferedIOMixin
.tell(self
) - len(self
._read
_buf
) + self
._read
_pos
996 def seek(self
, pos
, whence
=0):
997 if not (0 <= whence
<= 2):
998 raise ValueError("invalid whence value")
999 with self
._read
_lock
:
1001 pos
-= len(self
._read
_buf
) - self
._read
_pos
1002 pos
= _BufferedIOMixin
.seek(self
, pos
, whence
)
1003 self
._reset
_read
_buf
()
1006 class BufferedWriter(_BufferedIOMixin
):
1008 """A buffer for a writeable sequential RawIO object.
1010 The constructor creates a BufferedWriter for the given writeable raw
1011 stream. If the buffer_size is not given, it defaults to
1012 DEFAULT_BUFFER_SIZE.
1015 _warning_stack_offset
= 2
1017 def __init__(self
, raw
,
1018 buffer_size
=DEFAULT_BUFFER_SIZE
, max_buffer_size
=None):
1019 if not raw
.writable():
1020 raise IOError('"raw" argument must be writable.')
1022 _BufferedIOMixin
.__init
__(self
, raw
)
1023 if buffer_size
<= 0:
1024 raise ValueError("invalid buffer size")
1025 if max_buffer_size
is not None:
1026 warnings
.warn("max_buffer_size is deprecated", DeprecationWarning,
1027 self
._warning
_stack
_offset
)
1028 self
.buffer_size
= buffer_size
1029 self
._write
_buf
= bytearray()
1030 self
._write
_lock
= Lock()
1034 raise ValueError("write to closed file")
1035 if isinstance(b
, unicode):
1036 raise TypeError("can't write unicode to binary stream")
1037 with self
._write
_lock
:
1038 # XXX we can implement some more tricks to try and avoid
1040 if len(self
._write
_buf
) > self
.buffer_size
:
1041 # We're full, so let's pre-flush the buffer
1043 self
._flush
_unlocked
()
1044 except BlockingIOError
as e
:
1045 # We can't accept anything else.
1046 # XXX Why not just let the exception pass through?
1047 raise BlockingIOError(e
.errno
, e
.strerror
, 0)
1048 before
= len(self
._write
_buf
)
1049 self
._write
_buf
.extend(b
)
1050 written
= len(self
._write
_buf
) - before
1051 if len(self
._write
_buf
) > self
.buffer_size
:
1053 self
._flush
_unlocked
()
1054 except BlockingIOError
as e
:
1055 if len(self
._write
_buf
) > self
.buffer_size
:
1056 # We've hit the buffer_size. We have to accept a partial
1057 # write and cut back our buffer.
1058 overage
= len(self
._write
_buf
) - self
.buffer_size
1060 self
._write
_buf
= self
._write
_buf
[:self
.buffer_size
]
1061 raise BlockingIOError(e
.errno
, e
.strerror
, written
)
1064 def truncate(self
, pos
=None):
1065 with self
._write
_lock
:
1066 self
._flush
_unlocked
()
1068 pos
= self
.raw
.tell()
1069 return self
.raw
.truncate(pos
)
1072 with self
._write
_lock
:
1073 self
._flush
_unlocked
()
1075 def _flush_unlocked(self
):
1077 raise ValueError("flush of closed file")
1080 while self
._write
_buf
:
1081 n
= self
.raw
.write(self
._write
_buf
)
1082 if n
> len(self
._write
_buf
) or n
< 0:
1083 raise IOError("write() returned incorrect number of bytes")
1084 del self
._write
_buf
[:n
]
1086 except BlockingIOError
as e
:
1087 n
= e
.characters_written
1088 del self
._write
_buf
[:n
]
1090 raise BlockingIOError(e
.errno
, e
.strerror
, written
)
1093 return _BufferedIOMixin
.tell(self
) + len(self
._write
_buf
)
1095 def seek(self
, pos
, whence
=0):
1096 if not (0 <= whence
<= 2):
1097 raise ValueError("invalid whence")
1098 with self
._write
_lock
:
1099 self
._flush
_unlocked
()
1100 return _BufferedIOMixin
.seek(self
, pos
, whence
)
1103 class BufferedRWPair(BufferedIOBase
):
1105 """A buffered reader and writer object together.
1107 A buffered reader object and buffered writer object put together to
1108 form a sequential IO object that can read and write. This is typically
1109 used with a socket or two-way pipe.
1111 reader and writer are RawIOBase objects that are readable and
1112 writeable respectively. If the buffer_size is omitted it defaults to
1113 DEFAULT_BUFFER_SIZE.
1116 # XXX The usefulness of this (compared to having two separate IO
1117 # objects) is questionable.
1119 def __init__(self
, reader
, writer
,
1120 buffer_size
=DEFAULT_BUFFER_SIZE
, max_buffer_size
=None):
1123 The arguments are two RawIO instances.
1125 if max_buffer_size
is not None:
1126 warnings
.warn("max_buffer_size is deprecated", DeprecationWarning, 2)
1128 if not reader
.readable():
1129 raise IOError('"reader" argument must be readable.')
1131 if not writer
.writable():
1132 raise IOError('"writer" argument must be writable.')
1134 self
.reader
= BufferedReader(reader
, buffer_size
)
1135 self
.writer
= BufferedWriter(writer
, buffer_size
)
1137 def read(self
, n
=None):
1140 return self
.reader
.read(n
)
1142 def readinto(self
, b
):
1143 return self
.reader
.readinto(b
)
1146 return self
.writer
.write(b
)
1148 def peek(self
, n
=0):
1149 return self
.reader
.peek(n
)
1152 return self
.reader
.read1(n
)
1155 return self
.reader
.readable()
1158 return self
.writer
.writable()
1161 return self
.writer
.flush()
1168 return self
.reader
.isatty() or self
.writer
.isatty()
1172 return self
.writer
.closed
1175 class BufferedRandom(BufferedWriter
, BufferedReader
):
1177 """A buffered interface to random access streams.
1179 The constructor creates a reader and writer for a seekable stream,
1180 raw, given in the first argument. If the buffer_size is omitted it
1181 defaults to DEFAULT_BUFFER_SIZE.
1184 _warning_stack_offset
= 3
1186 def __init__(self
, raw
,
1187 buffer_size
=DEFAULT_BUFFER_SIZE
, max_buffer_size
=None):
1188 raw
._checkSeekable
()
1189 BufferedReader
.__init
__(self
, raw
, buffer_size
)
1190 BufferedWriter
.__init
__(self
, raw
, buffer_size
, max_buffer_size
)
1192 def seek(self
, pos
, whence
=0):
1193 if not (0 <= whence
<= 2):
1194 raise ValueError("invalid whence")
1198 with self
._read
_lock
:
1199 self
.raw
.seek(self
._read
_pos
- len(self
._read
_buf
), 1)
1200 # First do the raw seek, then empty the read buffer, so that
1201 # if the raw seek fails, we don't lose buffered data forever.
1202 pos
= self
.raw
.seek(pos
, whence
)
1203 with self
._read
_lock
:
1204 self
._reset
_read
_buf
()
1206 raise IOError("seek() returned invalid position")
1211 return BufferedWriter
.tell(self
)
1213 return BufferedReader
.tell(self
)
1215 def truncate(self
, pos
=None):
1218 # Use seek to flush the read buffer.
1220 return BufferedWriter
.truncate(self
)
1222 def read(self
, n
=None):
1226 return BufferedReader
.read(self
, n
)
1228 def readinto(self
, b
):
1230 return BufferedReader
.readinto(self
, b
)
1232 def peek(self
, n
=0):
1234 return BufferedReader
.peek(self
, n
)
1238 return BufferedReader
.read1(self
, n
)
1243 with self
._read
_lock
:
1244 self
.raw
.seek(self
._read
_pos
- len(self
._read
_buf
), 1)
1245 self
._reset
_read
_buf
()
1246 return BufferedWriter
.write(self
, b
)
1249 class TextIOBase(IOBase
):
1251 """Base class for text I/O.
1253 This class provides a character and line based interface to stream
1254 I/O. There is no readinto method because Python's character strings
1255 are immutable. There is no public constructor.
1258 def read(self
, n
=-1):
1259 """Read at most n characters from stream.
1261 Read from underlying buffer until we have n characters or we hit EOF.
1262 If n is negative or omitted, read until EOF.
1264 self
._unsupported
("read")
1267 """Write string s to stream."""
1268 self
._unsupported
("write")
1270 def truncate(self
, pos
=None):
1271 """Truncate size to pos."""
1272 self
._unsupported
("truncate")
1275 """Read until newline or EOF.
1277 Returns an empty string if EOF is hit immediately.
1279 self
._unsupported
("readline")
1283 Separate the underlying buffer from the TextIOBase and return it.
1285 After the underlying buffer has been detached, the TextIO is in an
1288 self
._unsupported
("detach")
1292 """Subclasses should override."""
1297 """Line endings translated so far.
1299 Only line endings translated during reading are considered.
1301 Subclasses should override.
1307 """Error setting of the decoder or encoder.
1309 Subclasses should override."""
1312 io
.TextIOBase
.register(TextIOBase
)
1315 class IncrementalNewlineDecoder(codecs
.IncrementalDecoder
):
1316 r
"""Codec used when reading a file in universal newlines mode. It wraps
1317 another incremental decoder, translating \r\n and \r into \n. It also
1318 records the types of newlines encountered. When used with
1319 translate=False, it ensures that the newline sequence is returned in
1322 def __init__(self
, decoder
, translate
, errors
='strict'):
1323 codecs
.IncrementalDecoder
.__init
__(self
, errors
=errors
)
1324 self
.translate
= translate
1325 self
.decoder
= decoder
1327 self
.pendingcr
= False
1329 def decode(self
, input, final
=False):
1330 # decode input (with the eventual \r from a previous pass)
1331 if self
.decoder
is None:
1334 output
= self
.decoder
.decode(input, final
=final
)
1335 if self
.pendingcr
and (output
or final
):
1336 output
= "\r" + output
1337 self
.pendingcr
= False
1339 # retain last \r even when not translating data:
1340 # then readline() is sure to get \r\n in one pass
1341 if output
.endswith("\r") and not final
:
1342 output
= output
[:-1]
1343 self
.pendingcr
= True
1345 # Record which newlines are read
1346 crlf
= output
.count('\r\n')
1347 cr
= output
.count('\r') - crlf
1348 lf
= output
.count('\n') - crlf
1349 self
.seennl |
= (lf
and self
._LF
) |
(cr
and self
._CR
) \
1350 |
(crlf
and self
._CRLF
)
1354 output
= output
.replace("\r\n", "\n")
1356 output
= output
.replace("\r", "\n")
1361 if self
.decoder
is None:
1365 buf
, flag
= self
.decoder
.getstate()
1371 def setstate(self
, state
):
1373 self
.pendingcr
= bool(flag
& 1)
1374 if self
.decoder
is not None:
1375 self
.decoder
.setstate((buf
, flag
>> 1))
1379 self
.pendingcr
= False
1380 if self
.decoder
is not None:
1381 self
.decoder
.reset()
1396 ("\r", "\n", "\r\n")
1400 class TextIOWrapper(TextIOBase
):
1402 r
"""Character and line based layer over a BufferedIOBase object, buffer.
1404 encoding gives the name of the encoding that the stream will be
1405 decoded or encoded with. It defaults to locale.getpreferredencoding.
1407 errors determines the strictness of encoding and decoding (see the
1408 codecs.register) and defaults to "strict".
1410 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1411 handling of line endings. If it is None, universal newlines is
1412 enabled. With this enabled, on input, the lines endings '\n', '\r',
1413 or '\r\n' are translated to '\n' before being returned to the
1414 caller. Conversely, on output, '\n' is translated to the system
1415 default line seperator, os.linesep. If newline is any other of its
1416 legal values, that newline becomes the newline when the file is read
1417 and it is returned untranslated. On output, '\n' is converted to the
1420 If line_buffering is True, a call to flush is implied when a call to
1421 write contains a newline character.
1426 def __init__(self
, buffer, encoding
=None, errors
=None, newline
=None,
1427 line_buffering
=False):
1428 if newline
is not None and not isinstance(newline
, basestring
):
1429 raise TypeError("illegal newline type: %r" % (type(newline
),))
1430 if newline
not in (None, "", "\n", "\r", "\r\n"):
1431 raise ValueError("illegal newline value: %r" % (newline
,))
1432 if encoding
is None:
1434 encoding
= os
.device_encoding(buffer.fileno())
1435 except (AttributeError, UnsupportedOperation
):
1437 if encoding
is None:
1441 # Importing locale may fail if Python is being built
1444 encoding
= locale
.getpreferredencoding()
1446 if not isinstance(encoding
, basestring
):
1447 raise ValueError("invalid encoding: %r" % encoding
)
1452 if not isinstance(errors
, basestring
):
1453 raise ValueError("invalid errors: %r" % errors
)
1455 self
.buffer = buffer
1456 self
._line
_buffering
= line_buffering
1457 self
._encoding
= encoding
1458 self
._errors
= errors
1459 self
._readuniversal
= not newline
1460 self
._readtranslate
= newline
is None
1461 self
._readnl
= newline
1462 self
._writetranslate
= newline
!= ''
1463 self
._writenl
= newline
or os
.linesep
1464 self
._encoder
= None
1465 self
._decoder
= None
1466 self
._decoded
_chars
= '' # buffer for text returned from decoder
1467 self
._decoded
_chars
_used
= 0 # offset into _decoded_chars for read()
1468 self
._snapshot
= None # info for reconstructing decoder state
1469 self
._seekable
= self
._telling
= self
.buffer.seekable()
1471 if self
._seekable
and self
.writable():
1472 position
= self
.buffer.tell()
1475 self
._get
_encoder
().setstate(0)
1477 # Sometimes the encoder doesn't exist
1480 # self._snapshot is either None, or a tuple (dec_flags, next_input)
1481 # where dec_flags is the second (integer) item of the decoder state
1482 # and next_input is the chunk of input bytes that comes next after the
1483 # snapshot point. We use this to reconstruct decoder states in tell().
1485 # Naming convention:
1486 # - "bytes_..." for integer variables that count input bytes
1487 # - "chars_..." for integer variables that count decoded characters
1492 except AttributeError:
1493 return "<_pyio.TextIOWrapper encoding='{0}'>".format(self
.encoding
)
1495 return "<_pyio.TextIOWrapper name={0!r} encoding='{1}'>".format(
1496 name
, self
.encoding
)
1500 return self
._encoding
1507 def line_buffering(self
):
1508 return self
._line
_buffering
1511 return self
._seekable
1514 return self
.buffer.readable()
1517 return self
.buffer.writable()
1521 self
._telling
= self
._seekable
1524 if self
.buffer is not None:
1528 pass # If flush() fails, just give up
1533 return self
.buffer.closed
1537 return self
.buffer.name
1540 return self
.buffer.fileno()
1543 return self
.buffer.isatty()
1547 raise ValueError("write to closed file")
1548 if not isinstance(s
, unicode):
1549 raise TypeError("can't write %s to text stream" %
1550 s
.__class
__.__name
__)
1552 haslf
= (self
._writetranslate
or self
._line
_buffering
) and "\n" in s
1553 if haslf
and self
._writetranslate
and self
._writenl
!= "\n":
1554 s
= s
.replace("\n", self
._writenl
)
1555 encoder
= self
._encoder
or self
._get
_encoder
()
1556 # XXX What if we were just reading?
1557 b
= encoder
.encode(s
)
1558 self
.buffer.write(b
)
1559 if self
._line
_buffering
and (haslf
or "\r" in s
):
1561 self
._snapshot
= None
1563 self
._decoder
.reset()
1566 def _get_encoder(self
):
1567 make_encoder
= codecs
.getincrementalencoder(self
._encoding
)
1568 self
._encoder
= make_encoder(self
._errors
)
1569 return self
._encoder
1571 def _get_decoder(self
):
1572 make_decoder
= codecs
.getincrementaldecoder(self
._encoding
)
1573 decoder
= make_decoder(self
._errors
)
1574 if self
._readuniversal
:
1575 decoder
= IncrementalNewlineDecoder(decoder
, self
._readtranslate
)
1576 self
._decoder
= decoder
1579 # The following three methods implement an ADT for _decoded_chars.
1580 # Text returned from the decoder is buffered here until the client
1581 # requests it by calling our read() or readline() method.
1582 def _set_decoded_chars(self
, chars
):
1583 """Set the _decoded_chars buffer."""
1584 self
._decoded
_chars
= chars
1585 self
._decoded
_chars
_used
= 0
1587 def _get_decoded_chars(self
, n
=None):
1588 """Advance into the _decoded_chars buffer."""
1589 offset
= self
._decoded
_chars
_used
1591 chars
= self
._decoded
_chars
[offset
:]
1593 chars
= self
._decoded
_chars
[offset
:offset
+ n
]
1594 self
._decoded
_chars
_used
+= len(chars
)
1597 def _rewind_decoded_chars(self
, n
):
1598 """Rewind the _decoded_chars buffer."""
1599 if self
._decoded
_chars
_used
< n
:
1600 raise AssertionError("rewind decoded_chars out of bounds")
1601 self
._decoded
_chars
_used
-= n
1603 def _read_chunk(self
):
1605 Read and decode the next chunk of data from the BufferedReader.
1608 # The return value is True unless EOF was reached. The decoded
1609 # string is placed in self._decoded_chars (replacing its previous
1610 # value). The entire input chunk is sent to the decoder, though
1611 # some of it may remain buffered in the decoder, yet to be
1614 if self
._decoder
is None:
1615 raise ValueError("no decoder")
1618 # To prepare for tell(), we need to snapshot a point in the
1619 # file where the decoder's input buffer is empty.
1621 dec_buffer
, dec_flags
= self
._decoder
.getstate()
1622 # Given this, we know there was a valid snapshot point
1623 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1625 # Read a chunk, decode it, and put the result in self._decoded_chars.
1626 input_chunk
= self
.buffer.read1(self
._CHUNK
_SIZE
)
1627 eof
= not input_chunk
1628 self
._set
_decoded
_chars
(self
._decoder
.decode(input_chunk
, eof
))
1631 # At the snapshot point, len(dec_buffer) bytes before the read,
1632 # the next input to be decoded is dec_buffer + input_chunk.
1633 self
._snapshot
= (dec_flags
, dec_buffer
+ input_chunk
)
1637 def _pack_cookie(self
, position
, dec_flags
=0,
1638 bytes_to_feed
=0, need_eof
=0, chars_to_skip
=0):
1639 # The meaning of a tell() cookie is: seek to position, set the
1640 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
1641 # into the decoder with need_eof as the EOF flag, then skip
1642 # chars_to_skip characters of the decoded result. For most simple
1643 # decoders, tell() will often just give a byte offset in the file.
1644 return (position |
(dec_flags
<<64) |
(bytes_to_feed
<<128) |
1645 (chars_to_skip
<<192) |
bool(need_eof
)<<256)
1647 def _unpack_cookie(self
, bigint
):
1648 rest
, position
= divmod(bigint
, 1<<64)
1649 rest
, dec_flags
= divmod(rest
, 1<<64)
1650 rest
, bytes_to_feed
= divmod(rest
, 1<<64)
1651 need_eof
, chars_to_skip
= divmod(rest
, 1<<64)
1652 return position
, dec_flags
, bytes_to_feed
, need_eof
, chars_to_skip
1655 if not self
._seekable
:
1656 raise IOError("underlying stream is not seekable")
1657 if not self
._telling
:
1658 raise IOError("telling position disabled by next() call")
1660 position
= self
.buffer.tell()
1661 decoder
= self
._decoder
1662 if decoder
is None or self
._snapshot
is None:
1663 if self
._decoded
_chars
:
1664 # This should never happen.
1665 raise AssertionError("pending decoded text")
1668 # Skip backward to the snapshot point (see _read_chunk).
1669 dec_flags
, next_input
= self
._snapshot
1670 position
-= len(next_input
)
1672 # How many decoded characters have been used up since the snapshot?
1673 chars_to_skip
= self
._decoded
_chars
_used
1674 if chars_to_skip
== 0:
1675 # We haven't moved from the snapshot point.
1676 return self
._pack
_cookie
(position
, dec_flags
)
1678 # Starting from the snapshot position, we will walk the decoder
1679 # forward until it gives us enough decoded characters.
1680 saved_state
= decoder
.getstate()
1682 # Note our initial start point.
1683 decoder
.setstate((b
'', dec_flags
))
1684 start_pos
= position
1685 start_flags
, bytes_fed
, chars_decoded
= dec_flags
, 0, 0
1688 # Feed the decoder one byte at a time. As we go, note the
1689 # nearest "safe start point" before the current location
1690 # (a point where the decoder has nothing buffered, so seek()
1691 # can safely start from there and advance to this location).
1692 for next_byte
in next_input
:
1694 chars_decoded
+= len(decoder
.decode(next_byte
))
1695 dec_buffer
, dec_flags
= decoder
.getstate()
1696 if not dec_buffer
and chars_decoded
<= chars_to_skip
:
1697 # Decoder buffer is empty, so this is a safe start point.
1698 start_pos
+= bytes_fed
1699 chars_to_skip
-= chars_decoded
1700 start_flags
, bytes_fed
, chars_decoded
= dec_flags
, 0, 0
1701 if chars_decoded
>= chars_to_skip
:
1704 # We didn't get enough decoded data; signal EOF to get more.
1705 chars_decoded
+= len(decoder
.decode(b
'', final
=True))
1707 if chars_decoded
< chars_to_skip
:
1708 raise IOError("can't reconstruct logical file position")
1710 # The returned cookie corresponds to the last safe start point.
1711 return self
._pack
_cookie
(
1712 start_pos
, start_flags
, bytes_fed
, need_eof
, chars_to_skip
)
1714 decoder
.setstate(saved_state
)
1716 def truncate(self
, pos
=None):
1721 return self
.buffer.truncate()
1724 if self
.buffer is None:
1725 raise ValueError("buffer is already detached")
1727 buffer = self
.buffer
1731 def seek(self
, cookie
, whence
=0):
1733 raise ValueError("tell on closed file")
1734 if not self
._seekable
:
1735 raise IOError("underlying stream is not seekable")
1736 if whence
== 1: # seek relative to current position
1738 raise IOError("can't do nonzero cur-relative seeks")
1739 # Seeking to the current position should attempt to
1740 # sync the underlying buffer with the current position.
1742 cookie
= self
.tell()
1743 if whence
== 2: # seek relative to end of file
1745 raise IOError("can't do nonzero end-relative seeks")
1747 position
= self
.buffer.seek(0, 2)
1748 self
._set
_decoded
_chars
('')
1749 self
._snapshot
= None
1751 self
._decoder
.reset()
1754 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" %
1757 raise ValueError("negative seek position %r" % (cookie
,))
1760 # The strategy of seek() is to go back to the safe start point
1761 # and replay the effect of read(chars_to_skip) from there.
1762 start_pos
, dec_flags
, bytes_to_feed
, need_eof
, chars_to_skip
= \
1763 self
._unpack
_cookie
(cookie
)
1765 # Seek back to the safe start point.
1766 self
.buffer.seek(start_pos
)
1767 self
._set
_decoded
_chars
('')
1768 self
._snapshot
= None
1770 # Restore the decoder to its state from the safe start point.
1771 if cookie
== 0 and self
._decoder
:
1772 self
._decoder
.reset()
1773 elif self
._decoder
or dec_flags
or chars_to_skip
:
1774 self
._decoder
= self
._decoder
or self
._get
_decoder
()
1775 self
._decoder
.setstate((b
'', dec_flags
))
1776 self
._snapshot
= (dec_flags
, b
'')
1779 # Just like _read_chunk, feed the decoder and save a snapshot.
1780 input_chunk
= self
.buffer.read(bytes_to_feed
)
1781 self
._set
_decoded
_chars
(
1782 self
._decoder
.decode(input_chunk
, need_eof
))
1783 self
._snapshot
= (dec_flags
, input_chunk
)
1785 # Skip chars_to_skip of the decoded characters.
1786 if len(self
._decoded
_chars
) < chars_to_skip
:
1787 raise IOError("can't restore logical file position")
1788 self
._decoded
_chars
_used
= chars_to_skip
1790 # Finally, reset the encoder (merely useful for proper BOM handling)
1792 encoder
= self
._encoder
or self
._get
_encoder
()
1794 # Sometimes the encoder doesn't exist
1803 def read(self
, n
=None):
1804 self
._checkReadable
()
1807 decoder
= self
._decoder
or self
._get
_decoder
()
1810 result
= (self
._get
_decoded
_chars
() +
1811 decoder
.decode(self
.buffer.read(), final
=True))
1812 self
._set
_decoded
_chars
('')
1813 self
._snapshot
= None
1816 # Keep reading chunks until we have n characters to return.
1818 result
= self
._get
_decoded
_chars
(n
)
1819 while len(result
) < n
and not eof
:
1820 eof
= not self
._read
_chunk
()
1821 result
+= self
._get
_decoded
_chars
(n
- len(result
))
1825 self
._telling
= False
1826 line
= self
.readline()
1828 self
._snapshot
= None
1829 self
._telling
= self
._seekable
1833 def readline(self
, limit
=None):
1835 raise ValueError("read from closed file")
1838 elif not isinstance(limit
, (int, long)):
1839 raise TypeError("limit must be an integer")
1841 # Grab all the decoded text (we will rewind any extra bits later).
1842 line
= self
._get
_decoded
_chars
()
1845 # Make the decoder if it doesn't already exist.
1846 if not self
._decoder
:
1851 if self
._readtranslate
:
1852 # Newlines are already translated, only search for \n
1853 pos
= line
.find('\n', start
)
1860 elif self
._readuniversal
:
1861 # Universal newline search. Find any of \r, \r\n, \n
1862 # The decoder ensures that \r\n are not split in two pieces
1864 # In C we'd look for these in parallel of course.
1865 nlpos
= line
.find("\n", start
)
1866 crpos
= line
.find("\r", start
)
1883 elif nlpos
== crpos
+ 1:
1893 pos
= line
.find(self
._readnl
)
1895 endpos
= pos
+ len(self
._readnl
)
1898 if limit
>= 0 and len(line
) >= limit
:
1899 endpos
= limit
# reached length limit
1902 # No line ending seen yet - get more data'
1903 while self
._read
_chunk
():
1904 if self
._decoded
_chars
:
1906 if self
._decoded
_chars
:
1907 line
+= self
._get
_decoded
_chars
()
1910 self
._set
_decoded
_chars
('')
1911 self
._snapshot
= None
1914 if limit
>= 0 and endpos
> limit
:
1915 endpos
= limit
# don't exceed limit
1917 # Rewind _decoded_chars to just after the line ending we found.
1918 self
._rewind
_decoded
_chars
(len(line
) - endpos
)
1919 return line
[:endpos
]
1923 return self
._decoder
.newlines
if self
._decoder
else None
1926 class StringIO(TextIOWrapper
):
1927 """Text I/O implementation using an in-memory buffer.
1929 The initial_value argument sets the value of object. The newline
1930 argument is like the one of TextIOWrapper's constructor.
1933 def __init__(self
, initial_value
="", newline
="\n"):
1934 super(StringIO
, self
).__init
__(BytesIO(),
1938 # Issue #5645: make universal newlines semantics the same as in the
1939 # C version, even under Windows.
1941 self
._writetranslate
= False
1943 if not isinstance(initial_value
, unicode):
1944 initial_value
= unicode(initial_value
)
1945 self
.write(initial_value
)
1950 return self
.buffer.getvalue().decode(self
._encoding
, self
._errors
)
1953 # TextIOWrapper tells the encoding in its repr. In StringIO,
1954 # that's a implementation detail.
1955 return object.__repr
__(self
)
1966 # This doesn't make sense on StringIO.
1967 self
._unsupported
("detach")