blob: fe020fdc5dad73ba65c08bc94a4a0433b07aad00 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001"""
2Python implementation of the io module.
3"""
4
5import os
6import abc
7import codecs
Benjamin Peterson59406a92009-03-26 17:10:29 +00008import warnings
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00009# Import _thread instead of threading to reduce startup cost
10try:
11 from _thread import allocate_lock as Lock
12except ImportError:
13 from _dummy_thread import allocate_lock as Lock
14
15import io
16from io import __all__
Benjamin Peterson8d5fd4e2009-04-02 01:03:26 +000017from io import SEEK_SET, SEEK_CUR, SEEK_END
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000018
19# open() uses st_blksize whenever we can
20DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
21
22# NOTE: Base classes defined here are registered with the "official" ABCs
23# defined in io.py. We don't use real inheritance though, because we don't
24# want to inherit the C implementations.
25
26
27class BlockingIOError(IOError):
28
29 """Exception raised when I/O would block on a non-blocking I/O stream."""
30
31 def __init__(self, errno, strerror, characters_written=0):
32 super().__init__(errno, strerror)
33 if not isinstance(characters_written, int):
34 raise TypeError("characters_written must be a integer")
35 self.characters_written = characters_written
36
37
Benjamin Peterson9990e8c2009-04-18 14:47:50 +000038def open(file: (str, bytes), mode: str = "r", buffering: int = None,
39 encoding: str = None, errors: str = None,
40 newline: str = None, closefd: bool = True) -> "IOBase":
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000041
42 r"""Open file and return a stream. Raise IOError upon failure.
43
44 file is either a text or byte string giving the name (and the path
45 if the file isn't in the current working directory) of the file to
46 be opened or an integer file descriptor of the file to be
47 wrapped. (If a file descriptor is given, it is closed when the
48 returned I/O object is closed, unless closefd is set to False.)
49
50 mode is an optional string that specifies the mode in which the file
51 is opened. It defaults to 'r' which means open for reading in text
52 mode. Other common values are 'w' for writing (truncating the file if
53 it already exists), and 'a' for appending (which on some Unix systems,
54 means that all writes append to the end of the file regardless of the
55 current seek position). In text mode, if encoding is not specified the
56 encoding used is platform dependent. (For reading and writing raw
57 bytes use binary mode and leave encoding unspecified.) The available
58 modes are:
59
60 ========= ===============================================================
61 Character Meaning
62 --------- ---------------------------------------------------------------
63 'r' open for reading (default)
64 'w' open for writing, truncating the file first
65 'a' open for writing, appending to the end of the file if it exists
66 'b' binary mode
67 't' text mode (default)
68 '+' open a disk file for updating (reading and writing)
69 'U' universal newline mode (for backwards compatibility; unneeded
70 for new code)
71 ========= ===============================================================
72
73 The default mode is 'rt' (open for reading text). For binary random
74 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
75 'r+b' opens the file without truncation.
76
77 Python distinguishes between files opened in binary and text modes,
78 even when the underlying operating system doesn't. Files opened in
79 binary mode (appending 'b' to the mode argument) return contents as
80 bytes objects without any decoding. In text mode (the default, or when
81 't' is appended to the mode argument), the contents of the file are
82 returned as strings, the bytes having been first decoded using a
83 platform-dependent encoding or using the specified encoding if given.
84
85 buffering is an optional integer used to set the buffering policy. By
86 default full buffering is on. Pass 0 to switch buffering off (only
87 allowed in binary mode), 1 to set line buffering, and an integer > 1
88 for full buffering.
89
90 encoding is the name of the encoding used to decode or encode the
91 file. This should only be used in text mode. The default encoding is
92 platform dependent, but any encoding supported by Python can be
93 passed. See the codecs module for the list of supported encodings.
94
95 errors is an optional string that specifies how encoding errors are to
96 be handled---this argument should not be used in binary mode. Pass
97 'strict' to raise a ValueError exception if there is an encoding error
98 (the default of None has the same effect), or pass 'ignore' to ignore
99 errors. (Note that ignoring encoding errors can lead to data loss.)
100 See the documentation for codecs.register for a list of the permitted
101 encoding error strings.
102
103 newline controls how universal newlines works (it only applies to text
104 mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
105 follows:
106
107 * On input, if newline is None, universal newlines mode is
108 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
109 these are translated into '\n' before being returned to the
110 caller. If it is '', universal newline mode is enabled, but line
111 endings are returned to the caller untranslated. If it has any of
112 the other legal values, input lines are only terminated by the given
113 string, and the line ending is returned to the caller untranslated.
114
115 * On output, if newline is None, any '\n' characters written are
116 translated to the system default line separator, os.linesep. If
117 newline is '', no translation takes place. If newline is any of the
118 other legal values, any '\n' characters written are translated to
119 the given string.
120
121 If closefd is False, the underlying file descriptor will be kept open
122 when the file is closed. This does not work when a file name is given
123 and must be True in that case.
124
125 open() returns a file object whose type depends on the mode, and
126 through which the standard file operations such as reading and writing
127 are performed. When open() is used to open a file in a text mode ('w',
128 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
129 a file in a binary mode, the returned class varies: in read binary
130 mode, it returns a BufferedReader; in write binary and append binary
131 modes, it returns a BufferedWriter, and in read/write mode, it returns
132 a BufferedRandom.
133
134 It is also possible to use a string or bytearray as a file for both
135 reading and writing. For strings StringIO can be used like a file
136 opened in a text mode, and for bytes a BytesIO can be used like a file
137 opened in a binary mode.
138 """
139 if not isinstance(file, (str, bytes, int)):
140 raise TypeError("invalid file: %r" % file)
141 if not isinstance(mode, str):
142 raise TypeError("invalid mode: %r" % mode)
143 if buffering is not None and not isinstance(buffering, int):
144 raise TypeError("invalid buffering: %r" % buffering)
145 if encoding is not None and not isinstance(encoding, str):
146 raise TypeError("invalid encoding: %r" % encoding)
147 if errors is not None and not isinstance(errors, str):
148 raise TypeError("invalid errors: %r" % errors)
149 modes = set(mode)
150 if modes - set("arwb+tU") or len(mode) > len(modes):
151 raise ValueError("invalid mode: %r" % mode)
152 reading = "r" in modes
153 writing = "w" in modes
154 appending = "a" in modes
155 updating = "+" in modes
156 text = "t" in modes
157 binary = "b" in modes
158 if "U" in modes:
159 if writing or appending:
160 raise ValueError("can't use U and writing mode at once")
161 reading = True
162 if text and binary:
163 raise ValueError("can't have text and binary mode at once")
164 if reading + writing + appending > 1:
165 raise ValueError("can't have read/write/append mode at once")
166 if not (reading or writing or appending):
167 raise ValueError("must have exactly one of read/write/append mode")
168 if binary and encoding is not None:
169 raise ValueError("binary mode doesn't take an encoding argument")
170 if binary and errors is not None:
171 raise ValueError("binary mode doesn't take an errors argument")
172 if binary and newline is not None:
173 raise ValueError("binary mode doesn't take a newline argument")
174 raw = FileIO(file,
175 (reading and "r" or "") +
176 (writing and "w" or "") +
177 (appending and "a" or "") +
178 (updating and "+" or ""),
179 closefd)
180 if buffering is None:
181 buffering = -1
182 line_buffering = False
183 if buffering == 1 or buffering < 0 and raw.isatty():
184 buffering = -1
185 line_buffering = True
186 if buffering < 0:
187 buffering = DEFAULT_BUFFER_SIZE
188 try:
189 bs = os.fstat(raw.fileno()).st_blksize
190 except (os.error, AttributeError):
191 pass
192 else:
193 if bs > 1:
194 buffering = bs
195 if buffering < 0:
196 raise ValueError("invalid buffering size")
197 if buffering == 0:
198 if binary:
199 return raw
200 raise ValueError("can't have unbuffered text I/O")
201 if updating:
202 buffer = BufferedRandom(raw, buffering)
203 elif writing or appending:
204 buffer = BufferedWriter(raw, buffering)
205 elif reading:
206 buffer = BufferedReader(raw, buffering)
207 else:
208 raise ValueError("unknown mode: %r" % mode)
209 if binary:
210 return buffer
211 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
212 text.mode = mode
213 return text
214
215
216class DocDescriptor:
217 """Helper for builtins.open.__doc__
218 """
219 def __get__(self, obj, typ):
220 return (
221 "open(file, mode='r', buffering=None, encoding=None, "
222 "errors=None, newline=None, closefd=True)\n\n" +
223 open.__doc__)
224
225class OpenWrapper:
226 """Wrapper for builtins.open
227
228 Trick so that open won't become a bound method when stored
229 as a class variable (as dbm.dumb does).
230
231 See initstdio() in Python/pythonrun.c.
232 """
233 __doc__ = DocDescriptor()
234
235 def __new__(cls, *args, **kwargs):
236 return open(*args, **kwargs)
237
238
239class UnsupportedOperation(ValueError, IOError):
240 pass
241
242
243class IOBase(metaclass=abc.ABCMeta):
244
245 """The abstract base class for all I/O classes, acting on streams of
246 bytes. There is no public constructor.
247
248 This class provides dummy implementations for many methods that
249 derived classes can override selectively; the default implementations
250 represent a file that cannot be read, written or seeked.
251
252 Even though IOBase does not declare read, readinto, or write because
253 their signatures will vary, implementations and clients should
254 consider those methods part of the interface. Also, implementations
255 may raise a IOError when operations they do not support are called.
256
257 The basic type used for binary data read from or written to a file is
258 bytes. bytearrays are accepted too, and in some cases (such as
259 readinto) needed. Text I/O classes work with str data.
260
261 Note that calling any method (even inquiries) on a closed stream is
262 undefined. Implementations may raise IOError in this case.
263
264 IOBase (and its subclasses) support the iterator protocol, meaning
265 that an IOBase object can be iterated over yielding the lines in a
266 stream.
267
268 IOBase also supports the :keyword:`with` statement. In this example,
269 fp is closed after the suite of the with statement is complete:
270
271 with open('spam.txt', 'r') as fp:
272 fp.write('Spam and eggs!')
273 """
274
275 ### Internal ###
276
277 def _unsupported(self, name: str) -> IOError:
278 """Internal: raise an exception for unsupported operations."""
279 raise UnsupportedOperation("%s.%s() not supported" %
280 (self.__class__.__name__, name))
281
282 ### Positioning ###
283
284 def seek(self, pos: int, whence: int = 0) -> int:
285 """Change stream position.
286
287 Change the stream position to byte offset offset. offset is
288 interpreted relative to the position indicated by whence. Values
289 for whence are:
290
291 * 0 -- start of stream (the default); offset should be zero or positive
292 * 1 -- current stream position; offset may be negative
293 * 2 -- end of stream; offset is usually negative
294
295 Return the new absolute position.
296 """
297 self._unsupported("seek")
298
299 def tell(self) -> int:
300 """Return current stream position."""
301 return self.seek(0, 1)
302
303 def truncate(self, pos: int = None) -> int:
304 """Truncate file to size bytes.
305
306 Size defaults to the current IO position as reported by tell(). Return
307 the new size.
308 """
309 self._unsupported("truncate")
310
311 ### Flush and close ###
312
313 def flush(self) -> None:
314 """Flush write buffers, if applicable.
315
316 This is not implemented for read-only and non-blocking streams.
317 """
318 # XXX Should this return the number of bytes written???
319
320 __closed = False
321
322 def close(self) -> None:
323 """Flush and close the IO object.
324
325 This method has no effect if the file is already closed.
326 """
327 if not self.__closed:
328 try:
329 self.flush()
330 except IOError:
331 pass # If flush() fails, just give up
332 self.__closed = True
333
334 def __del__(self) -> None:
335 """Destructor. Calls close()."""
336 # The try/except block is in case this is called at program
337 # exit time, when it's possible that globals have already been
338 # deleted, and then the close() call might fail. Since
339 # there's nothing we can do about such failures and they annoy
340 # the end users, we suppress the traceback.
341 try:
342 self.close()
343 except:
344 pass
345
346 ### Inquiries ###
347
348 def seekable(self) -> bool:
349 """Return whether object supports random access.
350
351 If False, seek(), tell() and truncate() will raise IOError.
352 This method may need to do a test seek().
353 """
354 return False
355
356 def _checkSeekable(self, msg=None):
357 """Internal: raise an IOError if file is not seekable
358 """
359 if not self.seekable():
360 raise IOError("File or stream is not seekable."
361 if msg is None else msg)
362
363
364 def readable(self) -> bool:
365 """Return whether object was opened for reading.
366
367 If False, read() will raise IOError.
368 """
369 return False
370
371 def _checkReadable(self, msg=None):
372 """Internal: raise an IOError if file is not readable
373 """
374 if not self.readable():
375 raise IOError("File or stream is not readable."
376 if msg is None else msg)
377
378 def writable(self) -> bool:
379 """Return whether object was opened for writing.
380
381 If False, write() and truncate() will raise IOError.
382 """
383 return False
384
385 def _checkWritable(self, msg=None):
386 """Internal: raise an IOError if file is not writable
387 """
388 if not self.writable():
389 raise IOError("File or stream is not writable."
390 if msg is None else msg)
391
392 @property
393 def closed(self):
394 """closed: bool. True iff the file has been closed.
395
396 For backwards compatibility, this is a property, not a predicate.
397 """
398 return self.__closed
399
400 def _checkClosed(self, msg=None):
401 """Internal: raise an ValueError if file is closed
402 """
403 if self.closed:
404 raise ValueError("I/O operation on closed file."
405 if msg is None else msg)
406
407 ### Context manager ###
408
409 def __enter__(self) -> "IOBase": # That's a forward reference
410 """Context management protocol. Returns self."""
411 self._checkClosed()
412 return self
413
414 def __exit__(self, *args) -> None:
415 """Context management protocol. Calls close()"""
416 self.close()
417
418 ### Lower-level APIs ###
419
420 # XXX Should these be present even if unimplemented?
421
422 def fileno(self) -> int:
423 """Returns underlying file descriptor if one exists.
424
425 An IOError is raised if the IO object does not use a file descriptor.
426 """
427 self._unsupported("fileno")
428
429 def isatty(self) -> bool:
430 """Return whether this is an 'interactive' stream.
431
432 Return False if it can't be determined.
433 """
434 self._checkClosed()
435 return False
436
437 ### Readline[s] and writelines ###
438
439 def readline(self, limit: int = -1) -> bytes:
440 r"""Read and return a line from the stream.
441
442 If limit is specified, at most limit bytes will be read.
443
444 The line terminator is always b'\n' for binary files; for text
445 files, the newlines argument to open can be used to select the line
446 terminator(s) recognized.
447 """
448 # For backwards compatibility, a (slowish) readline().
449 if hasattr(self, "peek"):
450 def nreadahead():
451 readahead = self.peek(1)
452 if not readahead:
453 return 1
454 n = (readahead.find(b"\n") + 1) or len(readahead)
455 if limit >= 0:
456 n = min(n, limit)
457 return n
458 else:
459 def nreadahead():
460 return 1
461 if limit is None:
462 limit = -1
463 res = bytearray()
464 while limit < 0 or len(res) < limit:
465 b = self.read(nreadahead())
466 if not b:
467 break
468 res += b
469 if res.endswith(b"\n"):
470 break
471 return bytes(res)
472
473 def __iter__(self):
474 self._checkClosed()
475 return self
476
477 def __next__(self):
478 line = self.readline()
479 if not line:
480 raise StopIteration
481 return line
482
483 def readlines(self, hint=None):
484 """Return a list of lines from the stream.
485
486 hint can be specified to control the number of lines read: no more
487 lines will be read if the total size (in bytes/characters) of all
488 lines so far exceeds hint.
489 """
490 if hint is None or hint <= 0:
491 return list(self)
492 n = 0
493 lines = []
494 for line in self:
495 lines.append(line)
496 n += len(line)
497 if n >= hint:
498 break
499 return lines
500
501 def writelines(self, lines):
502 self._checkClosed()
503 for line in lines:
504 self.write(line)
505
506io.IOBase.register(IOBase)
507
508
509class RawIOBase(IOBase):
510
511 """Base class for raw binary I/O."""
512
513 # The read() method is implemented by calling readinto(); derived
514 # classes that want to support read() only need to implement
515 # readinto() as a primitive operation. In general, readinto() can be
516 # more efficient than read().
517
518 # (It would be tempting to also provide an implementation of
519 # readinto() in terms of read(), in case the latter is a more suitable
520 # primitive operation, but that would lead to nasty recursion in case
521 # a subclass doesn't implement either.)
522
523 def read(self, n: int = -1) -> bytes:
524 """Read and return up to n bytes.
525
526 Returns an empty bytes object on EOF, or None if the object is
527 set not to block and has no data to read.
528 """
529 if n is None:
530 n = -1
531 if n < 0:
532 return self.readall()
533 b = bytearray(n.__index__())
534 n = self.readinto(b)
535 del b[n:]
536 return bytes(b)
537
538 def readall(self):
539 """Read until EOF, using multiple read() call."""
540 res = bytearray()
541 while True:
542 data = self.read(DEFAULT_BUFFER_SIZE)
543 if not data:
544 break
545 res += data
546 return bytes(res)
547
548 def readinto(self, b: bytearray) -> int:
549 """Read up to len(b) bytes into b.
550
551 Returns number of bytes read (0 for EOF), or None if the object
552 is set not to block as has no data to read.
553 """
554 self._unsupported("readinto")
555
556 def write(self, b: bytes) -> int:
557 """Write the given buffer to the IO stream.
558
559 Returns the number of bytes written, which may be less than len(b).
560 """
561 self._unsupported("write")
562
563io.RawIOBase.register(RawIOBase)
564from _io import FileIO
565RawIOBase.register(FileIO)
566
567
568class BufferedIOBase(IOBase):
569
570 """Base class for buffered IO objects.
571
572 The main difference with RawIOBase is that the read() method
573 supports omitting the size argument, and does not have a default
574 implementation that defers to readinto().
575
576 In addition, read(), readinto() and write() may raise
577 BlockingIOError if the underlying raw stream is in non-blocking
578 mode and not ready; unlike their raw counterparts, they will never
579 return None.
580
581 A typical implementation should not inherit from a RawIOBase
582 implementation, but wrap one.
583 """
584
585 def read(self, n: int = None) -> bytes:
586 """Read and return up to n bytes.
587
588 If the argument is omitted, None, or negative, reads and
589 returns all data until EOF.
590
591 If the argument is positive, and the underlying raw stream is
592 not 'interactive', multiple raw reads may be issued to satisfy
593 the byte count (unless EOF is reached first). But for
594 interactive raw streams (XXX and for pipes?), at most one raw
595 read will be issued, and a short result does not imply that
596 EOF is imminent.
597
598 Returns an empty bytes array on EOF.
599
600 Raises BlockingIOError if the underlying raw stream has no
601 data at the moment.
602 """
603 self._unsupported("read")
604
605 def read1(self, n: int=None) -> bytes:
606 """Read up to n bytes with at most one read() system call."""
607 self._unsupported("read1")
608
609 def readinto(self, b: bytearray) -> int:
610 """Read up to len(b) bytes into b.
611
612 Like read(), this may issue multiple reads to the underlying raw
613 stream, unless the latter is 'interactive'.
614
615 Returns the number of bytes read (0 for EOF).
616
617 Raises BlockingIOError if the underlying raw stream has no
618 data at the moment.
619 """
620 # XXX This ought to work with anything that supports the buffer API
621 data = self.read(len(b))
622 n = len(data)
623 try:
624 b[:n] = data
625 except TypeError as err:
626 import array
627 if not isinstance(b, array.array):
628 raise err
629 b[:n] = array.array('b', data)
630 return n
631
632 def write(self, b: bytes) -> int:
633 """Write the given buffer to the IO stream.
634
635 Return the number of bytes written, which is never less than
636 len(b).
637
638 Raises BlockingIOError if the buffer is full and the
639 underlying raw stream cannot accept more data at the moment.
640 """
641 self._unsupported("write")
642
643io.BufferedIOBase.register(BufferedIOBase)
644
645
646class _BufferedIOMixin(BufferedIOBase):
647
648 """A mixin implementation of BufferedIOBase with an underlying raw stream.
649
650 This passes most requests on to the underlying raw stream. It
651 does *not* provide implementations of read(), readinto() or
652 write().
653 """
654
655 def __init__(self, raw):
656 self.raw = raw
657
658 ### Positioning ###
659
660 def seek(self, pos, whence=0):
661 new_position = self.raw.seek(pos, whence)
662 if new_position < 0:
663 raise IOError("seek() returned an invalid position")
664 return new_position
665
666 def tell(self):
667 pos = self.raw.tell()
668 if pos < 0:
669 raise IOError("tell() returned an invalid position")
670 return pos
671
672 def truncate(self, pos=None):
673 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
674 # and a flush may be necessary to synch both views of the current
675 # file state.
676 self.flush()
677
678 if pos is None:
679 pos = self.tell()
680 # XXX: Should seek() be used, instead of passing the position
681 # XXX directly to truncate?
682 return self.raw.truncate(pos)
683
684 ### Flush and close ###
685
686 def flush(self):
687 self.raw.flush()
688
689 def close(self):
690 if not self.closed:
691 try:
692 self.flush()
693 except IOError:
694 pass # If flush() fails, just give up
695 self.raw.close()
696
697 ### Inquiries ###
698
699 def seekable(self):
700 return self.raw.seekable()
701
702 def readable(self):
703 return self.raw.readable()
704
705 def writable(self):
706 return self.raw.writable()
707
708 @property
709 def closed(self):
710 return self.raw.closed
711
712 @property
713 def name(self):
714 return self.raw.name
715
716 @property
717 def mode(self):
718 return self.raw.mode
719
720 ### Lower-level APIs ###
721
722 def fileno(self):
723 return self.raw.fileno()
724
725 def isatty(self):
726 return self.raw.isatty()
727
728
729class BytesIO(BufferedIOBase):
730
731 """Buffered I/O implementation using an in-memory bytes buffer."""
732
733 def __init__(self, initial_bytes=None):
734 buf = bytearray()
735 if initial_bytes is not None:
736 buf += initial_bytes
737 self._buffer = buf
738 self._pos = 0
739
740 def getvalue(self):
741 """Return the bytes value (contents) of the buffer
742 """
743 if self.closed:
744 raise ValueError("getvalue on closed file")
745 return bytes(self._buffer)
746
747 def read(self, n=None):
748 if self.closed:
749 raise ValueError("read from closed file")
750 if n is None:
751 n = -1
752 if n < 0:
753 n = len(self._buffer)
754 if len(self._buffer) <= self._pos:
755 return b""
756 newpos = min(len(self._buffer), self._pos + n)
757 b = self._buffer[self._pos : newpos]
758 self._pos = newpos
759 return bytes(b)
760
761 def read1(self, n):
762 """This is the same as read.
763 """
764 return self.read(n)
765
766 def write(self, b):
767 if self.closed:
768 raise ValueError("write to closed file")
769 if isinstance(b, str):
770 raise TypeError("can't write str to binary stream")
771 n = len(b)
772 if n == 0:
773 return 0
774 pos = self._pos
775 if pos > len(self._buffer):
776 # Inserts null bytes between the current end of the file
777 # and the new write position.
778 padding = b'\x00' * (pos - len(self._buffer))
779 self._buffer += padding
780 self._buffer[pos:pos + n] = b
781 self._pos += n
782 return n
783
784 def seek(self, pos, whence=0):
785 if self.closed:
786 raise ValueError("seek on closed file")
787 try:
788 pos = pos.__index__()
789 except AttributeError as err:
790 raise TypeError("an integer is required") from err
791 if whence == 0:
792 if pos < 0:
793 raise ValueError("negative seek position %r" % (pos,))
794 self._pos = pos
795 elif whence == 1:
796 self._pos = max(0, self._pos + pos)
797 elif whence == 2:
798 self._pos = max(0, len(self._buffer) + pos)
799 else:
800 raise ValueError("invalid whence value")
801 return self._pos
802
803 def tell(self):
804 if self.closed:
805 raise ValueError("tell on closed file")
806 return self._pos
807
808 def truncate(self, pos=None):
809 if self.closed:
810 raise ValueError("truncate on closed file")
811 if pos is None:
812 pos = self._pos
813 elif pos < 0:
814 raise ValueError("negative truncate position %r" % (pos,))
815 del self._buffer[pos:]
816 return self.seek(pos)
817
818 def readable(self):
819 return True
820
821 def writable(self):
822 return True
823
824 def seekable(self):
825 return True
826
827
828class BufferedReader(_BufferedIOMixin):
829
830 """BufferedReader(raw[, buffer_size])
831
832 A buffer for a readable, sequential BaseRawIO object.
833
834 The constructor creates a BufferedReader for the given readable raw
835 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
836 is used.
837 """
838
839 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
840 """Create a new buffered reader using the given readable raw IO object.
841 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000842 if not raw.readable():
843 raise IOError('"raw" argument must be readable.')
844
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000845 _BufferedIOMixin.__init__(self, raw)
846 if buffer_size <= 0:
847 raise ValueError("invalid buffer size")
848 self.buffer_size = buffer_size
849 self._reset_read_buf()
850 self._read_lock = Lock()
851
852 def _reset_read_buf(self):
853 self._read_buf = b""
854 self._read_pos = 0
855
856 def read(self, n=None):
857 """Read n bytes.
858
859 Returns exactly n bytes of data unless the underlying raw IO
860 stream reaches EOF or if the call would block in non-blocking
861 mode. If n is negative, read until EOF or until read() would
862 block.
863 """
864 if n is not None and n < -1:
865 raise ValueError("invalid number of bytes to read")
866 with self._read_lock:
867 return self._read_unlocked(n)
868
869 def _read_unlocked(self, n=None):
870 nodata_val = b""
871 empty_values = (b"", None)
872 buf = self._read_buf
873 pos = self._read_pos
874
875 # Special case for when the number of bytes to read is unspecified.
876 if n is None or n == -1:
877 self._reset_read_buf()
878 chunks = [buf[pos:]] # Strip the consumed bytes.
879 current_size = 0
880 while True:
881 # Read until EOF or until read() would block.
882 chunk = self.raw.read()
883 if chunk in empty_values:
884 nodata_val = chunk
885 break
886 current_size += len(chunk)
887 chunks.append(chunk)
888 return b"".join(chunks) or nodata_val
889
890 # The number of bytes to read is specified, return at most n bytes.
891 avail = len(buf) - pos # Length of the available buffered data.
892 if n <= avail:
893 # Fast path: the data to read is fully buffered.
894 self._read_pos += n
895 return buf[pos:pos+n]
896 # Slow path: read from the stream until enough bytes are read,
897 # or until an EOF occurs or until read() would block.
898 chunks = [buf[pos:]]
899 wanted = max(self.buffer_size, n)
900 while avail < n:
901 chunk = self.raw.read(wanted)
902 if chunk in empty_values:
903 nodata_val = chunk
904 break
905 avail += len(chunk)
906 chunks.append(chunk)
907 # n is more then avail only when an EOF occurred or when
908 # read() would have blocked.
909 n = min(n, avail)
910 out = b"".join(chunks)
911 self._read_buf = out[n:] # Save the extra data in the buffer.
912 self._read_pos = 0
913 return out[:n] if out else nodata_val
914
915 def peek(self, n=0):
916 """Returns buffered bytes without advancing the position.
917
918 The argument indicates a desired minimal number of bytes; we
919 do at most one raw read to satisfy it. We never return more
920 than self.buffer_size.
921 """
922 with self._read_lock:
923 return self._peek_unlocked(n)
924
925 def _peek_unlocked(self, n=0):
926 want = min(n, self.buffer_size)
927 have = len(self._read_buf) - self._read_pos
928 if have < want or have <= 0:
929 to_read = self.buffer_size - have
930 current = self.raw.read(to_read)
931 if current:
932 self._read_buf = self._read_buf[self._read_pos:] + current
933 self._read_pos = 0
934 return self._read_buf[self._read_pos:]
935
936 def read1(self, n):
937 """Reads up to n bytes, with at most one read() system call."""
938 # Returns up to n bytes. If at least one byte is buffered, we
939 # only return buffered bytes. Otherwise, we do one raw read.
940 if n < 0:
941 raise ValueError("number of bytes to read must be positive")
942 if n == 0:
943 return b""
944 with self._read_lock:
945 self._peek_unlocked(1)
946 return self._read_unlocked(
947 min(n, len(self._read_buf) - self._read_pos))
948
949 def tell(self):
950 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
951
952 def seek(self, pos, whence=0):
953 if not (0 <= whence <= 2):
954 raise ValueError("invalid whence value")
955 with self._read_lock:
956 if whence == 1:
957 pos -= len(self._read_buf) - self._read_pos
958 pos = _BufferedIOMixin.seek(self, pos, whence)
959 self._reset_read_buf()
960 return pos
961
962class BufferedWriter(_BufferedIOMixin):
963
964 """A buffer for a writeable sequential RawIO object.
965
966 The constructor creates a BufferedWriter for the given writeable raw
967 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +0000968 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000969 """
970
Benjamin Peterson59406a92009-03-26 17:10:29 +0000971 _warning_stack_offset = 2
972
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000973 def __init__(self, raw,
974 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000975 if not raw.writable():
976 raise IOError('"raw" argument must be writable.')
977
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000978 _BufferedIOMixin.__init__(self, raw)
979 if buffer_size <= 0:
980 raise ValueError("invalid buffer size")
Benjamin Peterson59406a92009-03-26 17:10:29 +0000981 if max_buffer_size is not None:
982 warnings.warn("max_buffer_size is deprecated", DeprecationWarning,
983 self._warning_stack_offset)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000984 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000985 self._write_buf = bytearray()
986 self._write_lock = Lock()
987
988 def write(self, b):
989 if self.closed:
990 raise ValueError("write to closed file")
991 if isinstance(b, str):
992 raise TypeError("can't write str to binary stream")
993 with self._write_lock:
994 # XXX we can implement some more tricks to try and avoid
995 # partial writes
996 if len(self._write_buf) > self.buffer_size:
997 # We're full, so let's pre-flush the buffer
998 try:
999 self._flush_unlocked()
1000 except BlockingIOError as e:
1001 # We can't accept anything else.
1002 # XXX Why not just let the exception pass through?
1003 raise BlockingIOError(e.errno, e.strerror, 0)
1004 before = len(self._write_buf)
1005 self._write_buf.extend(b)
1006 written = len(self._write_buf) - before
1007 if len(self._write_buf) > self.buffer_size:
1008 try:
1009 self._flush_unlocked()
1010 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001011 if len(self._write_buf) > self.buffer_size:
1012 # We've hit the buffer_size. We have to accept a partial
1013 # write and cut back our buffer.
1014 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001015 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001016 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001017 raise BlockingIOError(e.errno, e.strerror, written)
1018 return written
1019
1020 def truncate(self, pos=None):
1021 with self._write_lock:
1022 self._flush_unlocked()
1023 if pos is None:
1024 pos = self.raw.tell()
1025 return self.raw.truncate(pos)
1026
1027 def flush(self):
1028 with self._write_lock:
1029 self._flush_unlocked()
1030
1031 def _flush_unlocked(self):
1032 if self.closed:
1033 raise ValueError("flush of closed file")
1034 written = 0
1035 try:
1036 while self._write_buf:
1037 n = self.raw.write(self._write_buf)
1038 if n > len(self._write_buf) or n < 0:
1039 raise IOError("write() returned incorrect number of bytes")
1040 del self._write_buf[:n]
1041 written += n
1042 except BlockingIOError as e:
1043 n = e.characters_written
1044 del self._write_buf[:n]
1045 written += n
1046 raise BlockingIOError(e.errno, e.strerror, written)
1047
1048 def tell(self):
1049 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1050
1051 def seek(self, pos, whence=0):
1052 if not (0 <= whence <= 2):
1053 raise ValueError("invalid whence")
1054 with self._write_lock:
1055 self._flush_unlocked()
1056 return _BufferedIOMixin.seek(self, pos, whence)
1057
1058
1059class BufferedRWPair(BufferedIOBase):
1060
1061 """A buffered reader and writer object together.
1062
1063 A buffered reader object and buffered writer object put together to
1064 form a sequential IO object that can read and write. This is typically
1065 used with a socket or two-way pipe.
1066
1067 reader and writer are RawIOBase objects that are readable and
1068 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001069 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001070 """
1071
1072 # XXX The usefulness of this (compared to having two separate IO
1073 # objects) is questionable.
1074
1075 def __init__(self, reader, writer,
1076 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1077 """Constructor.
1078
1079 The arguments are two RawIO instances.
1080 """
Benjamin Peterson59406a92009-03-26 17:10:29 +00001081 if max_buffer_size is not None:
1082 warnings.warn("max_buffer_size is deprecated", DeprecationWarning, 2)
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001083
1084 if not reader.readable():
1085 raise IOError('"reader" argument must be readable.')
1086
1087 if not writer.writable():
1088 raise IOError('"writer" argument must be writable.')
1089
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001090 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001091 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001092
1093 def read(self, n=None):
1094 if n is None:
1095 n = -1
1096 return self.reader.read(n)
1097
1098 def readinto(self, b):
1099 return self.reader.readinto(b)
1100
1101 def write(self, b):
1102 return self.writer.write(b)
1103
1104 def peek(self, n=0):
1105 return self.reader.peek(n)
1106
1107 def read1(self, n):
1108 return self.reader.read1(n)
1109
1110 def readable(self):
1111 return self.reader.readable()
1112
1113 def writable(self):
1114 return self.writer.writable()
1115
1116 def flush(self):
1117 return self.writer.flush()
1118
1119 def close(self):
1120 self.writer.close()
1121 self.reader.close()
1122
1123 def isatty(self):
1124 return self.reader.isatty() or self.writer.isatty()
1125
1126 @property
1127 def closed(self):
1128 return self.writer.closed
1129
1130
1131class BufferedRandom(BufferedWriter, BufferedReader):
1132
1133 """A buffered interface to random access streams.
1134
1135 The constructor creates a reader and writer for a seekable stream,
1136 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001137 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001138 """
1139
Benjamin Peterson59406a92009-03-26 17:10:29 +00001140 _warning_stack_offset = 3
1141
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001142 def __init__(self, raw,
1143 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1144 raw._checkSeekable()
1145 BufferedReader.__init__(self, raw, buffer_size)
1146 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
1147
1148 def seek(self, pos, whence=0):
1149 if not (0 <= whence <= 2):
1150 raise ValueError("invalid whence")
1151 self.flush()
1152 if self._read_buf:
1153 # Undo read ahead.
1154 with self._read_lock:
1155 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1156 # First do the raw seek, then empty the read buffer, so that
1157 # if the raw seek fails, we don't lose buffered data forever.
1158 pos = self.raw.seek(pos, whence)
1159 with self._read_lock:
1160 self._reset_read_buf()
1161 if pos < 0:
1162 raise IOError("seek() returned invalid position")
1163 return pos
1164
1165 def tell(self):
1166 if self._write_buf:
1167 return BufferedWriter.tell(self)
1168 else:
1169 return BufferedReader.tell(self)
1170
1171 def truncate(self, pos=None):
1172 if pos is None:
1173 pos = self.tell()
1174 # Use seek to flush the read buffer.
1175 self.seek(pos)
1176 return BufferedWriter.truncate(self)
1177
1178 def read(self, n=None):
1179 if n is None:
1180 n = -1
1181 self.flush()
1182 return BufferedReader.read(self, n)
1183
1184 def readinto(self, b):
1185 self.flush()
1186 return BufferedReader.readinto(self, b)
1187
1188 def peek(self, n=0):
1189 self.flush()
1190 return BufferedReader.peek(self, n)
1191
1192 def read1(self, n):
1193 self.flush()
1194 return BufferedReader.read1(self, n)
1195
1196 def write(self, b):
1197 if self._read_buf:
1198 # Undo readahead
1199 with self._read_lock:
1200 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1201 self._reset_read_buf()
1202 return BufferedWriter.write(self, b)
1203
1204
1205class TextIOBase(IOBase):
1206
1207 """Base class for text I/O.
1208
1209 This class provides a character and line based interface to stream
1210 I/O. There is no readinto method because Python's character strings
1211 are immutable. There is no public constructor.
1212 """
1213
1214 def read(self, n: int = -1) -> str:
1215 """Read at most n characters from stream.
1216
1217 Read from underlying buffer until we have n characters or we hit EOF.
1218 If n is negative or omitted, read until EOF.
1219 """
1220 self._unsupported("read")
1221
1222 def write(self, s: str) -> int:
1223 """Write string s to stream."""
1224 self._unsupported("write")
1225
1226 def truncate(self, pos: int = None) -> int:
1227 """Truncate size to pos."""
1228 self._unsupported("truncate")
1229
1230 def readline(self) -> str:
1231 """Read until newline or EOF.
1232
1233 Returns an empty string if EOF is hit immediately.
1234 """
1235 self._unsupported("readline")
1236
1237 @property
1238 def encoding(self):
1239 """Subclasses should override."""
1240 return None
1241
1242 @property
1243 def newlines(self):
1244 """Line endings translated so far.
1245
1246 Only line endings translated during reading are considered.
1247
1248 Subclasses should override.
1249 """
1250 return None
1251
1252io.TextIOBase.register(TextIOBase)
1253
1254
1255class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1256 r"""Codec used when reading a file in universal newlines mode. It wraps
1257 another incremental decoder, translating \r\n and \r into \n. It also
1258 records the types of newlines encountered. When used with
1259 translate=False, it ensures that the newline sequence is returned in
1260 one piece.
1261 """
1262 def __init__(self, decoder, translate, errors='strict'):
1263 codecs.IncrementalDecoder.__init__(self, errors=errors)
1264 self.translate = translate
1265 self.decoder = decoder
1266 self.seennl = 0
1267 self.pendingcr = False
1268
1269 def decode(self, input, final=False):
1270 # decode input (with the eventual \r from a previous pass)
1271 if self.decoder is None:
1272 output = input
1273 else:
1274 output = self.decoder.decode(input, final=final)
1275 if self.pendingcr and (output or final):
1276 output = "\r" + output
1277 self.pendingcr = False
1278
1279 # retain last \r even when not translating data:
1280 # then readline() is sure to get \r\n in one pass
1281 if output.endswith("\r") and not final:
1282 output = output[:-1]
1283 self.pendingcr = True
1284
1285 # Record which newlines are read
1286 crlf = output.count('\r\n')
1287 cr = output.count('\r') - crlf
1288 lf = output.count('\n') - crlf
1289 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1290 | (crlf and self._CRLF)
1291
1292 if self.translate:
1293 if crlf:
1294 output = output.replace("\r\n", "\n")
1295 if cr:
1296 output = output.replace("\r", "\n")
1297
1298 return output
1299
1300 def getstate(self):
1301 if self.decoder is None:
1302 buf = b""
1303 flag = 0
1304 else:
1305 buf, flag = self.decoder.getstate()
1306 flag <<= 1
1307 if self.pendingcr:
1308 flag |= 1
1309 return buf, flag
1310
1311 def setstate(self, state):
1312 buf, flag = state
1313 self.pendingcr = bool(flag & 1)
1314 if self.decoder is not None:
1315 self.decoder.setstate((buf, flag >> 1))
1316
1317 def reset(self):
1318 self.seennl = 0
1319 self.pendingcr = False
1320 if self.decoder is not None:
1321 self.decoder.reset()
1322
1323 _LF = 1
1324 _CR = 2
1325 _CRLF = 4
1326
1327 @property
1328 def newlines(self):
1329 return (None,
1330 "\n",
1331 "\r",
1332 ("\r", "\n"),
1333 "\r\n",
1334 ("\n", "\r\n"),
1335 ("\r", "\r\n"),
1336 ("\r", "\n", "\r\n")
1337 )[self.seennl]
1338
1339
1340class TextIOWrapper(TextIOBase):
1341
1342 r"""Character and line based layer over a BufferedIOBase object, buffer.
1343
1344 encoding gives the name of the encoding that the stream will be
1345 decoded or encoded with. It defaults to locale.getpreferredencoding.
1346
1347 errors determines the strictness of encoding and decoding (see the
1348 codecs.register) and defaults to "strict".
1349
1350 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1351 handling of line endings. If it is None, universal newlines is
1352 enabled. With this enabled, on input, the lines endings '\n', '\r',
1353 or '\r\n' are translated to '\n' before being returned to the
1354 caller. Conversely, on output, '\n' is translated to the system
1355 default line seperator, os.linesep. If newline is any other of its
1356 legal values, that newline becomes the newline when the file is read
1357 and it is returned untranslated. On output, '\n' is converted to the
1358 newline.
1359
1360 If line_buffering is True, a call to flush is implied when a call to
1361 write contains a newline character.
1362 """
1363
1364 _CHUNK_SIZE = 2048
1365
1366 def __init__(self, buffer, encoding=None, errors=None, newline=None,
1367 line_buffering=False):
1368 if newline is not None and not isinstance(newline, str):
1369 raise TypeError("illegal newline type: %r" % (type(newline),))
1370 if newline not in (None, "", "\n", "\r", "\r\n"):
1371 raise ValueError("illegal newline value: %r" % (newline,))
1372 if encoding is None:
1373 try:
1374 encoding = os.device_encoding(buffer.fileno())
1375 except (AttributeError, UnsupportedOperation):
1376 pass
1377 if encoding is None:
1378 try:
1379 import locale
1380 except ImportError:
1381 # Importing locale may fail if Python is being built
1382 encoding = "ascii"
1383 else:
1384 encoding = locale.getpreferredencoding()
1385
1386 if not isinstance(encoding, str):
1387 raise ValueError("invalid encoding: %r" % encoding)
1388
1389 if errors is None:
1390 errors = "strict"
1391 else:
1392 if not isinstance(errors, str):
1393 raise ValueError("invalid errors: %r" % errors)
1394
1395 self.buffer = buffer
1396 self._line_buffering = line_buffering
1397 self._encoding = encoding
1398 self._errors = errors
1399 self._readuniversal = not newline
1400 self._readtranslate = newline is None
1401 self._readnl = newline
1402 self._writetranslate = newline != ''
1403 self._writenl = newline or os.linesep
1404 self._encoder = None
1405 self._decoder = None
1406 self._decoded_chars = '' # buffer for text returned from decoder
1407 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1408 self._snapshot = None # info for reconstructing decoder state
1409 self._seekable = self._telling = self.buffer.seekable()
1410
1411 # self._snapshot is either None, or a tuple (dec_flags, next_input)
1412 # where dec_flags is the second (integer) item of the decoder state
1413 # and next_input is the chunk of input bytes that comes next after the
1414 # snapshot point. We use this to reconstruct decoder states in tell().
1415
1416 # Naming convention:
1417 # - "bytes_..." for integer variables that count input bytes
1418 # - "chars_..." for integer variables that count decoded characters
1419
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001420 def __repr__(self):
1421 return "<TextIOWrapper encoding={0}>".format(self.encoding)
1422
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001423 @property
1424 def encoding(self):
1425 return self._encoding
1426
1427 @property
1428 def errors(self):
1429 return self._errors
1430
1431 @property
1432 def line_buffering(self):
1433 return self._line_buffering
1434
1435 def seekable(self):
1436 return self._seekable
1437
1438 def readable(self):
1439 return self.buffer.readable()
1440
1441 def writable(self):
1442 return self.buffer.writable()
1443
1444 def flush(self):
1445 self.buffer.flush()
1446 self._telling = self._seekable
1447
1448 def close(self):
1449 try:
1450 self.flush()
Benjamin Peterson54f963e2009-04-14 22:02:08 +00001451 except IOError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001452 pass # If flush() fails, just give up
1453 self.buffer.close()
1454
1455 @property
1456 def closed(self):
1457 return self.buffer.closed
1458
1459 @property
1460 def name(self):
1461 return self.buffer.name
1462
1463 def fileno(self):
1464 return self.buffer.fileno()
1465
1466 def isatty(self):
1467 return self.buffer.isatty()
1468
1469 def write(self, s: str):
1470 if self.closed:
1471 raise ValueError("write to closed file")
1472 if not isinstance(s, str):
1473 raise TypeError("can't write %s to text stream" %
1474 s.__class__.__name__)
1475 length = len(s)
1476 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
1477 if haslf and self._writetranslate and self._writenl != "\n":
1478 s = s.replace("\n", self._writenl)
1479 encoder = self._encoder or self._get_encoder()
1480 # XXX What if we were just reading?
1481 b = encoder.encode(s)
1482 self.buffer.write(b)
1483 if self._line_buffering and (haslf or "\r" in s):
1484 self.flush()
1485 self._snapshot = None
1486 if self._decoder:
1487 self._decoder.reset()
1488 return length
1489
1490 def _get_encoder(self):
1491 make_encoder = codecs.getincrementalencoder(self._encoding)
1492 self._encoder = make_encoder(self._errors)
1493 return self._encoder
1494
1495 def _get_decoder(self):
1496 make_decoder = codecs.getincrementaldecoder(self._encoding)
1497 decoder = make_decoder(self._errors)
1498 if self._readuniversal:
1499 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
1500 self._decoder = decoder
1501 return decoder
1502
1503 # The following three methods implement an ADT for _decoded_chars.
1504 # Text returned from the decoder is buffered here until the client
1505 # requests it by calling our read() or readline() method.
1506 def _set_decoded_chars(self, chars):
1507 """Set the _decoded_chars buffer."""
1508 self._decoded_chars = chars
1509 self._decoded_chars_used = 0
1510
1511 def _get_decoded_chars(self, n=None):
1512 """Advance into the _decoded_chars buffer."""
1513 offset = self._decoded_chars_used
1514 if n is None:
1515 chars = self._decoded_chars[offset:]
1516 else:
1517 chars = self._decoded_chars[offset:offset + n]
1518 self._decoded_chars_used += len(chars)
1519 return chars
1520
1521 def _rewind_decoded_chars(self, n):
1522 """Rewind the _decoded_chars buffer."""
1523 if self._decoded_chars_used < n:
1524 raise AssertionError("rewind decoded_chars out of bounds")
1525 self._decoded_chars_used -= n
1526
1527 def _read_chunk(self):
1528 """
1529 Read and decode the next chunk of data from the BufferedReader.
1530 """
1531
1532 # The return value is True unless EOF was reached. The decoded
1533 # string is placed in self._decoded_chars (replacing its previous
1534 # value). The entire input chunk is sent to the decoder, though
1535 # some of it may remain buffered in the decoder, yet to be
1536 # converted.
1537
1538 if self._decoder is None:
1539 raise ValueError("no decoder")
1540
1541 if self._telling:
1542 # To prepare for tell(), we need to snapshot a point in the
1543 # file where the decoder's input buffer is empty.
1544
1545 dec_buffer, dec_flags = self._decoder.getstate()
1546 # Given this, we know there was a valid snapshot point
1547 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1548
1549 # Read a chunk, decode it, and put the result in self._decoded_chars.
1550 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1551 eof = not input_chunk
1552 self._set_decoded_chars(self._decoder.decode(input_chunk, eof))
1553
1554 if self._telling:
1555 # At the snapshot point, len(dec_buffer) bytes before the read,
1556 # the next input to be decoded is dec_buffer + input_chunk.
1557 self._snapshot = (dec_flags, dec_buffer + input_chunk)
1558
1559 return not eof
1560
1561 def _pack_cookie(self, position, dec_flags=0,
1562 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
1563 # The meaning of a tell() cookie is: seek to position, set the
1564 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
1565 # into the decoder with need_eof as the EOF flag, then skip
1566 # chars_to_skip characters of the decoded result. For most simple
1567 # decoders, tell() will often just give a byte offset in the file.
1568 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
1569 (chars_to_skip<<192) | bool(need_eof)<<256)
1570
1571 def _unpack_cookie(self, bigint):
1572 rest, position = divmod(bigint, 1<<64)
1573 rest, dec_flags = divmod(rest, 1<<64)
1574 rest, bytes_to_feed = divmod(rest, 1<<64)
1575 need_eof, chars_to_skip = divmod(rest, 1<<64)
1576 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
1577
1578 def tell(self):
1579 if not self._seekable:
1580 raise IOError("underlying stream is not seekable")
1581 if not self._telling:
1582 raise IOError("telling position disabled by next() call")
1583 self.flush()
1584 position = self.buffer.tell()
1585 decoder = self._decoder
1586 if decoder is None or self._snapshot is None:
1587 if self._decoded_chars:
1588 # This should never happen.
1589 raise AssertionError("pending decoded text")
1590 return position
1591
1592 # Skip backward to the snapshot point (see _read_chunk).
1593 dec_flags, next_input = self._snapshot
1594 position -= len(next_input)
1595
1596 # How many decoded characters have been used up since the snapshot?
1597 chars_to_skip = self._decoded_chars_used
1598 if chars_to_skip == 0:
1599 # We haven't moved from the snapshot point.
1600 return self._pack_cookie(position, dec_flags)
1601
1602 # Starting from the snapshot position, we will walk the decoder
1603 # forward until it gives us enough decoded characters.
1604 saved_state = decoder.getstate()
1605 try:
1606 # Note our initial start point.
1607 decoder.setstate((b'', dec_flags))
1608 start_pos = position
1609 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1610 need_eof = 0
1611
1612 # Feed the decoder one byte at a time. As we go, note the
1613 # nearest "safe start point" before the current location
1614 # (a point where the decoder has nothing buffered, so seek()
1615 # can safely start from there and advance to this location).
1616 next_byte = bytearray(1)
1617 for next_byte[0] in next_input:
1618 bytes_fed += 1
1619 chars_decoded += len(decoder.decode(next_byte))
1620 dec_buffer, dec_flags = decoder.getstate()
1621 if not dec_buffer and chars_decoded <= chars_to_skip:
1622 # Decoder buffer is empty, so this is a safe start point.
1623 start_pos += bytes_fed
1624 chars_to_skip -= chars_decoded
1625 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1626 if chars_decoded >= chars_to_skip:
1627 break
1628 else:
1629 # We didn't get enough decoded data; signal EOF to get more.
1630 chars_decoded += len(decoder.decode(b'', final=True))
1631 need_eof = 1
1632 if chars_decoded < chars_to_skip:
1633 raise IOError("can't reconstruct logical file position")
1634
1635 # The returned cookie corresponds to the last safe start point.
1636 return self._pack_cookie(
1637 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
1638 finally:
1639 decoder.setstate(saved_state)
1640
1641 def truncate(self, pos=None):
1642 self.flush()
1643 if pos is None:
1644 pos = self.tell()
1645 self.seek(pos)
1646 return self.buffer.truncate()
1647
1648 def seek(self, cookie, whence=0):
1649 if self.closed:
1650 raise ValueError("tell on closed file")
1651 if not self._seekable:
1652 raise IOError("underlying stream is not seekable")
1653 if whence == 1: # seek relative to current position
1654 if cookie != 0:
1655 raise IOError("can't do nonzero cur-relative seeks")
1656 # Seeking to the current position should attempt to
1657 # sync the underlying buffer with the current position.
1658 whence = 0
1659 cookie = self.tell()
1660 if whence == 2: # seek relative to end of file
1661 if cookie != 0:
1662 raise IOError("can't do nonzero end-relative seeks")
1663 self.flush()
1664 position = self.buffer.seek(0, 2)
1665 self._set_decoded_chars('')
1666 self._snapshot = None
1667 if self._decoder:
1668 self._decoder.reset()
1669 return position
1670 if whence != 0:
1671 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" %
1672 (whence,))
1673 if cookie < 0:
1674 raise ValueError("negative seek position %r" % (cookie,))
1675 self.flush()
1676
1677 # The strategy of seek() is to go back to the safe start point
1678 # and replay the effect of read(chars_to_skip) from there.
1679 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
1680 self._unpack_cookie(cookie)
1681
1682 # Seek back to the safe start point.
1683 self.buffer.seek(start_pos)
1684 self._set_decoded_chars('')
1685 self._snapshot = None
1686
1687 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00001688 if cookie == 0 and self._decoder:
1689 self._decoder.reset()
1690 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001691 self._decoder = self._decoder or self._get_decoder()
1692 self._decoder.setstate((b'', dec_flags))
1693 self._snapshot = (dec_flags, b'')
1694
1695 if chars_to_skip:
1696 # Just like _read_chunk, feed the decoder and save a snapshot.
1697 input_chunk = self.buffer.read(bytes_to_feed)
1698 self._set_decoded_chars(
1699 self._decoder.decode(input_chunk, need_eof))
1700 self._snapshot = (dec_flags, input_chunk)
1701
1702 # Skip chars_to_skip of the decoded characters.
1703 if len(self._decoded_chars) < chars_to_skip:
1704 raise IOError("can't restore logical file position")
1705 self._decoded_chars_used = chars_to_skip
1706
1707 return cookie
1708
1709 def read(self, n=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00001710 self._checkReadable()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001711 if n is None:
1712 n = -1
1713 decoder = self._decoder or self._get_decoder()
1714 if n < 0:
1715 # Read everything.
1716 result = (self._get_decoded_chars() +
1717 decoder.decode(self.buffer.read(), final=True))
1718 self._set_decoded_chars('')
1719 self._snapshot = None
1720 return result
1721 else:
1722 # Keep reading chunks until we have n characters to return.
1723 eof = False
1724 result = self._get_decoded_chars(n)
1725 while len(result) < n and not eof:
1726 eof = not self._read_chunk()
1727 result += self._get_decoded_chars(n - len(result))
1728 return result
1729
1730 def __next__(self):
1731 self._telling = False
1732 line = self.readline()
1733 if not line:
1734 self._snapshot = None
1735 self._telling = self._seekable
1736 raise StopIteration
1737 return line
1738
1739 def readline(self, limit=None):
1740 if self.closed:
1741 raise ValueError("read from closed file")
1742 if limit is None:
1743 limit = -1
1744
1745 # Grab all the decoded text (we will rewind any extra bits later).
1746 line = self._get_decoded_chars()
1747
1748 start = 0
1749 # Make the decoder if it doesn't already exist.
1750 if not self._decoder:
1751 self._get_decoder()
1752
1753 pos = endpos = None
1754 while True:
1755 if self._readtranslate:
1756 # Newlines are already translated, only search for \n
1757 pos = line.find('\n', start)
1758 if pos >= 0:
1759 endpos = pos + 1
1760 break
1761 else:
1762 start = len(line)
1763
1764 elif self._readuniversal:
1765 # Universal newline search. Find any of \r, \r\n, \n
1766 # The decoder ensures that \r\n are not split in two pieces
1767
1768 # In C we'd look for these in parallel of course.
1769 nlpos = line.find("\n", start)
1770 crpos = line.find("\r", start)
1771 if crpos == -1:
1772 if nlpos == -1:
1773 # Nothing found
1774 start = len(line)
1775 else:
1776 # Found \n
1777 endpos = nlpos + 1
1778 break
1779 elif nlpos == -1:
1780 # Found lone \r
1781 endpos = crpos + 1
1782 break
1783 elif nlpos < crpos:
1784 # Found \n
1785 endpos = nlpos + 1
1786 break
1787 elif nlpos == crpos + 1:
1788 # Found \r\n
1789 endpos = crpos + 2
1790 break
1791 else:
1792 # Found \r
1793 endpos = crpos + 1
1794 break
1795 else:
1796 # non-universal
1797 pos = line.find(self._readnl)
1798 if pos >= 0:
1799 endpos = pos + len(self._readnl)
1800 break
1801
1802 if limit >= 0 and len(line) >= limit:
1803 endpos = limit # reached length limit
1804 break
1805
1806 # No line ending seen yet - get more data'
1807 while self._read_chunk():
1808 if self._decoded_chars:
1809 break
1810 if self._decoded_chars:
1811 line += self._get_decoded_chars()
1812 else:
1813 # end of file
1814 self._set_decoded_chars('')
1815 self._snapshot = None
1816 return line
1817
1818 if limit >= 0 and endpos > limit:
1819 endpos = limit # don't exceed limit
1820
1821 # Rewind _decoded_chars to just after the line ending we found.
1822 self._rewind_decoded_chars(len(line) - endpos)
1823 return line[:endpos]
1824
1825 @property
1826 def newlines(self):
1827 return self._decoder.newlines if self._decoder else None
1828
1829
1830class StringIO(TextIOWrapper):
1831 """Text I/O implementation using an in-memory buffer.
1832
1833 The initial_value argument sets the value of object. The newline
1834 argument is like the one of TextIOWrapper's constructor.
1835 """
1836
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001837 def __init__(self, initial_value="", newline="\n"):
1838 super(StringIO, self).__init__(BytesIO(),
1839 encoding="utf-8",
1840 errors="strict",
1841 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00001842 # Issue #5645: make universal newlines semantics the same as in the
1843 # C version, even under Windows.
1844 if newline is None:
1845 self._writetranslate = False
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001846 if initial_value:
1847 if not isinstance(initial_value, str):
1848 initial_value = str(initial_value)
1849 self.write(initial_value)
1850 self.seek(0)
1851
1852 def getvalue(self):
1853 self.flush()
1854 return self.buffer.getvalue().decode(self._encoding, self._errors)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00001855
1856 def __repr__(self):
1857 # TextIOWrapper tells the encoding in its repr. In StringIO,
1858 # that's a implementation detail.
1859 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00001860
1861 @property
1862 def encoding(self):
1863 return None