Mention Giampolo R's new FTP TLS support in the what's new file
[python.git] / Lib / _pyio.py
blobb0da0458e00e369d505e7150441f293488b13454
1 """
2 Python implementation of the io module.
3 """
5 from __future__ import print_function
6 from __future__ import unicode_literals
8 import os
9 import abc
10 import codecs
11 import warnings
12 # Import _thread instead of threading to reduce startup cost
13 try:
14 from thread import allocate_lock as Lock
15 except ImportError:
16 from dummy_thread import allocate_lock as Lock
18 import io
19 from io import __all__
20 from io import SEEK_SET, SEEK_CUR, SEEK_END
22 __metaclass__ = type
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
63 modes are:
65 ========= ===============================================================
66 Character Meaning
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
71 'b' binary mode
72 't' text mode (default)
73 '+' open a disk file for updating (reading and writing)
74 'U' universal newline mode (for backwards compatibility; unneeded
75 for new code)
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
93 for full buffering.
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
110 follows:
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
124 the given string.
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
137 a BufferedRandom.
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)
154 modes = set(mode)
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
161 text = "t" in modes
162 binary = "b" in modes
163 if "U" in modes:
164 if writing or appending:
165 raise ValueError("can't use U and writing mode at once")
166 reading = True
167 if text and binary:
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")
179 raw = FileIO(file,
180 (reading and "r" or "") +
181 (writing and "w" or "") +
182 (appending and "a" or "") +
183 (updating and "+" or ""),
184 closefd)
185 if buffering is None:
186 buffering = -1
187 line_buffering = False
188 if buffering == 1 or buffering < 0 and raw.isatty():
189 buffering = -1
190 line_buffering = True
191 if buffering < 0:
192 buffering = DEFAULT_BUFFER_SIZE
193 try:
194 bs = os.fstat(raw.fileno()).st_blksize
195 except (os.error, AttributeError):
196 pass
197 else:
198 if bs > 1:
199 buffering = bs
200 if buffering < 0:
201 raise ValueError("invalid buffering size")
202 if buffering == 0:
203 if binary:
204 return raw
205 raise ValueError("can't have unbuffered text I/O")
206 if updating:
207 buffer = BufferedRandom(raw, buffering)
208 elif writing or appending:
209 buffer = BufferedWriter(raw, buffering)
210 elif reading:
211 buffer = BufferedReader(raw, buffering)
212 else:
213 raise ValueError("unknown mode: %r" % mode)
214 if binary:
215 return buffer
216 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
217 text.mode = mode
218 return text
221 class DocDescriptor:
222 """Helper for builtins.open.__doc__
224 def __get__(self, obj, typ):
225 return (
226 "open(file, mode='r', buffering=None, encoding=None, "
227 "errors=None, newline=None, closefd=True)\n\n" +
228 open.__doc__)
230 class OpenWrapper:
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):
245 pass
248 class IOBase:
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
272 stream.
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!')
281 ### Internal ###
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))
288 ### Positioning ###
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
295 for whence are:
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")
305 def tell(self):
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
313 the new size.
315 self._unsupported("truncate")
317 ### Flush and close ###
319 def flush(self):
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???
326 __closed = False
328 def close(self):
329 """Flush and close the IO object.
331 This method has no effect if the file is already closed.
333 if not self.__closed:
334 try:
335 self.flush()
336 except IOError:
337 pass # If flush() fails, just give up
338 self.__closed = True
340 def __del__(self):
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.
347 try:
348 self.close()
349 except:
350 pass
352 ### Inquiries ###
354 def seekable(self):
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().
360 return False
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)
370 def readable(self):
371 """Return whether object was opened for reading.
373 If False, read() will raise IOError.
375 return False
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)
384 def writable(self):
385 """Return whether object was opened for writing.
387 If False, write() and truncate() will raise IOError.
389 return False
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)
398 @property
399 def closed(self):
400 """closed: bool. True iff the file has been closed.
402 For backwards compatibility, this is a property, not a predicate.
404 return self.__closed
406 def _checkClosed(self, msg=None):
407 """Internal: raise an ValueError if file is closed
409 if self.closed:
410 raise ValueError("I/O operation on closed file."
411 if msg is None else msg)
413 ### Context manager ###
415 def __enter__(self):
416 """Context management protocol. Returns self."""
417 self._checkClosed()
418 return self
420 def __exit__(self, *args):
421 """Context management protocol. Calls close()"""
422 self.close()
424 ### Lower-level APIs ###
426 # XXX Should these be present even if unimplemented?
428 def fileno(self):
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")
435 def isatty(self):
436 """Return whether this is an 'interactive' stream.
438 Return False if it can't be determined.
440 self._checkClosed()
441 return False
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"):
456 def nreadahead():
457 readahead = self.peek(1)
458 if not readahead:
459 return 1
460 n = (readahead.find(b"\n") + 1) or len(readahead)
461 if limit >= 0:
462 n = min(n, limit)
463 return n
464 else:
465 def nreadahead():
466 return 1
467 if limit is None:
468 limit = -1
469 elif not isinstance(limit, (int, long)):
470 raise TypeError("limit must be an integer")
471 res = bytearray()
472 while limit < 0 or len(res) < limit:
473 b = self.read(nreadahead())
474 if not b:
475 break
476 res += b
477 if res.endswith(b"\n"):
478 break
479 return bytes(res)
481 def __iter__(self):
482 self._checkClosed()
483 return self
485 def next(self):
486 line = self.readline()
487 if not line:
488 raise StopIteration
489 return line
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:
501 return list(self)
502 n = 0
503 lines = []
504 for line in self:
505 lines.append(line)
506 n += len(line)
507 if n >= hint:
508 break
509 return lines
511 def writelines(self, lines):
512 self._checkClosed()
513 for line in lines:
514 self.write(line)
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.
539 if n is None:
540 n = -1
541 if n < 0:
542 return self.readall()
543 b = bytearray(n.__index__())
544 n = self.readinto(b)
545 del b[n:]
546 return bytes(b)
548 def readall(self):
549 """Read until EOF, using multiple read() call."""
550 res = bytearray()
551 while True:
552 data = self.read(DEFAULT_BUFFER_SIZE)
553 if not data:
554 break
555 res += data
556 return bytes(res)
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")
566 def write(self, b):
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
589 return None.
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
606 EOF is imminent.
608 Returns an empty bytes array on EOF.
610 Raises BlockingIOError if the underlying raw stream has no
611 data at the moment.
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
628 data at the moment.
630 # XXX This ought to work with anything that supports the buffer API
631 data = self.read(len(b))
632 n = len(data)
633 try:
634 b[:n] = data
635 except TypeError as err:
636 import array
637 if not isinstance(b, array.array):
638 raise err
639 b[:n] = array.array(b'b', data)
640 return n
642 def write(self, b):
643 """Write the given buffer to the IO stream.
645 Return the number of bytes written, which is never less than
646 len(b).
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")
653 def detach(self):
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
658 state.
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
671 write().
674 def __init__(self, raw):
675 self.raw = raw
677 ### Positioning ###
679 def seek(self, pos, whence=0):
680 new_position = self.raw.seek(pos, whence)
681 if new_position < 0:
682 raise IOError("seek() returned an invalid position")
683 return new_position
685 def tell(self):
686 pos = self.raw.tell()
687 if pos < 0:
688 raise IOError("tell() returned an invalid position")
689 return pos
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
694 # file state.
695 self.flush()
697 if pos is None:
698 pos = self.tell()
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 ###
705 def flush(self):
706 self.raw.flush()
708 def close(self):
709 if not self.closed and self.raw is not None:
710 try:
711 self.flush()
712 except IOError:
713 pass # If flush() fails, just give up
714 self.raw.close()
716 def detach(self):
717 if self.raw is None:
718 raise ValueError("raw stream already detached")
719 self.flush()
720 raw = self.raw
721 self.raw = None
722 return raw
724 ### Inquiries ###
726 def seekable(self):
727 return self.raw.seekable()
729 def readable(self):
730 return self.raw.readable()
732 def writable(self):
733 return self.raw.writable()
735 @property
736 def closed(self):
737 return self.raw.closed
739 @property
740 def name(self):
741 return self.raw.name
743 @property
744 def mode(self):
745 return self.raw.mode
747 def __repr__(self):
748 clsname = self.__class__.__name__
749 try:
750 name = self.name
751 except AttributeError:
752 return "<_pyio.{0}>".format(clsname)
753 else:
754 return "<_pyio.{0} name={1!r}>".format(clsname, name)
756 ### Lower-level APIs ###
758 def fileno(self):
759 return self.raw.fileno()
761 def isatty(self):
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):
770 buf = bytearray()
771 if initial_bytes is not None:
772 buf.extend(initial_bytes)
773 self._buffer = buf
774 self._pos = 0
776 def __getstate__(self):
777 if self.closed:
778 raise ValueError("__getstate__ on closed file")
779 return self.__dict__.copy()
781 def getvalue(self):
782 """Return the bytes value (contents) of the buffer
784 if self.closed:
785 raise ValueError("getvalue on closed file")
786 return bytes(self._buffer)
788 def read(self, n=None):
789 if self.closed:
790 raise ValueError("read from closed file")
791 if n is None:
792 n = -1
793 if not isinstance(n, (int, long)):
794 raise TypeError("integer argument expected, got {0!r}".format(
795 type(n)))
796 if n < 0:
797 n = len(self._buffer)
798 if len(self._buffer) <= self._pos:
799 return b""
800 newpos = min(len(self._buffer), self._pos + n)
801 b = self._buffer[self._pos : newpos]
802 self._pos = newpos
803 return bytes(b)
805 def read1(self, n):
806 """This is the same as read.
808 return self.read(n)
810 def write(self, b):
811 if self.closed:
812 raise ValueError("write to closed file")
813 if isinstance(b, unicode):
814 raise TypeError("can't write unicode to binary stream")
815 n = len(b)
816 if n == 0:
817 return 0
818 pos = self._pos
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
825 self._pos += n
826 return n
828 def seek(self, pos, whence=0):
829 if self.closed:
830 raise ValueError("seek on closed file")
831 try:
832 pos = pos.__index__()
833 except AttributeError as err:
834 raise TypeError("an integer is required")
835 if whence == 0:
836 if pos < 0:
837 raise ValueError("negative seek position %r" % (pos,))
838 self._pos = pos
839 elif whence == 1:
840 self._pos = max(0, self._pos + pos)
841 elif whence == 2:
842 self._pos = max(0, len(self._buffer) + pos)
843 else:
844 raise ValueError("invalid whence value")
845 return self._pos
847 def tell(self):
848 if self.closed:
849 raise ValueError("tell on closed file")
850 return self._pos
852 def truncate(self, pos=None):
853 if self.closed:
854 raise ValueError("truncate on closed file")
855 if pos is None:
856 pos = self._pos
857 elif pos < 0:
858 raise ValueError("negative truncate position %r" % (pos,))
859 del self._buffer[pos:]
860 return self.seek(pos)
862 def readable(self):
863 return True
865 def writable(self):
866 return True
868 def seekable(self):
869 return True
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
880 is used.
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)
890 if buffer_size <= 0:
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):
897 self._read_buf = b""
898 self._read_pos = 0
900 def read(self, n=None):
901 """Read n bytes.
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
906 block.
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):
914 nodata_val = b""
915 empty_values = (b"", None)
916 buf = self._read_buf
917 pos = self._read_pos
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.
923 current_size = 0
924 while True:
925 # Read until EOF or until read() would block.
926 chunk = self.raw.read()
927 if chunk in empty_values:
928 nodata_val = chunk
929 break
930 current_size += len(chunk)
931 chunks.append(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.
936 if n <= avail:
937 # Fast path: the data to read is fully buffered.
938 self._read_pos += n
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.
942 chunks = [buf[pos:]]
943 wanted = max(self.buffer_size, n)
944 while avail < n:
945 chunk = self.raw.read(wanted)
946 if chunk in empty_values:
947 nodata_val = chunk
948 break
949 avail += len(chunk)
950 chunks.append(chunk)
951 # n is more then avail only when an EOF occurred or when
952 # read() would have blocked.
953 n = min(n, avail)
954 out = b"".join(chunks)
955 self._read_buf = out[n:] # Save the extra data in the buffer.
956 self._read_pos = 0
957 return out[:n] if out else nodata_val
959 def peek(self, n=0):
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)
975 if current:
976 self._read_buf = self._read_buf[self._read_pos:] + current
977 self._read_pos = 0
978 return self._read_buf[self._read_pos:]
980 def read1(self, n):
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.
984 if n < 0:
985 raise ValueError("number of bytes to read must be positive")
986 if n == 0:
987 return b""
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))
993 def tell(self):
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:
1000 if whence == 1:
1001 pos -= len(self._read_buf) - self._read_pos
1002 pos = _BufferedIOMixin.seek(self, pos, whence)
1003 self._reset_read_buf()
1004 return pos
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()
1032 def write(self, b):
1033 if self.closed:
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
1039 # partial writes
1040 if len(self._write_buf) > self.buffer_size:
1041 # We're full, so let's pre-flush the buffer
1042 try:
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:
1052 try:
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
1059 written -= overage
1060 self._write_buf = self._write_buf[:self.buffer_size]
1061 raise BlockingIOError(e.errno, e.strerror, written)
1062 return written
1064 def truncate(self, pos=None):
1065 with self._write_lock:
1066 self._flush_unlocked()
1067 if pos is None:
1068 pos = self.raw.tell()
1069 return self.raw.truncate(pos)
1071 def flush(self):
1072 with self._write_lock:
1073 self._flush_unlocked()
1075 def _flush_unlocked(self):
1076 if self.closed:
1077 raise ValueError("flush of closed file")
1078 written = 0
1079 try:
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]
1085 written += n
1086 except BlockingIOError as e:
1087 n = e.characters_written
1088 del self._write_buf[:n]
1089 written += n
1090 raise BlockingIOError(e.errno, e.strerror, written)
1092 def tell(self):
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):
1121 """Constructor.
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):
1138 if n is None:
1139 n = -1
1140 return self.reader.read(n)
1142 def readinto(self, b):
1143 return self.reader.readinto(b)
1145 def write(self, b):
1146 return self.writer.write(b)
1148 def peek(self, n=0):
1149 return self.reader.peek(n)
1151 def read1(self, n):
1152 return self.reader.read1(n)
1154 def readable(self):
1155 return self.reader.readable()
1157 def writable(self):
1158 return self.writer.writable()
1160 def flush(self):
1161 return self.writer.flush()
1163 def close(self):
1164 self.writer.close()
1165 self.reader.close()
1167 def isatty(self):
1168 return self.reader.isatty() or self.writer.isatty()
1170 @property
1171 def closed(self):
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")
1195 self.flush()
1196 if self._read_buf:
1197 # Undo read ahead.
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()
1205 if pos < 0:
1206 raise IOError("seek() returned invalid position")
1207 return pos
1209 def tell(self):
1210 if self._write_buf:
1211 return BufferedWriter.tell(self)
1212 else:
1213 return BufferedReader.tell(self)
1215 def truncate(self, pos=None):
1216 if pos is None:
1217 pos = self.tell()
1218 # Use seek to flush the read buffer.
1219 self.seek(pos)
1220 return BufferedWriter.truncate(self)
1222 def read(self, n=None):
1223 if n is None:
1224 n = -1
1225 self.flush()
1226 return BufferedReader.read(self, n)
1228 def readinto(self, b):
1229 self.flush()
1230 return BufferedReader.readinto(self, b)
1232 def peek(self, n=0):
1233 self.flush()
1234 return BufferedReader.peek(self, n)
1236 def read1(self, n):
1237 self.flush()
1238 return BufferedReader.read1(self, n)
1240 def write(self, b):
1241 if self._read_buf:
1242 # Undo readahead
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")
1266 def write(self, s):
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")
1274 def readline(self):
1275 """Read until newline or EOF.
1277 Returns an empty string if EOF is hit immediately.
1279 self._unsupported("readline")
1281 def detach(self):
1283 Separate the underlying buffer from the TextIOBase and return it.
1285 After the underlying buffer has been detached, the TextIO is in an
1286 unusable state.
1288 self._unsupported("detach")
1290 @property
1291 def encoding(self):
1292 """Subclasses should override."""
1293 return None
1295 @property
1296 def newlines(self):
1297 """Line endings translated so far.
1299 Only line endings translated during reading are considered.
1301 Subclasses should override.
1303 return None
1305 @property
1306 def errors(self):
1307 """Error setting of the decoder or encoder.
1309 Subclasses should override."""
1310 return None
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
1320 one piece.
1322 def __init__(self, decoder, translate, errors='strict'):
1323 codecs.IncrementalDecoder.__init__(self, errors=errors)
1324 self.translate = translate
1325 self.decoder = decoder
1326 self.seennl = 0
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:
1332 output = input
1333 else:
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)
1352 if self.translate:
1353 if crlf:
1354 output = output.replace("\r\n", "\n")
1355 if cr:
1356 output = output.replace("\r", "\n")
1358 return output
1360 def getstate(self):
1361 if self.decoder is None:
1362 buf = b""
1363 flag = 0
1364 else:
1365 buf, flag = self.decoder.getstate()
1366 flag <<= 1
1367 if self.pendingcr:
1368 flag |= 1
1369 return buf, flag
1371 def setstate(self, state):
1372 buf, flag = state
1373 self.pendingcr = bool(flag & 1)
1374 if self.decoder is not None:
1375 self.decoder.setstate((buf, flag >> 1))
1377 def reset(self):
1378 self.seennl = 0
1379 self.pendingcr = False
1380 if self.decoder is not None:
1381 self.decoder.reset()
1383 _LF = 1
1384 _CR = 2
1385 _CRLF = 4
1387 @property
1388 def newlines(self):
1389 return (None,
1390 "\n",
1391 "\r",
1392 ("\r", "\n"),
1393 "\r\n",
1394 ("\n", "\r\n"),
1395 ("\r", "\r\n"),
1396 ("\r", "\n", "\r\n")
1397 )[self.seennl]
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
1418 newline.
1420 If line_buffering is True, a call to flush is implied when a call to
1421 write contains a newline character.
1424 _CHUNK_SIZE = 2048
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:
1433 try:
1434 encoding = os.device_encoding(buffer.fileno())
1435 except (AttributeError, UnsupportedOperation):
1436 pass
1437 if encoding is None:
1438 try:
1439 import locale
1440 except ImportError:
1441 # Importing locale may fail if Python is being built
1442 encoding = "ascii"
1443 else:
1444 encoding = locale.getpreferredencoding()
1446 if not isinstance(encoding, basestring):
1447 raise ValueError("invalid encoding: %r" % encoding)
1449 if errors is None:
1450 errors = "strict"
1451 else:
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()
1473 if position != 0:
1474 try:
1475 self._get_encoder().setstate(0)
1476 except LookupError:
1477 # Sometimes the encoder doesn't exist
1478 pass
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
1489 def __repr__(self):
1490 try:
1491 name = self.name
1492 except AttributeError:
1493 return "<_pyio.TextIOWrapper encoding='{0}'>".format(self.encoding)
1494 else:
1495 return "<_pyio.TextIOWrapper name={0!r} encoding='{1}'>".format(
1496 name, self.encoding)
1498 @property
1499 def encoding(self):
1500 return self._encoding
1502 @property
1503 def errors(self):
1504 return self._errors
1506 @property
1507 def line_buffering(self):
1508 return self._line_buffering
1510 def seekable(self):
1511 return self._seekable
1513 def readable(self):
1514 return self.buffer.readable()
1516 def writable(self):
1517 return self.buffer.writable()
1519 def flush(self):
1520 self.buffer.flush()
1521 self._telling = self._seekable
1523 def close(self):
1524 if self.buffer is not None:
1525 try:
1526 self.flush()
1527 except IOError:
1528 pass # If flush() fails, just give up
1529 self.buffer.close()
1531 @property
1532 def closed(self):
1533 return self.buffer.closed
1535 @property
1536 def name(self):
1537 return self.buffer.name
1539 def fileno(self):
1540 return self.buffer.fileno()
1542 def isatty(self):
1543 return self.buffer.isatty()
1545 def write(self, s):
1546 if self.closed:
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__)
1551 length = len(s)
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):
1560 self.flush()
1561 self._snapshot = None
1562 if self._decoder:
1563 self._decoder.reset()
1564 return length
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
1577 return 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
1590 if n is None:
1591 chars = self._decoded_chars[offset:]
1592 else:
1593 chars = self._decoded_chars[offset:offset + n]
1594 self._decoded_chars_used += len(chars)
1595 return 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
1612 # converted.
1614 if self._decoder is None:
1615 raise ValueError("no decoder")
1617 if self._telling:
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))
1630 if self._telling:
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)
1635 return not eof
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
1654 def tell(self):
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")
1659 self.flush()
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")
1666 return position
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()
1681 try:
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
1686 need_eof = 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:
1693 bytes_fed += 1
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:
1702 break
1703 else:
1704 # We didn't get enough decoded data; signal EOF to get more.
1705 chars_decoded += len(decoder.decode(b'', final=True))
1706 need_eof = 1
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)
1713 finally:
1714 decoder.setstate(saved_state)
1716 def truncate(self, pos=None):
1717 self.flush()
1718 if pos is None:
1719 pos = self.tell()
1720 self.seek(pos)
1721 return self.buffer.truncate()
1723 def detach(self):
1724 if self.buffer is None:
1725 raise ValueError("buffer is already detached")
1726 self.flush()
1727 buffer = self.buffer
1728 self.buffer = None
1729 return buffer
1731 def seek(self, cookie, whence=0):
1732 if self.closed:
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
1737 if cookie != 0:
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.
1741 whence = 0
1742 cookie = self.tell()
1743 if whence == 2: # seek relative to end of file
1744 if cookie != 0:
1745 raise IOError("can't do nonzero end-relative seeks")
1746 self.flush()
1747 position = self.buffer.seek(0, 2)
1748 self._set_decoded_chars('')
1749 self._snapshot = None
1750 if self._decoder:
1751 self._decoder.reset()
1752 return position
1753 if whence != 0:
1754 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" %
1755 (whence,))
1756 if cookie < 0:
1757 raise ValueError("negative seek position %r" % (cookie,))
1758 self.flush()
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'')
1778 if chars_to_skip:
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)
1791 try:
1792 encoder = self._encoder or self._get_encoder()
1793 except LookupError:
1794 # Sometimes the encoder doesn't exist
1795 pass
1796 else:
1797 if cookie != 0:
1798 encoder.setstate(0)
1799 else:
1800 encoder.reset()
1801 return cookie
1803 def read(self, n=None):
1804 self._checkReadable()
1805 if n is None:
1806 n = -1
1807 decoder = self._decoder or self._get_decoder()
1808 if n < 0:
1809 # Read everything.
1810 result = (self._get_decoded_chars() +
1811 decoder.decode(self.buffer.read(), final=True))
1812 self._set_decoded_chars('')
1813 self._snapshot = None
1814 return result
1815 else:
1816 # Keep reading chunks until we have n characters to return.
1817 eof = False
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))
1822 return result
1824 def next(self):
1825 self._telling = False
1826 line = self.readline()
1827 if not line:
1828 self._snapshot = None
1829 self._telling = self._seekable
1830 raise StopIteration
1831 return line
1833 def readline(self, limit=None):
1834 if self.closed:
1835 raise ValueError("read from closed file")
1836 if limit is None:
1837 limit = -1
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()
1844 start = 0
1845 # Make the decoder if it doesn't already exist.
1846 if not self._decoder:
1847 self._get_decoder()
1849 pos = endpos = None
1850 while True:
1851 if self._readtranslate:
1852 # Newlines are already translated, only search for \n
1853 pos = line.find('\n', start)
1854 if pos >= 0:
1855 endpos = pos + 1
1856 break
1857 else:
1858 start = len(line)
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)
1867 if crpos == -1:
1868 if nlpos == -1:
1869 # Nothing found
1870 start = len(line)
1871 else:
1872 # Found \n
1873 endpos = nlpos + 1
1874 break
1875 elif nlpos == -1:
1876 # Found lone \r
1877 endpos = crpos + 1
1878 break
1879 elif nlpos < crpos:
1880 # Found \n
1881 endpos = nlpos + 1
1882 break
1883 elif nlpos == crpos + 1:
1884 # Found \r\n
1885 endpos = crpos + 2
1886 break
1887 else:
1888 # Found \r
1889 endpos = crpos + 1
1890 break
1891 else:
1892 # non-universal
1893 pos = line.find(self._readnl)
1894 if pos >= 0:
1895 endpos = pos + len(self._readnl)
1896 break
1898 if limit >= 0 and len(line) >= limit:
1899 endpos = limit # reached length limit
1900 break
1902 # No line ending seen yet - get more data'
1903 while self._read_chunk():
1904 if self._decoded_chars:
1905 break
1906 if self._decoded_chars:
1907 line += self._get_decoded_chars()
1908 else:
1909 # end of file
1910 self._set_decoded_chars('')
1911 self._snapshot = None
1912 return line
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]
1921 @property
1922 def newlines(self):
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(),
1935 encoding="utf-8",
1936 errors="strict",
1937 newline=newline)
1938 # Issue #5645: make universal newlines semantics the same as in the
1939 # C version, even under Windows.
1940 if newline is None:
1941 self._writetranslate = False
1942 if initial_value:
1943 if not isinstance(initial_value, unicode):
1944 initial_value = unicode(initial_value)
1945 self.write(initial_value)
1946 self.seek(0)
1948 def getvalue(self):
1949 self.flush()
1950 return self.buffer.getvalue().decode(self._encoding, self._errors)
1952 def __repr__(self):
1953 # TextIOWrapper tells the encoding in its repr. In StringIO,
1954 # that's a implementation detail.
1955 return object.__repr__(self)
1957 @property
1958 def errors(self):
1959 return None
1961 @property
1962 def encoding(self):
1963 return None
1965 def detach(self):
1966 # This doesn't make sense on StringIO.
1967 self._unsupported("detach")