blob: c35516430589c06b94cdb5b80d1742a09722e653 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001"""
2Python implementation of the io module.
3"""
4
5import os
6import abc
7import codecs
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01008import errno
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03009import stat
Serhiy Storchakaf0f55a02015-08-28 22:17:04 +030010import sys
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000011# Import _thread instead of threading to reduce startup cost
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020012from _thread import allocate_lock as Lock
Serhiy Storchakaf0f55a02015-08-28 22:17:04 +030013if sys.platform in {'win32', 'cygwin'}:
Serhiy Storchaka71fd2242015-04-10 16:16:16 +030014 from msvcrt import setmode as _setmode
15else:
16 _setmode = None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000017
18import io
Benjamin Petersonc3be11a2010-04-27 21:24:03 +000019from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000020
Jesus Cea94363612012-06-22 18:32:07 +020021valid_seek_flags = {0, 1, 2} # Hardwired values
22if hasattr(os, 'SEEK_HOLE') :
23 valid_seek_flags.add(os.SEEK_HOLE)
24 valid_seek_flags.add(os.SEEK_DATA)
25
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000026# open() uses st_blksize whenever we can
27DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
28
29# NOTE: Base classes defined here are registered with the "official" ABCs
Benjamin Peterson86fdbf32015-03-18 21:35:38 -050030# defined in io.py. We don't use real inheritance though, because we don't want
31# to inherit the C implementations.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000032
Antoine Pitrou6b4883d2011-10-12 02:54:14 +020033# Rebind for compatibility
34BlockingIOError = BlockingIOError
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000035
Victor Stinnerbc2aa812019-05-23 03:45:09 +020036# Does io.IOBase finalizer log the exception if the close() method fails?
37# The exception is ignored silently by default in release build.
38_IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
Victor Stinner22eb6892019-06-26 00:51:05 +020039# Does open() check its 'errors' argument?
40_CHECK_ERRORS = _IOBASE_EMITS_UNRAISABLE
Victor Stinnerbc2aa812019-05-23 03:45:09 +020041
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000042
Georg Brandl4d73b572011-01-13 07:13:06 +000043def open(file, mode="r", buffering=-1, encoding=None, errors=None,
Ross Lagerwall59142db2011-10-31 20:34:46 +020044 newline=None, closefd=True, opener=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000045
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020046 r"""Open file and return a stream. Raise OSError upon failure.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000047
48 file is either a text or byte string giving the name (and the path
49 if the file isn't in the current working directory) of the file to
50 be opened or an integer file descriptor of the file to be
51 wrapped. (If a file descriptor is given, it is closed when the
52 returned I/O object is closed, unless closefd is set to False.)
53
Charles-François Natalidc3044c2012-01-09 22:40:02 +010054 mode is an optional string that specifies the mode in which the file is
55 opened. It defaults to 'r' which means open for reading in text mode. Other
56 common values are 'w' for writing (truncating the file if it already
Charles-François Natalid612de12012-01-14 11:51:00 +010057 exists), 'x' for exclusive creation of a new file, and 'a' for appending
Charles-François Natalidc3044c2012-01-09 22:40:02 +010058 (which on some Unix systems, means that all writes append to the end of the
59 file regardless of the current seek position). In text mode, if encoding is
60 not specified the encoding used is platform dependent. (For reading and
61 writing raw bytes use binary mode and leave encoding unspecified.) The
62 available modes are:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000063
64 ========= ===============================================================
65 Character Meaning
66 --------- ---------------------------------------------------------------
67 'r' open for reading (default)
68 'w' open for writing, truncating the file first
Charles-François Natalidc3044c2012-01-09 22:40:02 +010069 'x' create a new file and open it for writing
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000070 'a' open for writing, appending to the end of the file if it exists
71 'b' binary mode
72 't' text mode (default)
73 '+' open a disk file for updating (reading and writing)
Serhiy Storchaka6787a382013-11-23 22:12:06 +020074 'U' universal newline mode (deprecated)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000075 ========= ===============================================================
76
77 The default mode is 'rt' (open for reading text). For binary random
78 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
Charles-François Natalidc3044c2012-01-09 22:40:02 +010079 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
80 raises an `FileExistsError` if the file already exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000081
82 Python distinguishes between files opened in binary and text modes,
83 even when the underlying operating system doesn't. Files opened in
84 binary mode (appending 'b' to the mode argument) return contents as
85 bytes objects without any decoding. In text mode (the default, or when
86 't' is appended to the mode argument), the contents of the file are
87 returned as strings, the bytes having been first decoded using a
88 platform-dependent encoding or using the specified encoding if given.
89
Serhiy Storchaka6787a382013-11-23 22:12:06 +020090 'U' mode is deprecated and will raise an exception in future versions
91 of Python. It has no effect in Python 3. Use newline to control
92 universal newlines mode.
93
Antoine Pitroud5587bc2009-12-19 21:08:31 +000094 buffering is an optional integer used to set the buffering policy.
95 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
96 line buffering (only usable in text mode), and an integer > 1 to indicate
97 the size of a fixed-size chunk buffer. When no buffering argument is
98 given, the default buffering policy works as follows:
99
100 * Binary files are buffered in fixed-size chunks; the size of the buffer
101 is chosen using a heuristic trying to determine the underlying device's
102 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
103 On many systems, the buffer will typically be 4096 or 8192 bytes long.
104
105 * "Interactive" text files (files for which isatty() returns True)
106 use line buffering. Other text files use the policy described above
107 for binary files.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000108
Raymond Hettingercbb80892011-01-13 18:15:51 +0000109 encoding is the str name of the encoding used to decode or encode the
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000110 file. This should only be used in text mode. The default encoding is
111 platform dependent, but any encoding supported by Python can be
112 passed. See the codecs module for the list of supported encodings.
113
114 errors is an optional string that specifies how encoding errors are to
115 be handled---this argument should not be used in binary mode. Pass
116 'strict' to raise a ValueError exception if there is an encoding error
117 (the default of None has the same effect), or pass 'ignore' to ignore
118 errors. (Note that ignoring encoding errors can lead to data loss.)
119 See the documentation for codecs.register for a list of the permitted
120 encoding error strings.
121
Raymond Hettingercbb80892011-01-13 18:15:51 +0000122 newline is a string controlling how universal newlines works (it only
123 applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
124 as follows:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000125
126 * On input, if newline is None, universal newlines mode is
127 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
128 these are translated into '\n' before being returned to the
129 caller. If it is '', universal newline mode is enabled, but line
130 endings are returned to the caller untranslated. If it has any of
131 the other legal values, input lines are only terminated by the given
132 string, and the line ending is returned to the caller untranslated.
133
134 * On output, if newline is None, any '\n' characters written are
135 translated to the system default line separator, os.linesep. If
136 newline is '', no translation takes place. If newline is any of the
137 other legal values, any '\n' characters written are translated to
138 the given string.
139
Raymond Hettingercbb80892011-01-13 18:15:51 +0000140 closedfd is a bool. If closefd is False, the underlying file descriptor will
141 be kept open when the file is closed. This does not work when a file name is
142 given and must be True in that case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000143
Victor Stinnerdaf45552013-08-28 00:53:59 +0200144 The newly created file is non-inheritable.
145
Ross Lagerwall59142db2011-10-31 20:34:46 +0200146 A custom opener can be used by passing a callable as *opener*. The
147 underlying file descriptor for the file object is then obtained by calling
148 *opener* with (*file*, *flags*). *opener* must return an open file
149 descriptor (passing os.open as *opener* results in functionality similar to
150 passing None).
151
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000152 open() returns a file object whose type depends on the mode, and
153 through which the standard file operations such as reading and writing
154 are performed. When open() is used to open a file in a text mode ('w',
155 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
156 a file in a binary mode, the returned class varies: in read binary
157 mode, it returns a BufferedReader; in write binary and append binary
158 modes, it returns a BufferedWriter, and in read/write mode, it returns
159 a BufferedRandom.
160
161 It is also possible to use a string or bytearray as a file for both
162 reading and writing. For strings StringIO can be used like a file
163 opened in a text mode, and for bytes a BytesIO can be used like a file
164 opened in a binary mode.
165 """
Ethan Furmand62548a2016-06-04 14:38:43 -0700166 if not isinstance(file, int):
167 file = os.fspath(file)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168 if not isinstance(file, (str, bytes, int)):
169 raise TypeError("invalid file: %r" % file)
170 if not isinstance(mode, str):
171 raise TypeError("invalid mode: %r" % mode)
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000172 if not isinstance(buffering, int):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000173 raise TypeError("invalid buffering: %r" % buffering)
174 if encoding is not None and not isinstance(encoding, str):
175 raise TypeError("invalid encoding: %r" % encoding)
176 if errors is not None and not isinstance(errors, str):
177 raise TypeError("invalid errors: %r" % errors)
178 modes = set(mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100179 if modes - set("axrwb+tU") or len(mode) > len(modes):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180 raise ValueError("invalid mode: %r" % mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100181 creating = "x" in modes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000182 reading = "r" in modes
183 writing = "w" in modes
184 appending = "a" in modes
185 updating = "+" in modes
186 text = "t" in modes
187 binary = "b" in modes
188 if "U" in modes:
Robert Collinsc94a1dc2015-07-26 06:43:13 +1200189 if creating or writing or appending or updating:
190 raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200191 import warnings
192 warnings.warn("'U' mode is deprecated",
193 DeprecationWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000194 reading = True
195 if text and binary:
196 raise ValueError("can't have text and binary mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100197 if creating + reading + writing + appending > 1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000198 raise ValueError("can't have read/write/append mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100199 if not (creating or reading or writing or appending):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000200 raise ValueError("must have exactly one of read/write/append mode")
201 if binary and encoding is not None:
202 raise ValueError("binary mode doesn't take an encoding argument")
203 if binary and errors is not None:
204 raise ValueError("binary mode doesn't take an errors argument")
205 if binary and newline is not None:
206 raise ValueError("binary mode doesn't take a newline argument")
Alexey Izbysheva2670562018-10-20 03:22:31 +0300207 if binary and buffering == 1:
208 import warnings
209 warnings.warn("line buffering (buffering=1) isn't supported in binary "
210 "mode, the default buffer size will be used",
211 RuntimeWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000212 raw = FileIO(file,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100213 (creating and "x" or "") +
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000214 (reading and "r" or "") +
215 (writing and "w" or "") +
216 (appending and "a" or "") +
217 (updating and "+" or ""),
Ross Lagerwall59142db2011-10-31 20:34:46 +0200218 closefd, opener=opener)
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300219 result = raw
220 try:
221 line_buffering = False
222 if buffering == 1 or buffering < 0 and raw.isatty():
223 buffering = -1
224 line_buffering = True
225 if buffering < 0:
226 buffering = DEFAULT_BUFFER_SIZE
227 try:
228 bs = os.fstat(raw.fileno()).st_blksize
229 except (OSError, AttributeError):
230 pass
231 else:
232 if bs > 1:
233 buffering = bs
234 if buffering < 0:
235 raise ValueError("invalid buffering size")
236 if buffering == 0:
237 if binary:
238 return result
239 raise ValueError("can't have unbuffered text I/O")
240 if updating:
241 buffer = BufferedRandom(raw, buffering)
242 elif creating or writing or appending:
243 buffer = BufferedWriter(raw, buffering)
244 elif reading:
245 buffer = BufferedReader(raw, buffering)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000246 else:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300247 raise ValueError("unknown mode: %r" % mode)
248 result = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000249 if binary:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300250 return result
251 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
252 result = text
253 text.mode = mode
254 return result
255 except:
256 result.close()
257 raise
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000258
Steve Dowerb82e17e2019-05-23 08:45:22 -0700259# Define a default pure-Python implementation for open_code()
260# that does not allow hooks. Warn on first use. Defined for tests.
261def _open_code_with_warning(path):
262 """Opens the provided file with mode ``'rb'``. This function
263 should be used when the intent is to treat the contents as
264 executable code.
265
266 ``path`` should be an absolute path.
267
268 When supported by the runtime, this function can be hooked
269 in order to allow embedders more control over code files.
270 This functionality is not supported on the current runtime.
271 """
272 import warnings
273 warnings.warn("_pyio.open_code() may not be using hooks",
274 RuntimeWarning, 2)
275 return open(path, "rb")
276
277try:
278 open_code = io.open_code
279except AttributeError:
280 open_code = _open_code_with_warning
281
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000282
283class DocDescriptor:
284 """Helper for builtins.open.__doc__
285 """
286 def __get__(self, obj, typ):
287 return (
Benjamin Petersonc3be11a2010-04-27 21:24:03 +0000288 "open(file, mode='r', buffering=-1, encoding=None, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000289 "errors=None, newline=None, closefd=True)\n\n" +
290 open.__doc__)
291
292class OpenWrapper:
293 """Wrapper for builtins.open
294
295 Trick so that open won't become a bound method when stored
296 as a class variable (as dbm.dumb does).
297
Nick Coghland6009512014-11-20 21:39:37 +1000298 See initstdio() in Python/pylifecycle.c.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000299 """
300 __doc__ = DocDescriptor()
301
302 def __new__(cls, *args, **kwargs):
303 return open(*args, **kwargs)
304
305
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000306# In normal operation, both `UnsupportedOperation`s should be bound to the
307# same object.
308try:
309 UnsupportedOperation = io.UnsupportedOperation
310except AttributeError:
Serhiy Storchaka606ab862016-12-07 13:31:20 +0200311 class UnsupportedOperation(OSError, ValueError):
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000312 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000313
314
315class IOBase(metaclass=abc.ABCMeta):
316
317 """The abstract base class for all I/O classes, acting on streams of
318 bytes. There is no public constructor.
319
320 This class provides dummy implementations for many methods that
321 derived classes can override selectively; the default implementations
322 represent a file that cannot be read, written or seeked.
323
Steve Palmer7b97ab32019-04-09 05:35:27 +0100324 Even though IOBase does not declare read or write because
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000325 their signatures will vary, implementations and clients should
326 consider those methods part of the interface. Also, implementations
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000327 may raise UnsupportedOperation when operations they do not support are
328 called.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000329
330 The basic type used for binary data read from or written to a file is
Steve Palmer7b97ab32019-04-09 05:35:27 +0100331 bytes. Other bytes-like objects are accepted as method arguments too.
332 Text I/O classes work with str data.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000333
334 Note that calling any method (even inquiries) on a closed stream is
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200335 undefined. Implementations may raise OSError in this case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000336
337 IOBase (and its subclasses) support the iterator protocol, meaning
338 that an IOBase object can be iterated over yielding the lines in a
339 stream.
340
341 IOBase also supports the :keyword:`with` statement. In this example,
342 fp is closed after the suite of the with statement is complete:
343
344 with open('spam.txt', 'r') as fp:
345 fp.write('Spam and eggs!')
346 """
347
348 ### Internal ###
349
Raymond Hettinger3c940242011-01-12 23:39:31 +0000350 def _unsupported(self, name):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200351 """Internal: raise an OSError exception for unsupported operations."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000352 raise UnsupportedOperation("%s.%s() not supported" %
353 (self.__class__.__name__, name))
354
355 ### Positioning ###
356
Georg Brandl4d73b572011-01-13 07:13:06 +0000357 def seek(self, pos, whence=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000358 """Change stream position.
359
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400360 Change the stream position to byte offset pos. Argument pos is
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000361 interpreted relative to the position indicated by whence. Values
Raymond Hettingercbb80892011-01-13 18:15:51 +0000362 for whence are ints:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000363
364 * 0 -- start of stream (the default); offset should be zero or positive
365 * 1 -- current stream position; offset may be negative
366 * 2 -- end of stream; offset is usually negative
Jesus Cea94363612012-06-22 18:32:07 +0200367 Some operating systems / file systems could provide additional values.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000368
Raymond Hettingercbb80892011-01-13 18:15:51 +0000369 Return an int indicating the new absolute position.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000370 """
371 self._unsupported("seek")
372
Raymond Hettinger3c940242011-01-12 23:39:31 +0000373 def tell(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000374 """Return an int indicating the current stream position."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000375 return self.seek(0, 1)
376
Georg Brandl4d73b572011-01-13 07:13:06 +0000377 def truncate(self, pos=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000378 """Truncate file to size bytes.
379
380 Size defaults to the current IO position as reported by tell(). Return
381 the new size.
382 """
383 self._unsupported("truncate")
384
385 ### Flush and close ###
386
Raymond Hettinger3c940242011-01-12 23:39:31 +0000387 def flush(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000388 """Flush write buffers, if applicable.
389
390 This is not implemented for read-only and non-blocking streams.
391 """
Antoine Pitrou6be88762010-05-03 16:48:20 +0000392 self._checkClosed()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000393 # XXX Should this return the number of bytes written???
394
395 __closed = False
396
Raymond Hettinger3c940242011-01-12 23:39:31 +0000397 def close(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000398 """Flush and close the IO object.
399
400 This method has no effect if the file is already closed.
401 """
402 if not self.__closed:
Benjamin Peterson68623612012-12-20 11:53:11 -0600403 try:
404 self.flush()
405 finally:
406 self.__closed = True
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000407
Raymond Hettinger3c940242011-01-12 23:39:31 +0000408 def __del__(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000409 """Destructor. Calls close()."""
Victor Stinner4f6f7c52019-06-11 02:49:06 +0200410 try:
411 closed = self.closed
412 except Exception:
413 # If getting closed fails, then the object is probably
414 # in an unusable state, so ignore.
415 return
416
417 if closed:
418 return
419
Victor Stinnerbc2aa812019-05-23 03:45:09 +0200420 if _IOBASE_EMITS_UNRAISABLE:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000421 self.close()
Victor Stinnerbc2aa812019-05-23 03:45:09 +0200422 else:
423 # The try/except block is in case this is called at program
424 # exit time, when it's possible that globals have already been
425 # deleted, and then the close() call might fail. Since
426 # there's nothing we can do about such failures and they annoy
427 # the end users, we suppress the traceback.
428 try:
429 self.close()
430 except:
431 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000432
433 ### Inquiries ###
434
Raymond Hettinger3c940242011-01-12 23:39:31 +0000435 def seekable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000436 """Return a bool indicating whether object supports random access.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000437
Martin Panter754aab22016-03-31 07:21:56 +0000438 If False, seek(), tell() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000439 This method may need to do a test seek().
440 """
441 return False
442
443 def _checkSeekable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000444 """Internal: raise UnsupportedOperation if file is not seekable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000445 """
446 if not self.seekable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000447 raise UnsupportedOperation("File or stream is not seekable."
448 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000449
Raymond Hettinger3c940242011-01-12 23:39:31 +0000450 def readable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000451 """Return a bool indicating whether object was opened for reading.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000452
Martin Panter754aab22016-03-31 07:21:56 +0000453 If False, read() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000454 """
455 return False
456
457 def _checkReadable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000458 """Internal: raise UnsupportedOperation if file is not readable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000459 """
460 if not self.readable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000461 raise UnsupportedOperation("File or stream is not readable."
462 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000463
Raymond Hettinger3c940242011-01-12 23:39:31 +0000464 def writable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000465 """Return a bool indicating whether object was opened for writing.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000466
Martin Panter754aab22016-03-31 07:21:56 +0000467 If False, write() and truncate() will raise OSError.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000468 """
469 return False
470
471 def _checkWritable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000472 """Internal: raise UnsupportedOperation if file is not writable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000473 """
474 if not self.writable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000475 raise UnsupportedOperation("File or stream is not writable."
476 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477
478 @property
479 def closed(self):
480 """closed: bool. True iff the file has been closed.
481
482 For backwards compatibility, this is a property, not a predicate.
483 """
484 return self.__closed
485
486 def _checkClosed(self, msg=None):
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300487 """Internal: raise a ValueError if file is closed
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000488 """
489 if self.closed:
490 raise ValueError("I/O operation on closed file."
491 if msg is None else msg)
492
493 ### Context manager ###
494
Raymond Hettinger3c940242011-01-12 23:39:31 +0000495 def __enter__(self): # That's a forward reference
Raymond Hettingercbb80892011-01-13 18:15:51 +0000496 """Context management protocol. Returns self (an instance of IOBase)."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000497 self._checkClosed()
498 return self
499
Raymond Hettinger3c940242011-01-12 23:39:31 +0000500 def __exit__(self, *args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000501 """Context management protocol. Calls close()"""
502 self.close()
503
504 ### Lower-level APIs ###
505
506 # XXX Should these be present even if unimplemented?
507
Raymond Hettinger3c940242011-01-12 23:39:31 +0000508 def fileno(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000509 """Returns underlying file descriptor (an int) if one exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200511 An OSError is raised if the IO object does not use a file descriptor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000512 """
513 self._unsupported("fileno")
514
Raymond Hettinger3c940242011-01-12 23:39:31 +0000515 def isatty(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000516 """Return a bool indicating whether this is an 'interactive' stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000517
518 Return False if it can't be determined.
519 """
520 self._checkClosed()
521 return False
522
523 ### Readline[s] and writelines ###
524
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300525 def readline(self, size=-1):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000526 r"""Read and return a line of bytes from the stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000527
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300528 If size is specified, at most size bytes will be read.
529 Size should be an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000530
531 The line terminator is always b'\n' for binary files; for text
532 files, the newlines argument to open can be used to select the line
533 terminator(s) recognized.
534 """
535 # For backwards compatibility, a (slowish) readline().
536 if hasattr(self, "peek"):
537 def nreadahead():
538 readahead = self.peek(1)
539 if not readahead:
540 return 1
541 n = (readahead.find(b"\n") + 1) or len(readahead)
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300542 if size >= 0:
543 n = min(n, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000544 return n
545 else:
546 def nreadahead():
547 return 1
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300548 if size is None:
549 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300550 else:
551 try:
552 size_index = size.__index__
553 except AttributeError:
554 raise TypeError(f"{size!r} is not an integer")
555 else:
556 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000557 res = bytearray()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300558 while size < 0 or len(res) < size:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000559 b = self.read(nreadahead())
560 if not b:
561 break
562 res += b
563 if res.endswith(b"\n"):
564 break
565 return bytes(res)
566
567 def __iter__(self):
568 self._checkClosed()
569 return self
570
571 def __next__(self):
572 line = self.readline()
573 if not line:
574 raise StopIteration
575 return line
576
577 def readlines(self, hint=None):
578 """Return a list of lines from the stream.
579
580 hint can be specified to control the number of lines read: no more
581 lines will be read if the total size (in bytes/characters) of all
582 lines so far exceeds hint.
583 """
584 if hint is None or hint <= 0:
585 return list(self)
586 n = 0
587 lines = []
588 for line in self:
589 lines.append(line)
590 n += len(line)
591 if n >= hint:
592 break
593 return lines
594
595 def writelines(self, lines):
Marcin Niemiraab865212019-04-22 21:13:51 +1000596 """Write a list of lines to the stream.
597
598 Line separators are not added, so it is usual for each of the lines
599 provided to have a line separator at the end.
600 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000601 self._checkClosed()
602 for line in lines:
603 self.write(line)
604
605io.IOBase.register(IOBase)
606
607
608class RawIOBase(IOBase):
609
610 """Base class for raw binary I/O."""
611
612 # The read() method is implemented by calling readinto(); derived
613 # classes that want to support read() only need to implement
614 # readinto() as a primitive operation. In general, readinto() can be
615 # more efficient than read().
616
617 # (It would be tempting to also provide an implementation of
618 # readinto() in terms of read(), in case the latter is a more suitable
619 # primitive operation, but that would lead to nasty recursion in case
620 # a subclass doesn't implement either.)
621
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300622 def read(self, size=-1):
623 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000624
625 Returns an empty bytes object on EOF, or None if the object is
626 set not to block and has no data to read.
627 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300628 if size is None:
629 size = -1
630 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000631 return self.readall()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300632 b = bytearray(size.__index__())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000633 n = self.readinto(b)
Antoine Pitrou328ec742010-09-14 18:37:24 +0000634 if n is None:
635 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000636 del b[n:]
637 return bytes(b)
638
639 def readall(self):
640 """Read until EOF, using multiple read() call."""
641 res = bytearray()
642 while True:
643 data = self.read(DEFAULT_BUFFER_SIZE)
644 if not data:
645 break
646 res += data
Victor Stinnera80987f2011-05-25 22:47:16 +0200647 if res:
648 return bytes(res)
649 else:
650 # b'' or None
651 return data
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000652
Raymond Hettinger3c940242011-01-12 23:39:31 +0000653 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000654 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000655
Raymond Hettingercbb80892011-01-13 18:15:51 +0000656 Returns an int representing the number of bytes read (0 for EOF), or
657 None if the object is set not to block and has no data to read.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000658 """
659 self._unsupported("readinto")
660
Raymond Hettinger3c940242011-01-12 23:39:31 +0000661 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000662 """Write the given buffer to the IO stream.
663
Martin Panter6bb91f32016-05-28 00:41:57 +0000664 Returns the number of bytes written, which may be less than the
665 length of b in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000666 """
667 self._unsupported("write")
668
669io.RawIOBase.register(RawIOBase)
670from _io import FileIO
671RawIOBase.register(FileIO)
672
673
674class BufferedIOBase(IOBase):
675
676 """Base class for buffered IO objects.
677
678 The main difference with RawIOBase is that the read() method
679 supports omitting the size argument, and does not have a default
680 implementation that defers to readinto().
681
682 In addition, read(), readinto() and write() may raise
683 BlockingIOError if the underlying raw stream is in non-blocking
684 mode and not ready; unlike their raw counterparts, they will never
685 return None.
686
687 A typical implementation should not inherit from a RawIOBase
688 implementation, but wrap one.
689 """
690
Martin Panterccb2c0e2016-10-20 23:48:14 +0000691 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300692 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000693
694 If the argument is omitted, None, or negative, reads and
695 returns all data until EOF.
696
697 If the argument is positive, and the underlying raw stream is
698 not 'interactive', multiple raw reads may be issued to satisfy
699 the byte count (unless EOF is reached first). But for
700 interactive raw streams (XXX and for pipes?), at most one raw
701 read will be issued, and a short result does not imply that
702 EOF is imminent.
703
704 Returns an empty bytes array on EOF.
705
706 Raises BlockingIOError if the underlying raw stream has no
707 data at the moment.
708 """
709 self._unsupported("read")
710
Martin Panterccb2c0e2016-10-20 23:48:14 +0000711 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300712 """Read up to size bytes with at most one read() system call,
713 where size is an int.
Raymond Hettingercbb80892011-01-13 18:15:51 +0000714 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000715 self._unsupported("read1")
716
Raymond Hettinger3c940242011-01-12 23:39:31 +0000717 def readinto(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000718 """Read bytes into a pre-allocated bytes-like object b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000719
720 Like read(), this may issue multiple reads to the underlying raw
721 stream, unless the latter is 'interactive'.
722
Raymond Hettingercbb80892011-01-13 18:15:51 +0000723 Returns an int representing the number of bytes read (0 for EOF).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724
725 Raises BlockingIOError if the underlying raw stream has no
726 data at the moment.
727 """
Benjamin Petersona96fea02014-06-22 14:17:44 -0700728
729 return self._readinto(b, read1=False)
730
731 def readinto1(self, b):
Martin Panter6bb91f32016-05-28 00:41:57 +0000732 """Read bytes into buffer *b*, using at most one system call
Benjamin Petersona96fea02014-06-22 14:17:44 -0700733
734 Returns an int representing the number of bytes read (0 for EOF).
735
736 Raises BlockingIOError if the underlying raw stream has no
737 data at the moment.
738 """
739
740 return self._readinto(b, read1=True)
741
742 def _readinto(self, b, read1):
743 if not isinstance(b, memoryview):
744 b = memoryview(b)
745 b = b.cast('B')
746
747 if read1:
748 data = self.read1(len(b))
749 else:
750 data = self.read(len(b))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000751 n = len(data)
Benjamin Petersona96fea02014-06-22 14:17:44 -0700752
753 b[:n] = data
754
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000755 return n
756
Raymond Hettinger3c940242011-01-12 23:39:31 +0000757 def write(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000758 """Write the given bytes buffer to the IO stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000759
Martin Panter6bb91f32016-05-28 00:41:57 +0000760 Return the number of bytes written, which is always the length of b
761 in bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000762
763 Raises BlockingIOError if the buffer is full and the
764 underlying raw stream cannot accept more data at the moment.
765 """
766 self._unsupported("write")
767
Raymond Hettinger3c940242011-01-12 23:39:31 +0000768 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000769 """
770 Separate the underlying raw stream from the buffer and return it.
771
772 After the raw stream has been detached, the buffer is in an unusable
773 state.
774 """
775 self._unsupported("detach")
776
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000777io.BufferedIOBase.register(BufferedIOBase)
778
779
780class _BufferedIOMixin(BufferedIOBase):
781
782 """A mixin implementation of BufferedIOBase with an underlying raw stream.
783
784 This passes most requests on to the underlying raw stream. It
785 does *not* provide implementations of read(), readinto() or
786 write().
787 """
788
789 def __init__(self, raw):
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000790 self._raw = raw
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000791
792 ### Positioning ###
793
794 def seek(self, pos, whence=0):
795 new_position = self.raw.seek(pos, whence)
796 if new_position < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200797 raise OSError("seek() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000798 return new_position
799
800 def tell(self):
801 pos = self.raw.tell()
802 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200803 raise OSError("tell() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000804 return pos
805
806 def truncate(self, pos=None):
807 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
808 # and a flush may be necessary to synch both views of the current
809 # file state.
810 self.flush()
811
812 if pos is None:
813 pos = self.tell()
814 # XXX: Should seek() be used, instead of passing the position
815 # XXX directly to truncate?
816 return self.raw.truncate(pos)
817
818 ### Flush and close ###
819
820 def flush(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000821 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +0300822 raise ValueError("flush on closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000823 self.raw.flush()
824
825 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000826 if self.raw is not None and not self.closed:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100827 try:
828 # may raise BlockingIOError or BrokenPipeError etc
829 self.flush()
830 finally:
831 self.raw.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000832
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000833 def detach(self):
834 if self.raw is None:
835 raise ValueError("raw stream already detached")
836 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000837 raw = self._raw
838 self._raw = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000839 return raw
840
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000841 ### Inquiries ###
842
843 def seekable(self):
844 return self.raw.seekable()
845
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000846 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000847 def raw(self):
848 return self._raw
849
850 @property
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000851 def closed(self):
852 return self.raw.closed
853
854 @property
855 def name(self):
856 return self.raw.name
857
858 @property
859 def mode(self):
860 return self.raw.mode
861
Antoine Pitrou243757e2010-11-05 21:15:39 +0000862 def __getstate__(self):
Serhiy Storchaka0353b4e2018-10-31 02:28:07 +0200863 raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
Antoine Pitrou243757e2010-11-05 21:15:39 +0000864
Antoine Pitrou716c4442009-05-23 19:04:03 +0000865 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300866 modname = self.__class__.__module__
867 clsname = self.__class__.__qualname__
Antoine Pitrou716c4442009-05-23 19:04:03 +0000868 try:
869 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -0600870 except Exception:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300871 return "<{}.{}>".format(modname, clsname)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000872 else:
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300873 return "<{}.{} name={!r}>".format(modname, clsname, name)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000874
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000875 ### Lower-level APIs ###
876
877 def fileno(self):
878 return self.raw.fileno()
879
880 def isatty(self):
881 return self.raw.isatty()
882
883
884class BytesIO(BufferedIOBase):
885
886 """Buffered I/O implementation using an in-memory bytes buffer."""
887
Victor Stinnera3568412019-05-28 01:44:21 +0200888 # Initialize _buffer as soon as possible since it's used by __del__()
889 # which calls close()
890 _buffer = None
891
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000892 def __init__(self, initial_bytes=None):
893 buf = bytearray()
894 if initial_bytes is not None:
895 buf += initial_bytes
896 self._buffer = buf
897 self._pos = 0
898
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000899 def __getstate__(self):
900 if self.closed:
901 raise ValueError("__getstate__ on closed file")
902 return self.__dict__.copy()
903
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904 def getvalue(self):
905 """Return the bytes value (contents) of the buffer
906 """
907 if self.closed:
908 raise ValueError("getvalue on closed file")
909 return bytes(self._buffer)
910
Antoine Pitrou972ee132010-09-06 18:48:21 +0000911 def getbuffer(self):
912 """Return a readable and writable view of the buffer.
913 """
Serhiy Storchakac057c382015-02-03 02:00:18 +0200914 if self.closed:
915 raise ValueError("getbuffer on closed file")
Antoine Pitrou972ee132010-09-06 18:48:21 +0000916 return memoryview(self._buffer)
917
Serhiy Storchakac057c382015-02-03 02:00:18 +0200918 def close(self):
Victor Stinnera3568412019-05-28 01:44:21 +0200919 if self._buffer is not None:
920 self._buffer.clear()
Serhiy Storchakac057c382015-02-03 02:00:18 +0200921 super().close()
922
Martin Panterccb2c0e2016-10-20 23:48:14 +0000923 def read(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000924 if self.closed:
925 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300926 if size is None:
927 size = -1
Oren Milmande503602017-08-24 21:33:42 +0300928 else:
929 try:
930 size_index = size.__index__
931 except AttributeError:
932 raise TypeError(f"{size!r} is not an integer")
933 else:
934 size = size_index()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300935 if size < 0:
936 size = len(self._buffer)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000937 if len(self._buffer) <= self._pos:
938 return b""
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300939 newpos = min(len(self._buffer), self._pos + size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000940 b = self._buffer[self._pos : newpos]
941 self._pos = newpos
942 return bytes(b)
943
Martin Panterccb2c0e2016-10-20 23:48:14 +0000944 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000945 """This is the same as read.
946 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300947 return self.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000948
949 def write(self, b):
950 if self.closed:
951 raise ValueError("write to closed file")
952 if isinstance(b, str):
953 raise TypeError("can't write str to binary stream")
Martin Panter6bb91f32016-05-28 00:41:57 +0000954 with memoryview(b) as view:
955 n = view.nbytes # Size of any bytes-like object
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000956 if n == 0:
957 return 0
958 pos = self._pos
959 if pos > len(self._buffer):
960 # Inserts null bytes between the current end of the file
961 # and the new write position.
962 padding = b'\x00' * (pos - len(self._buffer))
963 self._buffer += padding
964 self._buffer[pos:pos + n] = b
965 self._pos += n
966 return n
967
968 def seek(self, pos, whence=0):
969 if self.closed:
970 raise ValueError("seek on closed file")
971 try:
Oren Milmande503602017-08-24 21:33:42 +0300972 pos_index = pos.__index__
973 except AttributeError:
974 raise TypeError(f"{pos!r} is not an integer")
975 else:
976 pos = pos_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000977 if whence == 0:
978 if pos < 0:
979 raise ValueError("negative seek position %r" % (pos,))
980 self._pos = pos
981 elif whence == 1:
982 self._pos = max(0, self._pos + pos)
983 elif whence == 2:
984 self._pos = max(0, len(self._buffer) + pos)
985 else:
Jesus Cea94363612012-06-22 18:32:07 +0200986 raise ValueError("unsupported whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000987 return self._pos
988
989 def tell(self):
990 if self.closed:
991 raise ValueError("tell on closed file")
992 return self._pos
993
994 def truncate(self, pos=None):
995 if self.closed:
996 raise ValueError("truncate on closed file")
997 if pos is None:
998 pos = self._pos
Florent Xiclunab14930c2010-03-13 15:26:44 +0000999 else:
1000 try:
Oren Milmande503602017-08-24 21:33:42 +03001001 pos_index = pos.__index__
1002 except AttributeError:
1003 raise TypeError(f"{pos!r} is not an integer")
1004 else:
1005 pos = pos_index()
Florent Xiclunab14930c2010-03-13 15:26:44 +00001006 if pos < 0:
1007 raise ValueError("negative truncate position %r" % (pos,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001008 del self._buffer[pos:]
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001009 return pos
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001010
1011 def readable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02001012 if self.closed:
1013 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001014 return True
1015
1016 def writable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02001017 if self.closed:
1018 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001019 return True
1020
1021 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02001022 if self.closed:
1023 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001024 return True
1025
1026
1027class BufferedReader(_BufferedIOMixin):
1028
1029 """BufferedReader(raw[, buffer_size])
1030
1031 A buffer for a readable, sequential BaseRawIO object.
1032
1033 The constructor creates a BufferedReader for the given readable raw
1034 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
1035 is used.
1036 """
1037
1038 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
1039 """Create a new buffered reader using the given readable raw IO object.
1040 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001041 if not raw.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001042 raise OSError('"raw" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001043
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001044 _BufferedIOMixin.__init__(self, raw)
1045 if buffer_size <= 0:
1046 raise ValueError("invalid buffer size")
1047 self.buffer_size = buffer_size
1048 self._reset_read_buf()
1049 self._read_lock = Lock()
1050
Martin Panter754aab22016-03-31 07:21:56 +00001051 def readable(self):
1052 return self.raw.readable()
1053
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001054 def _reset_read_buf(self):
1055 self._read_buf = b""
1056 self._read_pos = 0
1057
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001058 def read(self, size=None):
1059 """Read size bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001060
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001061 Returns exactly size bytes of data unless the underlying raw IO
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001062 stream reaches EOF or if the call would block in non-blocking
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001063 mode. If size is negative, read until EOF or until read() would
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001064 block.
1065 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001066 if size is not None and size < -1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001067 raise ValueError("invalid number of bytes to read")
1068 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001069 return self._read_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001070
1071 def _read_unlocked(self, n=None):
1072 nodata_val = b""
1073 empty_values = (b"", None)
1074 buf = self._read_buf
1075 pos = self._read_pos
1076
1077 # Special case for when the number of bytes to read is unspecified.
1078 if n is None or n == -1:
1079 self._reset_read_buf()
Victor Stinnerb57f1082011-05-26 00:19:38 +02001080 if hasattr(self.raw, 'readall'):
1081 chunk = self.raw.readall()
1082 if chunk is None:
1083 return buf[pos:] or None
1084 else:
1085 return buf[pos:] + chunk
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001086 chunks = [buf[pos:]] # Strip the consumed bytes.
1087 current_size = 0
1088 while True:
1089 # Read until EOF or until read() would block.
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001090 chunk = self.raw.read()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001091 if chunk in empty_values:
1092 nodata_val = chunk
1093 break
1094 current_size += len(chunk)
1095 chunks.append(chunk)
1096 return b"".join(chunks) or nodata_val
1097
1098 # The number of bytes to read is specified, return at most n bytes.
1099 avail = len(buf) - pos # Length of the available buffered data.
1100 if n <= avail:
1101 # Fast path: the data to read is fully buffered.
1102 self._read_pos += n
1103 return buf[pos:pos+n]
1104 # Slow path: read from the stream until enough bytes are read,
1105 # or until an EOF occurs or until read() would block.
1106 chunks = [buf[pos:]]
1107 wanted = max(self.buffer_size, n)
1108 while avail < n:
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001109 chunk = self.raw.read(wanted)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001110 if chunk in empty_values:
1111 nodata_val = chunk
1112 break
1113 avail += len(chunk)
1114 chunks.append(chunk)
Martin Pantere26da7c2016-06-02 10:07:09 +00001115 # n is more than avail only when an EOF occurred or when
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001116 # read() would have blocked.
1117 n = min(n, avail)
1118 out = b"".join(chunks)
1119 self._read_buf = out[n:] # Save the extra data in the buffer.
1120 self._read_pos = 0
1121 return out[:n] if out else nodata_val
1122
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001123 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001124 """Returns buffered bytes without advancing the position.
1125
1126 The argument indicates a desired minimal number of bytes; we
1127 do at most one raw read to satisfy it. We never return more
1128 than self.buffer_size.
1129 """
1130 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001131 return self._peek_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001132
1133 def _peek_unlocked(self, n=0):
1134 want = min(n, self.buffer_size)
1135 have = len(self._read_buf) - self._read_pos
1136 if have < want or have <= 0:
1137 to_read = self.buffer_size - have
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001138 current = self.raw.read(to_read)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001139 if current:
1140 self._read_buf = self._read_buf[self._read_pos:] + current
1141 self._read_pos = 0
1142 return self._read_buf[self._read_pos:]
1143
Martin Panterccb2c0e2016-10-20 23:48:14 +00001144 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001145 """Reads up to size bytes, with at most one read() system call."""
1146 # Returns up to size bytes. If at least one byte is buffered, we
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001147 # only return buffered bytes. Otherwise, we do one raw read.
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001148 if size < 0:
Martin Panterccb2c0e2016-10-20 23:48:14 +00001149 size = self.buffer_size
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001150 if size == 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001151 return b""
1152 with self._read_lock:
1153 self._peek_unlocked(1)
1154 return self._read_unlocked(
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001155 min(size, len(self._read_buf) - self._read_pos))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001156
Benjamin Petersona96fea02014-06-22 14:17:44 -07001157 # Implementing readinto() and readinto1() is not strictly necessary (we
1158 # could rely on the base class that provides an implementation in terms of
1159 # read() and read1()). We do it anyway to keep the _pyio implementation
1160 # similar to the io implementation (which implements the methods for
1161 # performance reasons).
1162 def _readinto(self, buf, read1):
1163 """Read data into *buf* with at most one system call."""
1164
Benjamin Petersona96fea02014-06-22 14:17:44 -07001165 # Need to create a memoryview object of type 'b', otherwise
1166 # we may not be able to assign bytes to it, and slicing it
1167 # would create a new object.
1168 if not isinstance(buf, memoryview):
1169 buf = memoryview(buf)
Martin Panter6bb91f32016-05-28 00:41:57 +00001170 if buf.nbytes == 0:
1171 return 0
Benjamin Petersona96fea02014-06-22 14:17:44 -07001172 buf = buf.cast('B')
1173
1174 written = 0
1175 with self._read_lock:
1176 while written < len(buf):
1177
1178 # First try to read from internal buffer
1179 avail = min(len(self._read_buf) - self._read_pos, len(buf))
1180 if avail:
1181 buf[written:written+avail] = \
1182 self._read_buf[self._read_pos:self._read_pos+avail]
1183 self._read_pos += avail
1184 written += avail
1185 if written == len(buf):
1186 break
1187
1188 # If remaining space in callers buffer is larger than
1189 # internal buffer, read directly into callers buffer
1190 if len(buf) - written > self.buffer_size:
1191 n = self.raw.readinto(buf[written:])
1192 if not n:
1193 break # eof
1194 written += n
1195
1196 # Otherwise refill internal buffer - unless we're
1197 # in read1 mode and already got some data
1198 elif not (read1 and written):
1199 if not self._peek_unlocked(1):
1200 break # eof
1201
1202 # In readinto1 mode, return as soon as we have some data
1203 if read1 and written:
1204 break
1205
1206 return written
1207
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001208 def tell(self):
1209 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
1210
1211 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001212 if whence not in valid_seek_flags:
Jesus Cea990eff02012-04-26 17:05:31 +02001213 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001214 with self._read_lock:
1215 if whence == 1:
1216 pos -= len(self._read_buf) - self._read_pos
1217 pos = _BufferedIOMixin.seek(self, pos, whence)
1218 self._reset_read_buf()
1219 return pos
1220
1221class BufferedWriter(_BufferedIOMixin):
1222
1223 """A buffer for a writeable sequential RawIO object.
1224
1225 The constructor creates a BufferedWriter for the given writeable raw
1226 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001227 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001228 """
1229
Florent Xicluna109d5732012-07-07 17:03:22 +02001230 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001231 if not raw.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001232 raise OSError('"raw" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001233
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001234 _BufferedIOMixin.__init__(self, raw)
1235 if buffer_size <= 0:
1236 raise ValueError("invalid buffer size")
1237 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001238 self._write_buf = bytearray()
1239 self._write_lock = Lock()
1240
Martin Panter754aab22016-03-31 07:21:56 +00001241 def writable(self):
1242 return self.raw.writable()
1243
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001244 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001245 if isinstance(b, str):
1246 raise TypeError("can't write str to binary stream")
1247 with self._write_lock:
benfogle9703f092017-11-10 16:03:40 -05001248 if self.closed:
1249 raise ValueError("write to closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001250 # XXX we can implement some more tricks to try and avoid
1251 # partial writes
1252 if len(self._write_buf) > self.buffer_size:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001253 # We're full, so let's pre-flush the buffer. (This may
1254 # raise BlockingIOError with characters_written == 0.)
1255 self._flush_unlocked()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001256 before = len(self._write_buf)
1257 self._write_buf.extend(b)
1258 written = len(self._write_buf) - before
1259 if len(self._write_buf) > self.buffer_size:
1260 try:
1261 self._flush_unlocked()
1262 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001263 if len(self._write_buf) > self.buffer_size:
1264 # We've hit the buffer_size. We have to accept a partial
1265 # write and cut back our buffer.
1266 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001267 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001268 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001269 raise BlockingIOError(e.errno, e.strerror, written)
1270 return written
1271
1272 def truncate(self, pos=None):
1273 with self._write_lock:
1274 self._flush_unlocked()
1275 if pos is None:
1276 pos = self.raw.tell()
1277 return self.raw.truncate(pos)
1278
1279 def flush(self):
1280 with self._write_lock:
1281 self._flush_unlocked()
1282
1283 def _flush_unlocked(self):
1284 if self.closed:
Jim Fasarakis-Hilliard1e73dbb2017-03-26 23:59:08 +03001285 raise ValueError("flush on closed file")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001286 while self._write_buf:
1287 try:
1288 n = self.raw.write(self._write_buf)
1289 except BlockingIOError:
1290 raise RuntimeError("self.raw should implement RawIOBase: it "
1291 "should not raise BlockingIOError")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001292 if n is None:
1293 raise BlockingIOError(
1294 errno.EAGAIN,
1295 "write could not complete without blocking", 0)
1296 if n > len(self._write_buf) or n < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001297 raise OSError("write() returned incorrect number of bytes")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001298 del self._write_buf[:n]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001299
1300 def tell(self):
1301 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1302
1303 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001304 if whence not in valid_seek_flags:
1305 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001306 with self._write_lock:
1307 self._flush_unlocked()
1308 return _BufferedIOMixin.seek(self, pos, whence)
1309
benfogle9703f092017-11-10 16:03:40 -05001310 def close(self):
1311 with self._write_lock:
1312 if self.raw is None or self.closed:
1313 return
1314 # We have to release the lock and call self.flush() (which will
1315 # probably just re-take the lock) in case flush has been overridden in
1316 # a subclass or the user set self.flush to something. This is the same
1317 # behavior as the C implementation.
1318 try:
1319 # may raise BlockingIOError or BrokenPipeError etc
1320 self.flush()
1321 finally:
1322 with self._write_lock:
1323 self.raw.close()
1324
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001325
1326class BufferedRWPair(BufferedIOBase):
1327
1328 """A buffered reader and writer object together.
1329
1330 A buffered reader object and buffered writer object put together to
1331 form a sequential IO object that can read and write. This is typically
1332 used with a socket or two-way pipe.
1333
1334 reader and writer are RawIOBase objects that are readable and
1335 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001336 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001337 """
1338
1339 # XXX The usefulness of this (compared to having two separate IO
1340 # objects) is questionable.
1341
Florent Xicluna109d5732012-07-07 17:03:22 +02001342 def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001343 """Constructor.
1344
1345 The arguments are two RawIO instances.
1346 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001347 if not reader.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001348 raise OSError('"reader" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001349
1350 if not writer.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001351 raise OSError('"writer" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001352
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001353 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001354 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001355
Martin Panterccb2c0e2016-10-20 23:48:14 +00001356 def read(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001357 if size is None:
1358 size = -1
1359 return self.reader.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001360
1361 def readinto(self, b):
1362 return self.reader.readinto(b)
1363
1364 def write(self, b):
1365 return self.writer.write(b)
1366
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001367 def peek(self, size=0):
1368 return self.reader.peek(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001369
Martin Panterccb2c0e2016-10-20 23:48:14 +00001370 def read1(self, size=-1):
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001371 return self.reader.read1(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001372
Benjamin Petersona96fea02014-06-22 14:17:44 -07001373 def readinto1(self, b):
1374 return self.reader.readinto1(b)
1375
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001376 def readable(self):
1377 return self.reader.readable()
1378
1379 def writable(self):
1380 return self.writer.writable()
1381
1382 def flush(self):
1383 return self.writer.flush()
1384
1385 def close(self):
Serhiy Storchaka7665be62015-03-24 23:21:57 +02001386 try:
1387 self.writer.close()
1388 finally:
1389 self.reader.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001390
1391 def isatty(self):
1392 return self.reader.isatty() or self.writer.isatty()
1393
1394 @property
1395 def closed(self):
1396 return self.writer.closed
1397
1398
1399class BufferedRandom(BufferedWriter, BufferedReader):
1400
1401 """A buffered interface to random access streams.
1402
1403 The constructor creates a reader and writer for a seekable stream,
1404 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001405 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001406 """
1407
Florent Xicluna109d5732012-07-07 17:03:22 +02001408 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001409 raw._checkSeekable()
1410 BufferedReader.__init__(self, raw, buffer_size)
Florent Xicluna109d5732012-07-07 17:03:22 +02001411 BufferedWriter.__init__(self, raw, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001412
1413 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001414 if whence not in valid_seek_flags:
1415 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001416 self.flush()
1417 if self._read_buf:
1418 # Undo read ahead.
1419 with self._read_lock:
1420 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1421 # First do the raw seek, then empty the read buffer, so that
1422 # if the raw seek fails, we don't lose buffered data forever.
1423 pos = self.raw.seek(pos, whence)
1424 with self._read_lock:
1425 self._reset_read_buf()
1426 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001427 raise OSError("seek() returned invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001428 return pos
1429
1430 def tell(self):
1431 if self._write_buf:
1432 return BufferedWriter.tell(self)
1433 else:
1434 return BufferedReader.tell(self)
1435
1436 def truncate(self, pos=None):
1437 if pos is None:
1438 pos = self.tell()
1439 # Use seek to flush the read buffer.
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001440 return BufferedWriter.truncate(self, pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001441
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001442 def read(self, size=None):
1443 if size is None:
1444 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001445 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001446 return BufferedReader.read(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001447
1448 def readinto(self, b):
1449 self.flush()
1450 return BufferedReader.readinto(self, b)
1451
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001452 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001453 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001454 return BufferedReader.peek(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001455
Martin Panterccb2c0e2016-10-20 23:48:14 +00001456 def read1(self, size=-1):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001457 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001458 return BufferedReader.read1(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001459
Benjamin Petersona96fea02014-06-22 14:17:44 -07001460 def readinto1(self, b):
1461 self.flush()
1462 return BufferedReader.readinto1(self, b)
1463
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001464 def write(self, b):
1465 if self._read_buf:
1466 # Undo readahead
1467 with self._read_lock:
1468 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1469 self._reset_read_buf()
1470 return BufferedWriter.write(self, b)
1471
1472
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001473class FileIO(RawIOBase):
1474 _fd = -1
1475 _created = False
1476 _readable = False
1477 _writable = False
1478 _appending = False
1479 _seekable = None
1480 _closefd = True
1481
1482 def __init__(self, file, mode='r', closefd=True, opener=None):
1483 """Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
1484 writing, exclusive creation or appending. The file will be created if it
1485 doesn't exist when opened for writing or appending; it will be truncated
1486 when opened for writing. A FileExistsError will be raised if it already
1487 exists when opened for creating. Opening a file for creating implies
1488 writing so this mode behaves in a similar way to 'w'. Add a '+' to the mode
1489 to allow simultaneous reading and writing. A custom opener can be used by
1490 passing a callable as *opener*. The underlying file descriptor for the file
1491 object is then obtained by calling opener with (*name*, *flags*).
1492 *opener* must return an open file descriptor (passing os.open as *opener*
1493 results in functionality similar to passing None).
1494 """
1495 if self._fd >= 0:
1496 # Have to close the existing file first.
1497 try:
1498 if self._closefd:
1499 os.close(self._fd)
1500 finally:
1501 self._fd = -1
1502
1503 if isinstance(file, float):
1504 raise TypeError('integer argument expected, got float')
1505 if isinstance(file, int):
1506 fd = file
1507 if fd < 0:
1508 raise ValueError('negative file descriptor')
1509 else:
1510 fd = -1
1511
1512 if not isinstance(mode, str):
1513 raise TypeError('invalid mode: %s' % (mode,))
1514 if not set(mode) <= set('xrwab+'):
1515 raise ValueError('invalid mode: %s' % (mode,))
1516 if sum(c in 'rwax' for c in mode) != 1 or mode.count('+') > 1:
1517 raise ValueError('Must have exactly one of create/read/write/append '
1518 'mode and at most one plus')
1519
1520 if 'x' in mode:
1521 self._created = True
1522 self._writable = True
1523 flags = os.O_EXCL | os.O_CREAT
1524 elif 'r' in mode:
1525 self._readable = True
1526 flags = 0
1527 elif 'w' in mode:
1528 self._writable = True
1529 flags = os.O_CREAT | os.O_TRUNC
1530 elif 'a' in mode:
1531 self._writable = True
1532 self._appending = True
1533 flags = os.O_APPEND | os.O_CREAT
1534
1535 if '+' in mode:
1536 self._readable = True
1537 self._writable = True
1538
1539 if self._readable and self._writable:
1540 flags |= os.O_RDWR
1541 elif self._readable:
1542 flags |= os.O_RDONLY
1543 else:
1544 flags |= os.O_WRONLY
1545
1546 flags |= getattr(os, 'O_BINARY', 0)
1547
1548 noinherit_flag = (getattr(os, 'O_NOINHERIT', 0) or
1549 getattr(os, 'O_CLOEXEC', 0))
1550 flags |= noinherit_flag
1551
1552 owned_fd = None
1553 try:
1554 if fd < 0:
1555 if not closefd:
1556 raise ValueError('Cannot use closefd=False with file name')
1557 if opener is None:
1558 fd = os.open(file, flags, 0o666)
1559 else:
1560 fd = opener(file, flags)
1561 if not isinstance(fd, int):
1562 raise TypeError('expected integer from opener')
1563 if fd < 0:
1564 raise OSError('Negative file descriptor')
1565 owned_fd = fd
1566 if not noinherit_flag:
1567 os.set_inheritable(fd, False)
1568
1569 self._closefd = closefd
1570 fdfstat = os.fstat(fd)
1571 try:
1572 if stat.S_ISDIR(fdfstat.st_mode):
1573 raise IsADirectoryError(errno.EISDIR,
1574 os.strerror(errno.EISDIR), file)
1575 except AttributeError:
1576 # Ignore the AttribueError if stat.S_ISDIR or errno.EISDIR
1577 # don't exist.
1578 pass
1579 self._blksize = getattr(fdfstat, 'st_blksize', 0)
1580 if self._blksize <= 1:
1581 self._blksize = DEFAULT_BUFFER_SIZE
1582
1583 if _setmode:
1584 # don't translate newlines (\r\n <=> \n)
1585 _setmode(fd, os.O_BINARY)
1586
1587 self.name = file
1588 if self._appending:
1589 # For consistent behaviour, we explicitly seek to the
1590 # end of file (otherwise, it might be done only on the
1591 # first write()).
1592 os.lseek(fd, 0, SEEK_END)
1593 except:
1594 if owned_fd is not None:
1595 os.close(owned_fd)
1596 raise
1597 self._fd = fd
1598
1599 def __del__(self):
1600 if self._fd >= 0 and self._closefd and not self.closed:
1601 import warnings
1602 warnings.warn('unclosed file %r' % (self,), ResourceWarning,
Victor Stinnere19558a2016-03-23 00:28:08 +01001603 stacklevel=2, source=self)
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001604 self.close()
1605
1606 def __getstate__(self):
Serhiy Storchaka0353b4e2018-10-31 02:28:07 +02001607 raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
Serhiy Storchaka71fd2242015-04-10 16:16:16 +03001608
1609 def __repr__(self):
1610 class_name = '%s.%s' % (self.__class__.__module__,
1611 self.__class__.__qualname__)
1612 if self.closed:
1613 return '<%s [closed]>' % class_name
1614 try:
1615 name = self.name
1616 except AttributeError:
1617 return ('<%s fd=%d mode=%r closefd=%r>' %
1618 (class_name, self._fd, self.mode, self._closefd))
1619 else:
1620 return ('<%s name=%r mode=%r closefd=%r>' %
1621 (class_name, name, self.mode, self._closefd))
1622
1623 def _checkReadable(self):
1624 if not self._readable:
1625 raise UnsupportedOperation('File not open for reading')
1626
1627 def _checkWritable(self, msg=None):
1628 if not self._writable:
1629 raise UnsupportedOperation('File not open for writing')
1630
1631 def read(self, size=None):
1632 """Read at most size bytes, returned as bytes.
1633
1634 Only makes one system call, so less data may be returned than requested
1635 In non-blocking mode, returns None if no data is available.
1636 Return an empty bytes object at EOF.
1637 """
1638 self._checkClosed()
1639 self._checkReadable()
1640 if size is None or size < 0:
1641 return self.readall()
1642 try:
1643 return os.read(self._fd, size)
1644 except BlockingIOError:
1645 return None
1646
1647 def readall(self):
1648 """Read all data from the file, returned as bytes.
1649
1650 In non-blocking mode, returns as much as is immediately available,
1651 or None if no data is available. Return an empty bytes object at EOF.
1652 """
1653 self._checkClosed()
1654 self._checkReadable()
1655 bufsize = DEFAULT_BUFFER_SIZE
1656 try:
1657 pos = os.lseek(self._fd, 0, SEEK_CUR)
1658 end = os.fstat(self._fd).st_size
1659 if end >= pos:
1660 bufsize = end - pos + 1
1661 except OSError:
1662 pass
1663
1664 result = bytearray()
1665 while True:
1666 if len(result) >= bufsize:
1667 bufsize = len(result)
1668 bufsize += max(bufsize, DEFAULT_BUFFER_SIZE)
1669 n = bufsize - len(result)
1670 try:
1671 chunk = os.read(self._fd, n)
1672 except BlockingIOError:
1673 if result:
1674 break
1675 return None
1676 if not chunk: # reached the end of the file
1677 break
1678 result += chunk
1679
1680 return bytes(result)
1681
1682 def readinto(self, b):
1683 """Same as RawIOBase.readinto()."""
1684 m = memoryview(b).cast('B')
1685 data = self.read(len(m))
1686 n = len(data)
1687 m[:n] = data
1688 return n
1689
1690 def write(self, b):
1691 """Write bytes b to file, return number written.
1692
1693 Only makes one system call, so not all of the data may be written.
1694 The number of bytes actually written is returned. In non-blocking mode,
1695 returns None if the write would block.
1696 """
1697 self._checkClosed()
1698 self._checkWritable()
1699 try:
1700 return os.write(self._fd, b)
1701 except BlockingIOError:
1702 return None
1703
1704 def seek(self, pos, whence=SEEK_SET):
1705 """Move to new file position.
1706
1707 Argument offset is a byte count. Optional argument whence defaults to
1708 SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
1709 are SEEK_CUR or 1 (move relative to current position, positive or negative),
1710 and SEEK_END or 2 (move relative to end of file, usually negative, although
1711 many platforms allow seeking beyond the end of a file).
1712
1713 Note that not all file objects are seekable.
1714 """
1715 if isinstance(pos, float):
1716 raise TypeError('an integer is required')
1717 self._checkClosed()
1718 return os.lseek(self._fd, pos, whence)
1719
1720 def tell(self):
1721 """tell() -> int. Current file position.
1722
1723 Can raise OSError for non seekable files."""
1724 self._checkClosed()
1725 return os.lseek(self._fd, 0, SEEK_CUR)
1726
1727 def truncate(self, size=None):
1728 """Truncate the file to at most size bytes.
1729
1730 Size defaults to the current file position, as returned by tell().
1731 The current file position is changed to the value of size.
1732 """
1733 self._checkClosed()
1734 self._checkWritable()
1735 if size is None:
1736 size = self.tell()
1737 os.ftruncate(self._fd, size)
1738 return size
1739
1740 def close(self):
1741 """Close the file.
1742
1743 A closed file cannot be used for further I/O operations. close() may be
1744 called more than once without error.
1745 """
1746 if not self.closed:
1747 try:
1748 if self._closefd:
1749 os.close(self._fd)
1750 finally:
1751 super().close()
1752
1753 def seekable(self):
1754 """True if file supports random-access."""
1755 self._checkClosed()
1756 if self._seekable is None:
1757 try:
1758 self.tell()
1759 except OSError:
1760 self._seekable = False
1761 else:
1762 self._seekable = True
1763 return self._seekable
1764
1765 def readable(self):
1766 """True if file was opened in a read mode."""
1767 self._checkClosed()
1768 return self._readable
1769
1770 def writable(self):
1771 """True if file was opened in a write mode."""
1772 self._checkClosed()
1773 return self._writable
1774
1775 def fileno(self):
1776 """Return the underlying file descriptor (an integer)."""
1777 self._checkClosed()
1778 return self._fd
1779
1780 def isatty(self):
1781 """True if the file is connected to a TTY device."""
1782 self._checkClosed()
1783 return os.isatty(self._fd)
1784
1785 @property
1786 def closefd(self):
1787 """True if the file descriptor will be closed by close()."""
1788 return self._closefd
1789
1790 @property
1791 def mode(self):
1792 """String giving the file mode"""
1793 if self._created:
1794 if self._readable:
1795 return 'xb+'
1796 else:
1797 return 'xb'
1798 elif self._appending:
1799 if self._readable:
1800 return 'ab+'
1801 else:
1802 return 'ab'
1803 elif self._readable:
1804 if self._writable:
1805 return 'rb+'
1806 else:
1807 return 'rb'
1808 else:
1809 return 'wb'
1810
1811
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001812class TextIOBase(IOBase):
1813
1814 """Base class for text I/O.
1815
1816 This class provides a character and line based interface to stream
Steve Palmer7b97ab32019-04-09 05:35:27 +01001817 I/O. There is no public constructor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001818 """
1819
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001820 def read(self, size=-1):
1821 """Read at most size characters from stream, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001822
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001823 Read from underlying buffer until we have size characters or we hit EOF.
1824 If size is negative or omitted, read until EOF.
Raymond Hettingercbb80892011-01-13 18:15:51 +00001825
1826 Returns a string.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001827 """
1828 self._unsupported("read")
1829
Raymond Hettinger3c940242011-01-12 23:39:31 +00001830 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001831 """Write string s to stream and returning an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001832 self._unsupported("write")
1833
Georg Brandl4d73b572011-01-13 07:13:06 +00001834 def truncate(self, pos=None):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001835 """Truncate size to pos, where pos is an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001836 self._unsupported("truncate")
1837
Raymond Hettinger3c940242011-01-12 23:39:31 +00001838 def readline(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001839 """Read until newline or EOF.
1840
1841 Returns an empty string if EOF is hit immediately.
1842 """
1843 self._unsupported("readline")
1844
Raymond Hettinger3c940242011-01-12 23:39:31 +00001845 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001846 """
1847 Separate the underlying buffer from the TextIOBase and return it.
1848
1849 After the underlying buffer has been detached, the TextIO is in an
1850 unusable state.
1851 """
1852 self._unsupported("detach")
1853
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001854 @property
1855 def encoding(self):
1856 """Subclasses should override."""
1857 return None
1858
1859 @property
1860 def newlines(self):
1861 """Line endings translated so far.
1862
1863 Only line endings translated during reading are considered.
1864
1865 Subclasses should override.
1866 """
1867 return None
1868
Benjamin Peterson0926ad12009-06-06 18:02:12 +00001869 @property
1870 def errors(self):
1871 """Error setting of the decoder or encoder.
1872
1873 Subclasses should override."""
1874 return None
1875
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001876io.TextIOBase.register(TextIOBase)
1877
1878
1879class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1880 r"""Codec used when reading a file in universal newlines mode. It wraps
1881 another incremental decoder, translating \r\n and \r into \n. It also
1882 records the types of newlines encountered. When used with
1883 translate=False, it ensures that the newline sequence is returned in
1884 one piece.
1885 """
1886 def __init__(self, decoder, translate, errors='strict'):
1887 codecs.IncrementalDecoder.__init__(self, errors=errors)
1888 self.translate = translate
1889 self.decoder = decoder
1890 self.seennl = 0
1891 self.pendingcr = False
1892
1893 def decode(self, input, final=False):
1894 # decode input (with the eventual \r from a previous pass)
1895 if self.decoder is None:
1896 output = input
1897 else:
1898 output = self.decoder.decode(input, final=final)
1899 if self.pendingcr and (output or final):
1900 output = "\r" + output
1901 self.pendingcr = False
1902
1903 # retain last \r even when not translating data:
1904 # then readline() is sure to get \r\n in one pass
1905 if output.endswith("\r") and not final:
1906 output = output[:-1]
1907 self.pendingcr = True
1908
1909 # Record which newlines are read
1910 crlf = output.count('\r\n')
1911 cr = output.count('\r') - crlf
1912 lf = output.count('\n') - crlf
1913 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1914 | (crlf and self._CRLF)
1915
1916 if self.translate:
1917 if crlf:
1918 output = output.replace("\r\n", "\n")
1919 if cr:
1920 output = output.replace("\r", "\n")
1921
1922 return output
1923
1924 def getstate(self):
1925 if self.decoder is None:
1926 buf = b""
1927 flag = 0
1928 else:
1929 buf, flag = self.decoder.getstate()
1930 flag <<= 1
1931 if self.pendingcr:
1932 flag |= 1
1933 return buf, flag
1934
1935 def setstate(self, state):
1936 buf, flag = state
1937 self.pendingcr = bool(flag & 1)
1938 if self.decoder is not None:
1939 self.decoder.setstate((buf, flag >> 1))
1940
1941 def reset(self):
1942 self.seennl = 0
1943 self.pendingcr = False
1944 if self.decoder is not None:
1945 self.decoder.reset()
1946
1947 _LF = 1
1948 _CR = 2
1949 _CRLF = 4
1950
1951 @property
1952 def newlines(self):
1953 return (None,
1954 "\n",
1955 "\r",
1956 ("\r", "\n"),
1957 "\r\n",
1958 ("\n", "\r\n"),
1959 ("\r", "\r\n"),
1960 ("\r", "\n", "\r\n")
1961 )[self.seennl]
1962
1963
1964class TextIOWrapper(TextIOBase):
1965
1966 r"""Character and line based layer over a BufferedIOBase object, buffer.
1967
1968 encoding gives the name of the encoding that the stream will be
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001969 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001970
1971 errors determines the strictness of encoding and decoding (see the
1972 codecs.register) and defaults to "strict".
1973
1974 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1975 handling of line endings. If it is None, universal newlines is
1976 enabled. With this enabled, on input, the lines endings '\n', '\r',
1977 or '\r\n' are translated to '\n' before being returned to the
1978 caller. Conversely, on output, '\n' is translated to the system
Éric Araujo39242302011-11-03 00:08:48 +01001979 default line separator, os.linesep. If newline is any other of its
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001980 legal values, that newline becomes the newline when the file is read
1981 and it is returned untranslated. On output, '\n' is converted to the
1982 newline.
1983
1984 If line_buffering is True, a call to flush is implied when a call to
1985 write contains a newline character.
1986 """
1987
1988 _CHUNK_SIZE = 2048
1989
Victor Stinnera3568412019-05-28 01:44:21 +02001990 # Initialize _buffer as soon as possible since it's used by __del__()
1991 # which calls close()
1992 _buffer = None
1993
Andrew Svetlov4e9e9c12012-08-13 16:09:54 +03001994 # The write_through argument has no effect here since this
1995 # implementation always writes through. The argument is present only
1996 # so that the signature can match the signature of the C version.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001997 def __init__(self, buffer, encoding=None, errors=None, newline=None,
Antoine Pitroue96ec682011-07-23 21:46:35 +02001998 line_buffering=False, write_through=False):
INADA Naoki507434f2017-12-21 09:59:53 +09001999 self._check_newline(newline)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002000 if encoding is None:
2001 try:
2002 encoding = os.device_encoding(buffer.fileno())
2003 except (AttributeError, UnsupportedOperation):
2004 pass
2005 if encoding is None:
2006 try:
2007 import locale
Brett Cannoncd171c82013-07-04 17:43:24 -04002008 except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002009 # Importing locale may fail if Python is being built
2010 encoding = "ascii"
2011 else:
Victor Stinnerf86a5e82012-06-05 13:43:22 +02002012 encoding = locale.getpreferredencoding(False)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002013
2014 if not isinstance(encoding, str):
2015 raise ValueError("invalid encoding: %r" % encoding)
2016
Nick Coghlana9b15242014-02-04 22:11:18 +10002017 if not codecs.lookup(encoding)._is_text_encoding:
2018 msg = ("%r is not a text encoding; "
2019 "use codecs.open() to handle arbitrary codecs")
2020 raise LookupError(msg % encoding)
2021
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002022 if errors is None:
2023 errors = "strict"
2024 else:
2025 if not isinstance(errors, str):
2026 raise ValueError("invalid errors: %r" % errors)
Victor Stinner22eb6892019-06-26 00:51:05 +02002027 if _CHECK_ERRORS:
2028 codecs.lookup_error(errors)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002029
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002030 self._buffer = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002031 self._decoded_chars = '' # buffer for text returned from decoder
2032 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
2033 self._snapshot = None # info for reconstructing decoder state
2034 self._seekable = self._telling = self.buffer.seekable()
Antoine Pitroue96ec682011-07-23 21:46:35 +02002035 self._has_read1 = hasattr(self.buffer, 'read1')
INADA Naoki507434f2017-12-21 09:59:53 +09002036 self._configure(encoding, errors, newline,
2037 line_buffering, write_through)
2038
2039 def _check_newline(self, newline):
2040 if newline is not None and not isinstance(newline, str):
2041 raise TypeError("illegal newline type: %r" % (type(newline),))
2042 if newline not in (None, "", "\n", "\r", "\r\n"):
2043 raise ValueError("illegal newline value: %r" % (newline,))
2044
2045 def _configure(self, encoding=None, errors=None, newline=None,
2046 line_buffering=False, write_through=False):
2047 self._encoding = encoding
2048 self._errors = errors
2049 self._encoder = None
2050 self._decoder = None
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002051 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002052
INADA Naoki507434f2017-12-21 09:59:53 +09002053 self._readuniversal = not newline
2054 self._readtranslate = newline is None
2055 self._readnl = newline
2056 self._writetranslate = newline != ''
2057 self._writenl = newline or os.linesep
2058
2059 self._line_buffering = line_buffering
2060 self._write_through = write_through
2061
2062 # don't write a BOM in the middle of a file
Antoine Pitroue4501852009-05-14 18:55:55 +00002063 if self._seekable and self.writable():
2064 position = self.buffer.tell()
2065 if position != 0:
2066 try:
2067 self._get_encoder().setstate(0)
2068 except LookupError:
2069 # Sometimes the encoder doesn't exist
2070 pass
2071
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002072 # self._snapshot is either None, or a tuple (dec_flags, next_input)
2073 # where dec_flags is the second (integer) item of the decoder state
2074 # and next_input is the chunk of input bytes that comes next after the
2075 # snapshot point. We use this to reconstruct decoder states in tell().
2076
2077 # Naming convention:
2078 # - "bytes_..." for integer variables that count input bytes
2079 # - "chars_..." for integer variables that count decoded characters
2080
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002081 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +03002082 result = "<{}.{}".format(self.__class__.__module__,
2083 self.__class__.__qualname__)
Antoine Pitrou716c4442009-05-23 19:04:03 +00002084 try:
2085 name = self.name
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002086 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002087 pass
Antoine Pitrou716c4442009-05-23 19:04:03 +00002088 else:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002089 result += " name={0!r}".format(name)
2090 try:
2091 mode = self.mode
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002092 except Exception:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002093 pass
2094 else:
2095 result += " mode={0!r}".format(mode)
2096 return result + " encoding={0!r}>".format(self.encoding)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002097
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002098 @property
2099 def encoding(self):
2100 return self._encoding
2101
2102 @property
2103 def errors(self):
2104 return self._errors
2105
2106 @property
2107 def line_buffering(self):
2108 return self._line_buffering
2109
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002110 @property
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002111 def write_through(self):
2112 return self._write_through
2113
2114 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002115 def buffer(self):
2116 return self._buffer
2117
INADA Naoki507434f2017-12-21 09:59:53 +09002118 def reconfigure(self, *,
2119 encoding=None, errors=None, newline=Ellipsis,
2120 line_buffering=None, write_through=None):
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002121 """Reconfigure the text stream with new parameters.
2122
2123 This also flushes the stream.
2124 """
INADA Naoki507434f2017-12-21 09:59:53 +09002125 if (self._decoder is not None
2126 and (encoding is not None or errors is not None
2127 or newline is not Ellipsis)):
2128 raise UnsupportedOperation(
2129 "It is not possible to set the encoding or newline of stream "
2130 "after the first read")
2131
2132 if errors is None:
2133 if encoding is None:
2134 errors = self._errors
2135 else:
2136 errors = 'strict'
2137 elif not isinstance(errors, str):
2138 raise TypeError("invalid errors: %r" % errors)
2139
2140 if encoding is None:
2141 encoding = self._encoding
2142 else:
2143 if not isinstance(encoding, str):
2144 raise TypeError("invalid encoding: %r" % encoding)
2145
2146 if newline is Ellipsis:
2147 newline = self._readnl
2148 self._check_newline(newline)
2149
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002150 if line_buffering is None:
2151 line_buffering = self.line_buffering
2152 if write_through is None:
2153 write_through = self.write_through
INADA Naoki507434f2017-12-21 09:59:53 +09002154
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002155 self.flush()
INADA Naoki507434f2017-12-21 09:59:53 +09002156 self._configure(encoding, errors, newline,
2157 line_buffering, write_through)
Antoine Pitrou3c2817b2017-06-03 12:32:28 +02002158
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002159 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02002160 if self.closed:
2161 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002162 return self._seekable
2163
2164 def readable(self):
2165 return self.buffer.readable()
2166
2167 def writable(self):
2168 return self.buffer.writable()
2169
2170 def flush(self):
2171 self.buffer.flush()
2172 self._telling = self._seekable
2173
2174 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +00002175 if self.buffer is not None and not self.closed:
Benjamin Peterson68623612012-12-20 11:53:11 -06002176 try:
2177 self.flush()
2178 finally:
2179 self.buffer.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002180
2181 @property
2182 def closed(self):
2183 return self.buffer.closed
2184
2185 @property
2186 def name(self):
2187 return self.buffer.name
2188
2189 def fileno(self):
2190 return self.buffer.fileno()
2191
2192 def isatty(self):
2193 return self.buffer.isatty()
2194
Raymond Hettinger00fa0392011-01-13 02:52:26 +00002195 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00002196 'Write data, where s is a str'
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002197 if self.closed:
2198 raise ValueError("write to closed file")
2199 if not isinstance(s, str):
2200 raise TypeError("can't write %s to text stream" %
2201 s.__class__.__name__)
2202 length = len(s)
2203 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
2204 if haslf and self._writetranslate and self._writenl != "\n":
2205 s = s.replace("\n", self._writenl)
2206 encoder = self._encoder or self._get_encoder()
2207 # XXX What if we were just reading?
2208 b = encoder.encode(s)
2209 self.buffer.write(b)
2210 if self._line_buffering and (haslf or "\r" in s):
2211 self.flush()
Zackery Spytz23db9352018-06-29 04:14:58 -06002212 self._set_decoded_chars('')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002213 self._snapshot = None
2214 if self._decoder:
2215 self._decoder.reset()
2216 return length
2217
2218 def _get_encoder(self):
2219 make_encoder = codecs.getincrementalencoder(self._encoding)
2220 self._encoder = make_encoder(self._errors)
2221 return self._encoder
2222
2223 def _get_decoder(self):
2224 make_decoder = codecs.getincrementaldecoder(self._encoding)
2225 decoder = make_decoder(self._errors)
2226 if self._readuniversal:
2227 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
2228 self._decoder = decoder
2229 return decoder
2230
2231 # The following three methods implement an ADT for _decoded_chars.
2232 # Text returned from the decoder is buffered here until the client
2233 # requests it by calling our read() or readline() method.
2234 def _set_decoded_chars(self, chars):
2235 """Set the _decoded_chars buffer."""
2236 self._decoded_chars = chars
2237 self._decoded_chars_used = 0
2238
2239 def _get_decoded_chars(self, n=None):
2240 """Advance into the _decoded_chars buffer."""
2241 offset = self._decoded_chars_used
2242 if n is None:
2243 chars = self._decoded_chars[offset:]
2244 else:
2245 chars = self._decoded_chars[offset:offset + n]
2246 self._decoded_chars_used += len(chars)
2247 return chars
2248
2249 def _rewind_decoded_chars(self, n):
2250 """Rewind the _decoded_chars buffer."""
2251 if self._decoded_chars_used < n:
2252 raise AssertionError("rewind decoded_chars out of bounds")
2253 self._decoded_chars_used -= n
2254
2255 def _read_chunk(self):
2256 """
2257 Read and decode the next chunk of data from the BufferedReader.
2258 """
2259
2260 # The return value is True unless EOF was reached. The decoded
2261 # string is placed in self._decoded_chars (replacing its previous
2262 # value). The entire input chunk is sent to the decoder, though
2263 # some of it may remain buffered in the decoder, yet to be
2264 # converted.
2265
2266 if self._decoder is None:
2267 raise ValueError("no decoder")
2268
2269 if self._telling:
2270 # To prepare for tell(), we need to snapshot a point in the
2271 # file where the decoder's input buffer is empty.
2272
2273 dec_buffer, dec_flags = self._decoder.getstate()
2274 # Given this, we know there was a valid snapshot point
2275 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
2276
2277 # Read a chunk, decode it, and put the result in self._decoded_chars.
Antoine Pitroue96ec682011-07-23 21:46:35 +02002278 if self._has_read1:
2279 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
2280 else:
2281 input_chunk = self.buffer.read(self._CHUNK_SIZE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002282 eof = not input_chunk
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002283 decoded_chars = self._decoder.decode(input_chunk, eof)
2284 self._set_decoded_chars(decoded_chars)
2285 if decoded_chars:
2286 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
2287 else:
2288 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002289
2290 if self._telling:
2291 # At the snapshot point, len(dec_buffer) bytes before the read,
2292 # the next input to be decoded is dec_buffer + input_chunk.
2293 self._snapshot = (dec_flags, dec_buffer + input_chunk)
2294
2295 return not eof
2296
2297 def _pack_cookie(self, position, dec_flags=0,
2298 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
2299 # The meaning of a tell() cookie is: seek to position, set the
2300 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
2301 # into the decoder with need_eof as the EOF flag, then skip
2302 # chars_to_skip characters of the decoded result. For most simple
2303 # decoders, tell() will often just give a byte offset in the file.
2304 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
2305 (chars_to_skip<<192) | bool(need_eof)<<256)
2306
2307 def _unpack_cookie(self, bigint):
2308 rest, position = divmod(bigint, 1<<64)
2309 rest, dec_flags = divmod(rest, 1<<64)
2310 rest, bytes_to_feed = divmod(rest, 1<<64)
2311 need_eof, chars_to_skip = divmod(rest, 1<<64)
2312 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
2313
2314 def tell(self):
2315 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002316 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002317 if not self._telling:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002318 raise OSError("telling position disabled by next() call")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002319 self.flush()
2320 position = self.buffer.tell()
2321 decoder = self._decoder
2322 if decoder is None or self._snapshot is None:
2323 if self._decoded_chars:
2324 # This should never happen.
2325 raise AssertionError("pending decoded text")
2326 return position
2327
2328 # Skip backward to the snapshot point (see _read_chunk).
2329 dec_flags, next_input = self._snapshot
2330 position -= len(next_input)
2331
2332 # How many decoded characters have been used up since the snapshot?
2333 chars_to_skip = self._decoded_chars_used
2334 if chars_to_skip == 0:
2335 # We haven't moved from the snapshot point.
2336 return self._pack_cookie(position, dec_flags)
2337
2338 # Starting from the snapshot position, we will walk the decoder
2339 # forward until it gives us enough decoded characters.
2340 saved_state = decoder.getstate()
2341 try:
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002342 # Fast search for an acceptable start point, close to our
2343 # current pos.
2344 # Rationale: calling decoder.decode() has a large overhead
2345 # regardless of chunk size; we want the number of such calls to
Raymond Hettinger14010182018-09-13 21:17:40 -07002346 # be O(1) in most situations (common decoders, sensible input).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002347 # Actually, it will be exactly 1 for fixed-size codecs (all
2348 # 8-bit codecs, also UTF-16 and UTF-32).
2349 skip_bytes = int(self._b2cratio * chars_to_skip)
2350 skip_back = 1
2351 assert skip_bytes <= len(next_input)
2352 while skip_bytes > 0:
2353 decoder.setstate((b'', dec_flags))
2354 # Decode up to temptative start point
2355 n = len(decoder.decode(next_input[:skip_bytes]))
2356 if n <= chars_to_skip:
2357 b, d = decoder.getstate()
2358 if not b:
2359 # Before pos and no bytes buffered in decoder => OK
2360 dec_flags = d
2361 chars_to_skip -= n
2362 break
2363 # Skip back by buffered amount and reset heuristic
2364 skip_bytes -= len(b)
2365 skip_back = 1
2366 else:
2367 # We're too far ahead, skip back a bit
2368 skip_bytes -= skip_back
2369 skip_back = skip_back * 2
2370 else:
2371 skip_bytes = 0
2372 decoder.setstate((b'', dec_flags))
2373
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002374 # Note our initial start point.
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002375 start_pos = position + skip_bytes
2376 start_flags = dec_flags
2377 if chars_to_skip == 0:
2378 # We haven't moved from the start point.
2379 return self._pack_cookie(start_pos, start_flags)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002380
2381 # Feed the decoder one byte at a time. As we go, note the
2382 # nearest "safe start point" before the current location
2383 # (a point where the decoder has nothing buffered, so seek()
2384 # can safely start from there and advance to this location).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002385 bytes_fed = 0
2386 need_eof = 0
2387 # Chars decoded since `start_pos`
2388 chars_decoded = 0
2389 for i in range(skip_bytes, len(next_input)):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002390 bytes_fed += 1
Antoine Pitrou211b81d2011-02-25 20:27:33 +00002391 chars_decoded += len(decoder.decode(next_input[i:i+1]))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002392 dec_buffer, dec_flags = decoder.getstate()
2393 if not dec_buffer and chars_decoded <= chars_to_skip:
2394 # Decoder buffer is empty, so this is a safe start point.
2395 start_pos += bytes_fed
2396 chars_to_skip -= chars_decoded
2397 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
2398 if chars_decoded >= chars_to_skip:
2399 break
2400 else:
2401 # We didn't get enough decoded data; signal EOF to get more.
2402 chars_decoded += len(decoder.decode(b'', final=True))
2403 need_eof = 1
2404 if chars_decoded < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002405 raise OSError("can't reconstruct logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002406
2407 # The returned cookie corresponds to the last safe start point.
2408 return self._pack_cookie(
2409 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
2410 finally:
2411 decoder.setstate(saved_state)
2412
2413 def truncate(self, pos=None):
2414 self.flush()
2415 if pos is None:
2416 pos = self.tell()
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00002417 return self.buffer.truncate(pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002418
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002419 def detach(self):
2420 if self.buffer is None:
2421 raise ValueError("buffer is already detached")
2422 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002423 buffer = self._buffer
2424 self._buffer = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002425 return buffer
2426
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002427 def seek(self, cookie, whence=0):
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002428 def _reset_encoder(position):
2429 """Reset the encoder (merely useful for proper BOM handling)"""
2430 try:
2431 encoder = self._encoder or self._get_encoder()
2432 except LookupError:
2433 # Sometimes the encoder doesn't exist
2434 pass
2435 else:
2436 if position != 0:
2437 encoder.setstate(0)
2438 else:
2439 encoder.reset()
2440
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002441 if self.closed:
2442 raise ValueError("tell on closed file")
2443 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002444 raise UnsupportedOperation("underlying stream is not seekable")
ngie-eign848037c2019-03-02 23:28:26 -08002445 if whence == SEEK_CUR:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002446 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002447 raise UnsupportedOperation("can't do nonzero cur-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002448 # Seeking to the current position should attempt to
2449 # sync the underlying buffer with the current position.
2450 whence = 0
2451 cookie = self.tell()
ngie-eign848037c2019-03-02 23:28:26 -08002452 elif whence == SEEK_END:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002453 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002454 raise UnsupportedOperation("can't do nonzero end-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002455 self.flush()
ngie-eign848037c2019-03-02 23:28:26 -08002456 position = self.buffer.seek(0, whence)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002457 self._set_decoded_chars('')
2458 self._snapshot = None
2459 if self._decoder:
2460 self._decoder.reset()
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002461 _reset_encoder(position)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002462 return position
2463 if whence != 0:
Jesus Cea94363612012-06-22 18:32:07 +02002464 raise ValueError("unsupported whence (%r)" % (whence,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002465 if cookie < 0:
2466 raise ValueError("negative seek position %r" % (cookie,))
2467 self.flush()
2468
2469 # The strategy of seek() is to go back to the safe start point
2470 # and replay the effect of read(chars_to_skip) from there.
2471 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
2472 self._unpack_cookie(cookie)
2473
2474 # Seek back to the safe start point.
2475 self.buffer.seek(start_pos)
2476 self._set_decoded_chars('')
2477 self._snapshot = None
2478
2479 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00002480 if cookie == 0 and self._decoder:
2481 self._decoder.reset()
2482 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002483 self._decoder = self._decoder or self._get_decoder()
2484 self._decoder.setstate((b'', dec_flags))
2485 self._snapshot = (dec_flags, b'')
2486
2487 if chars_to_skip:
2488 # Just like _read_chunk, feed the decoder and save a snapshot.
2489 input_chunk = self.buffer.read(bytes_to_feed)
2490 self._set_decoded_chars(
2491 self._decoder.decode(input_chunk, need_eof))
2492 self._snapshot = (dec_flags, input_chunk)
2493
2494 # Skip chars_to_skip of the decoded characters.
2495 if len(self._decoded_chars) < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002496 raise OSError("can't restore logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002497 self._decoded_chars_used = chars_to_skip
2498
Antoine Pitrou85e3ee72015-04-13 20:01:21 +02002499 _reset_encoder(cookie)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002500 return cookie
2501
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002502 def read(self, size=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00002503 self._checkReadable()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002504 if size is None:
2505 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002506 else:
2507 try:
2508 size_index = size.__index__
2509 except AttributeError:
2510 raise TypeError(f"{size!r} is not an integer")
2511 else:
2512 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002513 decoder = self._decoder or self._get_decoder()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002514 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002515 # Read everything.
2516 result = (self._get_decoded_chars() +
2517 decoder.decode(self.buffer.read(), final=True))
2518 self._set_decoded_chars('')
2519 self._snapshot = None
2520 return result
2521 else:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002522 # Keep reading chunks until we have size characters to return.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002523 eof = False
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002524 result = self._get_decoded_chars(size)
2525 while len(result) < size and not eof:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002526 eof = not self._read_chunk()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002527 result += self._get_decoded_chars(size - len(result))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002528 return result
2529
2530 def __next__(self):
2531 self._telling = False
2532 line = self.readline()
2533 if not line:
2534 self._snapshot = None
2535 self._telling = self._seekable
2536 raise StopIteration
2537 return line
2538
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002539 def readline(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002540 if self.closed:
2541 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002542 if size is None:
2543 size = -1
Oren Milmande503602017-08-24 21:33:42 +03002544 else:
2545 try:
2546 size_index = size.__index__
2547 except AttributeError:
2548 raise TypeError(f"{size!r} is not an integer")
2549 else:
2550 size = size_index()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002551
2552 # Grab all the decoded text (we will rewind any extra bits later).
2553 line = self._get_decoded_chars()
2554
2555 start = 0
2556 # Make the decoder if it doesn't already exist.
2557 if not self._decoder:
2558 self._get_decoder()
2559
2560 pos = endpos = None
2561 while True:
2562 if self._readtranslate:
2563 # Newlines are already translated, only search for \n
2564 pos = line.find('\n', start)
2565 if pos >= 0:
2566 endpos = pos + 1
2567 break
2568 else:
2569 start = len(line)
2570
2571 elif self._readuniversal:
2572 # Universal newline search. Find any of \r, \r\n, \n
2573 # The decoder ensures that \r\n are not split in two pieces
2574
2575 # In C we'd look for these in parallel of course.
2576 nlpos = line.find("\n", start)
2577 crpos = line.find("\r", start)
2578 if crpos == -1:
2579 if nlpos == -1:
2580 # Nothing found
2581 start = len(line)
2582 else:
2583 # Found \n
2584 endpos = nlpos + 1
2585 break
2586 elif nlpos == -1:
2587 # Found lone \r
2588 endpos = crpos + 1
2589 break
2590 elif nlpos < crpos:
2591 # Found \n
2592 endpos = nlpos + 1
2593 break
2594 elif nlpos == crpos + 1:
2595 # Found \r\n
2596 endpos = crpos + 2
2597 break
2598 else:
2599 # Found \r
2600 endpos = crpos + 1
2601 break
2602 else:
2603 # non-universal
2604 pos = line.find(self._readnl)
2605 if pos >= 0:
2606 endpos = pos + len(self._readnl)
2607 break
2608
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002609 if size >= 0 and len(line) >= size:
2610 endpos = size # reached length size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002611 break
2612
2613 # No line ending seen yet - get more data'
2614 while self._read_chunk():
2615 if self._decoded_chars:
2616 break
2617 if self._decoded_chars:
2618 line += self._get_decoded_chars()
2619 else:
2620 # end of file
2621 self._set_decoded_chars('')
2622 self._snapshot = None
2623 return line
2624
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002625 if size >= 0 and endpos > size:
2626 endpos = size # don't exceed size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002627
2628 # Rewind _decoded_chars to just after the line ending we found.
2629 self._rewind_decoded_chars(len(line) - endpos)
2630 return line[:endpos]
2631
2632 @property
2633 def newlines(self):
2634 return self._decoder.newlines if self._decoder else None
2635
2636
2637class StringIO(TextIOWrapper):
2638 """Text I/O implementation using an in-memory buffer.
2639
2640 The initial_value argument sets the value of object. The newline
2641 argument is like the one of TextIOWrapper's constructor.
2642 """
2643
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002644 def __init__(self, initial_value="", newline="\n"):
2645 super(StringIO, self).__init__(BytesIO(),
2646 encoding="utf-8",
Serhiy Storchakac92ea762014-01-29 11:33:26 +02002647 errors="surrogatepass",
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002648 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00002649 # Issue #5645: make universal newlines semantics the same as in the
2650 # C version, even under Windows.
2651 if newline is None:
2652 self._writetranslate = False
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002653 if initial_value is not None:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002654 if not isinstance(initial_value, str):
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002655 raise TypeError("initial_value must be str or None, not {0}"
2656 .format(type(initial_value).__name__))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002657 self.write(initial_value)
2658 self.seek(0)
2659
2660 def getvalue(self):
2661 self.flush()
Antoine Pitrou57839a62014-02-02 23:37:29 +01002662 decoder = self._decoder or self._get_decoder()
2663 old_state = decoder.getstate()
2664 decoder.reset()
2665 try:
2666 return decoder.decode(self.buffer.getvalue(), final=True)
2667 finally:
2668 decoder.setstate(old_state)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002669
2670 def __repr__(self):
2671 # TextIOWrapper tells the encoding in its repr. In StringIO,
Martin Panter7462b6492015-11-02 03:37:02 +00002672 # that's an implementation detail.
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002673 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00002674
2675 @property
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002676 def errors(self):
2677 return None
2678
2679 @property
Benjamin Petersonb487e632009-03-21 03:08:31 +00002680 def encoding(self):
2681 return None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002682
2683 def detach(self):
2684 # This doesn't make sense on StringIO.
2685 self._unsupported("detach")