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