blob: e580366df7c81713be023b1cca09a081f170e9af [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
Benjamin Petersonb01138a2009-04-24 22:59:52 +0000463 elif not isinstance(limit, int):
464 raise TypeError("limit must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465 res = bytearray()
466 while limit < 0 or len(res) < limit:
467 b = self.read(nreadahead())
468 if not b:
469 break
470 res += b
471 if res.endswith(b"\n"):
472 break
473 return bytes(res)
474
475 def __iter__(self):
476 self._checkClosed()
477 return self
478
479 def __next__(self):
480 line = self.readline()
481 if not line:
482 raise StopIteration
483 return line
484
485 def readlines(self, hint=None):
486 """Return a list of lines from the stream.
487
488 hint can be specified to control the number of lines read: no more
489 lines will be read if the total size (in bytes/characters) of all
490 lines so far exceeds hint.
491 """
492 if hint is None or hint <= 0:
493 return list(self)
494 n = 0
495 lines = []
496 for line in self:
497 lines.append(line)
498 n += len(line)
499 if n >= hint:
500 break
501 return lines
502
503 def writelines(self, lines):
504 self._checkClosed()
505 for line in lines:
506 self.write(line)
507
508io.IOBase.register(IOBase)
509
510
511class RawIOBase(IOBase):
512
513 """Base class for raw binary I/O."""
514
515 # The read() method is implemented by calling readinto(); derived
516 # classes that want to support read() only need to implement
517 # readinto() as a primitive operation. In general, readinto() can be
518 # more efficient than read().
519
520 # (It would be tempting to also provide an implementation of
521 # readinto() in terms of read(), in case the latter is a more suitable
522 # primitive operation, but that would lead to nasty recursion in case
523 # a subclass doesn't implement either.)
524
525 def read(self, n: int = -1) -> bytes:
526 """Read and return up to n bytes.
527
528 Returns an empty bytes object on EOF, or None if the object is
529 set not to block and has no data to read.
530 """
531 if n is None:
532 n = -1
533 if n < 0:
534 return self.readall()
535 b = bytearray(n.__index__())
536 n = self.readinto(b)
537 del b[n:]
538 return bytes(b)
539
540 def readall(self):
541 """Read until EOF, using multiple read() call."""
542 res = bytearray()
543 while True:
544 data = self.read(DEFAULT_BUFFER_SIZE)
545 if not data:
546 break
547 res += data
548 return bytes(res)
549
550 def readinto(self, b: bytearray) -> int:
551 """Read up to len(b) bytes into b.
552
553 Returns number of bytes read (0 for EOF), or None if the object
554 is set not to block as has no data to read.
555 """
556 self._unsupported("readinto")
557
558 def write(self, b: bytes) -> int:
559 """Write the given buffer to the IO stream.
560
561 Returns the number of bytes written, which may be less than len(b).
562 """
563 self._unsupported("write")
564
565io.RawIOBase.register(RawIOBase)
566from _io import FileIO
567RawIOBase.register(FileIO)
568
569
570class BufferedIOBase(IOBase):
571
572 """Base class for buffered IO objects.
573
574 The main difference with RawIOBase is that the read() method
575 supports omitting the size argument, and does not have a default
576 implementation that defers to readinto().
577
578 In addition, read(), readinto() and write() may raise
579 BlockingIOError if the underlying raw stream is in non-blocking
580 mode and not ready; unlike their raw counterparts, they will never
581 return None.
582
583 A typical implementation should not inherit from a RawIOBase
584 implementation, but wrap one.
585 """
586
587 def read(self, n: int = None) -> bytes:
588 """Read and return up to n bytes.
589
590 If the argument is omitted, None, or negative, reads and
591 returns all data until EOF.
592
593 If the argument is positive, and the underlying raw stream is
594 not 'interactive', multiple raw reads may be issued to satisfy
595 the byte count (unless EOF is reached first). But for
596 interactive raw streams (XXX and for pipes?), at most one raw
597 read will be issued, and a short result does not imply that
598 EOF is imminent.
599
600 Returns an empty bytes array on EOF.
601
602 Raises BlockingIOError if the underlying raw stream has no
603 data at the moment.
604 """
605 self._unsupported("read")
606
607 def read1(self, n: int=None) -> bytes:
608 """Read up to n bytes with at most one read() system call."""
609 self._unsupported("read1")
610
611 def readinto(self, b: bytearray) -> int:
612 """Read up to len(b) bytes into b.
613
614 Like read(), this may issue multiple reads to the underlying raw
615 stream, unless the latter is 'interactive'.
616
617 Returns the number of bytes read (0 for EOF).
618
619 Raises BlockingIOError if the underlying raw stream has no
620 data at the moment.
621 """
622 # XXX This ought to work with anything that supports the buffer API
623 data = self.read(len(b))
624 n = len(data)
625 try:
626 b[:n] = data
627 except TypeError as err:
628 import array
629 if not isinstance(b, array.array):
630 raise err
631 b[:n] = array.array('b', data)
632 return n
633
634 def write(self, b: bytes) -> int:
635 """Write the given buffer to the IO stream.
636
637 Return the number of bytes written, which is never less than
638 len(b).
639
640 Raises BlockingIOError if the buffer is full and the
641 underlying raw stream cannot accept more data at the moment.
642 """
643 self._unsupported("write")
644
645io.BufferedIOBase.register(BufferedIOBase)
646
647
648class _BufferedIOMixin(BufferedIOBase):
649
650 """A mixin implementation of BufferedIOBase with an underlying raw stream.
651
652 This passes most requests on to the underlying raw stream. It
653 does *not* provide implementations of read(), readinto() or
654 write().
655 """
656
657 def __init__(self, raw):
658 self.raw = raw
659
660 ### Positioning ###
661
662 def seek(self, pos, whence=0):
663 new_position = self.raw.seek(pos, whence)
664 if new_position < 0:
665 raise IOError("seek() returned an invalid position")
666 return new_position
667
668 def tell(self):
669 pos = self.raw.tell()
670 if pos < 0:
671 raise IOError("tell() returned an invalid position")
672 return pos
673
674 def truncate(self, pos=None):
675 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
676 # and a flush may be necessary to synch both views of the current
677 # file state.
678 self.flush()
679
680 if pos is None:
681 pos = self.tell()
682 # XXX: Should seek() be used, instead of passing the position
683 # XXX directly to truncate?
684 return self.raw.truncate(pos)
685
686 ### Flush and close ###
687
688 def flush(self):
689 self.raw.flush()
690
691 def close(self):
692 if not self.closed:
693 try:
694 self.flush()
695 except IOError:
696 pass # If flush() fails, just give up
697 self.raw.close()
698
699 ### Inquiries ###
700
701 def seekable(self):
702 return self.raw.seekable()
703
704 def readable(self):
705 return self.raw.readable()
706
707 def writable(self):
708 return self.raw.writable()
709
710 @property
711 def closed(self):
712 return self.raw.closed
713
714 @property
715 def name(self):
716 return self.raw.name
717
718 @property
719 def mode(self):
720 return self.raw.mode
721
722 ### Lower-level APIs ###
723
724 def fileno(self):
725 return self.raw.fileno()
726
727 def isatty(self):
728 return self.raw.isatty()
729
730
731class BytesIO(BufferedIOBase):
732
733 """Buffered I/O implementation using an in-memory bytes buffer."""
734
735 def __init__(self, initial_bytes=None):
736 buf = bytearray()
737 if initial_bytes is not None:
738 buf += initial_bytes
739 self._buffer = buf
740 self._pos = 0
741
742 def getvalue(self):
743 """Return the bytes value (contents) of the buffer
744 """
745 if self.closed:
746 raise ValueError("getvalue on closed file")
747 return bytes(self._buffer)
748
749 def read(self, n=None):
750 if self.closed:
751 raise ValueError("read from closed file")
752 if n is None:
753 n = -1
754 if n < 0:
755 n = len(self._buffer)
756 if len(self._buffer) <= self._pos:
757 return b""
758 newpos = min(len(self._buffer), self._pos + n)
759 b = self._buffer[self._pos : newpos]
760 self._pos = newpos
761 return bytes(b)
762
763 def read1(self, n):
764 """This is the same as read.
765 """
766 return self.read(n)
767
768 def write(self, b):
769 if self.closed:
770 raise ValueError("write to closed file")
771 if isinstance(b, str):
772 raise TypeError("can't write str to binary stream")
773 n = len(b)
774 if n == 0:
775 return 0
776 pos = self._pos
777 if pos > len(self._buffer):
778 # Inserts null bytes between the current end of the file
779 # and the new write position.
780 padding = b'\x00' * (pos - len(self._buffer))
781 self._buffer += padding
782 self._buffer[pos:pos + n] = b
783 self._pos += n
784 return n
785
786 def seek(self, pos, whence=0):
787 if self.closed:
788 raise ValueError("seek on closed file")
789 try:
790 pos = pos.__index__()
791 except AttributeError as err:
792 raise TypeError("an integer is required") from err
793 if whence == 0:
794 if pos < 0:
795 raise ValueError("negative seek position %r" % (pos,))
796 self._pos = pos
797 elif whence == 1:
798 self._pos = max(0, self._pos + pos)
799 elif whence == 2:
800 self._pos = max(0, len(self._buffer) + pos)
801 else:
802 raise ValueError("invalid whence value")
803 return self._pos
804
805 def tell(self):
806 if self.closed:
807 raise ValueError("tell on closed file")
808 return self._pos
809
810 def truncate(self, pos=None):
811 if self.closed:
812 raise ValueError("truncate on closed file")
813 if pos is None:
814 pos = self._pos
815 elif pos < 0:
816 raise ValueError("negative truncate position %r" % (pos,))
817 del self._buffer[pos:]
818 return self.seek(pos)
819
820 def readable(self):
821 return True
822
823 def writable(self):
824 return True
825
826 def seekable(self):
827 return True
828
829
830class BufferedReader(_BufferedIOMixin):
831
832 """BufferedReader(raw[, buffer_size])
833
834 A buffer for a readable, sequential BaseRawIO object.
835
836 The constructor creates a BufferedReader for the given readable raw
837 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
838 is used.
839 """
840
841 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
842 """Create a new buffered reader using the given readable raw IO object.
843 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000844 if not raw.readable():
845 raise IOError('"raw" argument must be readable.')
846
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000847 _BufferedIOMixin.__init__(self, raw)
848 if buffer_size <= 0:
849 raise ValueError("invalid buffer size")
850 self.buffer_size = buffer_size
851 self._reset_read_buf()
852 self._read_lock = Lock()
853
854 def _reset_read_buf(self):
855 self._read_buf = b""
856 self._read_pos = 0
857
858 def read(self, n=None):
859 """Read n bytes.
860
861 Returns exactly n bytes of data unless the underlying raw IO
862 stream reaches EOF or if the call would block in non-blocking
863 mode. If n is negative, read until EOF or until read() would
864 block.
865 """
866 if n is not None and n < -1:
867 raise ValueError("invalid number of bytes to read")
868 with self._read_lock:
869 return self._read_unlocked(n)
870
871 def _read_unlocked(self, n=None):
872 nodata_val = b""
873 empty_values = (b"", None)
874 buf = self._read_buf
875 pos = self._read_pos
876
877 # Special case for when the number of bytes to read is unspecified.
878 if n is None or n == -1:
879 self._reset_read_buf()
880 chunks = [buf[pos:]] # Strip the consumed bytes.
881 current_size = 0
882 while True:
883 # Read until EOF or until read() would block.
884 chunk = self.raw.read()
885 if chunk in empty_values:
886 nodata_val = chunk
887 break
888 current_size += len(chunk)
889 chunks.append(chunk)
890 return b"".join(chunks) or nodata_val
891
892 # The number of bytes to read is specified, return at most n bytes.
893 avail = len(buf) - pos # Length of the available buffered data.
894 if n <= avail:
895 # Fast path: the data to read is fully buffered.
896 self._read_pos += n
897 return buf[pos:pos+n]
898 # Slow path: read from the stream until enough bytes are read,
899 # or until an EOF occurs or until read() would block.
900 chunks = [buf[pos:]]
901 wanted = max(self.buffer_size, n)
902 while avail < n:
903 chunk = self.raw.read(wanted)
904 if chunk in empty_values:
905 nodata_val = chunk
906 break
907 avail += len(chunk)
908 chunks.append(chunk)
909 # n is more then avail only when an EOF occurred or when
910 # read() would have blocked.
911 n = min(n, avail)
912 out = b"".join(chunks)
913 self._read_buf = out[n:] # Save the extra data in the buffer.
914 self._read_pos = 0
915 return out[:n] if out else nodata_val
916
917 def peek(self, n=0):
918 """Returns buffered bytes without advancing the position.
919
920 The argument indicates a desired minimal number of bytes; we
921 do at most one raw read to satisfy it. We never return more
922 than self.buffer_size.
923 """
924 with self._read_lock:
925 return self._peek_unlocked(n)
926
927 def _peek_unlocked(self, n=0):
928 want = min(n, self.buffer_size)
929 have = len(self._read_buf) - self._read_pos
930 if have < want or have <= 0:
931 to_read = self.buffer_size - have
932 current = self.raw.read(to_read)
933 if current:
934 self._read_buf = self._read_buf[self._read_pos:] + current
935 self._read_pos = 0
936 return self._read_buf[self._read_pos:]
937
938 def read1(self, n):
939 """Reads up to n bytes, with at most one read() system call."""
940 # Returns up to n bytes. If at least one byte is buffered, we
941 # only return buffered bytes. Otherwise, we do one raw read.
942 if n < 0:
943 raise ValueError("number of bytes to read must be positive")
944 if n == 0:
945 return b""
946 with self._read_lock:
947 self._peek_unlocked(1)
948 return self._read_unlocked(
949 min(n, len(self._read_buf) - self._read_pos))
950
951 def tell(self):
952 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
953
954 def seek(self, pos, whence=0):
955 if not (0 <= whence <= 2):
956 raise ValueError("invalid whence value")
957 with self._read_lock:
958 if whence == 1:
959 pos -= len(self._read_buf) - self._read_pos
960 pos = _BufferedIOMixin.seek(self, pos, whence)
961 self._reset_read_buf()
962 return pos
963
964class BufferedWriter(_BufferedIOMixin):
965
966 """A buffer for a writeable sequential RawIO object.
967
968 The constructor creates a BufferedWriter for the given writeable raw
969 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +0000970 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000971 """
972
Benjamin Peterson59406a92009-03-26 17:10:29 +0000973 _warning_stack_offset = 2
974
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000975 def __init__(self, raw,
976 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000977 if not raw.writable():
978 raise IOError('"raw" argument must be writable.')
979
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000980 _BufferedIOMixin.__init__(self, raw)
981 if buffer_size <= 0:
982 raise ValueError("invalid buffer size")
Benjamin Peterson59406a92009-03-26 17:10:29 +0000983 if max_buffer_size is not None:
984 warnings.warn("max_buffer_size is deprecated", DeprecationWarning,
985 self._warning_stack_offset)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000986 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000987 self._write_buf = bytearray()
988 self._write_lock = Lock()
989
990 def write(self, b):
991 if self.closed:
992 raise ValueError("write to closed file")
993 if isinstance(b, str):
994 raise TypeError("can't write str to binary stream")
995 with self._write_lock:
996 # XXX we can implement some more tricks to try and avoid
997 # partial writes
998 if len(self._write_buf) > self.buffer_size:
999 # We're full, so let's pre-flush the buffer
1000 try:
1001 self._flush_unlocked()
1002 except BlockingIOError as e:
1003 # We can't accept anything else.
1004 # XXX Why not just let the exception pass through?
1005 raise BlockingIOError(e.errno, e.strerror, 0)
1006 before = len(self._write_buf)
1007 self._write_buf.extend(b)
1008 written = len(self._write_buf) - before
1009 if len(self._write_buf) > self.buffer_size:
1010 try:
1011 self._flush_unlocked()
1012 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001013 if len(self._write_buf) > self.buffer_size:
1014 # We've hit the buffer_size. We have to accept a partial
1015 # write and cut back our buffer.
1016 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001017 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001018 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001019 raise BlockingIOError(e.errno, e.strerror, written)
1020 return written
1021
1022 def truncate(self, pos=None):
1023 with self._write_lock:
1024 self._flush_unlocked()
1025 if pos is None:
1026 pos = self.raw.tell()
1027 return self.raw.truncate(pos)
1028
1029 def flush(self):
1030 with self._write_lock:
1031 self._flush_unlocked()
1032
1033 def _flush_unlocked(self):
1034 if self.closed:
1035 raise ValueError("flush of closed file")
1036 written = 0
1037 try:
1038 while self._write_buf:
1039 n = self.raw.write(self._write_buf)
1040 if n > len(self._write_buf) or n < 0:
1041 raise IOError("write() returned incorrect number of bytes")
1042 del self._write_buf[:n]
1043 written += n
1044 except BlockingIOError as e:
1045 n = e.characters_written
1046 del self._write_buf[:n]
1047 written += n
1048 raise BlockingIOError(e.errno, e.strerror, written)
1049
1050 def tell(self):
1051 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1052
1053 def seek(self, pos, whence=0):
1054 if not (0 <= whence <= 2):
1055 raise ValueError("invalid whence")
1056 with self._write_lock:
1057 self._flush_unlocked()
1058 return _BufferedIOMixin.seek(self, pos, whence)
1059
1060
1061class BufferedRWPair(BufferedIOBase):
1062
1063 """A buffered reader and writer object together.
1064
1065 A buffered reader object and buffered writer object put together to
1066 form a sequential IO object that can read and write. This is typically
1067 used with a socket or two-way pipe.
1068
1069 reader and writer are RawIOBase objects that are readable and
1070 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001071 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001072 """
1073
1074 # XXX The usefulness of this (compared to having two separate IO
1075 # objects) is questionable.
1076
1077 def __init__(self, reader, writer,
1078 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1079 """Constructor.
1080
1081 The arguments are two RawIO instances.
1082 """
Benjamin Peterson59406a92009-03-26 17:10:29 +00001083 if max_buffer_size is not None:
1084 warnings.warn("max_buffer_size is deprecated", DeprecationWarning, 2)
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001085
1086 if not reader.readable():
1087 raise IOError('"reader" argument must be readable.')
1088
1089 if not writer.writable():
1090 raise IOError('"writer" argument must be writable.')
1091
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001092 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001093 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001094
1095 def read(self, n=None):
1096 if n is None:
1097 n = -1
1098 return self.reader.read(n)
1099
1100 def readinto(self, b):
1101 return self.reader.readinto(b)
1102
1103 def write(self, b):
1104 return self.writer.write(b)
1105
1106 def peek(self, n=0):
1107 return self.reader.peek(n)
1108
1109 def read1(self, n):
1110 return self.reader.read1(n)
1111
1112 def readable(self):
1113 return self.reader.readable()
1114
1115 def writable(self):
1116 return self.writer.writable()
1117
1118 def flush(self):
1119 return self.writer.flush()
1120
1121 def close(self):
1122 self.writer.close()
1123 self.reader.close()
1124
1125 def isatty(self):
1126 return self.reader.isatty() or self.writer.isatty()
1127
1128 @property
1129 def closed(self):
1130 return self.writer.closed
1131
1132
1133class BufferedRandom(BufferedWriter, BufferedReader):
1134
1135 """A buffered interface to random access streams.
1136
1137 The constructor creates a reader and writer for a seekable stream,
1138 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001139 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001140 """
1141
Benjamin Peterson59406a92009-03-26 17:10:29 +00001142 _warning_stack_offset = 3
1143
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001144 def __init__(self, raw,
1145 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1146 raw._checkSeekable()
1147 BufferedReader.__init__(self, raw, buffer_size)
1148 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
1149
1150 def seek(self, pos, whence=0):
1151 if not (0 <= whence <= 2):
1152 raise ValueError("invalid whence")
1153 self.flush()
1154 if self._read_buf:
1155 # Undo read ahead.
1156 with self._read_lock:
1157 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1158 # First do the raw seek, then empty the read buffer, so that
1159 # if the raw seek fails, we don't lose buffered data forever.
1160 pos = self.raw.seek(pos, whence)
1161 with self._read_lock:
1162 self._reset_read_buf()
1163 if pos < 0:
1164 raise IOError("seek() returned invalid position")
1165 return pos
1166
1167 def tell(self):
1168 if self._write_buf:
1169 return BufferedWriter.tell(self)
1170 else:
1171 return BufferedReader.tell(self)
1172
1173 def truncate(self, pos=None):
1174 if pos is None:
1175 pos = self.tell()
1176 # Use seek to flush the read buffer.
1177 self.seek(pos)
1178 return BufferedWriter.truncate(self)
1179
1180 def read(self, n=None):
1181 if n is None:
1182 n = -1
1183 self.flush()
1184 return BufferedReader.read(self, n)
1185
1186 def readinto(self, b):
1187 self.flush()
1188 return BufferedReader.readinto(self, b)
1189
1190 def peek(self, n=0):
1191 self.flush()
1192 return BufferedReader.peek(self, n)
1193
1194 def read1(self, n):
1195 self.flush()
1196 return BufferedReader.read1(self, n)
1197
1198 def write(self, b):
1199 if self._read_buf:
1200 # Undo readahead
1201 with self._read_lock:
1202 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1203 self._reset_read_buf()
1204 return BufferedWriter.write(self, b)
1205
1206
1207class TextIOBase(IOBase):
1208
1209 """Base class for text I/O.
1210
1211 This class provides a character and line based interface to stream
1212 I/O. There is no readinto method because Python's character strings
1213 are immutable. There is no public constructor.
1214 """
1215
1216 def read(self, n: int = -1) -> str:
1217 """Read at most n characters from stream.
1218
1219 Read from underlying buffer until we have n characters or we hit EOF.
1220 If n is negative or omitted, read until EOF.
1221 """
1222 self._unsupported("read")
1223
1224 def write(self, s: str) -> int:
1225 """Write string s to stream."""
1226 self._unsupported("write")
1227
1228 def truncate(self, pos: int = None) -> int:
1229 """Truncate size to pos."""
1230 self._unsupported("truncate")
1231
1232 def readline(self) -> str:
1233 """Read until newline or EOF.
1234
1235 Returns an empty string if EOF is hit immediately.
1236 """
1237 self._unsupported("readline")
1238
1239 @property
1240 def encoding(self):
1241 """Subclasses should override."""
1242 return None
1243
1244 @property
1245 def newlines(self):
1246 """Line endings translated so far.
1247
1248 Only line endings translated during reading are considered.
1249
1250 Subclasses should override.
1251 """
1252 return None
1253
1254io.TextIOBase.register(TextIOBase)
1255
1256
1257class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1258 r"""Codec used when reading a file in universal newlines mode. It wraps
1259 another incremental decoder, translating \r\n and \r into \n. It also
1260 records the types of newlines encountered. When used with
1261 translate=False, it ensures that the newline sequence is returned in
1262 one piece.
1263 """
1264 def __init__(self, decoder, translate, errors='strict'):
1265 codecs.IncrementalDecoder.__init__(self, errors=errors)
1266 self.translate = translate
1267 self.decoder = decoder
1268 self.seennl = 0
1269 self.pendingcr = False
1270
1271 def decode(self, input, final=False):
1272 # decode input (with the eventual \r from a previous pass)
1273 if self.decoder is None:
1274 output = input
1275 else:
1276 output = self.decoder.decode(input, final=final)
1277 if self.pendingcr and (output or final):
1278 output = "\r" + output
1279 self.pendingcr = False
1280
1281 # retain last \r even when not translating data:
1282 # then readline() is sure to get \r\n in one pass
1283 if output.endswith("\r") and not final:
1284 output = output[:-1]
1285 self.pendingcr = True
1286
1287 # Record which newlines are read
1288 crlf = output.count('\r\n')
1289 cr = output.count('\r') - crlf
1290 lf = output.count('\n') - crlf
1291 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1292 | (crlf and self._CRLF)
1293
1294 if self.translate:
1295 if crlf:
1296 output = output.replace("\r\n", "\n")
1297 if cr:
1298 output = output.replace("\r", "\n")
1299
1300 return output
1301
1302 def getstate(self):
1303 if self.decoder is None:
1304 buf = b""
1305 flag = 0
1306 else:
1307 buf, flag = self.decoder.getstate()
1308 flag <<= 1
1309 if self.pendingcr:
1310 flag |= 1
1311 return buf, flag
1312
1313 def setstate(self, state):
1314 buf, flag = state
1315 self.pendingcr = bool(flag & 1)
1316 if self.decoder is not None:
1317 self.decoder.setstate((buf, flag >> 1))
1318
1319 def reset(self):
1320 self.seennl = 0
1321 self.pendingcr = False
1322 if self.decoder is not None:
1323 self.decoder.reset()
1324
1325 _LF = 1
1326 _CR = 2
1327 _CRLF = 4
1328
1329 @property
1330 def newlines(self):
1331 return (None,
1332 "\n",
1333 "\r",
1334 ("\r", "\n"),
1335 "\r\n",
1336 ("\n", "\r\n"),
1337 ("\r", "\r\n"),
1338 ("\r", "\n", "\r\n")
1339 )[self.seennl]
1340
1341
1342class TextIOWrapper(TextIOBase):
1343
1344 r"""Character and line based layer over a BufferedIOBase object, buffer.
1345
1346 encoding gives the name of the encoding that the stream will be
1347 decoded or encoded with. It defaults to locale.getpreferredencoding.
1348
1349 errors determines the strictness of encoding and decoding (see the
1350 codecs.register) and defaults to "strict".
1351
1352 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1353 handling of line endings. If it is None, universal newlines is
1354 enabled. With this enabled, on input, the lines endings '\n', '\r',
1355 or '\r\n' are translated to '\n' before being returned to the
1356 caller. Conversely, on output, '\n' is translated to the system
1357 default line seperator, os.linesep. If newline is any other of its
1358 legal values, that newline becomes the newline when the file is read
1359 and it is returned untranslated. On output, '\n' is converted to the
1360 newline.
1361
1362 If line_buffering is True, a call to flush is implied when a call to
1363 write contains a newline character.
1364 """
1365
1366 _CHUNK_SIZE = 2048
1367
1368 def __init__(self, buffer, encoding=None, errors=None, newline=None,
1369 line_buffering=False):
1370 if newline is not None and not isinstance(newline, str):
1371 raise TypeError("illegal newline type: %r" % (type(newline),))
1372 if newline not in (None, "", "\n", "\r", "\r\n"):
1373 raise ValueError("illegal newline value: %r" % (newline,))
1374 if encoding is None:
1375 try:
1376 encoding = os.device_encoding(buffer.fileno())
1377 except (AttributeError, UnsupportedOperation):
1378 pass
1379 if encoding is None:
1380 try:
1381 import locale
1382 except ImportError:
1383 # Importing locale may fail if Python is being built
1384 encoding = "ascii"
1385 else:
1386 encoding = locale.getpreferredencoding()
1387
1388 if not isinstance(encoding, str):
1389 raise ValueError("invalid encoding: %r" % encoding)
1390
1391 if errors is None:
1392 errors = "strict"
1393 else:
1394 if not isinstance(errors, str):
1395 raise ValueError("invalid errors: %r" % errors)
1396
1397 self.buffer = buffer
1398 self._line_buffering = line_buffering
1399 self._encoding = encoding
1400 self._errors = errors
1401 self._readuniversal = not newline
1402 self._readtranslate = newline is None
1403 self._readnl = newline
1404 self._writetranslate = newline != ''
1405 self._writenl = newline or os.linesep
1406 self._encoder = None
1407 self._decoder = None
1408 self._decoded_chars = '' # buffer for text returned from decoder
1409 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1410 self._snapshot = None # info for reconstructing decoder state
1411 self._seekable = self._telling = self.buffer.seekable()
1412
1413 # self._snapshot is either None, or a tuple (dec_flags, next_input)
1414 # where dec_flags is the second (integer) item of the decoder state
1415 # and next_input is the chunk of input bytes that comes next after the
1416 # snapshot point. We use this to reconstruct decoder states in tell().
1417
1418 # Naming convention:
1419 # - "bytes_..." for integer variables that count input bytes
1420 # - "chars_..." for integer variables that count decoded characters
1421
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001422 def __repr__(self):
1423 return "<TextIOWrapper encoding={0}>".format(self.encoding)
1424
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001425 @property
1426 def encoding(self):
1427 return self._encoding
1428
1429 @property
1430 def errors(self):
1431 return self._errors
1432
1433 @property
1434 def line_buffering(self):
1435 return self._line_buffering
1436
1437 def seekable(self):
1438 return self._seekable
1439
1440 def readable(self):
1441 return self.buffer.readable()
1442
1443 def writable(self):
1444 return self.buffer.writable()
1445
1446 def flush(self):
1447 self.buffer.flush()
1448 self._telling = self._seekable
1449
1450 def close(self):
1451 try:
1452 self.flush()
Benjamin Peterson54f963e2009-04-14 22:02:08 +00001453 except IOError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001454 pass # If flush() fails, just give up
1455 self.buffer.close()
1456
1457 @property
1458 def closed(self):
1459 return self.buffer.closed
1460
1461 @property
1462 def name(self):
1463 return self.buffer.name
1464
1465 def fileno(self):
1466 return self.buffer.fileno()
1467
1468 def isatty(self):
1469 return self.buffer.isatty()
1470
1471 def write(self, s: str):
1472 if self.closed:
1473 raise ValueError("write to closed file")
1474 if not isinstance(s, str):
1475 raise TypeError("can't write %s to text stream" %
1476 s.__class__.__name__)
1477 length = len(s)
1478 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
1479 if haslf and self._writetranslate and self._writenl != "\n":
1480 s = s.replace("\n", self._writenl)
1481 encoder = self._encoder or self._get_encoder()
1482 # XXX What if we were just reading?
1483 b = encoder.encode(s)
1484 self.buffer.write(b)
1485 if self._line_buffering and (haslf or "\r" in s):
1486 self.flush()
1487 self._snapshot = None
1488 if self._decoder:
1489 self._decoder.reset()
1490 return length
1491
1492 def _get_encoder(self):
1493 make_encoder = codecs.getincrementalencoder(self._encoding)
1494 self._encoder = make_encoder(self._errors)
1495 return self._encoder
1496
1497 def _get_decoder(self):
1498 make_decoder = codecs.getincrementaldecoder(self._encoding)
1499 decoder = make_decoder(self._errors)
1500 if self._readuniversal:
1501 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
1502 self._decoder = decoder
1503 return decoder
1504
1505 # The following three methods implement an ADT for _decoded_chars.
1506 # Text returned from the decoder is buffered here until the client
1507 # requests it by calling our read() or readline() method.
1508 def _set_decoded_chars(self, chars):
1509 """Set the _decoded_chars buffer."""
1510 self._decoded_chars = chars
1511 self._decoded_chars_used = 0
1512
1513 def _get_decoded_chars(self, n=None):
1514 """Advance into the _decoded_chars buffer."""
1515 offset = self._decoded_chars_used
1516 if n is None:
1517 chars = self._decoded_chars[offset:]
1518 else:
1519 chars = self._decoded_chars[offset:offset + n]
1520 self._decoded_chars_used += len(chars)
1521 return chars
1522
1523 def _rewind_decoded_chars(self, n):
1524 """Rewind the _decoded_chars buffer."""
1525 if self._decoded_chars_used < n:
1526 raise AssertionError("rewind decoded_chars out of bounds")
1527 self._decoded_chars_used -= n
1528
1529 def _read_chunk(self):
1530 """
1531 Read and decode the next chunk of data from the BufferedReader.
1532 """
1533
1534 # The return value is True unless EOF was reached. The decoded
1535 # string is placed in self._decoded_chars (replacing its previous
1536 # value). The entire input chunk is sent to the decoder, though
1537 # some of it may remain buffered in the decoder, yet to be
1538 # converted.
1539
1540 if self._decoder is None:
1541 raise ValueError("no decoder")
1542
1543 if self._telling:
1544 # To prepare for tell(), we need to snapshot a point in the
1545 # file where the decoder's input buffer is empty.
1546
1547 dec_buffer, dec_flags = self._decoder.getstate()
1548 # Given this, we know there was a valid snapshot point
1549 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1550
1551 # Read a chunk, decode it, and put the result in self._decoded_chars.
1552 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1553 eof = not input_chunk
1554 self._set_decoded_chars(self._decoder.decode(input_chunk, eof))
1555
1556 if self._telling:
1557 # At the snapshot point, len(dec_buffer) bytes before the read,
1558 # the next input to be decoded is dec_buffer + input_chunk.
1559 self._snapshot = (dec_flags, dec_buffer + input_chunk)
1560
1561 return not eof
1562
1563 def _pack_cookie(self, position, dec_flags=0,
1564 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
1565 # The meaning of a tell() cookie is: seek to position, set the
1566 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
1567 # into the decoder with need_eof as the EOF flag, then skip
1568 # chars_to_skip characters of the decoded result. For most simple
1569 # decoders, tell() will often just give a byte offset in the file.
1570 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
1571 (chars_to_skip<<192) | bool(need_eof)<<256)
1572
1573 def _unpack_cookie(self, bigint):
1574 rest, position = divmod(bigint, 1<<64)
1575 rest, dec_flags = divmod(rest, 1<<64)
1576 rest, bytes_to_feed = divmod(rest, 1<<64)
1577 need_eof, chars_to_skip = divmod(rest, 1<<64)
1578 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
1579
1580 def tell(self):
1581 if not self._seekable:
1582 raise IOError("underlying stream is not seekable")
1583 if not self._telling:
1584 raise IOError("telling position disabled by next() call")
1585 self.flush()
1586 position = self.buffer.tell()
1587 decoder = self._decoder
1588 if decoder is None or self._snapshot is None:
1589 if self._decoded_chars:
1590 # This should never happen.
1591 raise AssertionError("pending decoded text")
1592 return position
1593
1594 # Skip backward to the snapshot point (see _read_chunk).
1595 dec_flags, next_input = self._snapshot
1596 position -= len(next_input)
1597
1598 # How many decoded characters have been used up since the snapshot?
1599 chars_to_skip = self._decoded_chars_used
1600 if chars_to_skip == 0:
1601 # We haven't moved from the snapshot point.
1602 return self._pack_cookie(position, dec_flags)
1603
1604 # Starting from the snapshot position, we will walk the decoder
1605 # forward until it gives us enough decoded characters.
1606 saved_state = decoder.getstate()
1607 try:
1608 # Note our initial start point.
1609 decoder.setstate((b'', dec_flags))
1610 start_pos = position
1611 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1612 need_eof = 0
1613
1614 # Feed the decoder one byte at a time. As we go, note the
1615 # nearest "safe start point" before the current location
1616 # (a point where the decoder has nothing buffered, so seek()
1617 # can safely start from there and advance to this location).
1618 next_byte = bytearray(1)
1619 for next_byte[0] in next_input:
1620 bytes_fed += 1
1621 chars_decoded += len(decoder.decode(next_byte))
1622 dec_buffer, dec_flags = decoder.getstate()
1623 if not dec_buffer and chars_decoded <= chars_to_skip:
1624 # Decoder buffer is empty, so this is a safe start point.
1625 start_pos += bytes_fed
1626 chars_to_skip -= chars_decoded
1627 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1628 if chars_decoded >= chars_to_skip:
1629 break
1630 else:
1631 # We didn't get enough decoded data; signal EOF to get more.
1632 chars_decoded += len(decoder.decode(b'', final=True))
1633 need_eof = 1
1634 if chars_decoded < chars_to_skip:
1635 raise IOError("can't reconstruct logical file position")
1636
1637 # The returned cookie corresponds to the last safe start point.
1638 return self._pack_cookie(
1639 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
1640 finally:
1641 decoder.setstate(saved_state)
1642
1643 def truncate(self, pos=None):
1644 self.flush()
1645 if pos is None:
1646 pos = self.tell()
1647 self.seek(pos)
1648 return self.buffer.truncate()
1649
1650 def seek(self, cookie, whence=0):
1651 if self.closed:
1652 raise ValueError("tell on closed file")
1653 if not self._seekable:
1654 raise IOError("underlying stream is not seekable")
1655 if whence == 1: # seek relative to current position
1656 if cookie != 0:
1657 raise IOError("can't do nonzero cur-relative seeks")
1658 # Seeking to the current position should attempt to
1659 # sync the underlying buffer with the current position.
1660 whence = 0
1661 cookie = self.tell()
1662 if whence == 2: # seek relative to end of file
1663 if cookie != 0:
1664 raise IOError("can't do nonzero end-relative seeks")
1665 self.flush()
1666 position = self.buffer.seek(0, 2)
1667 self._set_decoded_chars('')
1668 self._snapshot = None
1669 if self._decoder:
1670 self._decoder.reset()
1671 return position
1672 if whence != 0:
1673 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" %
1674 (whence,))
1675 if cookie < 0:
1676 raise ValueError("negative seek position %r" % (cookie,))
1677 self.flush()
1678
1679 # The strategy of seek() is to go back to the safe start point
1680 # and replay the effect of read(chars_to_skip) from there.
1681 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
1682 self._unpack_cookie(cookie)
1683
1684 # Seek back to the safe start point.
1685 self.buffer.seek(start_pos)
1686 self._set_decoded_chars('')
1687 self._snapshot = None
1688
1689 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00001690 if cookie == 0 and self._decoder:
1691 self._decoder.reset()
1692 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001693 self._decoder = self._decoder or self._get_decoder()
1694 self._decoder.setstate((b'', dec_flags))
1695 self._snapshot = (dec_flags, b'')
1696
1697 if chars_to_skip:
1698 # Just like _read_chunk, feed the decoder and save a snapshot.
1699 input_chunk = self.buffer.read(bytes_to_feed)
1700 self._set_decoded_chars(
1701 self._decoder.decode(input_chunk, need_eof))
1702 self._snapshot = (dec_flags, input_chunk)
1703
1704 # Skip chars_to_skip of the decoded characters.
1705 if len(self._decoded_chars) < chars_to_skip:
1706 raise IOError("can't restore logical file position")
1707 self._decoded_chars_used = chars_to_skip
1708
1709 return cookie
1710
1711 def read(self, n=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00001712 self._checkReadable()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001713 if n is None:
1714 n = -1
1715 decoder = self._decoder or self._get_decoder()
1716 if n < 0:
1717 # Read everything.
1718 result = (self._get_decoded_chars() +
1719 decoder.decode(self.buffer.read(), final=True))
1720 self._set_decoded_chars('')
1721 self._snapshot = None
1722 return result
1723 else:
1724 # Keep reading chunks until we have n characters to return.
1725 eof = False
1726 result = self._get_decoded_chars(n)
1727 while len(result) < n and not eof:
1728 eof = not self._read_chunk()
1729 result += self._get_decoded_chars(n - len(result))
1730 return result
1731
1732 def __next__(self):
1733 self._telling = False
1734 line = self.readline()
1735 if not line:
1736 self._snapshot = None
1737 self._telling = self._seekable
1738 raise StopIteration
1739 return line
1740
1741 def readline(self, limit=None):
1742 if self.closed:
1743 raise ValueError("read from closed file")
1744 if limit is None:
1745 limit = -1
Benjamin Petersonb01138a2009-04-24 22:59:52 +00001746 elif not isinstance(limit, int):
1747 raise TypeError("limit must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001748
1749 # Grab all the decoded text (we will rewind any extra bits later).
1750 line = self._get_decoded_chars()
1751
1752 start = 0
1753 # Make the decoder if it doesn't already exist.
1754 if not self._decoder:
1755 self._get_decoder()
1756
1757 pos = endpos = None
1758 while True:
1759 if self._readtranslate:
1760 # Newlines are already translated, only search for \n
1761 pos = line.find('\n', start)
1762 if pos >= 0:
1763 endpos = pos + 1
1764 break
1765 else:
1766 start = len(line)
1767
1768 elif self._readuniversal:
1769 # Universal newline search. Find any of \r, \r\n, \n
1770 # The decoder ensures that \r\n are not split in two pieces
1771
1772 # In C we'd look for these in parallel of course.
1773 nlpos = line.find("\n", start)
1774 crpos = line.find("\r", start)
1775 if crpos == -1:
1776 if nlpos == -1:
1777 # Nothing found
1778 start = len(line)
1779 else:
1780 # Found \n
1781 endpos = nlpos + 1
1782 break
1783 elif nlpos == -1:
1784 # Found lone \r
1785 endpos = crpos + 1
1786 break
1787 elif nlpos < crpos:
1788 # Found \n
1789 endpos = nlpos + 1
1790 break
1791 elif nlpos == crpos + 1:
1792 # Found \r\n
1793 endpos = crpos + 2
1794 break
1795 else:
1796 # Found \r
1797 endpos = crpos + 1
1798 break
1799 else:
1800 # non-universal
1801 pos = line.find(self._readnl)
1802 if pos >= 0:
1803 endpos = pos + len(self._readnl)
1804 break
1805
1806 if limit >= 0 and len(line) >= limit:
1807 endpos = limit # reached length limit
1808 break
1809
1810 # No line ending seen yet - get more data'
1811 while self._read_chunk():
1812 if self._decoded_chars:
1813 break
1814 if self._decoded_chars:
1815 line += self._get_decoded_chars()
1816 else:
1817 # end of file
1818 self._set_decoded_chars('')
1819 self._snapshot = None
1820 return line
1821
1822 if limit >= 0 and endpos > limit:
1823 endpos = limit # don't exceed limit
1824
1825 # Rewind _decoded_chars to just after the line ending we found.
1826 self._rewind_decoded_chars(len(line) - endpos)
1827 return line[:endpos]
1828
1829 @property
1830 def newlines(self):
1831 return self._decoder.newlines if self._decoder else None
1832
1833
1834class StringIO(TextIOWrapper):
1835 """Text I/O implementation using an in-memory buffer.
1836
1837 The initial_value argument sets the value of object. The newline
1838 argument is like the one of TextIOWrapper's constructor.
1839 """
1840
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001841 def __init__(self, initial_value="", newline="\n"):
1842 super(StringIO, self).__init__(BytesIO(),
1843 encoding="utf-8",
1844 errors="strict",
1845 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00001846 # Issue #5645: make universal newlines semantics the same as in the
1847 # C version, even under Windows.
1848 if newline is None:
1849 self._writetranslate = False
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001850 if initial_value:
1851 if not isinstance(initial_value, str):
1852 initial_value = str(initial_value)
1853 self.write(initial_value)
1854 self.seek(0)
1855
1856 def getvalue(self):
1857 self.flush()
1858 return self.buffer.getvalue().decode(self._encoding, self._errors)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00001859
1860 def __repr__(self):
1861 # TextIOWrapper tells the encoding in its repr. In StringIO,
1862 # that's a implementation detail.
1863 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00001864
1865 @property
1866 def encoding(self):
1867 return None