blob: e81cc5128881fe7f710cb1c301573a37004e2be7 [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
36
Georg Brandl4d73b572011-01-13 07:13:06 +000037def open(file, mode="r", buffering=-1, encoding=None, errors=None,
Ross Lagerwall59142db2011-10-31 20:34:46 +020038 newline=None, closefd=True, opener=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000039
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020040 r"""Open file and return a stream. Raise OSError upon failure.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000041
42 file is either a text or byte string giving the name (and the path
43 if the file isn't in the current working directory) of the file to
44 be opened or an integer file descriptor of the file to be
45 wrapped. (If a file descriptor is given, it is closed when the
46 returned I/O object is closed, unless closefd is set to False.)
47
Charles-François Natalidc3044c2012-01-09 22:40:02 +010048 mode is an optional string that specifies the mode in which the file is
49 opened. It defaults to 'r' which means open for reading in text mode. Other
50 common values are 'w' for writing (truncating the file if it already
Charles-François Natalid612de12012-01-14 11:51:00 +010051 exists), 'x' for exclusive creation of a new file, and 'a' for appending
Charles-François Natalidc3044c2012-01-09 22:40:02 +010052 (which on some Unix systems, means that all writes append to the end of the
53 file regardless of the current seek position). In text mode, if encoding is
54 not specified the encoding used is platform dependent. (For reading and
55 writing raw bytes use binary mode and leave encoding unspecified.) The
56 available modes are:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000057
58 ========= ===============================================================
59 Character Meaning
60 --------- ---------------------------------------------------------------
61 'r' open for reading (default)
62 'w' open for writing, truncating the file first
Charles-François Natalidc3044c2012-01-09 22:40:02 +010063 'x' create a new file and open it for writing
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000064 'a' open for writing, appending to the end of the file if it exists
65 'b' binary mode
66 't' text mode (default)
67 '+' open a disk file for updating (reading and writing)
Serhiy Storchaka6787a382013-11-23 22:12:06 +020068 'U' universal newline mode (deprecated)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000069 ========= ===============================================================
70
71 The default mode is 'rt' (open for reading text). For binary random
72 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
Charles-François Natalidc3044c2012-01-09 22:40:02 +010073 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
74 raises an `FileExistsError` if the file already exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000075
76 Python distinguishes between files opened in binary and text modes,
77 even when the underlying operating system doesn't. Files opened in
78 binary mode (appending 'b' to the mode argument) return contents as
79 bytes objects without any decoding. In text mode (the default, or when
80 't' is appended to the mode argument), the contents of the file are
81 returned as strings, the bytes having been first decoded using a
82 platform-dependent encoding or using the specified encoding if given.
83
Serhiy Storchaka6787a382013-11-23 22:12:06 +020084 'U' mode is deprecated and will raise an exception in future versions
85 of Python. It has no effect in Python 3. Use newline to control
86 universal newlines mode.
87
Antoine Pitroud5587bc2009-12-19 21:08:31 +000088 buffering is an optional integer used to set the buffering policy.
89 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
90 line buffering (only usable in text mode), and an integer > 1 to indicate
91 the size of a fixed-size chunk buffer. When no buffering argument is
92 given, the default buffering policy works as follows:
93
94 * Binary files are buffered in fixed-size chunks; the size of the buffer
95 is chosen using a heuristic trying to determine the underlying device's
96 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
97 On many systems, the buffer will typically be 4096 or 8192 bytes long.
98
99 * "Interactive" text files (files for which isatty() returns True)
100 use line buffering. Other text files use the policy described above
101 for binary files.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000102
Raymond Hettingercbb80892011-01-13 18:15:51 +0000103 encoding is the str name of the encoding used to decode or encode the
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000104 file. This should only be used in text mode. The default encoding is
105 platform dependent, but any encoding supported by Python can be
106 passed. See the codecs module for the list of supported encodings.
107
108 errors is an optional string that specifies how encoding errors are to
109 be handled---this argument should not be used in binary mode. Pass
110 'strict' to raise a ValueError exception if there is an encoding error
111 (the default of None has the same effect), or pass 'ignore' to ignore
112 errors. (Note that ignoring encoding errors can lead to data loss.)
113 See the documentation for codecs.register for a list of the permitted
114 encoding error strings.
115
Raymond Hettingercbb80892011-01-13 18:15:51 +0000116 newline is a string controlling how universal newlines works (it only
117 applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
118 as follows:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000119
120 * On input, if newline is None, universal newlines mode is
121 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
122 these are translated into '\n' before being returned to the
123 caller. If it is '', universal newline mode is enabled, but line
124 endings are returned to the caller untranslated. If it has any of
125 the other legal values, input lines are only terminated by the given
126 string, and the line ending is returned to the caller untranslated.
127
128 * On output, if newline is None, any '\n' characters written are
129 translated to the system default line separator, os.linesep. If
130 newline is '', no translation takes place. If newline is any of the
131 other legal values, any '\n' characters written are translated to
132 the given string.
133
Raymond Hettingercbb80892011-01-13 18:15:51 +0000134 closedfd is a bool. If closefd is False, the underlying file descriptor will
135 be kept open when the file is closed. This does not work when a file name is
136 given and must be True in that case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000137
Victor Stinnerdaf45552013-08-28 00:53:59 +0200138 The newly created file is non-inheritable.
139
Ross Lagerwall59142db2011-10-31 20:34:46 +0200140 A custom opener can be used by passing a callable as *opener*. The
141 underlying file descriptor for the file object is then obtained by calling
142 *opener* with (*file*, *flags*). *opener* must return an open file
143 descriptor (passing os.open as *opener* results in functionality similar to
144 passing None).
145
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000146 open() returns a file object whose type depends on the mode, and
147 through which the standard file operations such as reading and writing
148 are performed. When open() is used to open a file in a text mode ('w',
149 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
150 a file in a binary mode, the returned class varies: in read binary
151 mode, it returns a BufferedReader; in write binary and append binary
152 modes, it returns a BufferedWriter, and in read/write mode, it returns
153 a BufferedRandom.
154
155 It is also possible to use a string or bytearray as a file for both
156 reading and writing. For strings StringIO can be used like a file
157 opened in a text mode, and for bytes a BytesIO can be used like a file
158 opened in a binary mode.
159 """
Ethan Furmand62548a2016-06-04 14:38:43 -0700160 if not isinstance(file, int):
161 file = os.fspath(file)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000162 if not isinstance(file, (str, bytes, int)):
163 raise TypeError("invalid file: %r" % file)
164 if not isinstance(mode, str):
165 raise TypeError("invalid mode: %r" % mode)
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000166 if not isinstance(buffering, int):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000167 raise TypeError("invalid buffering: %r" % buffering)
168 if encoding is not None and not isinstance(encoding, str):
169 raise TypeError("invalid encoding: %r" % encoding)
170 if errors is not None and not isinstance(errors, str):
171 raise TypeError("invalid errors: %r" % errors)
172 modes = set(mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100173 if modes - set("axrwb+tU") or len(mode) > len(modes):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000174 raise ValueError("invalid mode: %r" % mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100175 creating = "x" in modes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000176 reading = "r" in modes
177 writing = "w" in modes
178 appending = "a" in modes
179 updating = "+" in modes
180 text = "t" in modes
181 binary = "b" in modes
182 if "U" in modes:
Robert Collinsc94a1dc2015-07-26 06:43:13 +1200183 if creating or writing or appending or updating:
184 raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200185 import warnings
186 warnings.warn("'U' mode is deprecated",
187 DeprecationWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000188 reading = True
189 if text and binary:
190 raise ValueError("can't have text and binary mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100191 if creating + reading + writing + appending > 1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000192 raise ValueError("can't have read/write/append mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100193 if not (creating or reading or writing or appending):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000194 raise ValueError("must have exactly one of read/write/append mode")
195 if binary and encoding is not None:
196 raise ValueError("binary mode doesn't take an encoding argument")
197 if binary and errors is not None:
198 raise ValueError("binary mode doesn't take an errors argument")
199 if binary and newline is not None:
200 raise ValueError("binary mode doesn't take a newline argument")
201 raw = FileIO(file,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100202 (creating and "x" or "") +
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000203 (reading and "r" or "") +
204 (writing and "w" or "") +
205 (appending and "a" or "") +
206 (updating and "+" or ""),
Ross Lagerwall59142db2011-10-31 20:34:46 +0200207 closefd, opener=opener)
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300208 result = raw
209 try:
210 line_buffering = False
211 if buffering == 1 or buffering < 0 and raw.isatty():
212 buffering = -1
213 line_buffering = True
214 if buffering < 0:
215 buffering = DEFAULT_BUFFER_SIZE
216 try:
217 bs = os.fstat(raw.fileno()).st_blksize
218 except (OSError, AttributeError):
219 pass
220 else:
221 if bs > 1:
222 buffering = bs
223 if buffering < 0:
224 raise ValueError("invalid buffering size")
225 if buffering == 0:
226 if binary:
227 return result
228 raise ValueError("can't have unbuffered text I/O")
229 if updating:
230 buffer = BufferedRandom(raw, buffering)
231 elif creating or writing or appending:
232 buffer = BufferedWriter(raw, buffering)
233 elif reading:
234 buffer = BufferedReader(raw, buffering)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000235 else:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300236 raise ValueError("unknown mode: %r" % mode)
237 result = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000238 if binary:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300239 return result
240 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
241 result = text
242 text.mode = mode
243 return result
244 except:
245 result.close()
246 raise
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000247
248
249class DocDescriptor:
250 """Helper for builtins.open.__doc__
251 """
252 def __get__(self, obj, typ):
253 return (
Benjamin Petersonc3be11a2010-04-27 21:24:03 +0000254 "open(file, mode='r', buffering=-1, encoding=None, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000255 "errors=None, newline=None, closefd=True)\n\n" +
256 open.__doc__)
257
258class OpenWrapper:
259 """Wrapper for builtins.open
260
261 Trick so that open won't become a bound method when stored
262 as a class variable (as dbm.dumb does).
263
Nick Coghland6009512014-11-20 21:39:37 +1000264 See initstdio() in Python/pylifecycle.c.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000265 """
266 __doc__ = DocDescriptor()
267
268 def __new__(cls, *args, **kwargs):
269 return open(*args, **kwargs)
270
271
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000272# In normal operation, both `UnsupportedOperation`s should be bound to the
273# same object.
274try:
275 UnsupportedOperation = io.UnsupportedOperation
276except AttributeError:
Serhiy Storchaka606ab862016-12-07 13:31:20 +0200277 class UnsupportedOperation(OSError, ValueError):
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000278 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000279
280
281class IOBase(metaclass=abc.ABCMeta):
282
283 """The abstract base class for all I/O classes, acting on streams of
284 bytes. There is no public constructor.
285
286 This class provides dummy implementations for many methods that
287 derived classes can override selectively; the default implementations
288 represent a file that cannot be read, written or seeked.
289
Miss Islington (bot)0a16bb12019-04-08 21:57:31 -0700290 Even though IOBase does not declare read or write because
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000291 their signatures will vary, implementations and clients should
292 consider those methods part of the interface. Also, implementations
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000293 may raise UnsupportedOperation when operations they do not support are
294 called.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000295
296 The basic type used for binary data read from or written to a file is
Miss Islington (bot)0a16bb12019-04-08 21:57:31 -0700297 bytes. Other bytes-like objects are accepted as method arguments too.
298 Text I/O classes work with str data.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000299
300 Note that calling any method (even inquiries) on a closed stream is
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200301 undefined. Implementations may raise OSError in this case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000302
303 IOBase (and its subclasses) support the iterator protocol, meaning
304 that an IOBase object can be iterated over yielding the lines in a
305 stream.
306
307 IOBase also supports the :keyword:`with` statement. In this example,
308 fp is closed after the suite of the with statement is complete:
309
310 with open('spam.txt', 'r') as fp:
311 fp.write('Spam and eggs!')
312 """
313
314 ### Internal ###
315
Raymond Hettinger3c940242011-01-12 23:39:31 +0000316 def _unsupported(self, name):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200317 """Internal: raise an OSError exception for unsupported operations."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000318 raise UnsupportedOperation("%s.%s() not supported" %
319 (self.__class__.__name__, name))
320
321 ### Positioning ###
322
Georg Brandl4d73b572011-01-13 07:13:06 +0000323 def seek(self, pos, whence=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000324 """Change stream position.
325
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400326 Change the stream position to byte offset pos. Argument pos is
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000327 interpreted relative to the position indicated by whence. Values
Raymond Hettingercbb80892011-01-13 18:15:51 +0000328 for whence are ints:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000329
330 * 0 -- start of stream (the default); offset should be zero or positive
331 * 1 -- current stream position; offset may be negative
332 * 2 -- end of stream; offset is usually negative
Jesus Cea94363612012-06-22 18:32:07 +0200333 Some operating systems / file systems could provide additional values.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000334
Raymond Hettingercbb80892011-01-13 18:15:51 +0000335 Return an int indicating the new absolute position.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000336 """
337 self._unsupported("seek")
338
Raymond Hettinger3c940242011-01-12 23:39:31 +0000339 def tell(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000340 """Return an int indicating the current stream position."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000341 return self.seek(0, 1)
342
Georg Brandl4d73b572011-01-13 07:13:06 +0000343 def truncate(self, pos=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000344 """Truncate file to size bytes.
345
346 Size defaults to the current IO position as reported by tell(). Return
347 the new size.
348 """
349 self._unsupported("truncate")
350
351 ### Flush and close ###
352
Raymond Hettinger3c940242011-01-12 23:39:31 +0000353 def flush(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000354 """Flush write buffers, if applicable.
355
356 This is not implemented for read-only and non-blocking streams.
357 """
Antoine Pitrou6be88762010-05-03 16:48:20 +0000358 self._checkClosed()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000359 # XXX Should this return the number of bytes written???
360
361 __closed = False
362
Raymond Hettinger3c940242011-01-12 23:39:31 +0000363 def close(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000364 """Flush and close the IO object.
365
366 This method has no effect if the file is already closed.
367 """
368 if not self.__closed:
Benjamin Peterson68623612012-12-20 11:53:11 -0600369 try:
370 self.flush()
371 finally:
372 self.__closed = True
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000373
Raymond Hettinger3c940242011-01-12 23:39:31 +0000374 def __del__(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000375 """Destructor. Calls close()."""
376 # The try/except block is in case this is called at program
377 # exit time, when it's possible that globals have already been
378 # deleted, and then the close() call might fail. Since
379 # there's nothing we can do about such failures and they annoy
380 # the end users, we suppress the traceback.
381 try:
382 self.close()
383 except:
384 pass
385
386 ### Inquiries ###
387
Raymond Hettinger3c940242011-01-12 23:39:31 +0000388 def seekable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000389 """Return a bool indicating whether object supports random access.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000390
Martin Panter754aab22016-03-31 07:21:56 +0000391 If False, seek(), tell() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000392 This method may need to do a test seek().
393 """
394 return False
395
396 def _checkSeekable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000397 """Internal: raise UnsupportedOperation if file is not seekable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000398 """
399 if not self.seekable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000400 raise UnsupportedOperation("File or stream is not seekable."
401 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000402
Raymond Hettinger3c940242011-01-12 23:39:31 +0000403 def readable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000404 """Return a bool indicating whether object was opened for reading.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000405
Martin Panter754aab22016-03-31 07:21:56 +0000406 If False, read() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000407 """
408 return False
409
410 def _checkReadable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000411 """Internal: raise UnsupportedOperation if file is not readable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000412 """
413 if not self.readable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000414 raise UnsupportedOperation("File or stream is not readable."
415 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000416
Raymond Hettinger3c940242011-01-12 23:39:31 +0000417 def writable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000418 """Return a bool indicating whether object was opened for writing.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000419
Martin Panter754aab22016-03-31 07:21:56 +0000420 If False, write() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000421 """
422 return False
423
424 def _checkWritable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000425 """Internal: raise UnsupportedOperation if file is not writable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000426 """
427 if not self.writable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000428 raise UnsupportedOperation("File or stream is not writable."
429 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000430
431 @property
432 def closed(self):
433 """closed: bool. True iff the file has been closed.
434
435 For backwards compatibility, this is a property, not a predicate.
436 """
437 return self.__closed
438
439 def _checkClosed(self, msg=None):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300440 """Internal: raise a ValueError if file is closed
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000441 """
442 if self.closed:
443 raise ValueError("I/O operation on closed file."
444 if msg is None else msg)
445
446 ### Context manager ###
447
Raymond Hettinger3c940242011-01-12 23:39:31 +0000448 def __enter__(self): # That's a forward reference
Raymond Hettingercbb80892011-01-13 18:15:51 +0000449 """Context management protocol. Returns self (an instance of IOBase)."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000450 self._checkClosed()
451 return self
452
Raymond Hettinger3c940242011-01-12 23:39:31 +0000453 def __exit__(self, *args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000454 """Context management protocol. Calls close()"""
455 self.close()
456
457 ### Lower-level APIs ###
458
459 # XXX Should these be present even if unimplemented?
460
Raymond Hettinger3c940242011-01-12 23:39:31 +0000461 def fileno(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000462 """Returns underlying file descriptor (an int) if one exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000463
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200464 An OSError is raised if the IO object does not use a file descriptor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465 """
466 self._unsupported("fileno")
467
Raymond Hettinger3c940242011-01-12 23:39:31 +0000468 def isatty(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000469 """Return a bool indicating whether this is an 'interactive' stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000470
471 Return False if it can't be determined.
472 """
473 self._checkClosed()
474 return False
475
476 ### Readline[s] and writelines ###
477
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300478 def readline(self, size=-1):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000479 r"""Read and return a line of bytes from the stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000480
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300481 If size is specified, at most size bytes will be read.
482 Size should be an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000483
484 The line terminator is always b'\n' for binary files; for text
485 files, the newlines argument to open can be used to select the line
486 terminator(s) recognized.
487 """
488 # For backwards compatibility, a (slowish) readline().
489 if hasattr(self, "peek"):
490 def nreadahead():
491 readahead = self.peek(1)
492 if not readahead:
493 return 1
494 n = (readahead.find(b"\n") + 1) or len(readahead)
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300495 if size >= 0:
496 n = min(n, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000497 return n
498 else:
499 def nreadahead():
500 return 1
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300501 if size is None:
502 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300503 else:
504 try:
505 size_index = size.__index__
506 except AttributeError:
507 raise TypeError(f"{size!r} is not an integer")
508 else:
509 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510 res = bytearray()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300511 while size < 0 or len(res) < size:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000512 b = self.read(nreadahead())
513 if not b:
514 break
515 res += b
516 if res.endswith(b"\n"):
517 break
518 return bytes(res)
519
520 def __iter__(self):
521 self._checkClosed()
522 return self
523
524 def __next__(self):
525 line = self.readline()
526 if not line:
527 raise StopIteration
528 return line
529
530 def readlines(self, hint=None):
531 """Return a list of lines from the stream.
532
533 hint can be specified to control the number of lines read: no more
534 lines will be read if the total size (in bytes/characters) of all
535 lines so far exceeds hint.
536 """
537 if hint is None or hint <= 0:
538 return list(self)
539 n = 0
540 lines = []
541 for line in self:
542 lines.append(line)
543 n += len(line)
544 if n >= hint:
545 break
546 return lines
547
548 def writelines(self, lines):
Marcin Niemira1100ae82019-04-22 22:08:24 +1000549 """Write a list of lines to the stream.
550
551 Line separators are not added, so it is usual for each of the lines
552 provided to have a line separator at the end.
553 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000554 self._checkClosed()
555 for line in lines:
556 self.write(line)
557
558io.IOBase.register(IOBase)
559
560
561class RawIOBase(IOBase):
562
563 """Base class for raw binary I/O."""
564
565 # The read() method is implemented by calling readinto(); derived
566 # classes that want to support read() only need to implement
567 # readinto() as a primitive operation. In general, readinto() can be
568 # more efficient than read().
569
570 # (It would be tempting to also provide an implementation of
571 # readinto() in terms of read(), in case the latter is a more suitable
572 # primitive operation, but that would lead to nasty recursion in case
573 # a subclass doesn't implement either.)
574
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300575 def read(self, size=-1):
576 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000577
578 Returns an empty bytes object on EOF, or None if the object is
579 set not to block and has no data to read.
580 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300581 if size is None:
582 size = -1
583 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000584 return self.readall()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300585 b = bytearray(size.__index__())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000586 n = self.readinto(b)
Antoine Pitrou328ec742010-09-14 18:37:24 +0000587 if n is None:
588 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000589 del b[n:]
590 return bytes(b)
591
592 def readall(self):
593 """Read until EOF, using multiple read() call."""
594 res = bytearray()
595 while True:
596 data = self.read(DEFAULT_BUFFER_SIZE)
597 if not data:
598 break
599 res += data
Victor Stinnera80987f2011-05-25 22:47:16 +0200600 if res:
601 return bytes(res)
602 else:
603 # b'' or None
604 return data
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000605
Raymond Hettinger3c940242011-01-12 23:39:31 +0000606 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000607 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000608
Raymond Hettingercbb80892011-01-13 18:15:51 +0000609 Returns an int representing the number of bytes read (0 for EOF), or
610 None if the object is set not to block and has no data to read.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000611 """
612 self._unsupported("readinto")
613
Raymond Hettinger3c940242011-01-12 23:39:31 +0000614 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000615 """Write the given buffer to the IO stream.
616
Martin Panter6bb91f32016-05-28 00:41:57 +0000617 Returns the number of bytes written, which may be less than the
618 length of b in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000619 """
620 self._unsupported("write")
621
622io.RawIOBase.register(RawIOBase)
623from _io import FileIO
624RawIOBase.register(FileIO)
625
626
627class BufferedIOBase(IOBase):
628
629 """Base class for buffered IO objects.
630
631 The main difference with RawIOBase is that the read() method
632 supports omitting the size argument, and does not have a default
633 implementation that defers to readinto().
634
635 In addition, read(), readinto() and write() may raise
636 BlockingIOError if the underlying raw stream is in non-blocking
637 mode and not ready; unlike their raw counterparts, they will never
638 return None.
639
640 A typical implementation should not inherit from a RawIOBase
641 implementation, but wrap one.
642 """
643
Martin Panterccb2c0e2016-10-20 23:48:14 +0000644 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300645 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000646
647 If the argument is omitted, None, or negative, reads and
648 returns all data until EOF.
649
650 If the argument is positive, and the underlying raw stream is
651 not 'interactive', multiple raw reads may be issued to satisfy
652 the byte count (unless EOF is reached first). But for
653 interactive raw streams (XXX and for pipes?), at most one raw
654 read will be issued, and a short result does not imply that
655 EOF is imminent.
656
657 Returns an empty bytes array on EOF.
658
659 Raises BlockingIOError if the underlying raw stream has no
660 data at the moment.
661 """
662 self._unsupported("read")
663
Martin Panterccb2c0e2016-10-20 23:48:14 +0000664 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300665 """Read up to size bytes with at most one read() system call,
666 where size is an int.
Raymond Hettingercbb80892011-01-13 18:15:51 +0000667 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000668 self._unsupported("read1")
669
Raymond Hettinger3c940242011-01-12 23:39:31 +0000670 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000671 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000672
673 Like read(), this may issue multiple reads to the underlying raw
674 stream, unless the latter is 'interactive'.
675
Raymond Hettingercbb80892011-01-13 18:15:51 +0000676 Returns an int representing the number of bytes read (0 for EOF).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000677
678 Raises BlockingIOError if the underlying raw stream has no
679 data at the moment.
680 """
Benjamin Petersona96fea02014-06-22 14:17:44 -0700681
682 return self._readinto(b, read1=False)
683
684 def readinto1(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000685 """Read bytes into buffer *b*, using at most one system call
Benjamin Petersona96fea02014-06-22 14:17:44 -0700686
687 Returns an int representing the number of bytes read (0 for EOF).
688
689 Raises BlockingIOError if the underlying raw stream has no
690 data at the moment.
691 """
692
693 return self._readinto(b, read1=True)
694
695 def _readinto(self, b, read1):
696 if not isinstance(b, memoryview):
697 b = memoryview(b)
698 b = b.cast('B')
699
700 if read1:
701 data = self.read1(len(b))
702 else:
703 data = self.read(len(b))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000704 n = len(data)
Benjamin Petersona96fea02014-06-22 14:17:44 -0700705
706 b[:n] = data
707
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000708 return n
709
Raymond Hettinger3c940242011-01-12 23:39:31 +0000710 def write(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000711 """Write the given bytes buffer to the IO stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000712
Martin Panter6bb91f32016-05-28 00:41:57 +0000713 Return the number of bytes written, which is always the length of b
714 in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000715
716 Raises BlockingIOError if the buffer is full and the
717 underlying raw stream cannot accept more data at the moment.
718 """
719 self._unsupported("write")
720
Raymond Hettinger3c940242011-01-12 23:39:31 +0000721 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000722 """
723 Separate the underlying raw stream from the buffer and return it.
724
725 After the raw stream has been detached, the buffer is in an unusable
726 state.
727 """
728 self._unsupported("detach")
729
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000730io.BufferedIOBase.register(BufferedIOBase)
731
732
733class _BufferedIOMixin(BufferedIOBase):
734
735 """A mixin implementation of BufferedIOBase with an underlying raw stream.
736
737 This passes most requests on to the underlying raw stream. It
738 does *not* provide implementations of read(), readinto() or
739 write().
740 """
741
742 def __init__(self, raw):
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000743 self._raw = raw
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000744
745 ### Positioning ###
746
747 def seek(self, pos, whence=0):
748 new_position = self.raw.seek(pos, whence)
749 if new_position < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200750 raise OSError("seek() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000751 return new_position
752
753 def tell(self):
754 pos = self.raw.tell()
755 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200756 raise OSError("tell() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000757 return pos
758
759 def truncate(self, pos=None):
760 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
761 # and a flush may be necessary to synch both views of the current
762 # file state.
763 self.flush()
764
765 if pos is None:
766 pos = self.tell()
767 # XXX: Should seek() be used, instead of passing the position
768 # XXX directly to truncate?
769 return self.raw.truncate(pos)
770
771 ### Flush and close ###
772
773 def flush(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000774 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +0300775 raise ValueError("flush on closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000776 self.raw.flush()
777
778 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000779 if self.raw is not None and not self.closed:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100780 try:
781 # may raise BlockingIOError or BrokenPipeError etc
782 self.flush()
783 finally:
784 self.raw.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000785
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000786 def detach(self):
787 if self.raw is None:
788 raise ValueError("raw stream already detached")
789 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000790 raw = self._raw
791 self._raw = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000792 return raw
793
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000794 ### Inquiries ###
795
796 def seekable(self):
797 return self.raw.seekable()
798
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000799 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000800 def raw(self):
801 return self._raw
802
803 @property
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000804 def closed(self):
805 return self.raw.closed
806
807 @property
808 def name(self):
809 return self.raw.name
810
811 @property
812 def mode(self):
813 return self.raw.mode
814
Antoine Pitrou243757e2010-11-05 21:15:39 +0000815 def __getstate__(self):
816 raise TypeError("can not serialize a '{0}' object"
817 .format(self.__class__.__name__))
818
Antoine Pitrou716c4442009-05-23 19:04:03 +0000819 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300820 modname = self.__class__.__module__
821 clsname = self.__class__.__qualname__
Antoine Pitrou716c4442009-05-23 19:04:03 +0000822 try:
823 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -0600824 except Exception:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300825 return "<{}.{}>".format(modname, clsname)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000826 else:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300827 return "<{}.{} name={!r}>".format(modname, clsname, name)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000828
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000829 ### Lower-level APIs ###
830
831 def fileno(self):
832 return self.raw.fileno()
833
834 def isatty(self):
835 return self.raw.isatty()
836
837
838class BytesIO(BufferedIOBase):
839
840 """Buffered I/O implementation using an in-memory bytes buffer."""
841
Miss Islington (bot)0f352d42019-05-27 17:05:49 -0700842 # Initialize _buffer as soon as possible since it's used by __del__()
843 # which calls close()
844 _buffer = None
845
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000846 def __init__(self, initial_bytes=None):
847 buf = bytearray()
848 if initial_bytes is not None:
849 buf += initial_bytes
850 self._buffer = buf
851 self._pos = 0
852
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000853 def __getstate__(self):
854 if self.closed:
855 raise ValueError("__getstate__ on closed file")
856 return self.__dict__.copy()
857
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000858 def getvalue(self):
859 """Return the bytes value (contents) of the buffer
860 """
861 if self.closed:
862 raise ValueError("getvalue on closed file")
863 return bytes(self._buffer)
864
Antoine Pitrou972ee132010-09-06 18:48:21 +0000865 def getbuffer(self):
866 """Return a readable and writable view of the buffer.
867 """
Serhiy Storchakac057c382015-02-03 02:00:18 +0200868 if self.closed:
869 raise ValueError("getbuffer on closed file")
Antoine Pitrou972ee132010-09-06 18:48:21 +0000870 return memoryview(self._buffer)
871
Serhiy Storchakac057c382015-02-03 02:00:18 +0200872 def close(self):
Miss Islington (bot)0f352d42019-05-27 17:05:49 -0700873 if self._buffer is not None:
874 self._buffer.clear()
Serhiy Storchakac057c382015-02-03 02:00:18 +0200875 super().close()
876
Martin Panterccb2c0e2016-10-20 23:48:14 +0000877 def read(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000878 if self.closed:
879 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300880 if size is None:
881 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300882 else:
883 try:
884 size_index = size.__index__
885 except AttributeError:
886 raise TypeError(f"{size!r} is not an integer")
887 else:
888 size = size_index()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300889 if size < 0:
890 size = len(self._buffer)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000891 if len(self._buffer) <= self._pos:
892 return b""
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300893 newpos = min(len(self._buffer), self._pos + size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000894 b = self._buffer[self._pos : newpos]
895 self._pos = newpos
896 return bytes(b)
897
Martin Panterccb2c0e2016-10-20 23:48:14 +0000898 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000899 """This is the same as read.
900 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300901 return self.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000902
903 def write(self, b):
904 if self.closed:
905 raise ValueError("write to closed file")
906 if isinstance(b, str):
907 raise TypeError("can't write str to binary stream")
Martin Panter6bb91f32016-05-28 00:41:57 +0000908 with memoryview(b) as view:
909 n = view.nbytes # Size of any bytes-like object
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000910 if n == 0:
911 return 0
912 pos = self._pos
913 if pos > len(self._buffer):
914 # Inserts null bytes between the current end of the file
915 # and the new write position.
916 padding = b'\x00' * (pos - len(self._buffer))
917 self._buffer += padding
918 self._buffer[pos:pos + n] = b
919 self._pos += n
920 return n
921
922 def seek(self, pos, whence=0):
923 if self.closed:
924 raise ValueError("seek on closed file")
925 try:
Oren Milmande503602017-08-24 21:33:42 +0300926 pos_index = pos.__index__
927 except AttributeError:
928 raise TypeError(f"{pos!r} is not an integer")
929 else:
930 pos = pos_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000931 if whence == 0:
932 if pos < 0:
933 raise ValueError("negative seek position %r" % (pos,))
934 self._pos = pos
935 elif whence == 1:
936 self._pos = max(0, self._pos + pos)
937 elif whence == 2:
938 self._pos = max(0, len(self._buffer) + pos)
939 else:
Jesus Cea94363612012-06-22 18:32:07 +0200940 raise ValueError("unsupported whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000941 return self._pos
942
943 def tell(self):
944 if self.closed:
945 raise ValueError("tell on closed file")
946 return self._pos
947
948 def truncate(self, pos=None):
949 if self.closed:
950 raise ValueError("truncate on closed file")
951 if pos is None:
952 pos = self._pos
Florent Xiclunab14930c2010-03-13 15:26:44 +0000953 else:
954 try:
Oren Milmande503602017-08-24 21:33:42 +0300955 pos_index = pos.__index__
956 except AttributeError:
957 raise TypeError(f"{pos!r} is not an integer")
958 else:
959 pos = pos_index()
Florent Xiclunab14930c2010-03-13 15:26:44 +0000960 if pos < 0:
961 raise ValueError("negative truncate position %r" % (pos,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000962 del self._buffer[pos:]
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000963 return pos
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000964
965 def readable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200966 if self.closed:
967 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000968 return True
969
970 def writable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200971 if self.closed:
972 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000973 return True
974
975 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200976 if self.closed:
977 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000978 return True
979
980
981class BufferedReader(_BufferedIOMixin):
982
983 """BufferedReader(raw[, buffer_size])
984
985 A buffer for a readable, sequential BaseRawIO object.
986
987 The constructor creates a BufferedReader for the given readable raw
988 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
989 is used.
990 """
991
992 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
993 """Create a new buffered reader using the given readable raw IO object.
994 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000995 if not raw.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200996 raise OSError('"raw" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000997
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000998 _BufferedIOMixin.__init__(self, raw)
999 if buffer_size <= 0:
1000 raise ValueError("invalid buffer size")
1001 self.buffer_size = buffer_size
1002 self._reset_read_buf()
1003 self._read_lock = Lock()
1004
Martin Panter754aab22016-03-31 07:21:56 +00001005 def readable(self):
1006 return self.raw.readable()
1007
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001008 def _reset_read_buf(self):
1009 self._read_buf = b""
1010 self._read_pos = 0
1011
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001012 def read(self, size=None):
1013 """Read size bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001014
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001015 Returns exactly size bytes of data unless the underlying raw IO
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001016 stream reaches EOF or if the call would block in non-blocking
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001017 mode. If size is negative, read until EOF or until read() would
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001018 block.
1019 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001020 if size is not None and size < -1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001021 raise ValueError("invalid number of bytes to read")
1022 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001023 return self._read_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001024
1025 def _read_unlocked(self, n=None):
1026 nodata_val = b""
1027 empty_values = (b"", None)
1028 buf = self._read_buf
1029 pos = self._read_pos
1030
1031 # Special case for when the number of bytes to read is unspecified.
1032 if n is None or n == -1:
1033 self._reset_read_buf()
Victor Stinnerb57f1082011-05-26 00:19:38 +02001034 if hasattr(self.raw, 'readall'):
1035 chunk = self.raw.readall()
1036 if chunk is None:
1037 return buf[pos:] or None
1038 else:
1039 return buf[pos:] + chunk
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001040 chunks = [buf[pos:]] # Strip the consumed bytes.
1041 current_size = 0
1042 while True:
1043 # Read until EOF or until read() would block.
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001044 chunk = self.raw.read()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001045 if chunk in empty_values:
1046 nodata_val = chunk
1047 break
1048 current_size += len(chunk)
1049 chunks.append(chunk)
1050 return b"".join(chunks) or nodata_val
1051
1052 # The number of bytes to read is specified, return at most n bytes.
1053 avail = len(buf) - pos # Length of the available buffered data.
1054 if n <= avail:
1055 # Fast path: the data to read is fully buffered.
1056 self._read_pos += n
1057 return buf[pos:pos+n]
1058 # Slow path: read from the stream until enough bytes are read,
1059 # or until an EOF occurs or until read() would block.
1060 chunks = [buf[pos:]]
1061 wanted = max(self.buffer_size, n)
1062 while avail < n:
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001063 chunk = self.raw.read(wanted)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001064 if chunk in empty_values:
1065 nodata_val = chunk
1066 break
1067 avail += len(chunk)
1068 chunks.append(chunk)
Martin Pantere26da7c2016-06-02 10:07:09 +00001069 # n is more than avail only when an EOF occurred or when
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001070 # read() would have blocked.
1071 n = min(n, avail)
1072 out = b"".join(chunks)
1073 self._read_buf = out[n:] # Save the extra data in the buffer.
1074 self._read_pos = 0
1075 return out[:n] if out else nodata_val
1076
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001077 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001078 """Returns buffered bytes without advancing the position.
1079
1080 The argument indicates a desired minimal number of bytes; we
1081 do at most one raw read to satisfy it. We never return more
1082 than self.buffer_size.
1083 """
1084 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001085 return self._peek_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001086
1087 def _peek_unlocked(self, n=0):
1088 want = min(n, self.buffer_size)
1089 have = len(self._read_buf) - self._read_pos
1090 if have < want or have <= 0:
1091 to_read = self.buffer_size - have
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001092 current = self.raw.read(to_read)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001093 if current:
1094 self._read_buf = self._read_buf[self._read_pos:] + current
1095 self._read_pos = 0
1096 return self._read_buf[self._read_pos:]
1097
Martin Panterccb2c0e2016-10-20 23:48:14 +00001098 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001099 """Reads up to size bytes, with at most one read() system call."""
1100 # Returns up to size bytes. If at least one byte is buffered, we
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001101 # only return buffered bytes. Otherwise, we do one raw read.
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001102 if size < 0:
Martin Panterccb2c0e2016-10-20 23:48:14 +00001103 size = self.buffer_size
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001104 if size == 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001105 return b""
1106 with self._read_lock:
1107 self._peek_unlocked(1)
1108 return self._read_unlocked(
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001109 min(size, len(self._read_buf) - self._read_pos))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001110
Benjamin Petersona96fea02014-06-22 14:17:44 -07001111 # Implementing readinto() and readinto1() is not strictly necessary (we
1112 # could rely on the base class that provides an implementation in terms of
1113 # read() and read1()). We do it anyway to keep the _pyio implementation
1114 # similar to the io implementation (which implements the methods for
1115 # performance reasons).
1116 def _readinto(self, buf, read1):
1117 """Read data into *buf* with at most one system call."""
1118
Benjamin Petersona96fea02014-06-22 14:17:44 -07001119 # Need to create a memoryview object of type 'b', otherwise
1120 # we may not be able to assign bytes to it, and slicing it
1121 # would create a new object.
1122 if not isinstance(buf, memoryview):
1123 buf = memoryview(buf)
Martin Panter6bb91f32016-05-28 00:41:57 +00001124 if buf.nbytes == 0:
1125 return 0
Benjamin Petersona96fea02014-06-22 14:17:44 -07001126 buf = buf.cast('B')
1127
1128 written = 0
1129 with self._read_lock:
1130 while written < len(buf):
1131
1132 # First try to read from internal buffer
1133 avail = min(len(self._read_buf) - self._read_pos, len(buf))
1134 if avail:
1135 buf[written:written+avail] = \
1136 self._read_buf[self._read_pos:self._read_pos+avail]
1137 self._read_pos += avail
1138 written += avail
1139 if written == len(buf):
1140 break
1141
1142 # If remaining space in callers buffer is larger than
1143 # internal buffer, read directly into callers buffer
1144 if len(buf) - written > self.buffer_size:
1145 n = self.raw.readinto(buf[written:])
1146 if not n:
1147 break # eof
1148 written += n
1149
1150 # Otherwise refill internal buffer - unless we're
1151 # in read1 mode and already got some data
1152 elif not (read1 and written):
1153 if not self._peek_unlocked(1):
1154 break # eof
1155
1156 # In readinto1 mode, return as soon as we have some data
1157 if read1 and written:
1158 break
1159
1160 return written
1161
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001162 def tell(self):
1163 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
1164
1165 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001166 if whence not in valid_seek_flags:
Jesus Cea990eff02012-04-26 17:05:31 +02001167 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001168 with self._read_lock:
1169 if whence == 1:
1170 pos -= len(self._read_buf) - self._read_pos
1171 pos = _BufferedIOMixin.seek(self, pos, whence)
1172 self._reset_read_buf()
1173 return pos
1174
1175class BufferedWriter(_BufferedIOMixin):
1176
1177 """A buffer for a writeable sequential RawIO object.
1178
1179 The constructor creates a BufferedWriter for the given writeable raw
1180 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001181 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001182 """
1183
Florent Xicluna109d5732012-07-07 17:03:22 +02001184 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001185 if not raw.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001186 raise OSError('"raw" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001187
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001188 _BufferedIOMixin.__init__(self, raw)
1189 if buffer_size <= 0:
1190 raise ValueError("invalid buffer size")
1191 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001192 self._write_buf = bytearray()
1193 self._write_lock = Lock()
1194
Martin Panter754aab22016-03-31 07:21:56 +00001195 def writable(self):
1196 return self.raw.writable()
1197
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001198 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001199 if isinstance(b, str):
1200 raise TypeError("can't write str to binary stream")
1201 with self._write_lock:
benfogle9703f092017-11-10 16:03:40 -05001202 if self.closed:
1203 raise ValueError("write to closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001204 # XXX we can implement some more tricks to try and avoid
1205 # partial writes
1206 if len(self._write_buf) > self.buffer_size:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001207 # We're full, so let's pre-flush the buffer. (This may
1208 # raise BlockingIOError with characters_written == 0.)
1209 self._flush_unlocked()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001210 before = len(self._write_buf)
1211 self._write_buf.extend(b)
1212 written = len(self._write_buf) - before
1213 if len(self._write_buf) > self.buffer_size:
1214 try:
1215 self._flush_unlocked()
1216 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001217 if len(self._write_buf) > self.buffer_size:
1218 # We've hit the buffer_size. We have to accept a partial
1219 # write and cut back our buffer.
1220 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001221 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001222 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001223 raise BlockingIOError(e.errno, e.strerror, written)
1224 return written
1225
1226 def truncate(self, pos=None):
1227 with self._write_lock:
1228 self._flush_unlocked()
1229 if pos is None:
1230 pos = self.raw.tell()
1231 return self.raw.truncate(pos)
1232
1233 def flush(self):
1234 with self._write_lock:
1235 self._flush_unlocked()
1236
1237 def _flush_unlocked(self):
1238 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +03001239 raise ValueError("flush on closed file")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001240 while self._write_buf:
1241 try:
1242 n = self.raw.write(self._write_buf)
1243 except BlockingIOError:
1244 raise RuntimeError("self.raw should implement RawIOBase: it "
1245 "should not raise BlockingIOError")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001246 if n is None:
1247 raise BlockingIOError(
1248 errno.EAGAIN,
1249 "write could not complete without blocking", 0)
1250 if n > len(self._write_buf) or n < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001251 raise OSError("write() returned incorrect number of bytes")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001252 del self._write_buf[:n]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001253
1254 def tell(self):
1255 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1256
1257 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001258 if whence not in valid_seek_flags:
1259 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001260 with self._write_lock:
1261 self._flush_unlocked()
1262 return _BufferedIOMixin.seek(self, pos, whence)
1263
benfogle9703f092017-11-10 16:03:40 -05001264 def close(self):
1265 with self._write_lock:
1266 if self.raw is None or self.closed:
1267 return
1268 # We have to release the lock and call self.flush() (which will
1269 # probably just re-take the lock) in case flush has been overridden in
1270 # a subclass or the user set self.flush to something. This is the same
1271 # behavior as the C implementation.
1272 try:
1273 # may raise BlockingIOError or BrokenPipeError etc
1274 self.flush()
1275 finally:
1276 with self._write_lock:
1277 self.raw.close()
1278
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001279
1280class BufferedRWPair(BufferedIOBase):
1281
1282 """A buffered reader and writer object together.
1283
1284 A buffered reader object and buffered writer object put together to
1285 form a sequential IO object that can read and write. This is typically
1286 used with a socket or two-way pipe.
1287
1288 reader and writer are RawIOBase objects that are readable and
1289 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001290 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001291 """
1292
1293 # XXX The usefulness of this (compared to having two separate IO
1294 # objects) is questionable.
1295
Florent Xicluna109d5732012-07-07 17:03:22 +02001296 def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001297 """Constructor.
1298
1299 The arguments are two RawIO instances.
1300 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001301 if not reader.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001302 raise OSError('"reader" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001303
1304 if not writer.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001305 raise OSError('"writer" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001306
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001307 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001308 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001309
Martin Panterccb2c0e2016-10-20 23:48:14 +00001310 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001311 if size is None:
1312 size = -1
1313 return self.reader.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001314
1315 def readinto(self, b):
1316 return self.reader.readinto(b)
1317
1318 def write(self, b):
1319 return self.writer.write(b)
1320
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001321 def peek(self, size=0):
1322 return self.reader.peek(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001323
Martin Panterccb2c0e2016-10-20 23:48:14 +00001324 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001325 return self.reader.read1(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001326
Benjamin Petersona96fea02014-06-22 14:17:44 -07001327 def readinto1(self, b):
1328 return self.reader.readinto1(b)
1329
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001330 def readable(self):
1331 return self.reader.readable()
1332
1333 def writable(self):
1334 return self.writer.writable()
1335
1336 def flush(self):
1337 return self.writer.flush()
1338
1339 def close(self):
Serhiy Storchaka7665be62015-03-24 23:21:57 +02001340 try:
1341 self.writer.close()
1342 finally:
1343 self.reader.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001344
1345 def isatty(self):
1346 return self.reader.isatty() or self.writer.isatty()
1347
1348 @property
1349 def closed(self):
1350 return self.writer.closed
1351
1352
1353class BufferedRandom(BufferedWriter, BufferedReader):
1354
1355 """A buffered interface to random access streams.
1356
1357 The constructor creates a reader and writer for a seekable stream,
1358 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001359 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001360 """
1361
Florent Xicluna109d5732012-07-07 17:03:22 +02001362 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001363 raw._checkSeekable()
1364 BufferedReader.__init__(self, raw, buffer_size)
Florent Xicluna109d5732012-07-07 17:03:22 +02001365 BufferedWriter.__init__(self, raw, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001366
1367 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001368 if whence not in valid_seek_flags:
1369 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001370 self.flush()
1371 if self._read_buf:
1372 # Undo read ahead.
1373 with self._read_lock:
1374 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1375 # First do the raw seek, then empty the read buffer, so that
1376 # if the raw seek fails, we don't lose buffered data forever.
1377 pos = self.raw.seek(pos, whence)
1378 with self._read_lock:
1379 self._reset_read_buf()
1380 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001381 raise OSError("seek() returned invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001382 return pos
1383
1384 def tell(self):
1385 if self._write_buf:
1386 return BufferedWriter.tell(self)
1387 else:
1388 return BufferedReader.tell(self)
1389
1390 def truncate(self, pos=None):
1391 if pos is None:
1392 pos = self.tell()
1393 # Use seek to flush the read buffer.
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001394 return BufferedWriter.truncate(self, pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001395
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001396 def read(self, size=None):
1397 if size is None:
1398 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001399 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001400 return BufferedReader.read(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001401
1402 def readinto(self, b):
1403 self.flush()
1404 return BufferedReader.readinto(self, b)
1405
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001406 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001407 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001408 return BufferedReader.peek(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001409
Martin Panterccb2c0e2016-10-20 23:48:14 +00001410 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001411 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001412 return BufferedReader.read1(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001413
Benjamin Petersona96fea02014-06-22 14:17:44 -07001414 def readinto1(self, b):
1415 self.flush()
1416 return BufferedReader.readinto1(self, b)
1417
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001418 def write(self, b):
1419 if self._read_buf:
1420 # Undo readahead
1421 with self._read_lock:
1422 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1423 self._reset_read_buf()
1424 return BufferedWriter.write(self, b)
1425
1426
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001427class FileIO(RawIOBase):
1428 _fd = -1
1429 _created = False
1430 _readable = False
1431 _writable = False
1432 _appending = False
1433 _seekable = None
1434 _closefd = True
1435
1436 def __init__(self, file, mode='r', closefd=True, opener=None):
1437 """Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
1438 writing, exclusive creation or appending. The file will be created if it
1439 doesn't exist when opened for writing or appending; it will be truncated
1440 when opened for writing. A FileExistsError will be raised if it already
1441 exists when opened for creating. Opening a file for creating implies
1442 writing so this mode behaves in a similar way to 'w'. Add a '+' to the mode
1443 to allow simultaneous reading and writing. A custom opener can be used by
1444 passing a callable as *opener*. The underlying file descriptor for the file
1445 object is then obtained by calling opener with (*name*, *flags*).
1446 *opener* must return an open file descriptor (passing os.open as *opener*
1447 results in functionality similar to passing None).
1448 """
1449 if self._fd >= 0:
1450 # Have to close the existing file first.
1451 try:
1452 if self._closefd:
1453 os.close(self._fd)
1454 finally:
1455 self._fd = -1
1456
1457 if isinstance(file, float):
1458 raise TypeError('integer argument expected, got float')
1459 if isinstance(file, int):
1460 fd = file
1461 if fd < 0:
1462 raise ValueError('negative file descriptor')
1463 else:
1464 fd = -1
1465
1466 if not isinstance(mode, str):
1467 raise TypeError('invalid mode: %s' % (mode,))
1468 if not set(mode) <= set('xrwab+'):
1469 raise ValueError('invalid mode: %s' % (mode,))
1470 if sum(c in 'rwax' for c in mode) != 1 or mode.count('+') > 1:
1471 raise ValueError('Must have exactly one of create/read/write/append '
1472 'mode and at most one plus')
1473
1474 if 'x' in mode:
1475 self._created = True
1476 self._writable = True
1477 flags = os.O_EXCL | os.O_CREAT
1478 elif 'r' in mode:
1479 self._readable = True
1480 flags = 0
1481 elif 'w' in mode:
1482 self._writable = True
1483 flags = os.O_CREAT | os.O_TRUNC
1484 elif 'a' in mode:
1485 self._writable = True
1486 self._appending = True
1487 flags = os.O_APPEND | os.O_CREAT
1488
1489 if '+' in mode:
1490 self._readable = True
1491 self._writable = True
1492
1493 if self._readable and self._writable:
1494 flags |= os.O_RDWR
1495 elif self._readable:
1496 flags |= os.O_RDONLY
1497 else:
1498 flags |= os.O_WRONLY
1499
1500 flags |= getattr(os, 'O_BINARY', 0)
1501
1502 noinherit_flag = (getattr(os, 'O_NOINHERIT', 0) or
1503 getattr(os, 'O_CLOEXEC', 0))
1504 flags |= noinherit_flag
1505
1506 owned_fd = None
1507 try:
1508 if fd < 0:
1509 if not closefd:
1510 raise ValueError('Cannot use closefd=False with file name')
1511 if opener is None:
1512 fd = os.open(file, flags, 0o666)
1513 else:
1514 fd = opener(file, flags)
1515 if not isinstance(fd, int):
1516 raise TypeError('expected integer from opener')
1517 if fd < 0:
1518 raise OSError('Negative file descriptor')
1519 owned_fd = fd
1520 if not noinherit_flag:
1521 os.set_inheritable(fd, False)
1522
1523 self._closefd = closefd
1524 fdfstat = os.fstat(fd)
1525 try:
1526 if stat.S_ISDIR(fdfstat.st_mode):
1527 raise IsADirectoryError(errno.EISDIR,
1528 os.strerror(errno.EISDIR), file)
1529 except AttributeError:
1530 # Ignore the AttribueError if stat.S_ISDIR or errno.EISDIR
1531 # don't exist.
1532 pass
1533 self._blksize = getattr(fdfstat, 'st_blksize', 0)
1534 if self._blksize <= 1:
1535 self._blksize = DEFAULT_BUFFER_SIZE
1536
1537 if _setmode:
1538 # don't translate newlines (\r\n <=> \n)
1539 _setmode(fd, os.O_BINARY)
1540
1541 self.name = file
1542 if self._appending:
1543 # For consistent behaviour, we explicitly seek to the
1544 # end of file (otherwise, it might be done only on the
1545 # first write()).
1546 os.lseek(fd, 0, SEEK_END)
1547 except:
1548 if owned_fd is not None:
1549 os.close(owned_fd)
1550 raise
1551 self._fd = fd
1552
1553 def __del__(self):
1554 if self._fd >= 0 and self._closefd and not self.closed:
1555 import warnings
1556 warnings.warn('unclosed file %r' % (self,), ResourceWarning,
Victor Stinnere19558a2016-03-23 00:28:08 +01001557 stacklevel=2, source=self)
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001558 self.close()
1559
1560 def __getstate__(self):
1561 raise TypeError("cannot serialize '%s' object", self.__class__.__name__)
1562
1563 def __repr__(self):
1564 class_name = '%s.%s' % (self.__class__.__module__,
1565 self.__class__.__qualname__)
1566 if self.closed:
1567 return '<%s [closed]>' % class_name
1568 try:
1569 name = self.name
1570 except AttributeError:
1571 return ('<%s fd=%d mode=%r closefd=%r>' %
1572 (class_name, self._fd, self.mode, self._closefd))
1573 else:
1574 return ('<%s name=%r mode=%r closefd=%r>' %
1575 (class_name, name, self.mode, self._closefd))
1576
1577 def _checkReadable(self):
1578 if not self._readable:
1579 raise UnsupportedOperation('File not open for reading')
1580
1581 def _checkWritable(self, msg=None):
1582 if not self._writable:
1583 raise UnsupportedOperation('File not open for writing')
1584
1585 def read(self, size=None):
1586 """Read at most size bytes, returned as bytes.
1587
1588 Only makes one system call, so less data may be returned than requested
1589 In non-blocking mode, returns None if no data is available.
1590 Return an empty bytes object at EOF.
1591 """
1592 self._checkClosed()
1593 self._checkReadable()
1594 if size is None or size < 0:
1595 return self.readall()
1596 try:
1597 return os.read(self._fd, size)
1598 except BlockingIOError:
1599 return None
1600
1601 def readall(self):
1602 """Read all data from the file, returned as bytes.
1603
1604 In non-blocking mode, returns as much as is immediately available,
1605 or None if no data is available. Return an empty bytes object at EOF.
1606 """
1607 self._checkClosed()
1608 self._checkReadable()
1609 bufsize = DEFAULT_BUFFER_SIZE
1610 try:
1611 pos = os.lseek(self._fd, 0, SEEK_CUR)
1612 end = os.fstat(self._fd).st_size
1613 if end >= pos:
1614 bufsize = end - pos + 1
1615 except OSError:
1616 pass
1617
1618 result = bytearray()
1619 while True:
1620 if len(result) >= bufsize:
1621 bufsize = len(result)
1622 bufsize += max(bufsize, DEFAULT_BUFFER_SIZE)
1623 n = bufsize - len(result)
1624 try:
1625 chunk = os.read(self._fd, n)
1626 except BlockingIOError:
1627 if result:
1628 break
1629 return None
1630 if not chunk: # reached the end of the file
1631 break
1632 result += chunk
1633
1634 return bytes(result)
1635
1636 def readinto(self, b):
1637 """Same as RawIOBase.readinto()."""
1638 m = memoryview(b).cast('B')
1639 data = self.read(len(m))
1640 n = len(data)
1641 m[:n] = data
1642 return n
1643
1644 def write(self, b):
1645 """Write bytes b to file, return number written.
1646
1647 Only makes one system call, so not all of the data may be written.
1648 The number of bytes actually written is returned. In non-blocking mode,
1649 returns None if the write would block.
1650 """
1651 self._checkClosed()
1652 self._checkWritable()
1653 try:
1654 return os.write(self._fd, b)
1655 except BlockingIOError:
1656 return None
1657
1658 def seek(self, pos, whence=SEEK_SET):
1659 """Move to new file position.
1660
1661 Argument offset is a byte count. Optional argument whence defaults to
1662 SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
1663 are SEEK_CUR or 1 (move relative to current position, positive or negative),
1664 and SEEK_END or 2 (move relative to end of file, usually negative, although
1665 many platforms allow seeking beyond the end of a file).
1666
1667 Note that not all file objects are seekable.
1668 """
1669 if isinstance(pos, float):
1670 raise TypeError('an integer is required')
1671 self._checkClosed()
1672 return os.lseek(self._fd, pos, whence)
1673
1674 def tell(self):
1675 """tell() -> int. Current file position.
1676
1677 Can raise OSError for non seekable files."""
1678 self._checkClosed()
1679 return os.lseek(self._fd, 0, SEEK_CUR)
1680
1681 def truncate(self, size=None):
1682 """Truncate the file to at most size bytes.
1683
1684 Size defaults to the current file position, as returned by tell().
1685 The current file position is changed to the value of size.
1686 """
1687 self._checkClosed()
1688 self._checkWritable()
1689 if size is None:
1690 size = self.tell()
1691 os.ftruncate(self._fd, size)
1692 return size
1693
1694 def close(self):
1695 """Close the file.
1696
1697 A closed file cannot be used for further I/O operations. close() may be
1698 called more than once without error.
1699 """
1700 if not self.closed:
1701 try:
1702 if self._closefd:
1703 os.close(self._fd)
1704 finally:
1705 super().close()
1706
1707 def seekable(self):
1708 """True if file supports random-access."""
1709 self._checkClosed()
1710 if self._seekable is None:
1711 try:
1712 self.tell()
1713 except OSError:
1714 self._seekable = False
1715 else:
1716 self._seekable = True
1717 return self._seekable
1718
1719 def readable(self):
1720 """True if file was opened in a read mode."""
1721 self._checkClosed()
1722 return self._readable
1723
1724 def writable(self):
1725 """True if file was opened in a write mode."""
1726 self._checkClosed()
1727 return self._writable
1728
1729 def fileno(self):
1730 """Return the underlying file descriptor (an integer)."""
1731 self._checkClosed()
1732 return self._fd
1733
1734 def isatty(self):
1735 """True if the file is connected to a TTY device."""
1736 self._checkClosed()
1737 return os.isatty(self._fd)
1738
1739 @property
1740 def closefd(self):
1741 """True if the file descriptor will be closed by close()."""
1742 return self._closefd
1743
1744 @property
1745 def mode(self):
1746 """String giving the file mode"""
1747 if self._created:
1748 if self._readable:
1749 return 'xb+'
1750 else:
1751 return 'xb'
1752 elif self._appending:
1753 if self._readable:
1754 return 'ab+'
1755 else:
1756 return 'ab'
1757 elif self._readable:
1758 if self._writable:
1759 return 'rb+'
1760 else:
1761 return 'rb'
1762 else:
1763 return 'wb'
1764
1765
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001766class TextIOBase(IOBase):
1767
1768 """Base class for text I/O.
1769
1770 This class provides a character and line based interface to stream
Miss Islington (bot)0a16bb12019-04-08 21:57:31 -07001771 I/O. There is no public constructor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001772 """
1773
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001774 def read(self, size=-1):
1775 """Read at most size characters from stream, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001776
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001777 Read from underlying buffer until we have size characters or we hit EOF.
1778 If size is negative or omitted, read until EOF.
Raymond Hettingercbb80892011-01-13 18:15:51 +00001779
1780 Returns a string.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001781 """
1782 self._unsupported("read")
1783
Raymond Hettinger3c940242011-01-12 23:39:31 +00001784 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001785 """Write string s to stream and returning an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001786 self._unsupported("write")
1787
Georg Brandl4d73b572011-01-13 07:13:06 +00001788 def truncate(self, pos=None):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001789 """Truncate size to pos, where pos is an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001790 self._unsupported("truncate")
1791
Raymond Hettinger3c940242011-01-12 23:39:31 +00001792 def readline(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001793 """Read until newline or EOF.
1794
1795 Returns an empty string if EOF is hit immediately.
1796 """
1797 self._unsupported("readline")
1798
Raymond Hettinger3c940242011-01-12 23:39:31 +00001799 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001800 """
1801 Separate the underlying buffer from the TextIOBase and return it.
1802
1803 After the underlying buffer has been detached, the TextIO is in an
1804 unusable state.
1805 """
1806 self._unsupported("detach")
1807
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001808 @property
1809 def encoding(self):
1810 """Subclasses should override."""
1811 return None
1812
1813 @property
1814 def newlines(self):
1815 """Line endings translated so far.
1816
1817 Only line endings translated during reading are considered.
1818
1819 Subclasses should override.
1820 """
1821 return None
1822
Benjamin Peterson0926ad12009-06-06 18:02:12 +00001823 @property
1824 def errors(self):
1825 """Error setting of the decoder or encoder.
1826
1827 Subclasses should override."""
1828 return None
1829
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001830io.TextIOBase.register(TextIOBase)
1831
1832
1833class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1834 r"""Codec used when reading a file in universal newlines mode. It wraps
1835 another incremental decoder, translating \r\n and \r into \n. It also
1836 records the types of newlines encountered. When used with
1837 translate=False, it ensures that the newline sequence is returned in
1838 one piece.
1839 """
1840 def __init__(self, decoder, translate, errors='strict'):
1841 codecs.IncrementalDecoder.__init__(self, errors=errors)
1842 self.translate = translate
1843 self.decoder = decoder
1844 self.seennl = 0
1845 self.pendingcr = False
1846
1847 def decode(self, input, final=False):
1848 # decode input (with the eventual \r from a previous pass)
1849 if self.decoder is None:
1850 output = input
1851 else:
1852 output = self.decoder.decode(input, final=final)
1853 if self.pendingcr and (output or final):
1854 output = "\r" + output
1855 self.pendingcr = False
1856
1857 # retain last \r even when not translating data:
1858 # then readline() is sure to get \r\n in one pass
1859 if output.endswith("\r") and not final:
1860 output = output[:-1]
1861 self.pendingcr = True
1862
1863 # Record which newlines are read
1864 crlf = output.count('\r\n')
1865 cr = output.count('\r') - crlf
1866 lf = output.count('\n') - crlf
1867 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1868 | (crlf and self._CRLF)
1869
1870 if self.translate:
1871 if crlf:
1872 output = output.replace("\r\n", "\n")
1873 if cr:
1874 output = output.replace("\r", "\n")
1875
1876 return output
1877
1878 def getstate(self):
1879 if self.decoder is None:
1880 buf = b""
1881 flag = 0
1882 else:
1883 buf, flag = self.decoder.getstate()
1884 flag <<= 1
1885 if self.pendingcr:
1886 flag |= 1
1887 return buf, flag
1888
1889 def setstate(self, state):
1890 buf, flag = state
1891 self.pendingcr = bool(flag & 1)
1892 if self.decoder is not None:
1893 self.decoder.setstate((buf, flag >> 1))
1894
1895 def reset(self):
1896 self.seennl = 0
1897 self.pendingcr = False
1898 if self.decoder is not None:
1899 self.decoder.reset()
1900
1901 _LF = 1
1902 _CR = 2
1903 _CRLF = 4
1904
1905 @property
1906 def newlines(self):
1907 return (None,
1908 "\n",
1909 "\r",
1910 ("\r", "\n"),
1911 "\r\n",
1912 ("\n", "\r\n"),
1913 ("\r", "\r\n"),
1914 ("\r", "\n", "\r\n")
1915 )[self.seennl]
1916
1917
1918class TextIOWrapper(TextIOBase):
1919
1920 r"""Character and line based layer over a BufferedIOBase object, buffer.
1921
1922 encoding gives the name of the encoding that the stream will be
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001923 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001924
1925 errors determines the strictness of encoding and decoding (see the
1926 codecs.register) and defaults to "strict".
1927
1928 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1929 handling of line endings. If it is None, universal newlines is
1930 enabled. With this enabled, on input, the lines endings '\n', '\r',
1931 or '\r\n' are translated to '\n' before being returned to the
1932 caller. Conversely, on output, '\n' is translated to the system
Éric Araujo39242302011-11-03 00:08:48 +01001933 default line separator, os.linesep. If newline is any other of its
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001934 legal values, that newline becomes the newline when the file is read
1935 and it is returned untranslated. On output, '\n' is converted to the
1936 newline.
1937
1938 If line_buffering is True, a call to flush is implied when a call to
1939 write contains a newline character.
1940 """
1941
1942 _CHUNK_SIZE = 2048
1943
Miss Islington (bot)0f352d42019-05-27 17:05:49 -07001944 # Initialize _buffer as soon as possible since it's used by __del__()
1945 # which calls close()
1946 _buffer = None
1947
Andrew Svetlov4e9e9c12012-08-13 16:09:54 +03001948 # The write_through argument has no effect here since this
1949 # implementation always writes through. The argument is present only
1950 # so that the signature can match the signature of the C version.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001951 def __init__(self, buffer, encoding=None, errors=None, newline=None,
Antoine Pitroue96ec682011-07-23 21:46:35 +02001952 line_buffering=False, write_through=False):
INADA Naoki507434f2017-12-21 09:59:53 +09001953 self._check_newline(newline)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001954 if encoding is None:
1955 try:
1956 encoding = os.device_encoding(buffer.fileno())
1957 except (AttributeError, UnsupportedOperation):
1958 pass
1959 if encoding is None:
1960 try:
1961 import locale
Brett Cannoncd171c82013-07-04 17:43:24 -04001962 except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001963 # Importing locale may fail if Python is being built
1964 encoding = "ascii"
1965 else:
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001966 encoding = locale.getpreferredencoding(False)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001967
1968 if not isinstance(encoding, str):
1969 raise ValueError("invalid encoding: %r" % encoding)
1970
Nick Coghlana9b15242014-02-04 22:11:18 +10001971 if not codecs.lookup(encoding)._is_text_encoding:
1972 msg = ("%r is not a text encoding; "
1973 "use codecs.open() to handle arbitrary codecs")
1974 raise LookupError(msg % encoding)
1975
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001976 if errors is None:
1977 errors = "strict"
1978 else:
1979 if not isinstance(errors, str):
1980 raise ValueError("invalid errors: %r" % errors)
1981
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001982 self._buffer = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001983 self._decoded_chars = '' # buffer for text returned from decoder
1984 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1985 self._snapshot = None # info for reconstructing decoder state
1986 self._seekable = self._telling = self.buffer.seekable()
Antoine Pitroue96ec682011-07-23 21:46:35 +02001987 self._has_read1 = hasattr(self.buffer, 'read1')
INADA Naoki507434f2017-12-21 09:59:53 +09001988 self._configure(encoding, errors, newline,
1989 line_buffering, write_through)
1990
1991 def _check_newline(self, newline):
1992 if newline is not None and not isinstance(newline, str):
1993 raise TypeError("illegal newline type: %r" % (type(newline),))
1994 if newline not in (None, "", "\n", "\r", "\r\n"):
1995 raise ValueError("illegal newline value: %r" % (newline,))
1996
1997 def _configure(self, encoding=None, errors=None, newline=None,
1998 line_buffering=False, write_through=False):
1999 self._encoding = encoding
2000 self._errors = errors
2001 self._encoder = None
2002 self._decoder = None
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002003 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002004
INADA Naoki507434f2017-12-21 09:59:53 +09002005 self._readuniversal = not newline
2006 self._readtranslate = newline is None
2007 self._readnl = newline
2008 self._writetranslate = newline != ''
2009 self._writenl = newline or os.linesep
2010
2011 self._line_buffering = line_buffering
2012 self._write_through = write_through
2013
2014 # don't write a BOM in the middle of a file
Antoine Pitroue4501852009-05-14 18:55:55 +00002015 if self._seekable and self.writable():
2016 position = self.buffer.tell()
2017 if position != 0:
2018 try:
2019 self._get_encoder().setstate(0)
2020 except LookupError:
2021 # Sometimes the encoder doesn't exist
2022 pass
2023
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002024 # self._snapshot is either None, or a tuple (dec_flags, next_input)
2025 # where dec_flags is the second (integer) item of the decoder state
2026 # and next_input is the chunk of input bytes that comes next after the
2027 # snapshot point. We use this to reconstruct decoder states in tell().
2028
2029 # Naming convention:
2030 # - "bytes_..." for integer variables that count input bytes
2031 # - "chars_..." for integer variables that count decoded characters
2032
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002033 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +03002034 result = "<{}.{}".format(self.__class__.__module__,
2035 self.__class__.__qualname__)
Antoine Pitrou716c4442009-05-23 19:04:03 +00002036 try:
2037 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002038 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002039 pass
Antoine Pitrou716c4442009-05-23 19:04:03 +00002040 else:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002041 result += " name={0!r}".format(name)
2042 try:
2043 mode = self.mode
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002044 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002045 pass
2046 else:
2047 result += " mode={0!r}".format(mode)
2048 return result + " encoding={0!r}>".format(self.encoding)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002049
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002050 @property
2051 def encoding(self):
2052 return self._encoding
2053
2054 @property
2055 def errors(self):
2056 return self._errors
2057
2058 @property
2059 def line_buffering(self):
2060 return self._line_buffering
2061
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002062 @property
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002063 def write_through(self):
2064 return self._write_through
2065
2066 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002067 def buffer(self):
2068 return self._buffer
2069
INADA Naoki507434f2017-12-21 09:59:53 +09002070 def reconfigure(self, *,
2071 encoding=None, errors=None, newline=Ellipsis,
2072 line_buffering=None, write_through=None):
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002073 """Reconfigure the text stream with new parameters.
2074
2075 This also flushes the stream.
2076 """
INADA Naoki507434f2017-12-21 09:59:53 +09002077 if (self._decoder is not None
2078 and (encoding is not None or errors is not None
2079 or newline is not Ellipsis)):
2080 raise UnsupportedOperation(
2081 "It is not possible to set the encoding or newline of stream "
2082 "after the first read")
2083
2084 if errors is None:
2085 if encoding is None:
2086 errors = self._errors
2087 else:
2088 errors = 'strict'
2089 elif not isinstance(errors, str):
2090 raise TypeError("invalid errors: %r" % errors)
2091
2092 if encoding is None:
2093 encoding = self._encoding
2094 else:
2095 if not isinstance(encoding, str):
2096 raise TypeError("invalid encoding: %r" % encoding)
2097
2098 if newline is Ellipsis:
2099 newline = self._readnl
2100 self._check_newline(newline)
2101
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002102 if line_buffering is None:
2103 line_buffering = self.line_buffering
2104 if write_through is None:
2105 write_through = self.write_through
INADA Naoki507434f2017-12-21 09:59:53 +09002106
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002107 self.flush()
INADA Naoki507434f2017-12-21 09:59:53 +09002108 self._configure(encoding, errors, newline,
2109 line_buffering, write_through)
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002110
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002111 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02002112 if self.closed:
2113 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002114 return self._seekable
2115
2116 def readable(self):
2117 return self.buffer.readable()
2118
2119 def writable(self):
2120 return self.buffer.writable()
2121
2122 def flush(self):
2123 self.buffer.flush()
2124 self._telling = self._seekable
2125
2126 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +00002127 if self.buffer is not None and not self.closed:
Benjamin Peterson68623612012-12-20 11:53:11 -06002128 try:
2129 self.flush()
2130 finally:
2131 self.buffer.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002132
2133 @property
2134 def closed(self):
2135 return self.buffer.closed
2136
2137 @property
2138 def name(self):
2139 return self.buffer.name
2140
2141 def fileno(self):
2142 return self.buffer.fileno()
2143
2144 def isatty(self):
2145 return self.buffer.isatty()
2146
Raymond Hettinger00fa0392011-01-13 02:52:26 +00002147 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00002148 'Write data, where s is a str'
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002149 if self.closed:
2150 raise ValueError("write to closed file")
2151 if not isinstance(s, str):
2152 raise TypeError("can't write %s to text stream" %
2153 s.__class__.__name__)
2154 length = len(s)
2155 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
2156 if haslf and self._writetranslate and self._writenl != "\n":
2157 s = s.replace("\n", self._writenl)
2158 encoder = self._encoder or self._get_encoder()
2159 # XXX What if we were just reading?
2160 b = encoder.encode(s)
2161 self.buffer.write(b)
2162 if self._line_buffering and (haslf or "\r" in s):
2163 self.flush()
Miss Islington (bot)eabebbb2018-06-29 03:34:34 -07002164 self._set_decoded_chars('')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002165 self._snapshot = None
2166 if self._decoder:
2167 self._decoder.reset()
2168 return length
2169
2170 def _get_encoder(self):
2171 make_encoder = codecs.getincrementalencoder(self._encoding)
2172 self._encoder = make_encoder(self._errors)
2173 return self._encoder
2174
2175 def _get_decoder(self):
2176 make_decoder = codecs.getincrementaldecoder(self._encoding)
2177 decoder = make_decoder(self._errors)
2178 if self._readuniversal:
2179 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
2180 self._decoder = decoder
2181 return decoder
2182
2183 # The following three methods implement an ADT for _decoded_chars.
2184 # Text returned from the decoder is buffered here until the client
2185 # requests it by calling our read() or readline() method.
2186 def _set_decoded_chars(self, chars):
2187 """Set the _decoded_chars buffer."""
2188 self._decoded_chars = chars
2189 self._decoded_chars_used = 0
2190
2191 def _get_decoded_chars(self, n=None):
2192 """Advance into the _decoded_chars buffer."""
2193 offset = self._decoded_chars_used
2194 if n is None:
2195 chars = self._decoded_chars[offset:]
2196 else:
2197 chars = self._decoded_chars[offset:offset + n]
2198 self._decoded_chars_used += len(chars)
2199 return chars
2200
2201 def _rewind_decoded_chars(self, n):
2202 """Rewind the _decoded_chars buffer."""
2203 if self._decoded_chars_used < n:
2204 raise AssertionError("rewind decoded_chars out of bounds")
2205 self._decoded_chars_used -= n
2206
2207 def _read_chunk(self):
2208 """
2209 Read and decode the next chunk of data from the BufferedReader.
2210 """
2211
2212 # The return value is True unless EOF was reached. The decoded
2213 # string is placed in self._decoded_chars (replacing its previous
2214 # value). The entire input chunk is sent to the decoder, though
2215 # some of it may remain buffered in the decoder, yet to be
2216 # converted.
2217
2218 if self._decoder is None:
2219 raise ValueError("no decoder")
2220
2221 if self._telling:
2222 # To prepare for tell(), we need to snapshot a point in the
2223 # file where the decoder's input buffer is empty.
2224
2225 dec_buffer, dec_flags = self._decoder.getstate()
2226 # Given this, we know there was a valid snapshot point
2227 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
2228
2229 # Read a chunk, decode it, and put the result in self._decoded_chars.
Antoine Pitroue96ec682011-07-23 21:46:35 +02002230 if self._has_read1:
2231 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
2232 else:
2233 input_chunk = self.buffer.read(self._CHUNK_SIZE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002234 eof = not input_chunk
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002235 decoded_chars = self._decoder.decode(input_chunk, eof)
2236 self._set_decoded_chars(decoded_chars)
2237 if decoded_chars:
2238 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
2239 else:
2240 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002241
2242 if self._telling:
2243 # At the snapshot point, len(dec_buffer) bytes before the read,
2244 # the next input to be decoded is dec_buffer + input_chunk.
2245 self._snapshot = (dec_flags, dec_buffer + input_chunk)
2246
2247 return not eof
2248
2249 def _pack_cookie(self, position, dec_flags=0,
2250 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
2251 # The meaning of a tell() cookie is: seek to position, set the
2252 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
2253 # into the decoder with need_eof as the EOF flag, then skip
2254 # chars_to_skip characters of the decoded result. For most simple
2255 # decoders, tell() will often just give a byte offset in the file.
2256 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
2257 (chars_to_skip<<192) | bool(need_eof)<<256)
2258
2259 def _unpack_cookie(self, bigint):
2260 rest, position = divmod(bigint, 1<<64)
2261 rest, dec_flags = divmod(rest, 1<<64)
2262 rest, bytes_to_feed = divmod(rest, 1<<64)
2263 need_eof, chars_to_skip = divmod(rest, 1<<64)
2264 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
2265
2266 def tell(self):
2267 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002268 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002269 if not self._telling:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002270 raise OSError("telling position disabled by next() call")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002271 self.flush()
2272 position = self.buffer.tell()
2273 decoder = self._decoder
2274 if decoder is None or self._snapshot is None:
2275 if self._decoded_chars:
2276 # This should never happen.
2277 raise AssertionError("pending decoded text")
2278 return position
2279
2280 # Skip backward to the snapshot point (see _read_chunk).
2281 dec_flags, next_input = self._snapshot
2282 position -= len(next_input)
2283
2284 # How many decoded characters have been used up since the snapshot?
2285 chars_to_skip = self._decoded_chars_used
2286 if chars_to_skip == 0:
2287 # We haven't moved from the snapshot point.
2288 return self._pack_cookie(position, dec_flags)
2289
2290 # Starting from the snapshot position, we will walk the decoder
2291 # forward until it gives us enough decoded characters.
2292 saved_state = decoder.getstate()
2293 try:
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002294 # Fast search for an acceptable start point, close to our
2295 # current pos.
2296 # Rationale: calling decoder.decode() has a large overhead
2297 # regardless of chunk size; we want the number of such calls to
2298 # be O(1) in most situations (common decoders, non-crazy input).
2299 # Actually, it will be exactly 1 for fixed-size codecs (all
2300 # 8-bit codecs, also UTF-16 and UTF-32).
2301 skip_bytes = int(self._b2cratio * chars_to_skip)
2302 skip_back = 1
2303 assert skip_bytes <= len(next_input)
2304 while skip_bytes > 0:
2305 decoder.setstate((b'', dec_flags))
2306 # Decode up to temptative start point
2307 n = len(decoder.decode(next_input[:skip_bytes]))
2308 if n <= chars_to_skip:
2309 b, d = decoder.getstate()
2310 if not b:
2311 # Before pos and no bytes buffered in decoder => OK
2312 dec_flags = d
2313 chars_to_skip -= n
2314 break
2315 # Skip back by buffered amount and reset heuristic
2316 skip_bytes -= len(b)
2317 skip_back = 1
2318 else:
2319 # We're too far ahead, skip back a bit
2320 skip_bytes -= skip_back
2321 skip_back = skip_back * 2
2322 else:
2323 skip_bytes = 0
2324 decoder.setstate((b'', dec_flags))
2325
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002326 # Note our initial start point.
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002327 start_pos = position + skip_bytes
2328 start_flags = dec_flags
2329 if chars_to_skip == 0:
2330 # We haven't moved from the start point.
2331 return self._pack_cookie(start_pos, start_flags)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002332
2333 # Feed the decoder one byte at a time. As we go, note the
2334 # nearest "safe start point" before the current location
2335 # (a point where the decoder has nothing buffered, so seek()
2336 # can safely start from there and advance to this location).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002337 bytes_fed = 0
2338 need_eof = 0
2339 # Chars decoded since `start_pos`
2340 chars_decoded = 0
2341 for i in range(skip_bytes, len(next_input)):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002342 bytes_fed += 1
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002343 chars_decoded += len(decoder.decode(next_input[i:i+1]))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002344 dec_buffer, dec_flags = decoder.getstate()
2345 if not dec_buffer and chars_decoded <= chars_to_skip:
2346 # Decoder buffer is empty, so this is a safe start point.
2347 start_pos += bytes_fed
2348 chars_to_skip -= chars_decoded
2349 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
2350 if chars_decoded >= chars_to_skip:
2351 break
2352 else:
2353 # We didn't get enough decoded data; signal EOF to get more.
2354 chars_decoded += len(decoder.decode(b'', final=True))
2355 need_eof = 1
2356 if chars_decoded < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002357 raise OSError("can't reconstruct logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002358
2359 # The returned cookie corresponds to the last safe start point.
2360 return self._pack_cookie(
2361 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
2362 finally:
2363 decoder.setstate(saved_state)
2364
2365 def truncate(self, pos=None):
2366 self.flush()
2367 if pos is None:
2368 pos = self.tell()
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00002369 return self.buffer.truncate(pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002370
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002371 def detach(self):
2372 if self.buffer is None:
2373 raise ValueError("buffer is already detached")
2374 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002375 buffer = self._buffer
2376 self._buffer = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002377 return buffer
2378
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002379 def seek(self, cookie, whence=0):
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002380 def _reset_encoder(position):
2381 """Reset the encoder (merely useful for proper BOM handling)"""
2382 try:
2383 encoder = self._encoder or self._get_encoder()
2384 except LookupError:
2385 # Sometimes the encoder doesn't exist
2386 pass
2387 else:
2388 if position != 0:
2389 encoder.setstate(0)
2390 else:
2391 encoder.reset()
2392
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002393 if self.closed:
2394 raise ValueError("tell on closed file")
2395 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002396 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002397 if whence == 1: # seek relative to current position
2398 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002399 raise UnsupportedOperation("can't do nonzero cur-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002400 # Seeking to the current position should attempt to
2401 # sync the underlying buffer with the current position.
2402 whence = 0
2403 cookie = self.tell()
2404 if whence == 2: # seek relative to end of file
2405 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002406 raise UnsupportedOperation("can't do nonzero end-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002407 self.flush()
2408 position = self.buffer.seek(0, 2)
2409 self._set_decoded_chars('')
2410 self._snapshot = None
2411 if self._decoder:
2412 self._decoder.reset()
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002413 _reset_encoder(position)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002414 return position
2415 if whence != 0:
Jesus Cea94363612012-06-22 18:32:07 +02002416 raise ValueError("unsupported whence (%r)" % (whence,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002417 if cookie < 0:
2418 raise ValueError("negative seek position %r" % (cookie,))
2419 self.flush()
2420
2421 # The strategy of seek() is to go back to the safe start point
2422 # and replay the effect of read(chars_to_skip) from there.
2423 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
2424 self._unpack_cookie(cookie)
2425
2426 # Seek back to the safe start point.
2427 self.buffer.seek(start_pos)
2428 self._set_decoded_chars('')
2429 self._snapshot = None
2430
2431 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00002432 if cookie == 0 and self._decoder:
2433 self._decoder.reset()
2434 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002435 self._decoder = self._decoder or self._get_decoder()
2436 self._decoder.setstate((b'', dec_flags))
2437 self._snapshot = (dec_flags, b'')
2438
2439 if chars_to_skip:
2440 # Just like _read_chunk, feed the decoder and save a snapshot.
2441 input_chunk = self.buffer.read(bytes_to_feed)
2442 self._set_decoded_chars(
2443 self._decoder.decode(input_chunk, need_eof))
2444 self._snapshot = (dec_flags, input_chunk)
2445
2446 # Skip chars_to_skip of the decoded characters.
2447 if len(self._decoded_chars) < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002448 raise OSError("can't restore logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002449 self._decoded_chars_used = chars_to_skip
2450
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002451 _reset_encoder(cookie)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002452 return cookie
2453
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002454 def read(self, size=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00002455 self._checkReadable()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002456 if size is None:
2457 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002458 else:
2459 try:
2460 size_index = size.__index__
2461 except AttributeError:
2462 raise TypeError(f"{size!r} is not an integer")
2463 else:
2464 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002465 decoder = self._decoder or self._get_decoder()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002466 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002467 # Read everything.
2468 result = (self._get_decoded_chars() +
2469 decoder.decode(self.buffer.read(), final=True))
2470 self._set_decoded_chars('')
2471 self._snapshot = None
2472 return result
2473 else:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002474 # Keep reading chunks until we have size characters to return.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002475 eof = False
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002476 result = self._get_decoded_chars(size)
2477 while len(result) < size and not eof:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002478 eof = not self._read_chunk()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002479 result += self._get_decoded_chars(size - len(result))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002480 return result
2481
2482 def __next__(self):
2483 self._telling = False
2484 line = self.readline()
2485 if not line:
2486 self._snapshot = None
2487 self._telling = self._seekable
2488 raise StopIteration
2489 return line
2490
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002491 def readline(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002492 if self.closed:
2493 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002494 if size is None:
2495 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002496 else:
2497 try:
2498 size_index = size.__index__
2499 except AttributeError:
2500 raise TypeError(f"{size!r} is not an integer")
2501 else:
2502 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002503
2504 # Grab all the decoded text (we will rewind any extra bits later).
2505 line = self._get_decoded_chars()
2506
2507 start = 0
2508 # Make the decoder if it doesn't already exist.
2509 if not self._decoder:
2510 self._get_decoder()
2511
2512 pos = endpos = None
2513 while True:
2514 if self._readtranslate:
2515 # Newlines are already translated, only search for \n
2516 pos = line.find('\n', start)
2517 if pos >= 0:
2518 endpos = pos + 1
2519 break
2520 else:
2521 start = len(line)
2522
2523 elif self._readuniversal:
2524 # Universal newline search. Find any of \r, \r\n, \n
2525 # The decoder ensures that \r\n are not split in two pieces
2526
2527 # In C we'd look for these in parallel of course.
2528 nlpos = line.find("\n", start)
2529 crpos = line.find("\r", start)
2530 if crpos == -1:
2531 if nlpos == -1:
2532 # Nothing found
2533 start = len(line)
2534 else:
2535 # Found \n
2536 endpos = nlpos + 1
2537 break
2538 elif nlpos == -1:
2539 # Found lone \r
2540 endpos = crpos + 1
2541 break
2542 elif nlpos < crpos:
2543 # Found \n
2544 endpos = nlpos + 1
2545 break
2546 elif nlpos == crpos + 1:
2547 # Found \r\n
2548 endpos = crpos + 2
2549 break
2550 else:
2551 # Found \r
2552 endpos = crpos + 1
2553 break
2554 else:
2555 # non-universal
2556 pos = line.find(self._readnl)
2557 if pos >= 0:
2558 endpos = pos + len(self._readnl)
2559 break
2560
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002561 if size >= 0 and len(line) >= size:
2562 endpos = size # reached length size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002563 break
2564
2565 # No line ending seen yet - get more data'
2566 while self._read_chunk():
2567 if self._decoded_chars:
2568 break
2569 if self._decoded_chars:
2570 line += self._get_decoded_chars()
2571 else:
2572 # end of file
2573 self._set_decoded_chars('')
2574 self._snapshot = None
2575 return line
2576
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002577 if size >= 0 and endpos > size:
2578 endpos = size # don't exceed size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002579
2580 # Rewind _decoded_chars to just after the line ending we found.
2581 self._rewind_decoded_chars(len(line) - endpos)
2582 return line[:endpos]
2583
2584 @property
2585 def newlines(self):
2586 return self._decoder.newlines if self._decoder else None
2587
2588
2589class StringIO(TextIOWrapper):
2590 """Text I/O implementation using an in-memory buffer.
2591
2592 The initial_value argument sets the value of object. The newline
2593 argument is like the one of TextIOWrapper's constructor.
2594 """
2595
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002596 def __init__(self, initial_value="", newline="\n"):
2597 super(StringIO, self).__init__(BytesIO(),
2598 encoding="utf-8",
Serhiy Storchakac92ea762014-01-29 11:33:26 +02002599 errors="surrogatepass",
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002600 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00002601 # Issue #5645: make universal newlines semantics the same as in the
2602 # C version, even under Windows.
2603 if newline is None:
2604 self._writetranslate = False
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002605 if initial_value is not None:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002606 if not isinstance(initial_value, str):
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002607 raise TypeError("initial_value must be str or None, not {0}"
2608 .format(type(initial_value).__name__))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002609 self.write(initial_value)
2610 self.seek(0)
2611
2612 def getvalue(self):
2613 self.flush()
Antoine Pitrou57839a62014-02-02 23:37:29 +01002614 decoder = self._decoder or self._get_decoder()
2615 old_state = decoder.getstate()
2616 decoder.reset()
2617 try:
2618 return decoder.decode(self.buffer.getvalue(), final=True)
2619 finally:
2620 decoder.setstate(old_state)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002621
2622 def __repr__(self):
2623 # TextIOWrapper tells the encoding in its repr. In StringIO,
Martin Panter7462b6492015-11-02 03:37:02 +00002624 # that's an implementation detail.
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002625 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00002626
2627 @property
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002628 def errors(self):
2629 return None
2630
2631 @property
Benjamin Petersonb487e632009-03-21 03:08:31 +00002632 def encoding(self):
2633 return None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002634
2635 def detach(self):
2636 # This doesn't make sense on StringIO.
2637 self._unsupported("detach")