blob: be5e4266daffd8339cd5818d2f481ca4e5fef10d [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
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01008import errno
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03009import stat
Serhiy Storchakaf0f55a02015-08-28 22:17:04 +030010import sys
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000011# Import _thread instead of threading to reduce startup cost
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020012from _thread import allocate_lock as Lock
Serhiy Storchakaf0f55a02015-08-28 22:17:04 +030013if sys.platform in {'win32', 'cygwin'}:
Serhiy Storchaka71fd2242015-04-10 16:16:16 +030014 from msvcrt import setmode as _setmode
15else:
16 _setmode = None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000017
18import io
Benjamin Petersonc3be11a2010-04-27 21:24:03 +000019from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000020
Jesus Cea94363612012-06-22 18:32:07 +020021valid_seek_flags = {0, 1, 2} # Hardwired values
22if hasattr(os, 'SEEK_HOLE') :
23 valid_seek_flags.add(os.SEEK_HOLE)
24 valid_seek_flags.add(os.SEEK_DATA)
25
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000026# open() uses st_blksize whenever we can
27DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
28
29# NOTE: Base classes defined here are registered with the "official" ABCs
Benjamin Peterson86fdbf32015-03-18 21:35:38 -050030# defined in io.py. We don't use real inheritance though, because we don't want
31# to inherit the C implementations.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000032
Antoine Pitrou6b4883d2011-10-12 02:54:14 +020033# Rebind for compatibility
34BlockingIOError = BlockingIOError
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000035
Victor Stinnerbc2aa812019-05-23 03:45:09 +020036# Does io.IOBase finalizer log the exception if the close() method fails?
37# The exception is ignored silently by default in release build.
38_IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
39
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000040
Georg Brandl4d73b572011-01-13 07:13:06 +000041def open(file, mode="r", buffering=-1, encoding=None, errors=None,
Ross Lagerwall59142db2011-10-31 20:34:46 +020042 newline=None, closefd=True, opener=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000043
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020044 r"""Open file and return a stream. Raise OSError upon failure.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000045
46 file is either a text or byte string giving the name (and the path
47 if the file isn't in the current working directory) of the file to
48 be opened or an integer file descriptor of the file to be
49 wrapped. (If a file descriptor is given, it is closed when the
50 returned I/O object is closed, unless closefd is set to False.)
51
Charles-François Natalidc3044c2012-01-09 22:40:02 +010052 mode is an optional string that specifies the mode in which the file is
53 opened. It defaults to 'r' which means open for reading in text mode. Other
54 common values are 'w' for writing (truncating the file if it already
Charles-François Natalid612de12012-01-14 11:51:00 +010055 exists), 'x' for exclusive creation of a new file, and 'a' for appending
Charles-François Natalidc3044c2012-01-09 22:40:02 +010056 (which on some Unix systems, means that all writes append to the end of the
57 file regardless of the current seek position). In text mode, if encoding is
58 not specified the encoding used is platform dependent. (For reading and
59 writing raw bytes use binary mode and leave encoding unspecified.) The
60 available modes are:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000061
62 ========= ===============================================================
63 Character Meaning
64 --------- ---------------------------------------------------------------
65 'r' open for reading (default)
66 'w' open for writing, truncating the file first
Charles-François Natalidc3044c2012-01-09 22:40:02 +010067 'x' create a new file and open it for writing
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068 'a' open for writing, appending to the end of the file if it exists
69 'b' binary mode
70 't' text mode (default)
71 '+' open a disk file for updating (reading and writing)
Serhiy Storchaka6787a382013-11-23 22:12:06 +020072 'U' universal newline mode (deprecated)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000073 ========= ===============================================================
74
75 The default mode is 'rt' (open for reading text). For binary random
76 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
Charles-François Natalidc3044c2012-01-09 22:40:02 +010077 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
78 raises an `FileExistsError` if the file already exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000079
80 Python distinguishes between files opened in binary and text modes,
81 even when the underlying operating system doesn't. Files opened in
82 binary mode (appending 'b' to the mode argument) return contents as
83 bytes objects without any decoding. In text mode (the default, or when
84 't' is appended to the mode argument), the contents of the file are
85 returned as strings, the bytes having been first decoded using a
86 platform-dependent encoding or using the specified encoding if given.
87
Serhiy Storchaka6787a382013-11-23 22:12:06 +020088 'U' mode is deprecated and will raise an exception in future versions
89 of Python. It has no effect in Python 3. Use newline to control
90 universal newlines mode.
91
Antoine Pitroud5587bc2009-12-19 21:08:31 +000092 buffering is an optional integer used to set the buffering policy.
93 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
94 line buffering (only usable in text mode), and an integer > 1 to indicate
95 the size of a fixed-size chunk buffer. When no buffering argument is
96 given, the default buffering policy works as follows:
97
98 * Binary files are buffered in fixed-size chunks; the size of the buffer
99 is chosen using a heuristic trying to determine the underlying device's
100 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
101 On many systems, the buffer will typically be 4096 or 8192 bytes long.
102
103 * "Interactive" text files (files for which isatty() returns True)
104 use line buffering. Other text files use the policy described above
105 for binary files.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000106
Raymond Hettingercbb80892011-01-13 18:15:51 +0000107 encoding is the str name of the encoding used to decode or encode the
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000108 file. This should only be used in text mode. The default encoding is
109 platform dependent, but any encoding supported by Python can be
110 passed. See the codecs module for the list of supported encodings.
111
112 errors is an optional string that specifies how encoding errors are to
113 be handled---this argument should not be used in binary mode. Pass
114 'strict' to raise a ValueError exception if there is an encoding error
115 (the default of None has the same effect), or pass 'ignore' to ignore
116 errors. (Note that ignoring encoding errors can lead to data loss.)
117 See the documentation for codecs.register for a list of the permitted
118 encoding error strings.
119
Raymond Hettingercbb80892011-01-13 18:15:51 +0000120 newline is a string controlling how universal newlines works (it only
121 applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
122 as follows:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000123
124 * On input, if newline is None, universal newlines mode is
125 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
126 these are translated into '\n' before being returned to the
127 caller. If it is '', universal newline mode is enabled, but line
128 endings are returned to the caller untranslated. If it has any of
129 the other legal values, input lines are only terminated by the given
130 string, and the line ending is returned to the caller untranslated.
131
132 * On output, if newline is None, any '\n' characters written are
133 translated to the system default line separator, os.linesep. If
134 newline is '', no translation takes place. If newline is any of the
135 other legal values, any '\n' characters written are translated to
136 the given string.
137
Raymond Hettingercbb80892011-01-13 18:15:51 +0000138 closedfd is a bool. If closefd is False, the underlying file descriptor will
139 be kept open when the file is closed. This does not work when a file name is
140 given and must be True in that case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000141
Victor Stinnerdaf45552013-08-28 00:53:59 +0200142 The newly created file is non-inheritable.
143
Ross Lagerwall59142db2011-10-31 20:34:46 +0200144 A custom opener can be used by passing a callable as *opener*. The
145 underlying file descriptor for the file object is then obtained by calling
146 *opener* with (*file*, *flags*). *opener* must return an open file
147 descriptor (passing os.open as *opener* results in functionality similar to
148 passing None).
149
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000150 open() returns a file object whose type depends on the mode, and
151 through which the standard file operations such as reading and writing
152 are performed. When open() is used to open a file in a text mode ('w',
153 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
154 a file in a binary mode, the returned class varies: in read binary
155 mode, it returns a BufferedReader; in write binary and append binary
156 modes, it returns a BufferedWriter, and in read/write mode, it returns
157 a BufferedRandom.
158
159 It is also possible to use a string or bytearray as a file for both
160 reading and writing. For strings StringIO can be used like a file
161 opened in a text mode, and for bytes a BytesIO can be used like a file
162 opened in a binary mode.
163 """
Ethan Furmand62548a2016-06-04 14:38:43 -0700164 if not isinstance(file, int):
165 file = os.fspath(file)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000166 if not isinstance(file, (str, bytes, int)):
167 raise TypeError("invalid file: %r" % file)
168 if not isinstance(mode, str):
169 raise TypeError("invalid mode: %r" % mode)
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000170 if not isinstance(buffering, int):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000171 raise TypeError("invalid buffering: %r" % buffering)
172 if encoding is not None and not isinstance(encoding, str):
173 raise TypeError("invalid encoding: %r" % encoding)
174 if errors is not None and not isinstance(errors, str):
175 raise TypeError("invalid errors: %r" % errors)
176 modes = set(mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100177 if modes - set("axrwb+tU") or len(mode) > len(modes):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000178 raise ValueError("invalid mode: %r" % mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100179 creating = "x" in modes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180 reading = "r" in modes
181 writing = "w" in modes
182 appending = "a" in modes
183 updating = "+" in modes
184 text = "t" in modes
185 binary = "b" in modes
186 if "U" in modes:
Robert Collinsc94a1dc2015-07-26 06:43:13 +1200187 if creating or writing or appending or updating:
188 raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200189 import warnings
190 warnings.warn("'U' mode is deprecated",
191 DeprecationWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000192 reading = True
193 if text and binary:
194 raise ValueError("can't have text and binary mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100195 if creating + reading + writing + appending > 1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000196 raise ValueError("can't have read/write/append mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100197 if not (creating or reading or writing or appending):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000198 raise ValueError("must have exactly one of read/write/append mode")
199 if binary and encoding is not None:
200 raise ValueError("binary mode doesn't take an encoding argument")
201 if binary and errors is not None:
202 raise ValueError("binary mode doesn't take an errors argument")
203 if binary and newline is not None:
204 raise ValueError("binary mode doesn't take a newline argument")
Alexey Izbysheva2670562018-10-20 03:22:31 +0300205 if binary and buffering == 1:
206 import warnings
207 warnings.warn("line buffering (buffering=1) isn't supported in binary "
208 "mode, the default buffer size will be used",
209 RuntimeWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000210 raw = FileIO(file,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100211 (creating and "x" or "") +
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000212 (reading and "r" or "") +
213 (writing and "w" or "") +
214 (appending and "a" or "") +
215 (updating and "+" or ""),
Ross Lagerwall59142db2011-10-31 20:34:46 +0200216 closefd, opener=opener)
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300217 result = raw
218 try:
219 line_buffering = False
220 if buffering == 1 or buffering < 0 and raw.isatty():
221 buffering = -1
222 line_buffering = True
223 if buffering < 0:
224 buffering = DEFAULT_BUFFER_SIZE
225 try:
226 bs = os.fstat(raw.fileno()).st_blksize
227 except (OSError, AttributeError):
228 pass
229 else:
230 if bs > 1:
231 buffering = bs
232 if buffering < 0:
233 raise ValueError("invalid buffering size")
234 if buffering == 0:
235 if binary:
236 return result
237 raise ValueError("can't have unbuffered text I/O")
238 if updating:
239 buffer = BufferedRandom(raw, buffering)
240 elif creating or writing or appending:
241 buffer = BufferedWriter(raw, buffering)
242 elif reading:
243 buffer = BufferedReader(raw, buffering)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000244 else:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300245 raise ValueError("unknown mode: %r" % mode)
246 result = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000247 if binary:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300248 return result
249 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
250 result = text
251 text.mode = mode
252 return result
253 except:
254 result.close()
255 raise
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000256
257
258class DocDescriptor:
259 """Helper for builtins.open.__doc__
260 """
261 def __get__(self, obj, typ):
262 return (
Benjamin Petersonc3be11a2010-04-27 21:24:03 +0000263 "open(file, mode='r', buffering=-1, encoding=None, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000264 "errors=None, newline=None, closefd=True)\n\n" +
265 open.__doc__)
266
267class OpenWrapper:
268 """Wrapper for builtins.open
269
270 Trick so that open won't become a bound method when stored
271 as a class variable (as dbm.dumb does).
272
Nick Coghland6009512014-11-20 21:39:37 +1000273 See initstdio() in Python/pylifecycle.c.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000274 """
275 __doc__ = DocDescriptor()
276
277 def __new__(cls, *args, **kwargs):
278 return open(*args, **kwargs)
279
280
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000281# In normal operation, both `UnsupportedOperation`s should be bound to the
282# same object.
283try:
284 UnsupportedOperation = io.UnsupportedOperation
285except AttributeError:
Serhiy Storchaka606ab862016-12-07 13:31:20 +0200286 class UnsupportedOperation(OSError, ValueError):
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000287 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000288
289
290class IOBase(metaclass=abc.ABCMeta):
291
292 """The abstract base class for all I/O classes, acting on streams of
293 bytes. There is no public constructor.
294
295 This class provides dummy implementations for many methods that
296 derived classes can override selectively; the default implementations
297 represent a file that cannot be read, written or seeked.
298
Steve Palmer7b97ab32019-04-09 05:35:27 +0100299 Even though IOBase does not declare read or write because
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000300 their signatures will vary, implementations and clients should
301 consider those methods part of the interface. Also, implementations
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000302 may raise UnsupportedOperation when operations they do not support are
303 called.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000304
305 The basic type used for binary data read from or written to a file is
Steve Palmer7b97ab32019-04-09 05:35:27 +0100306 bytes. Other bytes-like objects are accepted as method arguments too.
307 Text I/O classes work with str data.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000308
309 Note that calling any method (even inquiries) on a closed stream is
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200310 undefined. Implementations may raise OSError in this case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000311
312 IOBase (and its subclasses) support the iterator protocol, meaning
313 that an IOBase object can be iterated over yielding the lines in a
314 stream.
315
316 IOBase also supports the :keyword:`with` statement. In this example,
317 fp is closed after the suite of the with statement is complete:
318
319 with open('spam.txt', 'r') as fp:
320 fp.write('Spam and eggs!')
321 """
322
323 ### Internal ###
324
Raymond Hettinger3c940242011-01-12 23:39:31 +0000325 def _unsupported(self, name):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200326 """Internal: raise an OSError exception for unsupported operations."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000327 raise UnsupportedOperation("%s.%s() not supported" %
328 (self.__class__.__name__, name))
329
330 ### Positioning ###
331
Georg Brandl4d73b572011-01-13 07:13:06 +0000332 def seek(self, pos, whence=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000333 """Change stream position.
334
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400335 Change the stream position to byte offset pos. Argument pos is
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000336 interpreted relative to the position indicated by whence. Values
Raymond Hettingercbb80892011-01-13 18:15:51 +0000337 for whence are ints:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000338
339 * 0 -- start of stream (the default); offset should be zero or positive
340 * 1 -- current stream position; offset may be negative
341 * 2 -- end of stream; offset is usually negative
Jesus Cea94363612012-06-22 18:32:07 +0200342 Some operating systems / file systems could provide additional values.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000343
Raymond Hettingercbb80892011-01-13 18:15:51 +0000344 Return an int indicating the new absolute position.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000345 """
346 self._unsupported("seek")
347
Raymond Hettinger3c940242011-01-12 23:39:31 +0000348 def tell(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000349 """Return an int indicating the current stream position."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000350 return self.seek(0, 1)
351
Georg Brandl4d73b572011-01-13 07:13:06 +0000352 def truncate(self, pos=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000353 """Truncate file to size bytes.
354
355 Size defaults to the current IO position as reported by tell(). Return
356 the new size.
357 """
358 self._unsupported("truncate")
359
360 ### Flush and close ###
361
Raymond Hettinger3c940242011-01-12 23:39:31 +0000362 def flush(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000363 """Flush write buffers, if applicable.
364
365 This is not implemented for read-only and non-blocking streams.
366 """
Antoine Pitrou6be88762010-05-03 16:48:20 +0000367 self._checkClosed()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000368 # XXX Should this return the number of bytes written???
369
370 __closed = False
371
Raymond Hettinger3c940242011-01-12 23:39:31 +0000372 def close(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000373 """Flush and close the IO object.
374
375 This method has no effect if the file is already closed.
376 """
377 if not self.__closed:
Benjamin Peterson68623612012-12-20 11:53:11 -0600378 try:
379 self.flush()
380 finally:
381 self.__closed = True
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000382
Raymond Hettinger3c940242011-01-12 23:39:31 +0000383 def __del__(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000384 """Destructor. Calls close()."""
Victor Stinnerbc2aa812019-05-23 03:45:09 +0200385 if _IOBASE_EMITS_UNRAISABLE:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000386 self.close()
Victor Stinnerbc2aa812019-05-23 03:45:09 +0200387 else:
388 # The try/except block is in case this is called at program
389 # exit time, when it's possible that globals have already been
390 # deleted, and then the close() call might fail. Since
391 # there's nothing we can do about such failures and they annoy
392 # the end users, we suppress the traceback.
393 try:
394 self.close()
395 except:
396 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000397
398 ### Inquiries ###
399
Raymond Hettinger3c940242011-01-12 23:39:31 +0000400 def seekable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000401 """Return a bool indicating whether object supports random access.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000402
Martin Panter754aab22016-03-31 07:21:56 +0000403 If False, seek(), tell() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000404 This method may need to do a test seek().
405 """
406 return False
407
408 def _checkSeekable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000409 """Internal: raise UnsupportedOperation if file is not seekable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000410 """
411 if not self.seekable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000412 raise UnsupportedOperation("File or stream is not seekable."
413 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000414
Raymond Hettinger3c940242011-01-12 23:39:31 +0000415 def readable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000416 """Return a bool indicating whether object was opened for reading.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417
Martin Panter754aab22016-03-31 07:21:56 +0000418 If False, read() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000419 """
420 return False
421
422 def _checkReadable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000423 """Internal: raise UnsupportedOperation if file is not readable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000424 """
425 if not self.readable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000426 raise UnsupportedOperation("File or stream is not readable."
427 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000428
Raymond Hettinger3c940242011-01-12 23:39:31 +0000429 def writable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000430 """Return a bool indicating whether object was opened for writing.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000431
Martin Panter754aab22016-03-31 07:21:56 +0000432 If False, write() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000433 """
434 return False
435
436 def _checkWritable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000437 """Internal: raise UnsupportedOperation if file is not writable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000438 """
439 if not self.writable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000440 raise UnsupportedOperation("File or stream is not writable."
441 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000442
443 @property
444 def closed(self):
445 """closed: bool. True iff the file has been closed.
446
447 For backwards compatibility, this is a property, not a predicate.
448 """
449 return self.__closed
450
451 def _checkClosed(self, msg=None):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300452 """Internal: raise a ValueError if file is closed
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000453 """
454 if self.closed:
455 raise ValueError("I/O operation on closed file."
456 if msg is None else msg)
457
458 ### Context manager ###
459
Raymond Hettinger3c940242011-01-12 23:39:31 +0000460 def __enter__(self): # That's a forward reference
Raymond Hettingercbb80892011-01-13 18:15:51 +0000461 """Context management protocol. Returns self (an instance of IOBase)."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462 self._checkClosed()
463 return self
464
Raymond Hettinger3c940242011-01-12 23:39:31 +0000465 def __exit__(self, *args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000466 """Context management protocol. Calls close()"""
467 self.close()
468
469 ### Lower-level APIs ###
470
471 # XXX Should these be present even if unimplemented?
472
Raymond Hettinger3c940242011-01-12 23:39:31 +0000473 def fileno(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000474 """Returns underlying file descriptor (an int) if one exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000475
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200476 An OSError is raised if the IO object does not use a file descriptor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477 """
478 self._unsupported("fileno")
479
Raymond Hettinger3c940242011-01-12 23:39:31 +0000480 def isatty(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000481 """Return a bool indicating whether this is an 'interactive' stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000482
483 Return False if it can't be determined.
484 """
485 self._checkClosed()
486 return False
487
488 ### Readline[s] and writelines ###
489
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300490 def readline(self, size=-1):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000491 r"""Read and return a line of bytes from the stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000492
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300493 If size is specified, at most size bytes will be read.
494 Size should be an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000495
496 The line terminator is always b'\n' for binary files; for text
497 files, the newlines argument to open can be used to select the line
498 terminator(s) recognized.
499 """
500 # For backwards compatibility, a (slowish) readline().
501 if hasattr(self, "peek"):
502 def nreadahead():
503 readahead = self.peek(1)
504 if not readahead:
505 return 1
506 n = (readahead.find(b"\n") + 1) or len(readahead)
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300507 if size >= 0:
508 n = min(n, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000509 return n
510 else:
511 def nreadahead():
512 return 1
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300513 if size is None:
514 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300515 else:
516 try:
517 size_index = size.__index__
518 except AttributeError:
519 raise TypeError(f"{size!r} is not an integer")
520 else:
521 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000522 res = bytearray()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300523 while size < 0 or len(res) < size:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000524 b = self.read(nreadahead())
525 if not b:
526 break
527 res += b
528 if res.endswith(b"\n"):
529 break
530 return bytes(res)
531
532 def __iter__(self):
533 self._checkClosed()
534 return self
535
536 def __next__(self):
537 line = self.readline()
538 if not line:
539 raise StopIteration
540 return line
541
542 def readlines(self, hint=None):
543 """Return a list of lines from the stream.
544
545 hint can be specified to control the number of lines read: no more
546 lines will be read if the total size (in bytes/characters) of all
547 lines so far exceeds hint.
548 """
549 if hint is None or hint <= 0:
550 return list(self)
551 n = 0
552 lines = []
553 for line in self:
554 lines.append(line)
555 n += len(line)
556 if n >= hint:
557 break
558 return lines
559
560 def writelines(self, lines):
Marcin Niemiraab865212019-04-22 21:13:51 +1000561 """Write a list of lines to the stream.
562
563 Line separators are not added, so it is usual for each of the lines
564 provided to have a line separator at the end.
565 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000566 self._checkClosed()
567 for line in lines:
568 self.write(line)
569
570io.IOBase.register(IOBase)
571
572
573class RawIOBase(IOBase):
574
575 """Base class for raw binary I/O."""
576
577 # The read() method is implemented by calling readinto(); derived
578 # classes that want to support read() only need to implement
579 # readinto() as a primitive operation. In general, readinto() can be
580 # more efficient than read().
581
582 # (It would be tempting to also provide an implementation of
583 # readinto() in terms of read(), in case the latter is a more suitable
584 # primitive operation, but that would lead to nasty recursion in case
585 # a subclass doesn't implement either.)
586
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300587 def read(self, size=-1):
588 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000589
590 Returns an empty bytes object on EOF, or None if the object is
591 set not to block and has no data to read.
592 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300593 if size is None:
594 size = -1
595 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000596 return self.readall()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300597 b = bytearray(size.__index__())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000598 n = self.readinto(b)
Antoine Pitrou328ec742010-09-14 18:37:24 +0000599 if n is None:
600 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000601 del b[n:]
602 return bytes(b)
603
604 def readall(self):
605 """Read until EOF, using multiple read() call."""
606 res = bytearray()
607 while True:
608 data = self.read(DEFAULT_BUFFER_SIZE)
609 if not data:
610 break
611 res += data
Victor Stinnera80987f2011-05-25 22:47:16 +0200612 if res:
613 return bytes(res)
614 else:
615 # b'' or None
616 return data
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000617
Raymond Hettinger3c940242011-01-12 23:39:31 +0000618 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000619 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000620
Raymond Hettingercbb80892011-01-13 18:15:51 +0000621 Returns an int representing the number of bytes read (0 for EOF), or
622 None if the object is set not to block and has no data to read.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000623 """
624 self._unsupported("readinto")
625
Raymond Hettinger3c940242011-01-12 23:39:31 +0000626 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000627 """Write the given buffer to the IO stream.
628
Martin Panter6bb91f32016-05-28 00:41:57 +0000629 Returns the number of bytes written, which may be less than the
630 length of b in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000631 """
632 self._unsupported("write")
633
634io.RawIOBase.register(RawIOBase)
635from _io import FileIO
636RawIOBase.register(FileIO)
637
638
639class BufferedIOBase(IOBase):
640
641 """Base class for buffered IO objects.
642
643 The main difference with RawIOBase is that the read() method
644 supports omitting the size argument, and does not have a default
645 implementation that defers to readinto().
646
647 In addition, read(), readinto() and write() may raise
648 BlockingIOError if the underlying raw stream is in non-blocking
649 mode and not ready; unlike their raw counterparts, they will never
650 return None.
651
652 A typical implementation should not inherit from a RawIOBase
653 implementation, but wrap one.
654 """
655
Martin Panterccb2c0e2016-10-20 23:48:14 +0000656 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300657 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000658
659 If the argument is omitted, None, or negative, reads and
660 returns all data until EOF.
661
662 If the argument is positive, and the underlying raw stream is
663 not 'interactive', multiple raw reads may be issued to satisfy
664 the byte count (unless EOF is reached first). But for
665 interactive raw streams (XXX and for pipes?), at most one raw
666 read will be issued, and a short result does not imply that
667 EOF is imminent.
668
669 Returns an empty bytes array on EOF.
670
671 Raises BlockingIOError if the underlying raw stream has no
672 data at the moment.
673 """
674 self._unsupported("read")
675
Martin Panterccb2c0e2016-10-20 23:48:14 +0000676 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300677 """Read up to size bytes with at most one read() system call,
678 where size is an int.
Raymond Hettingercbb80892011-01-13 18:15:51 +0000679 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000680 self._unsupported("read1")
681
Raymond Hettinger3c940242011-01-12 23:39:31 +0000682 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000683 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000684
685 Like read(), this may issue multiple reads to the underlying raw
686 stream, unless the latter is 'interactive'.
687
Raymond Hettingercbb80892011-01-13 18:15:51 +0000688 Returns an int representing the number of bytes read (0 for EOF).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000689
690 Raises BlockingIOError if the underlying raw stream has no
691 data at the moment.
692 """
Benjamin Petersona96fea02014-06-22 14:17:44 -0700693
694 return self._readinto(b, read1=False)
695
696 def readinto1(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000697 """Read bytes into buffer *b*, using at most one system call
Benjamin Petersona96fea02014-06-22 14:17:44 -0700698
699 Returns an int representing the number of bytes read (0 for EOF).
700
701 Raises BlockingIOError if the underlying raw stream has no
702 data at the moment.
703 """
704
705 return self._readinto(b, read1=True)
706
707 def _readinto(self, b, read1):
708 if not isinstance(b, memoryview):
709 b = memoryview(b)
710 b = b.cast('B')
711
712 if read1:
713 data = self.read1(len(b))
714 else:
715 data = self.read(len(b))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000716 n = len(data)
Benjamin Petersona96fea02014-06-22 14:17:44 -0700717
718 b[:n] = data
719
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000720 return n
721
Raymond Hettinger3c940242011-01-12 23:39:31 +0000722 def write(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000723 """Write the given bytes buffer to the IO stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724
Martin Panter6bb91f32016-05-28 00:41:57 +0000725 Return the number of bytes written, which is always the length of b
726 in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000727
728 Raises BlockingIOError if the buffer is full and the
729 underlying raw stream cannot accept more data at the moment.
730 """
731 self._unsupported("write")
732
Raymond Hettinger3c940242011-01-12 23:39:31 +0000733 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000734 """
735 Separate the underlying raw stream from the buffer and return it.
736
737 After the raw stream has been detached, the buffer is in an unusable
738 state.
739 """
740 self._unsupported("detach")
741
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000742io.BufferedIOBase.register(BufferedIOBase)
743
744
745class _BufferedIOMixin(BufferedIOBase):
746
747 """A mixin implementation of BufferedIOBase with an underlying raw stream.
748
749 This passes most requests on to the underlying raw stream. It
750 does *not* provide implementations of read(), readinto() or
751 write().
752 """
753
754 def __init__(self, raw):
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000755 self._raw = raw
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000756
757 ### Positioning ###
758
759 def seek(self, pos, whence=0):
760 new_position = self.raw.seek(pos, whence)
761 if new_position < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200762 raise OSError("seek() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000763 return new_position
764
765 def tell(self):
766 pos = self.raw.tell()
767 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200768 raise OSError("tell() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000769 return pos
770
771 def truncate(self, pos=None):
772 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
773 # and a flush may be necessary to synch both views of the current
774 # file state.
775 self.flush()
776
777 if pos is None:
778 pos = self.tell()
779 # XXX: Should seek() be used, instead of passing the position
780 # XXX directly to truncate?
781 return self.raw.truncate(pos)
782
783 ### Flush and close ###
784
785 def flush(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000786 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +0300787 raise ValueError("flush on closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788 self.raw.flush()
789
790 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000791 if self.raw is not None and not self.closed:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100792 try:
793 # may raise BlockingIOError or BrokenPipeError etc
794 self.flush()
795 finally:
796 self.raw.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000797
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000798 def detach(self):
799 if self.raw is None:
800 raise ValueError("raw stream already detached")
801 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000802 raw = self._raw
803 self._raw = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000804 return raw
805
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000806 ### Inquiries ###
807
808 def seekable(self):
809 return self.raw.seekable()
810
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000811 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000812 def raw(self):
813 return self._raw
814
815 @property
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000816 def closed(self):
817 return self.raw.closed
818
819 @property
820 def name(self):
821 return self.raw.name
822
823 @property
824 def mode(self):
825 return self.raw.mode
826
Antoine Pitrou243757e2010-11-05 21:15:39 +0000827 def __getstate__(self):
Serhiy Storchaka0353b4e2018-10-31 02:28:07 +0200828 raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
Antoine Pitrou243757e2010-11-05 21:15:39 +0000829
Antoine Pitrou716c4442009-05-23 19:04:03 +0000830 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300831 modname = self.__class__.__module__
832 clsname = self.__class__.__qualname__
Antoine Pitrou716c4442009-05-23 19:04:03 +0000833 try:
834 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -0600835 except Exception:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300836 return "<{}.{}>".format(modname, clsname)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000837 else:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300838 return "<{}.{} name={!r}>".format(modname, clsname, name)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000839
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000840 ### Lower-level APIs ###
841
842 def fileno(self):
843 return self.raw.fileno()
844
845 def isatty(self):
846 return self.raw.isatty()
847
848
849class BytesIO(BufferedIOBase):
850
851 """Buffered I/O implementation using an in-memory bytes buffer."""
852
853 def __init__(self, initial_bytes=None):
854 buf = bytearray()
855 if initial_bytes is not None:
856 buf += initial_bytes
857 self._buffer = buf
858 self._pos = 0
859
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000860 def __getstate__(self):
861 if self.closed:
862 raise ValueError("__getstate__ on closed file")
863 return self.__dict__.copy()
864
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000865 def getvalue(self):
866 """Return the bytes value (contents) of the buffer
867 """
868 if self.closed:
869 raise ValueError("getvalue on closed file")
870 return bytes(self._buffer)
871
Antoine Pitrou972ee132010-09-06 18:48:21 +0000872 def getbuffer(self):
873 """Return a readable and writable view of the buffer.
874 """
Serhiy Storchakac057c382015-02-03 02:00:18 +0200875 if self.closed:
876 raise ValueError("getbuffer on closed file")
Antoine Pitrou972ee132010-09-06 18:48:21 +0000877 return memoryview(self._buffer)
878
Serhiy Storchakac057c382015-02-03 02:00:18 +0200879 def close(self):
880 self._buffer.clear()
881 super().close()
882
Martin Panterccb2c0e2016-10-20 23:48:14 +0000883 def read(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000884 if self.closed:
885 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300886 if size is None:
887 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300888 else:
889 try:
890 size_index = size.__index__
891 except AttributeError:
892 raise TypeError(f"{size!r} is not an integer")
893 else:
894 size = size_index()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300895 if size < 0:
896 size = len(self._buffer)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000897 if len(self._buffer) <= self._pos:
898 return b""
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300899 newpos = min(len(self._buffer), self._pos + size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000900 b = self._buffer[self._pos : newpos]
901 self._pos = newpos
902 return bytes(b)
903
Martin Panterccb2c0e2016-10-20 23:48:14 +0000904 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000905 """This is the same as read.
906 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300907 return self.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000908
909 def write(self, b):
910 if self.closed:
911 raise ValueError("write to closed file")
912 if isinstance(b, str):
913 raise TypeError("can't write str to binary stream")
Martin Panter6bb91f32016-05-28 00:41:57 +0000914 with memoryview(b) as view:
915 n = view.nbytes # Size of any bytes-like object
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000916 if n == 0:
917 return 0
918 pos = self._pos
919 if pos > len(self._buffer):
920 # Inserts null bytes between the current end of the file
921 # and the new write position.
922 padding = b'\x00' * (pos - len(self._buffer))
923 self._buffer += padding
924 self._buffer[pos:pos + n] = b
925 self._pos += n
926 return n
927
928 def seek(self, pos, whence=0):
929 if self.closed:
930 raise ValueError("seek on closed file")
931 try:
Oren Milmande503602017-08-24 21:33:42 +0300932 pos_index = pos.__index__
933 except AttributeError:
934 raise TypeError(f"{pos!r} is not an integer")
935 else:
936 pos = pos_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000937 if whence == 0:
938 if pos < 0:
939 raise ValueError("negative seek position %r" % (pos,))
940 self._pos = pos
941 elif whence == 1:
942 self._pos = max(0, self._pos + pos)
943 elif whence == 2:
944 self._pos = max(0, len(self._buffer) + pos)
945 else:
Jesus Cea94363612012-06-22 18:32:07 +0200946 raise ValueError("unsupported whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000947 return self._pos
948
949 def tell(self):
950 if self.closed:
951 raise ValueError("tell on closed file")
952 return self._pos
953
954 def truncate(self, pos=None):
955 if self.closed:
956 raise ValueError("truncate on closed file")
957 if pos is None:
958 pos = self._pos
Florent Xiclunab14930c2010-03-13 15:26:44 +0000959 else:
960 try:
Oren Milmande503602017-08-24 21:33:42 +0300961 pos_index = pos.__index__
962 except AttributeError:
963 raise TypeError(f"{pos!r} is not an integer")
964 else:
965 pos = pos_index()
Florent Xiclunab14930c2010-03-13 15:26:44 +0000966 if pos < 0:
967 raise ValueError("negative truncate position %r" % (pos,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000968 del self._buffer[pos:]
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000969 return pos
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000970
971 def readable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200972 if self.closed:
973 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000974 return True
975
976 def writable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200977 if self.closed:
978 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000979 return True
980
981 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200982 if self.closed:
983 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000984 return True
985
986
987class BufferedReader(_BufferedIOMixin):
988
989 """BufferedReader(raw[, buffer_size])
990
991 A buffer for a readable, sequential BaseRawIO object.
992
993 The constructor creates a BufferedReader for the given readable raw
994 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
995 is used.
996 """
997
998 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
999 """Create a new buffered reader using the given readable raw IO object.
1000 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001001 if not raw.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001002 raise OSError('"raw" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001003
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001004 _BufferedIOMixin.__init__(self, raw)
1005 if buffer_size <= 0:
1006 raise ValueError("invalid buffer size")
1007 self.buffer_size = buffer_size
1008 self._reset_read_buf()
1009 self._read_lock = Lock()
1010
Martin Panter754aab22016-03-31 07:21:56 +00001011 def readable(self):
1012 return self.raw.readable()
1013
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001014 def _reset_read_buf(self):
1015 self._read_buf = b""
1016 self._read_pos = 0
1017
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001018 def read(self, size=None):
1019 """Read size bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001020
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001021 Returns exactly size bytes of data unless the underlying raw IO
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001022 stream reaches EOF or if the call would block in non-blocking
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001023 mode. If size is negative, read until EOF or until read() would
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001024 block.
1025 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001026 if size is not None and size < -1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001027 raise ValueError("invalid number of bytes to read")
1028 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001029 return self._read_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001030
1031 def _read_unlocked(self, n=None):
1032 nodata_val = b""
1033 empty_values = (b"", None)
1034 buf = self._read_buf
1035 pos = self._read_pos
1036
1037 # Special case for when the number of bytes to read is unspecified.
1038 if n is None or n == -1:
1039 self._reset_read_buf()
Victor Stinnerb57f1082011-05-26 00:19:38 +02001040 if hasattr(self.raw, 'readall'):
1041 chunk = self.raw.readall()
1042 if chunk is None:
1043 return buf[pos:] or None
1044 else:
1045 return buf[pos:] + chunk
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001046 chunks = [buf[pos:]] # Strip the consumed bytes.
1047 current_size = 0
1048 while True:
1049 # Read until EOF or until read() would block.
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001050 chunk = self.raw.read()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001051 if chunk in empty_values:
1052 nodata_val = chunk
1053 break
1054 current_size += len(chunk)
1055 chunks.append(chunk)
1056 return b"".join(chunks) or nodata_val
1057
1058 # The number of bytes to read is specified, return at most n bytes.
1059 avail = len(buf) - pos # Length of the available buffered data.
1060 if n <= avail:
1061 # Fast path: the data to read is fully buffered.
1062 self._read_pos += n
1063 return buf[pos:pos+n]
1064 # Slow path: read from the stream until enough bytes are read,
1065 # or until an EOF occurs or until read() would block.
1066 chunks = [buf[pos:]]
1067 wanted = max(self.buffer_size, n)
1068 while avail < n:
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001069 chunk = self.raw.read(wanted)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001070 if chunk in empty_values:
1071 nodata_val = chunk
1072 break
1073 avail += len(chunk)
1074 chunks.append(chunk)
Martin Pantere26da7c2016-06-02 10:07:09 +00001075 # n is more than avail only when an EOF occurred or when
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001076 # read() would have blocked.
1077 n = min(n, avail)
1078 out = b"".join(chunks)
1079 self._read_buf = out[n:] # Save the extra data in the buffer.
1080 self._read_pos = 0
1081 return out[:n] if out else nodata_val
1082
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001083 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001084 """Returns buffered bytes without advancing the position.
1085
1086 The argument indicates a desired minimal number of bytes; we
1087 do at most one raw read to satisfy it. We never return more
1088 than self.buffer_size.
1089 """
1090 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001091 return self._peek_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001092
1093 def _peek_unlocked(self, n=0):
1094 want = min(n, self.buffer_size)
1095 have = len(self._read_buf) - self._read_pos
1096 if have < want or have <= 0:
1097 to_read = self.buffer_size - have
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001098 current = self.raw.read(to_read)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001099 if current:
1100 self._read_buf = self._read_buf[self._read_pos:] + current
1101 self._read_pos = 0
1102 return self._read_buf[self._read_pos:]
1103
Martin Panterccb2c0e2016-10-20 23:48:14 +00001104 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001105 """Reads up to size bytes, with at most one read() system call."""
1106 # Returns up to size bytes. If at least one byte is buffered, we
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001107 # only return buffered bytes. Otherwise, we do one raw read.
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001108 if size < 0:
Martin Panterccb2c0e2016-10-20 23:48:14 +00001109 size = self.buffer_size
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001110 if size == 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001111 return b""
1112 with self._read_lock:
1113 self._peek_unlocked(1)
1114 return self._read_unlocked(
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001115 min(size, len(self._read_buf) - self._read_pos))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001116
Benjamin Petersona96fea02014-06-22 14:17:44 -07001117 # Implementing readinto() and readinto1() is not strictly necessary (we
1118 # could rely on the base class that provides an implementation in terms of
1119 # read() and read1()). We do it anyway to keep the _pyio implementation
1120 # similar to the io implementation (which implements the methods for
1121 # performance reasons).
1122 def _readinto(self, buf, read1):
1123 """Read data into *buf* with at most one system call."""
1124
Benjamin Petersona96fea02014-06-22 14:17:44 -07001125 # Need to create a memoryview object of type 'b', otherwise
1126 # we may not be able to assign bytes to it, and slicing it
1127 # would create a new object.
1128 if not isinstance(buf, memoryview):
1129 buf = memoryview(buf)
Martin Panter6bb91f32016-05-28 00:41:57 +00001130 if buf.nbytes == 0:
1131 return 0
Benjamin Petersona96fea02014-06-22 14:17:44 -07001132 buf = buf.cast('B')
1133
1134 written = 0
1135 with self._read_lock:
1136 while written < len(buf):
1137
1138 # First try to read from internal buffer
1139 avail = min(len(self._read_buf) - self._read_pos, len(buf))
1140 if avail:
1141 buf[written:written+avail] = \
1142 self._read_buf[self._read_pos:self._read_pos+avail]
1143 self._read_pos += avail
1144 written += avail
1145 if written == len(buf):
1146 break
1147
1148 # If remaining space in callers buffer is larger than
1149 # internal buffer, read directly into callers buffer
1150 if len(buf) - written > self.buffer_size:
1151 n = self.raw.readinto(buf[written:])
1152 if not n:
1153 break # eof
1154 written += n
1155
1156 # Otherwise refill internal buffer - unless we're
1157 # in read1 mode and already got some data
1158 elif not (read1 and written):
1159 if not self._peek_unlocked(1):
1160 break # eof
1161
1162 # In readinto1 mode, return as soon as we have some data
1163 if read1 and written:
1164 break
1165
1166 return written
1167
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001168 def tell(self):
1169 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
1170
1171 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001172 if whence not in valid_seek_flags:
Jesus Cea990eff02012-04-26 17:05:31 +02001173 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001174 with self._read_lock:
1175 if whence == 1:
1176 pos -= len(self._read_buf) - self._read_pos
1177 pos = _BufferedIOMixin.seek(self, pos, whence)
1178 self._reset_read_buf()
1179 return pos
1180
1181class BufferedWriter(_BufferedIOMixin):
1182
1183 """A buffer for a writeable sequential RawIO object.
1184
1185 The constructor creates a BufferedWriter for the given writeable raw
1186 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001187 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001188 """
1189
Florent Xicluna109d5732012-07-07 17:03:22 +02001190 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001191 if not raw.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001192 raise OSError('"raw" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001193
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001194 _BufferedIOMixin.__init__(self, raw)
1195 if buffer_size <= 0:
1196 raise ValueError("invalid buffer size")
1197 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001198 self._write_buf = bytearray()
1199 self._write_lock = Lock()
1200
Martin Panter754aab22016-03-31 07:21:56 +00001201 def writable(self):
1202 return self.raw.writable()
1203
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001204 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001205 if isinstance(b, str):
1206 raise TypeError("can't write str to binary stream")
1207 with self._write_lock:
benfogle9703f092017-11-10 16:03:40 -05001208 if self.closed:
1209 raise ValueError("write to closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001210 # XXX we can implement some more tricks to try and avoid
1211 # partial writes
1212 if len(self._write_buf) > self.buffer_size:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001213 # We're full, so let's pre-flush the buffer. (This may
1214 # raise BlockingIOError with characters_written == 0.)
1215 self._flush_unlocked()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001216 before = len(self._write_buf)
1217 self._write_buf.extend(b)
1218 written = len(self._write_buf) - before
1219 if len(self._write_buf) > self.buffer_size:
1220 try:
1221 self._flush_unlocked()
1222 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001223 if len(self._write_buf) > self.buffer_size:
1224 # We've hit the buffer_size. We have to accept a partial
1225 # write and cut back our buffer.
1226 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001227 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001228 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001229 raise BlockingIOError(e.errno, e.strerror, written)
1230 return written
1231
1232 def truncate(self, pos=None):
1233 with self._write_lock:
1234 self._flush_unlocked()
1235 if pos is None:
1236 pos = self.raw.tell()
1237 return self.raw.truncate(pos)
1238
1239 def flush(self):
1240 with self._write_lock:
1241 self._flush_unlocked()
1242
1243 def _flush_unlocked(self):
1244 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +03001245 raise ValueError("flush on closed file")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001246 while self._write_buf:
1247 try:
1248 n = self.raw.write(self._write_buf)
1249 except BlockingIOError:
1250 raise RuntimeError("self.raw should implement RawIOBase: it "
1251 "should not raise BlockingIOError")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001252 if n is None:
1253 raise BlockingIOError(
1254 errno.EAGAIN,
1255 "write could not complete without blocking", 0)
1256 if n > len(self._write_buf) or n < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001257 raise OSError("write() returned incorrect number of bytes")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001258 del self._write_buf[:n]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001259
1260 def tell(self):
1261 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1262
1263 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001264 if whence not in valid_seek_flags:
1265 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001266 with self._write_lock:
1267 self._flush_unlocked()
1268 return _BufferedIOMixin.seek(self, pos, whence)
1269
benfogle9703f092017-11-10 16:03:40 -05001270 def close(self):
1271 with self._write_lock:
1272 if self.raw is None or self.closed:
1273 return
1274 # We have to release the lock and call self.flush() (which will
1275 # probably just re-take the lock) in case flush has been overridden in
1276 # a subclass or the user set self.flush to something. This is the same
1277 # behavior as the C implementation.
1278 try:
1279 # may raise BlockingIOError or BrokenPipeError etc
1280 self.flush()
1281 finally:
1282 with self._write_lock:
1283 self.raw.close()
1284
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001285
1286class BufferedRWPair(BufferedIOBase):
1287
1288 """A buffered reader and writer object together.
1289
1290 A buffered reader object and buffered writer object put together to
1291 form a sequential IO object that can read and write. This is typically
1292 used with a socket or two-way pipe.
1293
1294 reader and writer are RawIOBase objects that are readable and
1295 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001296 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001297 """
1298
1299 # XXX The usefulness of this (compared to having two separate IO
1300 # objects) is questionable.
1301
Florent Xicluna109d5732012-07-07 17:03:22 +02001302 def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001303 """Constructor.
1304
1305 The arguments are two RawIO instances.
1306 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001307 if not reader.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001308 raise OSError('"reader" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001309
1310 if not writer.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001311 raise OSError('"writer" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001312
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001313 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001314 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001315
Martin Panterccb2c0e2016-10-20 23:48:14 +00001316 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001317 if size is None:
1318 size = -1
1319 return self.reader.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001320
1321 def readinto(self, b):
1322 return self.reader.readinto(b)
1323
1324 def write(self, b):
1325 return self.writer.write(b)
1326
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001327 def peek(self, size=0):
1328 return self.reader.peek(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001329
Martin Panterccb2c0e2016-10-20 23:48:14 +00001330 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001331 return self.reader.read1(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001332
Benjamin Petersona96fea02014-06-22 14:17:44 -07001333 def readinto1(self, b):
1334 return self.reader.readinto1(b)
1335
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001336 def readable(self):
1337 return self.reader.readable()
1338
1339 def writable(self):
1340 return self.writer.writable()
1341
1342 def flush(self):
1343 return self.writer.flush()
1344
1345 def close(self):
Serhiy Storchaka7665be62015-03-24 23:21:57 +02001346 try:
1347 self.writer.close()
1348 finally:
1349 self.reader.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001350
1351 def isatty(self):
1352 return self.reader.isatty() or self.writer.isatty()
1353
1354 @property
1355 def closed(self):
1356 return self.writer.closed
1357
1358
1359class BufferedRandom(BufferedWriter, BufferedReader):
1360
1361 """A buffered interface to random access streams.
1362
1363 The constructor creates a reader and writer for a seekable stream,
1364 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001365 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001366 """
1367
Florent Xicluna109d5732012-07-07 17:03:22 +02001368 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001369 raw._checkSeekable()
1370 BufferedReader.__init__(self, raw, buffer_size)
Florent Xicluna109d5732012-07-07 17:03:22 +02001371 BufferedWriter.__init__(self, raw, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001372
1373 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001374 if whence not in valid_seek_flags:
1375 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001376 self.flush()
1377 if self._read_buf:
1378 # Undo read ahead.
1379 with self._read_lock:
1380 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1381 # First do the raw seek, then empty the read buffer, so that
1382 # if the raw seek fails, we don't lose buffered data forever.
1383 pos = self.raw.seek(pos, whence)
1384 with self._read_lock:
1385 self._reset_read_buf()
1386 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001387 raise OSError("seek() returned invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001388 return pos
1389
1390 def tell(self):
1391 if self._write_buf:
1392 return BufferedWriter.tell(self)
1393 else:
1394 return BufferedReader.tell(self)
1395
1396 def truncate(self, pos=None):
1397 if pos is None:
1398 pos = self.tell()
1399 # Use seek to flush the read buffer.
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001400 return BufferedWriter.truncate(self, pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001401
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001402 def read(self, size=None):
1403 if size is None:
1404 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001405 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001406 return BufferedReader.read(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001407
1408 def readinto(self, b):
1409 self.flush()
1410 return BufferedReader.readinto(self, b)
1411
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001412 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001413 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001414 return BufferedReader.peek(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001415
Martin Panterccb2c0e2016-10-20 23:48:14 +00001416 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001417 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001418 return BufferedReader.read1(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001419
Benjamin Petersona96fea02014-06-22 14:17:44 -07001420 def readinto1(self, b):
1421 self.flush()
1422 return BufferedReader.readinto1(self, b)
1423
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001424 def write(self, b):
1425 if self._read_buf:
1426 # Undo readahead
1427 with self._read_lock:
1428 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1429 self._reset_read_buf()
1430 return BufferedWriter.write(self, b)
1431
1432
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001433class FileIO(RawIOBase):
1434 _fd = -1
1435 _created = False
1436 _readable = False
1437 _writable = False
1438 _appending = False
1439 _seekable = None
1440 _closefd = True
1441
1442 def __init__(self, file, mode='r', closefd=True, opener=None):
1443 """Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
1444 writing, exclusive creation or appending. The file will be created if it
1445 doesn't exist when opened for writing or appending; it will be truncated
1446 when opened for writing. A FileExistsError will be raised if it already
1447 exists when opened for creating. Opening a file for creating implies
1448 writing so this mode behaves in a similar way to 'w'. Add a '+' to the mode
1449 to allow simultaneous reading and writing. A custom opener can be used by
1450 passing a callable as *opener*. The underlying file descriptor for the file
1451 object is then obtained by calling opener with (*name*, *flags*).
1452 *opener* must return an open file descriptor (passing os.open as *opener*
1453 results in functionality similar to passing None).
1454 """
1455 if self._fd >= 0:
1456 # Have to close the existing file first.
1457 try:
1458 if self._closefd:
1459 os.close(self._fd)
1460 finally:
1461 self._fd = -1
1462
1463 if isinstance(file, float):
1464 raise TypeError('integer argument expected, got float')
1465 if isinstance(file, int):
1466 fd = file
1467 if fd < 0:
1468 raise ValueError('negative file descriptor')
1469 else:
1470 fd = -1
1471
1472 if not isinstance(mode, str):
1473 raise TypeError('invalid mode: %s' % (mode,))
1474 if not set(mode) <= set('xrwab+'):
1475 raise ValueError('invalid mode: %s' % (mode,))
1476 if sum(c in 'rwax' for c in mode) != 1 or mode.count('+') > 1:
1477 raise ValueError('Must have exactly one of create/read/write/append '
1478 'mode and at most one plus')
1479
1480 if 'x' in mode:
1481 self._created = True
1482 self._writable = True
1483 flags = os.O_EXCL | os.O_CREAT
1484 elif 'r' in mode:
1485 self._readable = True
1486 flags = 0
1487 elif 'w' in mode:
1488 self._writable = True
1489 flags = os.O_CREAT | os.O_TRUNC
1490 elif 'a' in mode:
1491 self._writable = True
1492 self._appending = True
1493 flags = os.O_APPEND | os.O_CREAT
1494
1495 if '+' in mode:
1496 self._readable = True
1497 self._writable = True
1498
1499 if self._readable and self._writable:
1500 flags |= os.O_RDWR
1501 elif self._readable:
1502 flags |= os.O_RDONLY
1503 else:
1504 flags |= os.O_WRONLY
1505
1506 flags |= getattr(os, 'O_BINARY', 0)
1507
1508 noinherit_flag = (getattr(os, 'O_NOINHERIT', 0) or
1509 getattr(os, 'O_CLOEXEC', 0))
1510 flags |= noinherit_flag
1511
1512 owned_fd = None
1513 try:
1514 if fd < 0:
1515 if not closefd:
1516 raise ValueError('Cannot use closefd=False with file name')
1517 if opener is None:
1518 fd = os.open(file, flags, 0o666)
1519 else:
1520 fd = opener(file, flags)
1521 if not isinstance(fd, int):
1522 raise TypeError('expected integer from opener')
1523 if fd < 0:
1524 raise OSError('Negative file descriptor')
1525 owned_fd = fd
1526 if not noinherit_flag:
1527 os.set_inheritable(fd, False)
1528
1529 self._closefd = closefd
1530 fdfstat = os.fstat(fd)
1531 try:
1532 if stat.S_ISDIR(fdfstat.st_mode):
1533 raise IsADirectoryError(errno.EISDIR,
1534 os.strerror(errno.EISDIR), file)
1535 except AttributeError:
1536 # Ignore the AttribueError if stat.S_ISDIR or errno.EISDIR
1537 # don't exist.
1538 pass
1539 self._blksize = getattr(fdfstat, 'st_blksize', 0)
1540 if self._blksize <= 1:
1541 self._blksize = DEFAULT_BUFFER_SIZE
1542
1543 if _setmode:
1544 # don't translate newlines (\r\n <=> \n)
1545 _setmode(fd, os.O_BINARY)
1546
1547 self.name = file
1548 if self._appending:
1549 # For consistent behaviour, we explicitly seek to the
1550 # end of file (otherwise, it might be done only on the
1551 # first write()).
1552 os.lseek(fd, 0, SEEK_END)
1553 except:
1554 if owned_fd is not None:
1555 os.close(owned_fd)
1556 raise
1557 self._fd = fd
1558
1559 def __del__(self):
1560 if self._fd >= 0 and self._closefd and not self.closed:
1561 import warnings
1562 warnings.warn('unclosed file %r' % (self,), ResourceWarning,
Victor Stinnere19558a2016-03-23 00:28:08 +01001563 stacklevel=2, source=self)
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001564 self.close()
1565
1566 def __getstate__(self):
Serhiy Storchaka0353b4e2018-10-31 02:28:07 +02001567 raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001568
1569 def __repr__(self):
1570 class_name = '%s.%s' % (self.__class__.__module__,
1571 self.__class__.__qualname__)
1572 if self.closed:
1573 return '<%s [closed]>' % class_name
1574 try:
1575 name = self.name
1576 except AttributeError:
1577 return ('<%s fd=%d mode=%r closefd=%r>' %
1578 (class_name, self._fd, self.mode, self._closefd))
1579 else:
1580 return ('<%s name=%r mode=%r closefd=%r>' %
1581 (class_name, name, self.mode, self._closefd))
1582
1583 def _checkReadable(self):
1584 if not self._readable:
1585 raise UnsupportedOperation('File not open for reading')
1586
1587 def _checkWritable(self, msg=None):
1588 if not self._writable:
1589 raise UnsupportedOperation('File not open for writing')
1590
1591 def read(self, size=None):
1592 """Read at most size bytes, returned as bytes.
1593
1594 Only makes one system call, so less data may be returned than requested
1595 In non-blocking mode, returns None if no data is available.
1596 Return an empty bytes object at EOF.
1597 """
1598 self._checkClosed()
1599 self._checkReadable()
1600 if size is None or size < 0:
1601 return self.readall()
1602 try:
1603 return os.read(self._fd, size)
1604 except BlockingIOError:
1605 return None
1606
1607 def readall(self):
1608 """Read all data from the file, returned as bytes.
1609
1610 In non-blocking mode, returns as much as is immediately available,
1611 or None if no data is available. Return an empty bytes object at EOF.
1612 """
1613 self._checkClosed()
1614 self._checkReadable()
1615 bufsize = DEFAULT_BUFFER_SIZE
1616 try:
1617 pos = os.lseek(self._fd, 0, SEEK_CUR)
1618 end = os.fstat(self._fd).st_size
1619 if end >= pos:
1620 bufsize = end - pos + 1
1621 except OSError:
1622 pass
1623
1624 result = bytearray()
1625 while True:
1626 if len(result) >= bufsize:
1627 bufsize = len(result)
1628 bufsize += max(bufsize, DEFAULT_BUFFER_SIZE)
1629 n = bufsize - len(result)
1630 try:
1631 chunk = os.read(self._fd, n)
1632 except BlockingIOError:
1633 if result:
1634 break
1635 return None
1636 if not chunk: # reached the end of the file
1637 break
1638 result += chunk
1639
1640 return bytes(result)
1641
1642 def readinto(self, b):
1643 """Same as RawIOBase.readinto()."""
1644 m = memoryview(b).cast('B')
1645 data = self.read(len(m))
1646 n = len(data)
1647 m[:n] = data
1648 return n
1649
1650 def write(self, b):
1651 """Write bytes b to file, return number written.
1652
1653 Only makes one system call, so not all of the data may be written.
1654 The number of bytes actually written is returned. In non-blocking mode,
1655 returns None if the write would block.
1656 """
1657 self._checkClosed()
1658 self._checkWritable()
1659 try:
1660 return os.write(self._fd, b)
1661 except BlockingIOError:
1662 return None
1663
1664 def seek(self, pos, whence=SEEK_SET):
1665 """Move to new file position.
1666
1667 Argument offset is a byte count. Optional argument whence defaults to
1668 SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
1669 are SEEK_CUR or 1 (move relative to current position, positive or negative),
1670 and SEEK_END or 2 (move relative to end of file, usually negative, although
1671 many platforms allow seeking beyond the end of a file).
1672
1673 Note that not all file objects are seekable.
1674 """
1675 if isinstance(pos, float):
1676 raise TypeError('an integer is required')
1677 self._checkClosed()
1678 return os.lseek(self._fd, pos, whence)
1679
1680 def tell(self):
1681 """tell() -> int. Current file position.
1682
1683 Can raise OSError for non seekable files."""
1684 self._checkClosed()
1685 return os.lseek(self._fd, 0, SEEK_CUR)
1686
1687 def truncate(self, size=None):
1688 """Truncate the file to at most size bytes.
1689
1690 Size defaults to the current file position, as returned by tell().
1691 The current file position is changed to the value of size.
1692 """
1693 self._checkClosed()
1694 self._checkWritable()
1695 if size is None:
1696 size = self.tell()
1697 os.ftruncate(self._fd, size)
1698 return size
1699
1700 def close(self):
1701 """Close the file.
1702
1703 A closed file cannot be used for further I/O operations. close() may be
1704 called more than once without error.
1705 """
1706 if not self.closed:
1707 try:
1708 if self._closefd:
1709 os.close(self._fd)
1710 finally:
1711 super().close()
1712
1713 def seekable(self):
1714 """True if file supports random-access."""
1715 self._checkClosed()
1716 if self._seekable is None:
1717 try:
1718 self.tell()
1719 except OSError:
1720 self._seekable = False
1721 else:
1722 self._seekable = True
1723 return self._seekable
1724
1725 def readable(self):
1726 """True if file was opened in a read mode."""
1727 self._checkClosed()
1728 return self._readable
1729
1730 def writable(self):
1731 """True if file was opened in a write mode."""
1732 self._checkClosed()
1733 return self._writable
1734
1735 def fileno(self):
1736 """Return the underlying file descriptor (an integer)."""
1737 self._checkClosed()
1738 return self._fd
1739
1740 def isatty(self):
1741 """True if the file is connected to a TTY device."""
1742 self._checkClosed()
1743 return os.isatty(self._fd)
1744
1745 @property
1746 def closefd(self):
1747 """True if the file descriptor will be closed by close()."""
1748 return self._closefd
1749
1750 @property
1751 def mode(self):
1752 """String giving the file mode"""
1753 if self._created:
1754 if self._readable:
1755 return 'xb+'
1756 else:
1757 return 'xb'
1758 elif self._appending:
1759 if self._readable:
1760 return 'ab+'
1761 else:
1762 return 'ab'
1763 elif self._readable:
1764 if self._writable:
1765 return 'rb+'
1766 else:
1767 return 'rb'
1768 else:
1769 return 'wb'
1770
1771
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001772class TextIOBase(IOBase):
1773
1774 """Base class for text I/O.
1775
1776 This class provides a character and line based interface to stream
Steve Palmer7b97ab32019-04-09 05:35:27 +01001777 I/O. There is no public constructor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001778 """
1779
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001780 def read(self, size=-1):
1781 """Read at most size characters from stream, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001782
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001783 Read from underlying buffer until we have size characters or we hit EOF.
1784 If size is negative or omitted, read until EOF.
Raymond Hettingercbb80892011-01-13 18:15:51 +00001785
1786 Returns a string.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001787 """
1788 self._unsupported("read")
1789
Raymond Hettinger3c940242011-01-12 23:39:31 +00001790 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001791 """Write string s to stream and returning an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001792 self._unsupported("write")
1793
Georg Brandl4d73b572011-01-13 07:13:06 +00001794 def truncate(self, pos=None):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001795 """Truncate size to pos, where pos is an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001796 self._unsupported("truncate")
1797
Raymond Hettinger3c940242011-01-12 23:39:31 +00001798 def readline(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001799 """Read until newline or EOF.
1800
1801 Returns an empty string if EOF is hit immediately.
1802 """
1803 self._unsupported("readline")
1804
Raymond Hettinger3c940242011-01-12 23:39:31 +00001805 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001806 """
1807 Separate the underlying buffer from the TextIOBase and return it.
1808
1809 After the underlying buffer has been detached, the TextIO is in an
1810 unusable state.
1811 """
1812 self._unsupported("detach")
1813
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001814 @property
1815 def encoding(self):
1816 """Subclasses should override."""
1817 return None
1818
1819 @property
1820 def newlines(self):
1821 """Line endings translated so far.
1822
1823 Only line endings translated during reading are considered.
1824
1825 Subclasses should override.
1826 """
1827 return None
1828
Benjamin Peterson0926ad12009-06-06 18:02:12 +00001829 @property
1830 def errors(self):
1831 """Error setting of the decoder or encoder.
1832
1833 Subclasses should override."""
1834 return None
1835
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001836io.TextIOBase.register(TextIOBase)
1837
1838
1839class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1840 r"""Codec used when reading a file in universal newlines mode. It wraps
1841 another incremental decoder, translating \r\n and \r into \n. It also
1842 records the types of newlines encountered. When used with
1843 translate=False, it ensures that the newline sequence is returned in
1844 one piece.
1845 """
1846 def __init__(self, decoder, translate, errors='strict'):
1847 codecs.IncrementalDecoder.__init__(self, errors=errors)
1848 self.translate = translate
1849 self.decoder = decoder
1850 self.seennl = 0
1851 self.pendingcr = False
1852
1853 def decode(self, input, final=False):
1854 # decode input (with the eventual \r from a previous pass)
1855 if self.decoder is None:
1856 output = input
1857 else:
1858 output = self.decoder.decode(input, final=final)
1859 if self.pendingcr and (output or final):
1860 output = "\r" + output
1861 self.pendingcr = False
1862
1863 # retain last \r even when not translating data:
1864 # then readline() is sure to get \r\n in one pass
1865 if output.endswith("\r") and not final:
1866 output = output[:-1]
1867 self.pendingcr = True
1868
1869 # Record which newlines are read
1870 crlf = output.count('\r\n')
1871 cr = output.count('\r') - crlf
1872 lf = output.count('\n') - crlf
1873 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1874 | (crlf and self._CRLF)
1875
1876 if self.translate:
1877 if crlf:
1878 output = output.replace("\r\n", "\n")
1879 if cr:
1880 output = output.replace("\r", "\n")
1881
1882 return output
1883
1884 def getstate(self):
1885 if self.decoder is None:
1886 buf = b""
1887 flag = 0
1888 else:
1889 buf, flag = self.decoder.getstate()
1890 flag <<= 1
1891 if self.pendingcr:
1892 flag |= 1
1893 return buf, flag
1894
1895 def setstate(self, state):
1896 buf, flag = state
1897 self.pendingcr = bool(flag & 1)
1898 if self.decoder is not None:
1899 self.decoder.setstate((buf, flag >> 1))
1900
1901 def reset(self):
1902 self.seennl = 0
1903 self.pendingcr = False
1904 if self.decoder is not None:
1905 self.decoder.reset()
1906
1907 _LF = 1
1908 _CR = 2
1909 _CRLF = 4
1910
1911 @property
1912 def newlines(self):
1913 return (None,
1914 "\n",
1915 "\r",
1916 ("\r", "\n"),
1917 "\r\n",
1918 ("\n", "\r\n"),
1919 ("\r", "\r\n"),
1920 ("\r", "\n", "\r\n")
1921 )[self.seennl]
1922
1923
1924class TextIOWrapper(TextIOBase):
1925
1926 r"""Character and line based layer over a BufferedIOBase object, buffer.
1927
1928 encoding gives the name of the encoding that the stream will be
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001929 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001930
1931 errors determines the strictness of encoding and decoding (see the
1932 codecs.register) and defaults to "strict".
1933
1934 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1935 handling of line endings. If it is None, universal newlines is
1936 enabled. With this enabled, on input, the lines endings '\n', '\r',
1937 or '\r\n' are translated to '\n' before being returned to the
1938 caller. Conversely, on output, '\n' is translated to the system
Éric Araujo39242302011-11-03 00:08:48 +01001939 default line separator, os.linesep. If newline is any other of its
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001940 legal values, that newline becomes the newline when the file is read
1941 and it is returned untranslated. On output, '\n' is converted to the
1942 newline.
1943
1944 If line_buffering is True, a call to flush is implied when a call to
1945 write contains a newline character.
1946 """
1947
1948 _CHUNK_SIZE = 2048
1949
Andrew Svetlov4e9e9c12012-08-13 16:09:54 +03001950 # The write_through argument has no effect here since this
1951 # implementation always writes through. The argument is present only
1952 # so that the signature can match the signature of the C version.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001953 def __init__(self, buffer, encoding=None, errors=None, newline=None,
Antoine Pitroue96ec682011-07-23 21:46:35 +02001954 line_buffering=False, write_through=False):
INADA Naoki507434f2017-12-21 09:59:53 +09001955 self._check_newline(newline)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001956 if encoding is None:
1957 try:
1958 encoding = os.device_encoding(buffer.fileno())
1959 except (AttributeError, UnsupportedOperation):
1960 pass
1961 if encoding is None:
1962 try:
1963 import locale
Brett Cannoncd171c82013-07-04 17:43:24 -04001964 except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001965 # Importing locale may fail if Python is being built
1966 encoding = "ascii"
1967 else:
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001968 encoding = locale.getpreferredencoding(False)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001969
1970 if not isinstance(encoding, str):
1971 raise ValueError("invalid encoding: %r" % encoding)
1972
Nick Coghlana9b15242014-02-04 22:11:18 +10001973 if not codecs.lookup(encoding)._is_text_encoding:
1974 msg = ("%r is not a text encoding; "
1975 "use codecs.open() to handle arbitrary codecs")
1976 raise LookupError(msg % encoding)
1977
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001978 if errors is None:
1979 errors = "strict"
1980 else:
1981 if not isinstance(errors, str):
1982 raise ValueError("invalid errors: %r" % errors)
1983
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001984 self._buffer = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001985 self._decoded_chars = '' # buffer for text returned from decoder
1986 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1987 self._snapshot = None # info for reconstructing decoder state
1988 self._seekable = self._telling = self.buffer.seekable()
Antoine Pitroue96ec682011-07-23 21:46:35 +02001989 self._has_read1 = hasattr(self.buffer, 'read1')
INADA Naoki507434f2017-12-21 09:59:53 +09001990 self._configure(encoding, errors, newline,
1991 line_buffering, write_through)
1992
1993 def _check_newline(self, newline):
1994 if newline is not None and not isinstance(newline, str):
1995 raise TypeError("illegal newline type: %r" % (type(newline),))
1996 if newline not in (None, "", "\n", "\r", "\r\n"):
1997 raise ValueError("illegal newline value: %r" % (newline,))
1998
1999 def _configure(self, encoding=None, errors=None, newline=None,
2000 line_buffering=False, write_through=False):
2001 self._encoding = encoding
2002 self._errors = errors
2003 self._encoder = None
2004 self._decoder = None
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002005 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002006
INADA Naoki507434f2017-12-21 09:59:53 +09002007 self._readuniversal = not newline
2008 self._readtranslate = newline is None
2009 self._readnl = newline
2010 self._writetranslate = newline != ''
2011 self._writenl = newline or os.linesep
2012
2013 self._line_buffering = line_buffering
2014 self._write_through = write_through
2015
2016 # don't write a BOM in the middle of a file
Antoine Pitroue4501852009-05-14 18:55:55 +00002017 if self._seekable and self.writable():
2018 position = self.buffer.tell()
2019 if position != 0:
2020 try:
2021 self._get_encoder().setstate(0)
2022 except LookupError:
2023 # Sometimes the encoder doesn't exist
2024 pass
2025
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002026 # self._snapshot is either None, or a tuple (dec_flags, next_input)
2027 # where dec_flags is the second (integer) item of the decoder state
2028 # and next_input is the chunk of input bytes that comes next after the
2029 # snapshot point. We use this to reconstruct decoder states in tell().
2030
2031 # Naming convention:
2032 # - "bytes_..." for integer variables that count input bytes
2033 # - "chars_..." for integer variables that count decoded characters
2034
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002035 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +03002036 result = "<{}.{}".format(self.__class__.__module__,
2037 self.__class__.__qualname__)
Antoine Pitrou716c4442009-05-23 19:04:03 +00002038 try:
2039 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002040 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002041 pass
Antoine Pitrou716c4442009-05-23 19:04:03 +00002042 else:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002043 result += " name={0!r}".format(name)
2044 try:
2045 mode = self.mode
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002046 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002047 pass
2048 else:
2049 result += " mode={0!r}".format(mode)
2050 return result + " encoding={0!r}>".format(self.encoding)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002051
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002052 @property
2053 def encoding(self):
2054 return self._encoding
2055
2056 @property
2057 def errors(self):
2058 return self._errors
2059
2060 @property
2061 def line_buffering(self):
2062 return self._line_buffering
2063
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002064 @property
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002065 def write_through(self):
2066 return self._write_through
2067
2068 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002069 def buffer(self):
2070 return self._buffer
2071
INADA Naoki507434f2017-12-21 09:59:53 +09002072 def reconfigure(self, *,
2073 encoding=None, errors=None, newline=Ellipsis,
2074 line_buffering=None, write_through=None):
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002075 """Reconfigure the text stream with new parameters.
2076
2077 This also flushes the stream.
2078 """
INADA Naoki507434f2017-12-21 09:59:53 +09002079 if (self._decoder is not None
2080 and (encoding is not None or errors is not None
2081 or newline is not Ellipsis)):
2082 raise UnsupportedOperation(
2083 "It is not possible to set the encoding or newline of stream "
2084 "after the first read")
2085
2086 if errors is None:
2087 if encoding is None:
2088 errors = self._errors
2089 else:
2090 errors = 'strict'
2091 elif not isinstance(errors, str):
2092 raise TypeError("invalid errors: %r" % errors)
2093
2094 if encoding is None:
2095 encoding = self._encoding
2096 else:
2097 if not isinstance(encoding, str):
2098 raise TypeError("invalid encoding: %r" % encoding)
2099
2100 if newline is Ellipsis:
2101 newline = self._readnl
2102 self._check_newline(newline)
2103
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002104 if line_buffering is None:
2105 line_buffering = self.line_buffering
2106 if write_through is None:
2107 write_through = self.write_through
INADA Naoki507434f2017-12-21 09:59:53 +09002108
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002109 self.flush()
INADA Naoki507434f2017-12-21 09:59:53 +09002110 self._configure(encoding, errors, newline,
2111 line_buffering, write_through)
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002112
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002113 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02002114 if self.closed:
2115 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002116 return self._seekable
2117
2118 def readable(self):
2119 return self.buffer.readable()
2120
2121 def writable(self):
2122 return self.buffer.writable()
2123
2124 def flush(self):
2125 self.buffer.flush()
2126 self._telling = self._seekable
2127
2128 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +00002129 if self.buffer is not None and not self.closed:
Benjamin Peterson68623612012-12-20 11:53:11 -06002130 try:
2131 self.flush()
2132 finally:
2133 self.buffer.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002134
2135 @property
2136 def closed(self):
2137 return self.buffer.closed
2138
2139 @property
2140 def name(self):
2141 return self.buffer.name
2142
2143 def fileno(self):
2144 return self.buffer.fileno()
2145
2146 def isatty(self):
2147 return self.buffer.isatty()
2148
Raymond Hettinger00fa0392011-01-13 02:52:26 +00002149 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00002150 'Write data, where s is a str'
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002151 if self.closed:
2152 raise ValueError("write to closed file")
2153 if not isinstance(s, str):
2154 raise TypeError("can't write %s to text stream" %
2155 s.__class__.__name__)
2156 length = len(s)
2157 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
2158 if haslf and self._writetranslate and self._writenl != "\n":
2159 s = s.replace("\n", self._writenl)
2160 encoder = self._encoder or self._get_encoder()
2161 # XXX What if we were just reading?
2162 b = encoder.encode(s)
2163 self.buffer.write(b)
2164 if self._line_buffering and (haslf or "\r" in s):
2165 self.flush()
Zackery Spytz23db9352018-06-29 04:14:58 -06002166 self._set_decoded_chars('')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002167 self._snapshot = None
2168 if self._decoder:
2169 self._decoder.reset()
2170 return length
2171
2172 def _get_encoder(self):
2173 make_encoder = codecs.getincrementalencoder(self._encoding)
2174 self._encoder = make_encoder(self._errors)
2175 return self._encoder
2176
2177 def _get_decoder(self):
2178 make_decoder = codecs.getincrementaldecoder(self._encoding)
2179 decoder = make_decoder(self._errors)
2180 if self._readuniversal:
2181 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
2182 self._decoder = decoder
2183 return decoder
2184
2185 # The following three methods implement an ADT for _decoded_chars.
2186 # Text returned from the decoder is buffered here until the client
2187 # requests it by calling our read() or readline() method.
2188 def _set_decoded_chars(self, chars):
2189 """Set the _decoded_chars buffer."""
2190 self._decoded_chars = chars
2191 self._decoded_chars_used = 0
2192
2193 def _get_decoded_chars(self, n=None):
2194 """Advance into the _decoded_chars buffer."""
2195 offset = self._decoded_chars_used
2196 if n is None:
2197 chars = self._decoded_chars[offset:]
2198 else:
2199 chars = self._decoded_chars[offset:offset + n]
2200 self._decoded_chars_used += len(chars)
2201 return chars
2202
2203 def _rewind_decoded_chars(self, n):
2204 """Rewind the _decoded_chars buffer."""
2205 if self._decoded_chars_used < n:
2206 raise AssertionError("rewind decoded_chars out of bounds")
2207 self._decoded_chars_used -= n
2208
2209 def _read_chunk(self):
2210 """
2211 Read and decode the next chunk of data from the BufferedReader.
2212 """
2213
2214 # The return value is True unless EOF was reached. The decoded
2215 # string is placed in self._decoded_chars (replacing its previous
2216 # value). The entire input chunk is sent to the decoder, though
2217 # some of it may remain buffered in the decoder, yet to be
2218 # converted.
2219
2220 if self._decoder is None:
2221 raise ValueError("no decoder")
2222
2223 if self._telling:
2224 # To prepare for tell(), we need to snapshot a point in the
2225 # file where the decoder's input buffer is empty.
2226
2227 dec_buffer, dec_flags = self._decoder.getstate()
2228 # Given this, we know there was a valid snapshot point
2229 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
2230
2231 # Read a chunk, decode it, and put the result in self._decoded_chars.
Antoine Pitroue96ec682011-07-23 21:46:35 +02002232 if self._has_read1:
2233 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
2234 else:
2235 input_chunk = self.buffer.read(self._CHUNK_SIZE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002236 eof = not input_chunk
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002237 decoded_chars = self._decoder.decode(input_chunk, eof)
2238 self._set_decoded_chars(decoded_chars)
2239 if decoded_chars:
2240 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
2241 else:
2242 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002243
2244 if self._telling:
2245 # At the snapshot point, len(dec_buffer) bytes before the read,
2246 # the next input to be decoded is dec_buffer + input_chunk.
2247 self._snapshot = (dec_flags, dec_buffer + input_chunk)
2248
2249 return not eof
2250
2251 def _pack_cookie(self, position, dec_flags=0,
2252 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
2253 # The meaning of a tell() cookie is: seek to position, set the
2254 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
2255 # into the decoder with need_eof as the EOF flag, then skip
2256 # chars_to_skip characters of the decoded result. For most simple
2257 # decoders, tell() will often just give a byte offset in the file.
2258 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
2259 (chars_to_skip<<192) | bool(need_eof)<<256)
2260
2261 def _unpack_cookie(self, bigint):
2262 rest, position = divmod(bigint, 1<<64)
2263 rest, dec_flags = divmod(rest, 1<<64)
2264 rest, bytes_to_feed = divmod(rest, 1<<64)
2265 need_eof, chars_to_skip = divmod(rest, 1<<64)
2266 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
2267
2268 def tell(self):
2269 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002270 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002271 if not self._telling:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002272 raise OSError("telling position disabled by next() call")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002273 self.flush()
2274 position = self.buffer.tell()
2275 decoder = self._decoder
2276 if decoder is None or self._snapshot is None:
2277 if self._decoded_chars:
2278 # This should never happen.
2279 raise AssertionError("pending decoded text")
2280 return position
2281
2282 # Skip backward to the snapshot point (see _read_chunk).
2283 dec_flags, next_input = self._snapshot
2284 position -= len(next_input)
2285
2286 # How many decoded characters have been used up since the snapshot?
2287 chars_to_skip = self._decoded_chars_used
2288 if chars_to_skip == 0:
2289 # We haven't moved from the snapshot point.
2290 return self._pack_cookie(position, dec_flags)
2291
2292 # Starting from the snapshot position, we will walk the decoder
2293 # forward until it gives us enough decoded characters.
2294 saved_state = decoder.getstate()
2295 try:
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002296 # Fast search for an acceptable start point, close to our
2297 # current pos.
2298 # Rationale: calling decoder.decode() has a large overhead
2299 # regardless of chunk size; we want the number of such calls to
Raymond Hettinger14010182018-09-13 21:17:40 -07002300 # be O(1) in most situations (common decoders, sensible input).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002301 # Actually, it will be exactly 1 for fixed-size codecs (all
2302 # 8-bit codecs, also UTF-16 and UTF-32).
2303 skip_bytes = int(self._b2cratio * chars_to_skip)
2304 skip_back = 1
2305 assert skip_bytes <= len(next_input)
2306 while skip_bytes > 0:
2307 decoder.setstate((b'', dec_flags))
2308 # Decode up to temptative start point
2309 n = len(decoder.decode(next_input[:skip_bytes]))
2310 if n <= chars_to_skip:
2311 b, d = decoder.getstate()
2312 if not b:
2313 # Before pos and no bytes buffered in decoder => OK
2314 dec_flags = d
2315 chars_to_skip -= n
2316 break
2317 # Skip back by buffered amount and reset heuristic
2318 skip_bytes -= len(b)
2319 skip_back = 1
2320 else:
2321 # We're too far ahead, skip back a bit
2322 skip_bytes -= skip_back
2323 skip_back = skip_back * 2
2324 else:
2325 skip_bytes = 0
2326 decoder.setstate((b'', dec_flags))
2327
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002328 # Note our initial start point.
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002329 start_pos = position + skip_bytes
2330 start_flags = dec_flags
2331 if chars_to_skip == 0:
2332 # We haven't moved from the start point.
2333 return self._pack_cookie(start_pos, start_flags)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002334
2335 # Feed the decoder one byte at a time. As we go, note the
2336 # nearest "safe start point" before the current location
2337 # (a point where the decoder has nothing buffered, so seek()
2338 # can safely start from there and advance to this location).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002339 bytes_fed = 0
2340 need_eof = 0
2341 # Chars decoded since `start_pos`
2342 chars_decoded = 0
2343 for i in range(skip_bytes, len(next_input)):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002344 bytes_fed += 1
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002345 chars_decoded += len(decoder.decode(next_input[i:i+1]))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002346 dec_buffer, dec_flags = decoder.getstate()
2347 if not dec_buffer and chars_decoded <= chars_to_skip:
2348 # Decoder buffer is empty, so this is a safe start point.
2349 start_pos += bytes_fed
2350 chars_to_skip -= chars_decoded
2351 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
2352 if chars_decoded >= chars_to_skip:
2353 break
2354 else:
2355 # We didn't get enough decoded data; signal EOF to get more.
2356 chars_decoded += len(decoder.decode(b'', final=True))
2357 need_eof = 1
2358 if chars_decoded < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002359 raise OSError("can't reconstruct logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002360
2361 # The returned cookie corresponds to the last safe start point.
2362 return self._pack_cookie(
2363 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
2364 finally:
2365 decoder.setstate(saved_state)
2366
2367 def truncate(self, pos=None):
2368 self.flush()
2369 if pos is None:
2370 pos = self.tell()
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00002371 return self.buffer.truncate(pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002372
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002373 def detach(self):
2374 if self.buffer is None:
2375 raise ValueError("buffer is already detached")
2376 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002377 buffer = self._buffer
2378 self._buffer = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002379 return buffer
2380
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002381 def seek(self, cookie, whence=0):
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002382 def _reset_encoder(position):
2383 """Reset the encoder (merely useful for proper BOM handling)"""
2384 try:
2385 encoder = self._encoder or self._get_encoder()
2386 except LookupError:
2387 # Sometimes the encoder doesn't exist
2388 pass
2389 else:
2390 if position != 0:
2391 encoder.setstate(0)
2392 else:
2393 encoder.reset()
2394
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002395 if self.closed:
2396 raise ValueError("tell on closed file")
2397 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002398 raise UnsupportedOperation("underlying stream is not seekable")
ngie-eign848037c2019-03-02 23:28:26 -08002399 if whence == SEEK_CUR:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002400 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002401 raise UnsupportedOperation("can't do nonzero cur-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002402 # Seeking to the current position should attempt to
2403 # sync the underlying buffer with the current position.
2404 whence = 0
2405 cookie = self.tell()
ngie-eign848037c2019-03-02 23:28:26 -08002406 elif whence == SEEK_END:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002407 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002408 raise UnsupportedOperation("can't do nonzero end-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002409 self.flush()
ngie-eign848037c2019-03-02 23:28:26 -08002410 position = self.buffer.seek(0, whence)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002411 self._set_decoded_chars('')
2412 self._snapshot = None
2413 if self._decoder:
2414 self._decoder.reset()
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002415 _reset_encoder(position)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002416 return position
2417 if whence != 0:
Jesus Cea94363612012-06-22 18:32:07 +02002418 raise ValueError("unsupported whence (%r)" % (whence,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002419 if cookie < 0:
2420 raise ValueError("negative seek position %r" % (cookie,))
2421 self.flush()
2422
2423 # The strategy of seek() is to go back to the safe start point
2424 # and replay the effect of read(chars_to_skip) from there.
2425 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
2426 self._unpack_cookie(cookie)
2427
2428 # Seek back to the safe start point.
2429 self.buffer.seek(start_pos)
2430 self._set_decoded_chars('')
2431 self._snapshot = None
2432
2433 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00002434 if cookie == 0 and self._decoder:
2435 self._decoder.reset()
2436 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002437 self._decoder = self._decoder or self._get_decoder()
2438 self._decoder.setstate((b'', dec_flags))
2439 self._snapshot = (dec_flags, b'')
2440
2441 if chars_to_skip:
2442 # Just like _read_chunk, feed the decoder and save a snapshot.
2443 input_chunk = self.buffer.read(bytes_to_feed)
2444 self._set_decoded_chars(
2445 self._decoder.decode(input_chunk, need_eof))
2446 self._snapshot = (dec_flags, input_chunk)
2447
2448 # Skip chars_to_skip of the decoded characters.
2449 if len(self._decoded_chars) < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002450 raise OSError("can't restore logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002451 self._decoded_chars_used = chars_to_skip
2452
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002453 _reset_encoder(cookie)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002454 return cookie
2455
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002456 def read(self, size=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00002457 self._checkReadable()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002458 if size is None:
2459 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002460 else:
2461 try:
2462 size_index = size.__index__
2463 except AttributeError:
2464 raise TypeError(f"{size!r} is not an integer")
2465 else:
2466 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002467 decoder = self._decoder or self._get_decoder()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002468 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002469 # Read everything.
2470 result = (self._get_decoded_chars() +
2471 decoder.decode(self.buffer.read(), final=True))
2472 self._set_decoded_chars('')
2473 self._snapshot = None
2474 return result
2475 else:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002476 # Keep reading chunks until we have size characters to return.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002477 eof = False
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002478 result = self._get_decoded_chars(size)
2479 while len(result) < size and not eof:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002480 eof = not self._read_chunk()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002481 result += self._get_decoded_chars(size - len(result))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002482 return result
2483
2484 def __next__(self):
2485 self._telling = False
2486 line = self.readline()
2487 if not line:
2488 self._snapshot = None
2489 self._telling = self._seekable
2490 raise StopIteration
2491 return line
2492
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002493 def readline(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002494 if self.closed:
2495 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002496 if size is None:
2497 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002498 else:
2499 try:
2500 size_index = size.__index__
2501 except AttributeError:
2502 raise TypeError(f"{size!r} is not an integer")
2503 else:
2504 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002505
2506 # Grab all the decoded text (we will rewind any extra bits later).
2507 line = self._get_decoded_chars()
2508
2509 start = 0
2510 # Make the decoder if it doesn't already exist.
2511 if not self._decoder:
2512 self._get_decoder()
2513
2514 pos = endpos = None
2515 while True:
2516 if self._readtranslate:
2517 # Newlines are already translated, only search for \n
2518 pos = line.find('\n', start)
2519 if pos >= 0:
2520 endpos = pos + 1
2521 break
2522 else:
2523 start = len(line)
2524
2525 elif self._readuniversal:
2526 # Universal newline search. Find any of \r, \r\n, \n
2527 # The decoder ensures that \r\n are not split in two pieces
2528
2529 # In C we'd look for these in parallel of course.
2530 nlpos = line.find("\n", start)
2531 crpos = line.find("\r", start)
2532 if crpos == -1:
2533 if nlpos == -1:
2534 # Nothing found
2535 start = len(line)
2536 else:
2537 # Found \n
2538 endpos = nlpos + 1
2539 break
2540 elif nlpos == -1:
2541 # Found lone \r
2542 endpos = crpos + 1
2543 break
2544 elif nlpos < crpos:
2545 # Found \n
2546 endpos = nlpos + 1
2547 break
2548 elif nlpos == crpos + 1:
2549 # Found \r\n
2550 endpos = crpos + 2
2551 break
2552 else:
2553 # Found \r
2554 endpos = crpos + 1
2555 break
2556 else:
2557 # non-universal
2558 pos = line.find(self._readnl)
2559 if pos >= 0:
2560 endpos = pos + len(self._readnl)
2561 break
2562
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002563 if size >= 0 and len(line) >= size:
2564 endpos = size # reached length size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002565 break
2566
2567 # No line ending seen yet - get more data'
2568 while self._read_chunk():
2569 if self._decoded_chars:
2570 break
2571 if self._decoded_chars:
2572 line += self._get_decoded_chars()
2573 else:
2574 # end of file
2575 self._set_decoded_chars('')
2576 self._snapshot = None
2577 return line
2578
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002579 if size >= 0 and endpos > size:
2580 endpos = size # don't exceed size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002581
2582 # Rewind _decoded_chars to just after the line ending we found.
2583 self._rewind_decoded_chars(len(line) - endpos)
2584 return line[:endpos]
2585
2586 @property
2587 def newlines(self):
2588 return self._decoder.newlines if self._decoder else None
2589
2590
2591class StringIO(TextIOWrapper):
2592 """Text I/O implementation using an in-memory buffer.
2593
2594 The initial_value argument sets the value of object. The newline
2595 argument is like the one of TextIOWrapper's constructor.
2596 """
2597
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002598 def __init__(self, initial_value="", newline="\n"):
2599 super(StringIO, self).__init__(BytesIO(),
2600 encoding="utf-8",
Serhiy Storchakac92ea762014-01-29 11:33:26 +02002601 errors="surrogatepass",
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002602 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00002603 # Issue #5645: make universal newlines semantics the same as in the
2604 # C version, even under Windows.
2605 if newline is None:
2606 self._writetranslate = False
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002607 if initial_value is not None:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002608 if not isinstance(initial_value, str):
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002609 raise TypeError("initial_value must be str or None, not {0}"
2610 .format(type(initial_value).__name__))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002611 self.write(initial_value)
2612 self.seek(0)
2613
2614 def getvalue(self):
2615 self.flush()
Antoine Pitrou57839a62014-02-02 23:37:29 +01002616 decoder = self._decoder or self._get_decoder()
2617 old_state = decoder.getstate()
2618 decoder.reset()
2619 try:
2620 return decoder.decode(self.buffer.getvalue(), final=True)
2621 finally:
2622 decoder.setstate(old_state)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002623
2624 def __repr__(self):
2625 # TextIOWrapper tells the encoding in its repr. In StringIO,
Martin Panter7462b6492015-11-02 03:37:02 +00002626 # that's an implementation detail.
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002627 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00002628
2629 @property
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002630 def errors(self):
2631 return None
2632
2633 @property
Benjamin Petersonb487e632009-03-21 03:08:31 +00002634 def encoding(self):
2635 return None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002636
2637 def detach(self):
2638 # This doesn't make sense on StringIO.
2639 self._unsupported("detach")