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