blob: d6eee79e016b3c28c21100e6566682fcb82ccf05 [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
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00009# Import _thread instead of threading to reduce startup cost
10try:
11 from _thread import allocate_lock as Lock
Brett Cannoncd171c82013-07-04 17:43:24 -040012except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000013 from _dummy_thread import allocate_lock as Lock
14
15import io
Benjamin Petersonc3be11a2010-04-27 21:24:03 +000016from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000017
Jesus Cea94363612012-06-22 18:32:07 +020018valid_seek_flags = {0, 1, 2} # Hardwired values
19if hasattr(os, 'SEEK_HOLE') :
20 valid_seek_flags.add(os.SEEK_HOLE)
21 valid_seek_flags.add(os.SEEK_DATA)
22
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000023# open() uses st_blksize whenever we can
24DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
25
26# NOTE: Base classes defined here are registered with the "official" ABCs
27# defined in io.py. We don't use real inheritance though, because we don't
28# want to inherit the C implementations.
29
Antoine Pitrou6b4883d2011-10-12 02:54:14 +020030# Rebind for compatibility
31BlockingIOError = BlockingIOError
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000032
33
Georg Brandl4d73b572011-01-13 07:13:06 +000034def open(file, mode="r", buffering=-1, encoding=None, errors=None,
Ross Lagerwall59142db2011-10-31 20:34:46 +020035 newline=None, closefd=True, opener=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000036
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020037 r"""Open file and return a stream. Raise OSError upon failure.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000038
39 file is either a text or byte string giving the name (and the path
40 if the file isn't in the current working directory) of the file to
41 be opened or an integer file descriptor of the file to be
42 wrapped. (If a file descriptor is given, it is closed when the
43 returned I/O object is closed, unless closefd is set to False.)
44
Charles-François Natalidc3044c2012-01-09 22:40:02 +010045 mode is an optional string that specifies the mode in which the file is
46 opened. It defaults to 'r' which means open for reading in text mode. Other
47 common values are 'w' for writing (truncating the file if it already
Charles-François Natalid612de12012-01-14 11:51:00 +010048 exists), 'x' for exclusive creation of a new file, and 'a' for appending
Charles-François Natalidc3044c2012-01-09 22:40:02 +010049 (which on some Unix systems, means that all writes append to the end of the
50 file regardless of the current seek position). In text mode, if encoding is
51 not specified the encoding used is platform dependent. (For reading and
52 writing raw bytes use binary mode and leave encoding unspecified.) The
53 available modes are:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000054
55 ========= ===============================================================
56 Character Meaning
57 --------- ---------------------------------------------------------------
58 'r' open for reading (default)
59 'w' open for writing, truncating the file first
Charles-François Natalidc3044c2012-01-09 22:40:02 +010060 'x' create a new file and open it for writing
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000061 'a' open for writing, appending to the end of the file if it exists
62 'b' binary mode
63 't' text mode (default)
64 '+' open a disk file for updating (reading and writing)
Serhiy Storchaka6787a382013-11-23 22:12:06 +020065 'U' universal newline mode (deprecated)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000066 ========= ===============================================================
67
68 The default mode is 'rt' (open for reading text). For binary random
69 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
Charles-François Natalidc3044c2012-01-09 22:40:02 +010070 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
71 raises an `FileExistsError` if the file already exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000072
73 Python distinguishes between files opened in binary and text modes,
74 even when the underlying operating system doesn't. Files opened in
75 binary mode (appending 'b' to the mode argument) return contents as
76 bytes objects without any decoding. In text mode (the default, or when
77 't' is appended to the mode argument), the contents of the file are
78 returned as strings, the bytes having been first decoded using a
79 platform-dependent encoding or using the specified encoding if given.
80
Serhiy Storchaka6787a382013-11-23 22:12:06 +020081 'U' mode is deprecated and will raise an exception in future versions
82 of Python. It has no effect in Python 3. Use newline to control
83 universal newlines mode.
84
Antoine Pitroud5587bc2009-12-19 21:08:31 +000085 buffering is an optional integer used to set the buffering policy.
86 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
87 line buffering (only usable in text mode), and an integer > 1 to indicate
88 the size of a fixed-size chunk buffer. When no buffering argument is
89 given, the default buffering policy works as follows:
90
91 * Binary files are buffered in fixed-size chunks; the size of the buffer
92 is chosen using a heuristic trying to determine the underlying device's
93 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
94 On many systems, the buffer will typically be 4096 or 8192 bytes long.
95
96 * "Interactive" text files (files for which isatty() returns True)
97 use line buffering. Other text files use the policy described above
98 for binary files.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000099
Raymond Hettingercbb80892011-01-13 18:15:51 +0000100 encoding is the str name of the encoding used to decode or encode the
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000101 file. This should only be used in text mode. The default encoding is
102 platform dependent, but any encoding supported by Python can be
103 passed. See the codecs module for the list of supported encodings.
104
105 errors is an optional string that specifies how encoding errors are to
106 be handled---this argument should not be used in binary mode. Pass
107 'strict' to raise a ValueError exception if there is an encoding error
108 (the default of None has the same effect), or pass 'ignore' to ignore
109 errors. (Note that ignoring encoding errors can lead to data loss.)
110 See the documentation for codecs.register for a list of the permitted
111 encoding error strings.
112
Raymond Hettingercbb80892011-01-13 18:15:51 +0000113 newline is a string controlling how universal newlines works (it only
114 applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
115 as follows:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116
117 * On input, if newline is None, universal newlines mode is
118 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
119 these are translated into '\n' before being returned to the
120 caller. If it is '', universal newline mode is enabled, but line
121 endings are returned to the caller untranslated. If it has any of
122 the other legal values, input lines are only terminated by the given
123 string, and the line ending is returned to the caller untranslated.
124
125 * On output, if newline is None, any '\n' characters written are
126 translated to the system default line separator, os.linesep. If
127 newline is '', no translation takes place. If newline is any of the
128 other legal values, any '\n' characters written are translated to
129 the given string.
130
Raymond Hettingercbb80892011-01-13 18:15:51 +0000131 closedfd is a bool. If closefd is False, the underlying file descriptor will
132 be kept open when the file is closed. This does not work when a file name is
133 given and must be True in that case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000134
Victor Stinnerdaf45552013-08-28 00:53:59 +0200135 The newly created file is non-inheritable.
136
Ross Lagerwall59142db2011-10-31 20:34:46 +0200137 A custom opener can be used by passing a callable as *opener*. The
138 underlying file descriptor for the file object is then obtained by calling
139 *opener* with (*file*, *flags*). *opener* must return an open file
140 descriptor (passing os.open as *opener* results in functionality similar to
141 passing None).
142
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000143 open() returns a file object whose type depends on the mode, and
144 through which the standard file operations such as reading and writing
145 are performed. When open() is used to open a file in a text mode ('w',
146 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
147 a file in a binary mode, the returned class varies: in read binary
148 mode, it returns a BufferedReader; in write binary and append binary
149 modes, it returns a BufferedWriter, and in read/write mode, it returns
150 a BufferedRandom.
151
152 It is also possible to use a string or bytearray as a file for both
153 reading and writing. For strings StringIO can be used like a file
154 opened in a text mode, and for bytes a BytesIO can be used like a file
155 opened in a binary mode.
156 """
157 if not isinstance(file, (str, bytes, int)):
158 raise TypeError("invalid file: %r" % file)
159 if not isinstance(mode, str):
160 raise TypeError("invalid mode: %r" % mode)
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000161 if not isinstance(buffering, int):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000162 raise TypeError("invalid buffering: %r" % buffering)
163 if encoding is not None and not isinstance(encoding, str):
164 raise TypeError("invalid encoding: %r" % encoding)
165 if errors is not None and not isinstance(errors, str):
166 raise TypeError("invalid errors: %r" % errors)
167 modes = set(mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100168 if modes - set("axrwb+tU") or len(mode) > len(modes):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000169 raise ValueError("invalid mode: %r" % mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100170 creating = "x" in modes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000171 reading = "r" in modes
172 writing = "w" in modes
173 appending = "a" in modes
174 updating = "+" in modes
175 text = "t" in modes
176 binary = "b" in modes
177 if "U" in modes:
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100178 if creating or writing or appending:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000179 raise ValueError("can't use U and writing mode at once")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200180 import warnings
181 warnings.warn("'U' mode is deprecated",
182 DeprecationWarning, 2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000183 reading = True
184 if text and binary:
185 raise ValueError("can't have text and binary mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100186 if creating + reading + writing + appending > 1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000187 raise ValueError("can't have read/write/append mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100188 if not (creating or reading or writing or appending):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000189 raise ValueError("must have exactly one of read/write/append mode")
190 if binary and encoding is not None:
191 raise ValueError("binary mode doesn't take an encoding argument")
192 if binary and errors is not None:
193 raise ValueError("binary mode doesn't take an errors argument")
194 if binary and newline is not None:
195 raise ValueError("binary mode doesn't take a newline argument")
196 raw = FileIO(file,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100197 (creating and "x" or "") +
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000198 (reading and "r" or "") +
199 (writing and "w" or "") +
200 (appending and "a" or "") +
201 (updating and "+" or ""),
Ross Lagerwall59142db2011-10-31 20:34:46 +0200202 closefd, opener=opener)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000203 line_buffering = False
204 if buffering == 1 or buffering < 0 and raw.isatty():
205 buffering = -1
206 line_buffering = True
207 if buffering < 0:
208 buffering = DEFAULT_BUFFER_SIZE
209 try:
210 bs = os.fstat(raw.fileno()).st_blksize
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200211 except (OSError, AttributeError):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000212 pass
213 else:
214 if bs > 1:
215 buffering = bs
216 if buffering < 0:
217 raise ValueError("invalid buffering size")
218 if buffering == 0:
219 if binary:
220 return raw
221 raise ValueError("can't have unbuffered text I/O")
222 if updating:
223 buffer = BufferedRandom(raw, buffering)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100224 elif creating or writing or appending:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000225 buffer = BufferedWriter(raw, buffering)
226 elif reading:
227 buffer = BufferedReader(raw, buffering)
228 else:
229 raise ValueError("unknown mode: %r" % mode)
230 if binary:
231 return buffer
232 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
233 text.mode = mode
234 return text
235
236
237class DocDescriptor:
238 """Helper for builtins.open.__doc__
239 """
240 def __get__(self, obj, typ):
241 return (
Benjamin Petersonc3be11a2010-04-27 21:24:03 +0000242 "open(file, mode='r', buffering=-1, encoding=None, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000243 "errors=None, newline=None, closefd=True)\n\n" +
244 open.__doc__)
245
246class OpenWrapper:
247 """Wrapper for builtins.open
248
249 Trick so that open won't become a bound method when stored
250 as a class variable (as dbm.dumb does).
251
252 See initstdio() in Python/pythonrun.c.
253 """
254 __doc__ = DocDescriptor()
255
256 def __new__(cls, *args, **kwargs):
257 return open(*args, **kwargs)
258
259
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000260# In normal operation, both `UnsupportedOperation`s should be bound to the
261# same object.
262try:
263 UnsupportedOperation = io.UnsupportedOperation
264except AttributeError:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200265 class UnsupportedOperation(ValueError, OSError):
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000266 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000267
268
269class IOBase(metaclass=abc.ABCMeta):
270
271 """The abstract base class for all I/O classes, acting on streams of
272 bytes. There is no public constructor.
273
274 This class provides dummy implementations for many methods that
275 derived classes can override selectively; the default implementations
276 represent a file that cannot be read, written or seeked.
277
278 Even though IOBase does not declare read, readinto, or write because
279 their signatures will vary, implementations and clients should
280 consider those methods part of the interface. Also, implementations
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000281 may raise UnsupportedOperation when operations they do not support are
282 called.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000283
284 The basic type used for binary data read from or written to a file is
285 bytes. bytearrays are accepted too, and in some cases (such as
286 readinto) needed. Text I/O classes work with str data.
287
288 Note that calling any method (even inquiries) on a closed stream is
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200289 undefined. Implementations may raise OSError in this case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000290
291 IOBase (and its subclasses) support the iterator protocol, meaning
292 that an IOBase object can be iterated over yielding the lines in a
293 stream.
294
295 IOBase also supports the :keyword:`with` statement. In this example,
296 fp is closed after the suite of the with statement is complete:
297
298 with open('spam.txt', 'r') as fp:
299 fp.write('Spam and eggs!')
300 """
301
302 ### Internal ###
303
Raymond Hettinger3c940242011-01-12 23:39:31 +0000304 def _unsupported(self, name):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200305 """Internal: raise an OSError exception for unsupported operations."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000306 raise UnsupportedOperation("%s.%s() not supported" %
307 (self.__class__.__name__, name))
308
309 ### Positioning ###
310
Georg Brandl4d73b572011-01-13 07:13:06 +0000311 def seek(self, pos, whence=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000312 """Change stream position.
313
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400314 Change the stream position to byte offset pos. Argument pos is
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000315 interpreted relative to the position indicated by whence. Values
Raymond Hettingercbb80892011-01-13 18:15:51 +0000316 for whence are ints:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000317
318 * 0 -- start of stream (the default); offset should be zero or positive
319 * 1 -- current stream position; offset may be negative
320 * 2 -- end of stream; offset is usually negative
Jesus Cea94363612012-06-22 18:32:07 +0200321 Some operating systems / file systems could provide additional values.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000322
Raymond Hettingercbb80892011-01-13 18:15:51 +0000323 Return an int indicating the new absolute position.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000324 """
325 self._unsupported("seek")
326
Raymond Hettinger3c940242011-01-12 23:39:31 +0000327 def tell(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000328 """Return an int indicating the current stream position."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000329 return self.seek(0, 1)
330
Georg Brandl4d73b572011-01-13 07:13:06 +0000331 def truncate(self, pos=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000332 """Truncate file to size bytes.
333
334 Size defaults to the current IO position as reported by tell(). Return
335 the new size.
336 """
337 self._unsupported("truncate")
338
339 ### Flush and close ###
340
Raymond Hettinger3c940242011-01-12 23:39:31 +0000341 def flush(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000342 """Flush write buffers, if applicable.
343
344 This is not implemented for read-only and non-blocking streams.
345 """
Antoine Pitrou6be88762010-05-03 16:48:20 +0000346 self._checkClosed()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000347 # XXX Should this return the number of bytes written???
348
349 __closed = False
350
Raymond Hettinger3c940242011-01-12 23:39:31 +0000351 def close(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000352 """Flush and close the IO object.
353
354 This method has no effect if the file is already closed.
355 """
356 if not self.__closed:
Benjamin Peterson68623612012-12-20 11:53:11 -0600357 try:
358 self.flush()
359 finally:
360 self.__closed = True
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000361
Raymond Hettinger3c940242011-01-12 23:39:31 +0000362 def __del__(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000363 """Destructor. Calls close()."""
364 # The try/except block is in case this is called at program
365 # exit time, when it's possible that globals have already been
366 # deleted, and then the close() call might fail. Since
367 # there's nothing we can do about such failures and they annoy
368 # the end users, we suppress the traceback.
369 try:
370 self.close()
371 except:
372 pass
373
374 ### Inquiries ###
375
Raymond Hettinger3c940242011-01-12 23:39:31 +0000376 def seekable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000377 """Return a bool indicating whether object supports random access.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000378
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000379 If False, seek(), tell() and truncate() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000380 This method may need to do a test seek().
381 """
382 return False
383
384 def _checkSeekable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000385 """Internal: raise UnsupportedOperation if file is not seekable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000386 """
387 if not self.seekable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000388 raise UnsupportedOperation("File or stream is not seekable."
389 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000390
Raymond Hettinger3c940242011-01-12 23:39:31 +0000391 def readable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000392 """Return a bool indicating whether object was opened for reading.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000393
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000394 If False, read() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000395 """
396 return False
397
398 def _checkReadable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000399 """Internal: raise UnsupportedOperation if file is not readable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000400 """
401 if not self.readable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000402 raise UnsupportedOperation("File or stream is not readable."
403 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000404
Raymond Hettinger3c940242011-01-12 23:39:31 +0000405 def writable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000406 """Return a bool indicating whether object was opened for writing.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000407
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000408 If False, write() and truncate() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000409 """
410 return False
411
412 def _checkWritable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000413 """Internal: raise UnsupportedOperation if file is not writable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000414 """
415 if not self.writable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000416 raise UnsupportedOperation("File or stream is not writable."
417 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000418
419 @property
420 def closed(self):
421 """closed: bool. True iff the file has been closed.
422
423 For backwards compatibility, this is a property, not a predicate.
424 """
425 return self.__closed
426
427 def _checkClosed(self, msg=None):
428 """Internal: raise an ValueError if file is closed
429 """
430 if self.closed:
431 raise ValueError("I/O operation on closed file."
432 if msg is None else msg)
433
434 ### Context manager ###
435
Raymond Hettinger3c940242011-01-12 23:39:31 +0000436 def __enter__(self): # That's a forward reference
Raymond Hettingercbb80892011-01-13 18:15:51 +0000437 """Context management protocol. Returns self (an instance of IOBase)."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000438 self._checkClosed()
439 return self
440
Raymond Hettinger3c940242011-01-12 23:39:31 +0000441 def __exit__(self, *args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000442 """Context management protocol. Calls close()"""
443 self.close()
444
445 ### Lower-level APIs ###
446
447 # XXX Should these be present even if unimplemented?
448
Raymond Hettinger3c940242011-01-12 23:39:31 +0000449 def fileno(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000450 """Returns underlying file descriptor (an int) if one exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000451
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200452 An OSError is raised if the IO object does not use a file descriptor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000453 """
454 self._unsupported("fileno")
455
Raymond Hettinger3c940242011-01-12 23:39:31 +0000456 def isatty(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000457 """Return a bool indicating whether this is an 'interactive' stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000458
459 Return False if it can't be determined.
460 """
461 self._checkClosed()
462 return False
463
464 ### Readline[s] and writelines ###
465
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300466 def readline(self, size=-1):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000467 r"""Read and return a line of bytes from the stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000468
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300469 If size is specified, at most size bytes will be read.
470 Size should be an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000471
472 The line terminator is always b'\n' for binary files; for text
473 files, the newlines argument to open can be used to select the line
474 terminator(s) recognized.
475 """
476 # For backwards compatibility, a (slowish) readline().
477 if hasattr(self, "peek"):
478 def nreadahead():
479 readahead = self.peek(1)
480 if not readahead:
481 return 1
482 n = (readahead.find(b"\n") + 1) or len(readahead)
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300483 if size >= 0:
484 n = min(n, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000485 return n
486 else:
487 def nreadahead():
488 return 1
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300489 if size is None:
490 size = -1
491 elif not isinstance(size, int):
492 raise TypeError("size must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000493 res = bytearray()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300494 while size < 0 or len(res) < size:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000495 b = self.read(nreadahead())
496 if not b:
497 break
498 res += b
499 if res.endswith(b"\n"):
500 break
501 return bytes(res)
502
503 def __iter__(self):
504 self._checkClosed()
505 return self
506
507 def __next__(self):
508 line = self.readline()
509 if not line:
510 raise StopIteration
511 return line
512
513 def readlines(self, hint=None):
514 """Return a list of lines from the stream.
515
516 hint can be specified to control the number of lines read: no more
517 lines will be read if the total size (in bytes/characters) of all
518 lines so far exceeds hint.
519 """
520 if hint is None or hint <= 0:
521 return list(self)
522 n = 0
523 lines = []
524 for line in self:
525 lines.append(line)
526 n += len(line)
527 if n >= hint:
528 break
529 return lines
530
531 def writelines(self, lines):
532 self._checkClosed()
533 for line in lines:
534 self.write(line)
535
536io.IOBase.register(IOBase)
537
538
539class RawIOBase(IOBase):
540
541 """Base class for raw binary I/O."""
542
543 # The read() method is implemented by calling readinto(); derived
544 # classes that want to support read() only need to implement
545 # readinto() as a primitive operation. In general, readinto() can be
546 # more efficient than read().
547
548 # (It would be tempting to also provide an implementation of
549 # readinto() in terms of read(), in case the latter is a more suitable
550 # primitive operation, but that would lead to nasty recursion in case
551 # a subclass doesn't implement either.)
552
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300553 def read(self, size=-1):
554 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000555
556 Returns an empty bytes object on EOF, or None if the object is
557 set not to block and has no data to read.
558 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300559 if size is None:
560 size = -1
561 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000562 return self.readall()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300563 b = bytearray(size.__index__())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000564 n = self.readinto(b)
Antoine Pitrou328ec742010-09-14 18:37:24 +0000565 if n is None:
566 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000567 del b[n:]
568 return bytes(b)
569
570 def readall(self):
571 """Read until EOF, using multiple read() call."""
572 res = bytearray()
573 while True:
574 data = self.read(DEFAULT_BUFFER_SIZE)
575 if not data:
576 break
577 res += data
Victor Stinnera80987f2011-05-25 22:47:16 +0200578 if res:
579 return bytes(res)
580 else:
581 # b'' or None
582 return data
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000583
Raymond Hettinger3c940242011-01-12 23:39:31 +0000584 def readinto(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000585 """Read up to len(b) bytes into bytearray b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000586
Raymond Hettingercbb80892011-01-13 18:15:51 +0000587 Returns an int representing the number of bytes read (0 for EOF), or
588 None if the object is set not to block and has no data to read.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000589 """
590 self._unsupported("readinto")
591
Raymond Hettinger3c940242011-01-12 23:39:31 +0000592 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000593 """Write the given buffer to the IO stream.
594
595 Returns the number of bytes written, which may be less than len(b).
596 """
597 self._unsupported("write")
598
599io.RawIOBase.register(RawIOBase)
600from _io import FileIO
601RawIOBase.register(FileIO)
602
603
604class BufferedIOBase(IOBase):
605
606 """Base class for buffered IO objects.
607
608 The main difference with RawIOBase is that the read() method
609 supports omitting the size argument, and does not have a default
610 implementation that defers to readinto().
611
612 In addition, read(), readinto() and write() may raise
613 BlockingIOError if the underlying raw stream is in non-blocking
614 mode and not ready; unlike their raw counterparts, they will never
615 return None.
616
617 A typical implementation should not inherit from a RawIOBase
618 implementation, but wrap one.
619 """
620
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300621 def read(self, size=None):
622 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000623
624 If the argument is omitted, None, or negative, reads and
625 returns all data until EOF.
626
627 If the argument is positive, and the underlying raw stream is
628 not 'interactive', multiple raw reads may be issued to satisfy
629 the byte count (unless EOF is reached first). But for
630 interactive raw streams (XXX and for pipes?), at most one raw
631 read will be issued, and a short result does not imply that
632 EOF is imminent.
633
634 Returns an empty bytes array on EOF.
635
636 Raises BlockingIOError if the underlying raw stream has no
637 data at the moment.
638 """
639 self._unsupported("read")
640
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300641 def read1(self, size=None):
642 """Read up to size bytes with at most one read() system call,
643 where size is an int.
Raymond Hettingercbb80892011-01-13 18:15:51 +0000644 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000645 self._unsupported("read1")
646
Raymond Hettinger3c940242011-01-12 23:39:31 +0000647 def readinto(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000648 """Read up to len(b) bytes into bytearray b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000649
650 Like read(), this may issue multiple reads to the underlying raw
651 stream, unless the latter is 'interactive'.
652
Raymond Hettingercbb80892011-01-13 18:15:51 +0000653 Returns an int representing the number of bytes read (0 for EOF).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000654
655 Raises BlockingIOError if the underlying raw stream has no
656 data at the moment.
657 """
658 # XXX This ought to work with anything that supports the buffer API
659 data = self.read(len(b))
660 n = len(data)
661 try:
662 b[:n] = data
663 except TypeError as err:
664 import array
665 if not isinstance(b, array.array):
666 raise err
667 b[:n] = array.array('b', data)
668 return n
669
Raymond Hettinger3c940242011-01-12 23:39:31 +0000670 def write(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000671 """Write the given bytes buffer to the IO stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000672
673 Return the number of bytes written, which is never less than
674 len(b).
675
676 Raises BlockingIOError if the buffer is full and the
677 underlying raw stream cannot accept more data at the moment.
678 """
679 self._unsupported("write")
680
Raymond Hettinger3c940242011-01-12 23:39:31 +0000681 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000682 """
683 Separate the underlying raw stream from the buffer and return it.
684
685 After the raw stream has been detached, the buffer is in an unusable
686 state.
687 """
688 self._unsupported("detach")
689
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000690io.BufferedIOBase.register(BufferedIOBase)
691
692
693class _BufferedIOMixin(BufferedIOBase):
694
695 """A mixin implementation of BufferedIOBase with an underlying raw stream.
696
697 This passes most requests on to the underlying raw stream. It
698 does *not* provide implementations of read(), readinto() or
699 write().
700 """
701
702 def __init__(self, raw):
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000703 self._raw = raw
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000704
705 ### Positioning ###
706
707 def seek(self, pos, whence=0):
708 new_position = self.raw.seek(pos, whence)
709 if new_position < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200710 raise OSError("seek() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000711 return new_position
712
713 def tell(self):
714 pos = self.raw.tell()
715 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200716 raise OSError("tell() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000717 return pos
718
719 def truncate(self, pos=None):
720 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
721 # and a flush may be necessary to synch both views of the current
722 # file state.
723 self.flush()
724
725 if pos is None:
726 pos = self.tell()
727 # XXX: Should seek() be used, instead of passing the position
728 # XXX directly to truncate?
729 return self.raw.truncate(pos)
730
731 ### Flush and close ###
732
733 def flush(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000734 if self.closed:
735 raise ValueError("flush of closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000736 self.raw.flush()
737
738 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000739 if self.raw is not None and not self.closed:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100740 try:
741 # may raise BlockingIOError or BrokenPipeError etc
742 self.flush()
743 finally:
744 self.raw.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000745
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000746 def detach(self):
747 if self.raw is None:
748 raise ValueError("raw stream already detached")
749 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000750 raw = self._raw
751 self._raw = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000752 return raw
753
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000754 ### Inquiries ###
755
756 def seekable(self):
757 return self.raw.seekable()
758
759 def readable(self):
760 return self.raw.readable()
761
762 def writable(self):
763 return self.raw.writable()
764
765 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000766 def raw(self):
767 return self._raw
768
769 @property
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000770 def closed(self):
771 return self.raw.closed
772
773 @property
774 def name(self):
775 return self.raw.name
776
777 @property
778 def mode(self):
779 return self.raw.mode
780
Antoine Pitrou243757e2010-11-05 21:15:39 +0000781 def __getstate__(self):
782 raise TypeError("can not serialize a '{0}' object"
783 .format(self.__class__.__name__))
784
Antoine Pitrou716c4442009-05-23 19:04:03 +0000785 def __repr__(self):
786 clsname = self.__class__.__name__
787 try:
788 name = self.name
789 except AttributeError:
790 return "<_pyio.{0}>".format(clsname)
791 else:
792 return "<_pyio.{0} name={1!r}>".format(clsname, name)
793
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000794 ### Lower-level APIs ###
795
796 def fileno(self):
797 return self.raw.fileno()
798
799 def isatty(self):
800 return self.raw.isatty()
801
802
803class BytesIO(BufferedIOBase):
804
805 """Buffered I/O implementation using an in-memory bytes buffer."""
806
807 def __init__(self, initial_bytes=None):
808 buf = bytearray()
809 if initial_bytes is not None:
810 buf += initial_bytes
811 self._buffer = buf
812 self._pos = 0
813
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000814 def __getstate__(self):
815 if self.closed:
816 raise ValueError("__getstate__ on closed file")
817 return self.__dict__.copy()
818
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000819 def getvalue(self):
820 """Return the bytes value (contents) of the buffer
821 """
822 if self.closed:
823 raise ValueError("getvalue on closed file")
824 return bytes(self._buffer)
825
Antoine Pitrou972ee132010-09-06 18:48:21 +0000826 def getbuffer(self):
827 """Return a readable and writable view of the buffer.
828 """
829 return memoryview(self._buffer)
830
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300831 def read(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000832 if self.closed:
833 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300834 if size is None:
835 size = -1
836 if size < 0:
837 size = len(self._buffer)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000838 if len(self._buffer) <= self._pos:
839 return b""
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300840 newpos = min(len(self._buffer), self._pos + size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000841 b = self._buffer[self._pos : newpos]
842 self._pos = newpos
843 return bytes(b)
844
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300845 def read1(self, size):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000846 """This is the same as read.
847 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300848 return self.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000849
850 def write(self, b):
851 if self.closed:
852 raise ValueError("write to closed file")
853 if isinstance(b, str):
854 raise TypeError("can't write str to binary stream")
855 n = len(b)
856 if n == 0:
857 return 0
858 pos = self._pos
859 if pos > len(self._buffer):
860 # Inserts null bytes between the current end of the file
861 # and the new write position.
862 padding = b'\x00' * (pos - len(self._buffer))
863 self._buffer += padding
864 self._buffer[pos:pos + n] = b
865 self._pos += n
866 return n
867
868 def seek(self, pos, whence=0):
869 if self.closed:
870 raise ValueError("seek on closed file")
871 try:
Florent Xiclunab14930c2010-03-13 15:26:44 +0000872 pos.__index__
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000873 except AttributeError as err:
874 raise TypeError("an integer is required") from err
875 if whence == 0:
876 if pos < 0:
877 raise ValueError("negative seek position %r" % (pos,))
878 self._pos = pos
879 elif whence == 1:
880 self._pos = max(0, self._pos + pos)
881 elif whence == 2:
882 self._pos = max(0, len(self._buffer) + pos)
883 else:
Jesus Cea94363612012-06-22 18:32:07 +0200884 raise ValueError("unsupported whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000885 return self._pos
886
887 def tell(self):
888 if self.closed:
889 raise ValueError("tell on closed file")
890 return self._pos
891
892 def truncate(self, pos=None):
893 if self.closed:
894 raise ValueError("truncate on closed file")
895 if pos is None:
896 pos = self._pos
Florent Xiclunab14930c2010-03-13 15:26:44 +0000897 else:
898 try:
899 pos.__index__
900 except AttributeError as err:
901 raise TypeError("an integer is required") from err
902 if pos < 0:
903 raise ValueError("negative truncate position %r" % (pos,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904 del self._buffer[pos:]
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000905 return pos
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000906
907 def readable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200908 if self.closed:
909 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000910 return True
911
912 def writable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200913 if self.closed:
914 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000915 return True
916
917 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200918 if self.closed:
919 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000920 return True
921
922
923class BufferedReader(_BufferedIOMixin):
924
925 """BufferedReader(raw[, buffer_size])
926
927 A buffer for a readable, sequential BaseRawIO object.
928
929 The constructor creates a BufferedReader for the given readable raw
930 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
931 is used.
932 """
933
934 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
935 """Create a new buffered reader using the given readable raw IO object.
936 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000937 if not raw.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200938 raise OSError('"raw" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000939
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000940 _BufferedIOMixin.__init__(self, raw)
941 if buffer_size <= 0:
942 raise ValueError("invalid buffer size")
943 self.buffer_size = buffer_size
944 self._reset_read_buf()
945 self._read_lock = Lock()
946
947 def _reset_read_buf(self):
948 self._read_buf = b""
949 self._read_pos = 0
950
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300951 def read(self, size=None):
952 """Read size bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000953
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300954 Returns exactly size bytes of data unless the underlying raw IO
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000955 stream reaches EOF or if the call would block in non-blocking
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300956 mode. If size is negative, read until EOF or until read() would
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000957 block.
958 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300959 if size is not None and size < -1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000960 raise ValueError("invalid number of bytes to read")
961 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300962 return self._read_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000963
964 def _read_unlocked(self, n=None):
965 nodata_val = b""
966 empty_values = (b"", None)
967 buf = self._read_buf
968 pos = self._read_pos
969
970 # Special case for when the number of bytes to read is unspecified.
971 if n is None or n == -1:
972 self._reset_read_buf()
Victor Stinnerb57f1082011-05-26 00:19:38 +0200973 if hasattr(self.raw, 'readall'):
974 chunk = self.raw.readall()
975 if chunk is None:
976 return buf[pos:] or None
977 else:
978 return buf[pos:] + chunk
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000979 chunks = [buf[pos:]] # Strip the consumed bytes.
980 current_size = 0
981 while True:
982 # Read until EOF or until read() would block.
Antoine Pitrou707ce822011-02-25 21:24:11 +0000983 try:
984 chunk = self.raw.read()
Antoine Pitrou24d659d2011-10-23 23:49:42 +0200985 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +0000986 continue
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000987 if chunk in empty_values:
988 nodata_val = chunk
989 break
990 current_size += len(chunk)
991 chunks.append(chunk)
992 return b"".join(chunks) or nodata_val
993
994 # The number of bytes to read is specified, return at most n bytes.
995 avail = len(buf) - pos # Length of the available buffered data.
996 if n <= avail:
997 # Fast path: the data to read is fully buffered.
998 self._read_pos += n
999 return buf[pos:pos+n]
1000 # Slow path: read from the stream until enough bytes are read,
1001 # or until an EOF occurs or until read() would block.
1002 chunks = [buf[pos:]]
1003 wanted = max(self.buffer_size, n)
1004 while avail < n:
Antoine Pitrou707ce822011-02-25 21:24:11 +00001005 try:
1006 chunk = self.raw.read(wanted)
Antoine Pitrou24d659d2011-10-23 23:49:42 +02001007 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +00001008 continue
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001009 if chunk in empty_values:
1010 nodata_val = chunk
1011 break
1012 avail += len(chunk)
1013 chunks.append(chunk)
1014 # n is more then avail only when an EOF occurred or when
1015 # read() would have blocked.
1016 n = min(n, avail)
1017 out = b"".join(chunks)
1018 self._read_buf = out[n:] # Save the extra data in the buffer.
1019 self._read_pos = 0
1020 return out[:n] if out else nodata_val
1021
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001022 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001023 """Returns buffered bytes without advancing the position.
1024
1025 The argument indicates a desired minimal number of bytes; we
1026 do at most one raw read to satisfy it. We never return more
1027 than self.buffer_size.
1028 """
1029 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001030 return self._peek_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001031
1032 def _peek_unlocked(self, n=0):
1033 want = min(n, self.buffer_size)
1034 have = len(self._read_buf) - self._read_pos
1035 if have < want or have <= 0:
1036 to_read = self.buffer_size - have
Antoine Pitrou707ce822011-02-25 21:24:11 +00001037 while True:
1038 try:
1039 current = self.raw.read(to_read)
Antoine Pitrou24d659d2011-10-23 23:49:42 +02001040 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +00001041 continue
1042 break
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001043 if current:
1044 self._read_buf = self._read_buf[self._read_pos:] + current
1045 self._read_pos = 0
1046 return self._read_buf[self._read_pos:]
1047
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001048 def read1(self, size):
1049 """Reads up to size bytes, with at most one read() system call."""
1050 # Returns up to size bytes. If at least one byte is buffered, we
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001051 # only return buffered bytes. Otherwise, we do one raw read.
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001052 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001053 raise ValueError("number of bytes to read must be positive")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001054 if size == 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001055 return b""
1056 with self._read_lock:
1057 self._peek_unlocked(1)
1058 return self._read_unlocked(
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001059 min(size, len(self._read_buf) - self._read_pos))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001060
1061 def tell(self):
1062 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
1063
1064 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001065 if whence not in valid_seek_flags:
Jesus Cea990eff02012-04-26 17:05:31 +02001066 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001067 with self._read_lock:
1068 if whence == 1:
1069 pos -= len(self._read_buf) - self._read_pos
1070 pos = _BufferedIOMixin.seek(self, pos, whence)
1071 self._reset_read_buf()
1072 return pos
1073
1074class BufferedWriter(_BufferedIOMixin):
1075
1076 """A buffer for a writeable sequential RawIO object.
1077
1078 The constructor creates a BufferedWriter for the given writeable raw
1079 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001080 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001081 """
1082
Florent Xicluna109d5732012-07-07 17:03:22 +02001083 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001084 if not raw.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001085 raise OSError('"raw" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001086
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001087 _BufferedIOMixin.__init__(self, raw)
1088 if buffer_size <= 0:
1089 raise ValueError("invalid buffer size")
1090 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001091 self._write_buf = bytearray()
1092 self._write_lock = Lock()
1093
1094 def write(self, b):
1095 if self.closed:
1096 raise ValueError("write to closed file")
1097 if isinstance(b, str):
1098 raise TypeError("can't write str to binary stream")
1099 with self._write_lock:
1100 # XXX we can implement some more tricks to try and avoid
1101 # partial writes
1102 if len(self._write_buf) > self.buffer_size:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001103 # We're full, so let's pre-flush the buffer. (This may
1104 # raise BlockingIOError with characters_written == 0.)
1105 self._flush_unlocked()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001106 before = len(self._write_buf)
1107 self._write_buf.extend(b)
1108 written = len(self._write_buf) - before
1109 if len(self._write_buf) > self.buffer_size:
1110 try:
1111 self._flush_unlocked()
1112 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001113 if len(self._write_buf) > self.buffer_size:
1114 # We've hit the buffer_size. We have to accept a partial
1115 # write and cut back our buffer.
1116 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001117 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001118 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001119 raise BlockingIOError(e.errno, e.strerror, written)
1120 return written
1121
1122 def truncate(self, pos=None):
1123 with self._write_lock:
1124 self._flush_unlocked()
1125 if pos is None:
1126 pos = self.raw.tell()
1127 return self.raw.truncate(pos)
1128
1129 def flush(self):
1130 with self._write_lock:
1131 self._flush_unlocked()
1132
1133 def _flush_unlocked(self):
1134 if self.closed:
1135 raise ValueError("flush of closed file")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001136 while self._write_buf:
1137 try:
1138 n = self.raw.write(self._write_buf)
Antoine Pitrou7fe601c2011-11-21 20:22:01 +01001139 except InterruptedError:
1140 continue
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001141 except BlockingIOError:
1142 raise RuntimeError("self.raw should implement RawIOBase: it "
1143 "should not raise BlockingIOError")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001144 if n is None:
1145 raise BlockingIOError(
1146 errno.EAGAIN,
1147 "write could not complete without blocking", 0)
1148 if n > len(self._write_buf) or n < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001149 raise OSError("write() returned incorrect number of bytes")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001150 del self._write_buf[:n]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001151
1152 def tell(self):
1153 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1154
1155 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001156 if whence not in valid_seek_flags:
1157 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001158 with self._write_lock:
1159 self._flush_unlocked()
1160 return _BufferedIOMixin.seek(self, pos, whence)
1161
1162
1163class BufferedRWPair(BufferedIOBase):
1164
1165 """A buffered reader and writer object together.
1166
1167 A buffered reader object and buffered writer object put together to
1168 form a sequential IO object that can read and write. This is typically
1169 used with a socket or two-way pipe.
1170
1171 reader and writer are RawIOBase objects that are readable and
1172 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001173 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001174 """
1175
1176 # XXX The usefulness of this (compared to having two separate IO
1177 # objects) is questionable.
1178
Florent Xicluna109d5732012-07-07 17:03:22 +02001179 def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001180 """Constructor.
1181
1182 The arguments are two RawIO instances.
1183 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001184 if not reader.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001185 raise OSError('"reader" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001186
1187 if not writer.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001188 raise OSError('"writer" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001189
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001190 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001191 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001192
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001193 def read(self, size=None):
1194 if size is None:
1195 size = -1
1196 return self.reader.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001197
1198 def readinto(self, b):
1199 return self.reader.readinto(b)
1200
1201 def write(self, b):
1202 return self.writer.write(b)
1203
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001204 def peek(self, size=0):
1205 return self.reader.peek(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001206
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001207 def read1(self, size):
1208 return self.reader.read1(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001209
1210 def readable(self):
1211 return self.reader.readable()
1212
1213 def writable(self):
1214 return self.writer.writable()
1215
1216 def flush(self):
1217 return self.writer.flush()
1218
1219 def close(self):
1220 self.writer.close()
1221 self.reader.close()
1222
1223 def isatty(self):
1224 return self.reader.isatty() or self.writer.isatty()
1225
1226 @property
1227 def closed(self):
1228 return self.writer.closed
1229
1230
1231class BufferedRandom(BufferedWriter, BufferedReader):
1232
1233 """A buffered interface to random access streams.
1234
1235 The constructor creates a reader and writer for a seekable stream,
1236 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001237 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001238 """
1239
Florent Xicluna109d5732012-07-07 17:03:22 +02001240 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001241 raw._checkSeekable()
1242 BufferedReader.__init__(self, raw, buffer_size)
Florent Xicluna109d5732012-07-07 17:03:22 +02001243 BufferedWriter.__init__(self, raw, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001244
1245 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001246 if whence not in valid_seek_flags:
1247 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001248 self.flush()
1249 if self._read_buf:
1250 # Undo read ahead.
1251 with self._read_lock:
1252 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1253 # First do the raw seek, then empty the read buffer, so that
1254 # if the raw seek fails, we don't lose buffered data forever.
1255 pos = self.raw.seek(pos, whence)
1256 with self._read_lock:
1257 self._reset_read_buf()
1258 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001259 raise OSError("seek() returned invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001260 return pos
1261
1262 def tell(self):
1263 if self._write_buf:
1264 return BufferedWriter.tell(self)
1265 else:
1266 return BufferedReader.tell(self)
1267
1268 def truncate(self, pos=None):
1269 if pos is None:
1270 pos = self.tell()
1271 # Use seek to flush the read buffer.
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001272 return BufferedWriter.truncate(self, pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001273
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001274 def read(self, size=None):
1275 if size is None:
1276 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001277 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001278 return BufferedReader.read(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001279
1280 def readinto(self, b):
1281 self.flush()
1282 return BufferedReader.readinto(self, b)
1283
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001284 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001285 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001286 return BufferedReader.peek(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001287
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001288 def read1(self, size):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001289 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001290 return BufferedReader.read1(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001291
1292 def write(self, b):
1293 if self._read_buf:
1294 # Undo readahead
1295 with self._read_lock:
1296 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1297 self._reset_read_buf()
1298 return BufferedWriter.write(self, b)
1299
1300
1301class TextIOBase(IOBase):
1302
1303 """Base class for text I/O.
1304
1305 This class provides a character and line based interface to stream
1306 I/O. There is no readinto method because Python's character strings
1307 are immutable. There is no public constructor.
1308 """
1309
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001310 def read(self, size=-1):
1311 """Read at most size characters from stream, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001312
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001313 Read from underlying buffer until we have size characters or we hit EOF.
1314 If size is negative or omitted, read until EOF.
Raymond Hettingercbb80892011-01-13 18:15:51 +00001315
1316 Returns a string.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001317 """
1318 self._unsupported("read")
1319
Raymond Hettinger3c940242011-01-12 23:39:31 +00001320 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001321 """Write string s to stream and returning an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001322 self._unsupported("write")
1323
Georg Brandl4d73b572011-01-13 07:13:06 +00001324 def truncate(self, pos=None):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001325 """Truncate size to pos, where pos is an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001326 self._unsupported("truncate")
1327
Raymond Hettinger3c940242011-01-12 23:39:31 +00001328 def readline(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001329 """Read until newline or EOF.
1330
1331 Returns an empty string if EOF is hit immediately.
1332 """
1333 self._unsupported("readline")
1334
Raymond Hettinger3c940242011-01-12 23:39:31 +00001335 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001336 """
1337 Separate the underlying buffer from the TextIOBase and return it.
1338
1339 After the underlying buffer has been detached, the TextIO is in an
1340 unusable state.
1341 """
1342 self._unsupported("detach")
1343
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001344 @property
1345 def encoding(self):
1346 """Subclasses should override."""
1347 return None
1348
1349 @property
1350 def newlines(self):
1351 """Line endings translated so far.
1352
1353 Only line endings translated during reading are considered.
1354
1355 Subclasses should override.
1356 """
1357 return None
1358
Benjamin Peterson0926ad12009-06-06 18:02:12 +00001359 @property
1360 def errors(self):
1361 """Error setting of the decoder or encoder.
1362
1363 Subclasses should override."""
1364 return None
1365
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001366io.TextIOBase.register(TextIOBase)
1367
1368
1369class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1370 r"""Codec used when reading a file in universal newlines mode. It wraps
1371 another incremental decoder, translating \r\n and \r into \n. It also
1372 records the types of newlines encountered. When used with
1373 translate=False, it ensures that the newline sequence is returned in
1374 one piece.
1375 """
1376 def __init__(self, decoder, translate, errors='strict'):
1377 codecs.IncrementalDecoder.__init__(self, errors=errors)
1378 self.translate = translate
1379 self.decoder = decoder
1380 self.seennl = 0
1381 self.pendingcr = False
1382
1383 def decode(self, input, final=False):
1384 # decode input (with the eventual \r from a previous pass)
1385 if self.decoder is None:
1386 output = input
1387 else:
1388 output = self.decoder.decode(input, final=final)
1389 if self.pendingcr and (output or final):
1390 output = "\r" + output
1391 self.pendingcr = False
1392
1393 # retain last \r even when not translating data:
1394 # then readline() is sure to get \r\n in one pass
1395 if output.endswith("\r") and not final:
1396 output = output[:-1]
1397 self.pendingcr = True
1398
1399 # Record which newlines are read
1400 crlf = output.count('\r\n')
1401 cr = output.count('\r') - crlf
1402 lf = output.count('\n') - crlf
1403 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1404 | (crlf and self._CRLF)
1405
1406 if self.translate:
1407 if crlf:
1408 output = output.replace("\r\n", "\n")
1409 if cr:
1410 output = output.replace("\r", "\n")
1411
1412 return output
1413
1414 def getstate(self):
1415 if self.decoder is None:
1416 buf = b""
1417 flag = 0
1418 else:
1419 buf, flag = self.decoder.getstate()
1420 flag <<= 1
1421 if self.pendingcr:
1422 flag |= 1
1423 return buf, flag
1424
1425 def setstate(self, state):
1426 buf, flag = state
1427 self.pendingcr = bool(flag & 1)
1428 if self.decoder is not None:
1429 self.decoder.setstate((buf, flag >> 1))
1430
1431 def reset(self):
1432 self.seennl = 0
1433 self.pendingcr = False
1434 if self.decoder is not None:
1435 self.decoder.reset()
1436
1437 _LF = 1
1438 _CR = 2
1439 _CRLF = 4
1440
1441 @property
1442 def newlines(self):
1443 return (None,
1444 "\n",
1445 "\r",
1446 ("\r", "\n"),
1447 "\r\n",
1448 ("\n", "\r\n"),
1449 ("\r", "\r\n"),
1450 ("\r", "\n", "\r\n")
1451 )[self.seennl]
1452
1453
1454class TextIOWrapper(TextIOBase):
1455
1456 r"""Character and line based layer over a BufferedIOBase object, buffer.
1457
1458 encoding gives the name of the encoding that the stream will be
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001459 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001460
1461 errors determines the strictness of encoding and decoding (see the
1462 codecs.register) and defaults to "strict".
1463
1464 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1465 handling of line endings. If it is None, universal newlines is
1466 enabled. With this enabled, on input, the lines endings '\n', '\r',
1467 or '\r\n' are translated to '\n' before being returned to the
1468 caller. Conversely, on output, '\n' is translated to the system
Éric Araujo39242302011-11-03 00:08:48 +01001469 default line separator, os.linesep. If newline is any other of its
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001470 legal values, that newline becomes the newline when the file is read
1471 and it is returned untranslated. On output, '\n' is converted to the
1472 newline.
1473
1474 If line_buffering is True, a call to flush is implied when a call to
1475 write contains a newline character.
1476 """
1477
1478 _CHUNK_SIZE = 2048
1479
Andrew Svetlov4e9e9c12012-08-13 16:09:54 +03001480 # The write_through argument has no effect here since this
1481 # implementation always writes through. The argument is present only
1482 # so that the signature can match the signature of the C version.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001483 def __init__(self, buffer, encoding=None, errors=None, newline=None,
Antoine Pitroue96ec682011-07-23 21:46:35 +02001484 line_buffering=False, write_through=False):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001485 if newline is not None and not isinstance(newline, str):
1486 raise TypeError("illegal newline type: %r" % (type(newline),))
1487 if newline not in (None, "", "\n", "\r", "\r\n"):
1488 raise ValueError("illegal newline value: %r" % (newline,))
1489 if encoding is None:
1490 try:
1491 encoding = os.device_encoding(buffer.fileno())
1492 except (AttributeError, UnsupportedOperation):
1493 pass
1494 if encoding is None:
1495 try:
1496 import locale
Brett Cannoncd171c82013-07-04 17:43:24 -04001497 except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001498 # Importing locale may fail if Python is being built
1499 encoding = "ascii"
1500 else:
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001501 encoding = locale.getpreferredencoding(False)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001502
1503 if not isinstance(encoding, str):
1504 raise ValueError("invalid encoding: %r" % encoding)
1505
1506 if errors is None:
1507 errors = "strict"
1508 else:
1509 if not isinstance(errors, str):
1510 raise ValueError("invalid errors: %r" % errors)
1511
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001512 self._buffer = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001513 self._line_buffering = line_buffering
1514 self._encoding = encoding
1515 self._errors = errors
1516 self._readuniversal = not newline
1517 self._readtranslate = newline is None
1518 self._readnl = newline
1519 self._writetranslate = newline != ''
1520 self._writenl = newline or os.linesep
1521 self._encoder = None
1522 self._decoder = None
1523 self._decoded_chars = '' # buffer for text returned from decoder
1524 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1525 self._snapshot = None # info for reconstructing decoder state
1526 self._seekable = self._telling = self.buffer.seekable()
Antoine Pitroue96ec682011-07-23 21:46:35 +02001527 self._has_read1 = hasattr(self.buffer, 'read1')
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001528 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001529
Antoine Pitroue4501852009-05-14 18:55:55 +00001530 if self._seekable and self.writable():
1531 position = self.buffer.tell()
1532 if position != 0:
1533 try:
1534 self._get_encoder().setstate(0)
1535 except LookupError:
1536 # Sometimes the encoder doesn't exist
1537 pass
1538
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001539 # self._snapshot is either None, or a tuple (dec_flags, next_input)
1540 # where dec_flags is the second (integer) item of the decoder state
1541 # and next_input is the chunk of input bytes that comes next after the
1542 # snapshot point. We use this to reconstruct decoder states in tell().
1543
1544 # Naming convention:
1545 # - "bytes_..." for integer variables that count input bytes
1546 # - "chars_..." for integer variables that count decoded characters
1547
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001548 def __repr__(self):
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001549 result = "<_pyio.TextIOWrapper"
Antoine Pitrou716c4442009-05-23 19:04:03 +00001550 try:
1551 name = self.name
1552 except AttributeError:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001553 pass
Antoine Pitrou716c4442009-05-23 19:04:03 +00001554 else:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001555 result += " name={0!r}".format(name)
1556 try:
1557 mode = self.mode
1558 except AttributeError:
1559 pass
1560 else:
1561 result += " mode={0!r}".format(mode)
1562 return result + " encoding={0!r}>".format(self.encoding)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001563
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001564 @property
1565 def encoding(self):
1566 return self._encoding
1567
1568 @property
1569 def errors(self):
1570 return self._errors
1571
1572 @property
1573 def line_buffering(self):
1574 return self._line_buffering
1575
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001576 @property
1577 def buffer(self):
1578 return self._buffer
1579
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001580 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02001581 if self.closed:
1582 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001583 return self._seekable
1584
1585 def readable(self):
1586 return self.buffer.readable()
1587
1588 def writable(self):
1589 return self.buffer.writable()
1590
1591 def flush(self):
1592 self.buffer.flush()
1593 self._telling = self._seekable
1594
1595 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +00001596 if self.buffer is not None and not self.closed:
Benjamin Peterson68623612012-12-20 11:53:11 -06001597 try:
1598 self.flush()
1599 finally:
1600 self.buffer.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001601
1602 @property
1603 def closed(self):
1604 return self.buffer.closed
1605
1606 @property
1607 def name(self):
1608 return self.buffer.name
1609
1610 def fileno(self):
1611 return self.buffer.fileno()
1612
1613 def isatty(self):
1614 return self.buffer.isatty()
1615
Raymond Hettinger00fa0392011-01-13 02:52:26 +00001616 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001617 'Write data, where s is a str'
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001618 if self.closed:
1619 raise ValueError("write to closed file")
1620 if not isinstance(s, str):
1621 raise TypeError("can't write %s to text stream" %
1622 s.__class__.__name__)
1623 length = len(s)
1624 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
1625 if haslf and self._writetranslate and self._writenl != "\n":
1626 s = s.replace("\n", self._writenl)
1627 encoder = self._encoder or self._get_encoder()
1628 # XXX What if we were just reading?
1629 b = encoder.encode(s)
1630 self.buffer.write(b)
1631 if self._line_buffering and (haslf or "\r" in s):
1632 self.flush()
1633 self._snapshot = None
1634 if self._decoder:
1635 self._decoder.reset()
1636 return length
1637
1638 def _get_encoder(self):
1639 make_encoder = codecs.getincrementalencoder(self._encoding)
1640 self._encoder = make_encoder(self._errors)
1641 return self._encoder
1642
1643 def _get_decoder(self):
1644 make_decoder = codecs.getincrementaldecoder(self._encoding)
1645 decoder = make_decoder(self._errors)
1646 if self._readuniversal:
1647 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
1648 self._decoder = decoder
1649 return decoder
1650
1651 # The following three methods implement an ADT for _decoded_chars.
1652 # Text returned from the decoder is buffered here until the client
1653 # requests it by calling our read() or readline() method.
1654 def _set_decoded_chars(self, chars):
1655 """Set the _decoded_chars buffer."""
1656 self._decoded_chars = chars
1657 self._decoded_chars_used = 0
1658
1659 def _get_decoded_chars(self, n=None):
1660 """Advance into the _decoded_chars buffer."""
1661 offset = self._decoded_chars_used
1662 if n is None:
1663 chars = self._decoded_chars[offset:]
1664 else:
1665 chars = self._decoded_chars[offset:offset + n]
1666 self._decoded_chars_used += len(chars)
1667 return chars
1668
1669 def _rewind_decoded_chars(self, n):
1670 """Rewind the _decoded_chars buffer."""
1671 if self._decoded_chars_used < n:
1672 raise AssertionError("rewind decoded_chars out of bounds")
1673 self._decoded_chars_used -= n
1674
1675 def _read_chunk(self):
1676 """
1677 Read and decode the next chunk of data from the BufferedReader.
1678 """
1679
1680 # The return value is True unless EOF was reached. The decoded
1681 # string is placed in self._decoded_chars (replacing its previous
1682 # value). The entire input chunk is sent to the decoder, though
1683 # some of it may remain buffered in the decoder, yet to be
1684 # converted.
1685
1686 if self._decoder is None:
1687 raise ValueError("no decoder")
1688
1689 if self._telling:
1690 # To prepare for tell(), we need to snapshot a point in the
1691 # file where the decoder's input buffer is empty.
1692
1693 dec_buffer, dec_flags = self._decoder.getstate()
1694 # Given this, we know there was a valid snapshot point
1695 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1696
1697 # Read a chunk, decode it, and put the result in self._decoded_chars.
Antoine Pitroue96ec682011-07-23 21:46:35 +02001698 if self._has_read1:
1699 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1700 else:
1701 input_chunk = self.buffer.read(self._CHUNK_SIZE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001702 eof = not input_chunk
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001703 decoded_chars = self._decoder.decode(input_chunk, eof)
1704 self._set_decoded_chars(decoded_chars)
1705 if decoded_chars:
1706 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
1707 else:
1708 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001709
1710 if self._telling:
1711 # At the snapshot point, len(dec_buffer) bytes before the read,
1712 # the next input to be decoded is dec_buffer + input_chunk.
1713 self._snapshot = (dec_flags, dec_buffer + input_chunk)
1714
1715 return not eof
1716
1717 def _pack_cookie(self, position, dec_flags=0,
1718 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
1719 # The meaning of a tell() cookie is: seek to position, set the
1720 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
1721 # into the decoder with need_eof as the EOF flag, then skip
1722 # chars_to_skip characters of the decoded result. For most simple
1723 # decoders, tell() will often just give a byte offset in the file.
1724 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
1725 (chars_to_skip<<192) | bool(need_eof)<<256)
1726
1727 def _unpack_cookie(self, bigint):
1728 rest, position = divmod(bigint, 1<<64)
1729 rest, dec_flags = divmod(rest, 1<<64)
1730 rest, bytes_to_feed = divmod(rest, 1<<64)
1731 need_eof, chars_to_skip = divmod(rest, 1<<64)
1732 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
1733
1734 def tell(self):
1735 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001736 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001737 if not self._telling:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001738 raise OSError("telling position disabled by next() call")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001739 self.flush()
1740 position = self.buffer.tell()
1741 decoder = self._decoder
1742 if decoder is None or self._snapshot is None:
1743 if self._decoded_chars:
1744 # This should never happen.
1745 raise AssertionError("pending decoded text")
1746 return position
1747
1748 # Skip backward to the snapshot point (see _read_chunk).
1749 dec_flags, next_input = self._snapshot
1750 position -= len(next_input)
1751
1752 # How many decoded characters have been used up since the snapshot?
1753 chars_to_skip = self._decoded_chars_used
1754 if chars_to_skip == 0:
1755 # We haven't moved from the snapshot point.
1756 return self._pack_cookie(position, dec_flags)
1757
1758 # Starting from the snapshot position, we will walk the decoder
1759 # forward until it gives us enough decoded characters.
1760 saved_state = decoder.getstate()
1761 try:
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001762 # Fast search for an acceptable start point, close to our
1763 # current pos.
1764 # Rationale: calling decoder.decode() has a large overhead
1765 # regardless of chunk size; we want the number of such calls to
1766 # be O(1) in most situations (common decoders, non-crazy input).
1767 # Actually, it will be exactly 1 for fixed-size codecs (all
1768 # 8-bit codecs, also UTF-16 and UTF-32).
1769 skip_bytes = int(self._b2cratio * chars_to_skip)
1770 skip_back = 1
1771 assert skip_bytes <= len(next_input)
1772 while skip_bytes > 0:
1773 decoder.setstate((b'', dec_flags))
1774 # Decode up to temptative start point
1775 n = len(decoder.decode(next_input[:skip_bytes]))
1776 if n <= chars_to_skip:
1777 b, d = decoder.getstate()
1778 if not b:
1779 # Before pos and no bytes buffered in decoder => OK
1780 dec_flags = d
1781 chars_to_skip -= n
1782 break
1783 # Skip back by buffered amount and reset heuristic
1784 skip_bytes -= len(b)
1785 skip_back = 1
1786 else:
1787 # We're too far ahead, skip back a bit
1788 skip_bytes -= skip_back
1789 skip_back = skip_back * 2
1790 else:
1791 skip_bytes = 0
1792 decoder.setstate((b'', dec_flags))
1793
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001794 # Note our initial start point.
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001795 start_pos = position + skip_bytes
1796 start_flags = dec_flags
1797 if chars_to_skip == 0:
1798 # We haven't moved from the start point.
1799 return self._pack_cookie(start_pos, start_flags)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001800
1801 # Feed the decoder one byte at a time. As we go, note the
1802 # nearest "safe start point" before the current location
1803 # (a point where the decoder has nothing buffered, so seek()
1804 # can safely start from there and advance to this location).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001805 bytes_fed = 0
1806 need_eof = 0
1807 # Chars decoded since `start_pos`
1808 chars_decoded = 0
1809 for i in range(skip_bytes, len(next_input)):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001810 bytes_fed += 1
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001811 chars_decoded += len(decoder.decode(next_input[i:i+1]))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001812 dec_buffer, dec_flags = decoder.getstate()
1813 if not dec_buffer and chars_decoded <= chars_to_skip:
1814 # Decoder buffer is empty, so this is a safe start point.
1815 start_pos += bytes_fed
1816 chars_to_skip -= chars_decoded
1817 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1818 if chars_decoded >= chars_to_skip:
1819 break
1820 else:
1821 # We didn't get enough decoded data; signal EOF to get more.
1822 chars_decoded += len(decoder.decode(b'', final=True))
1823 need_eof = 1
1824 if chars_decoded < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001825 raise OSError("can't reconstruct logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001826
1827 # The returned cookie corresponds to the last safe start point.
1828 return self._pack_cookie(
1829 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
1830 finally:
1831 decoder.setstate(saved_state)
1832
1833 def truncate(self, pos=None):
1834 self.flush()
1835 if pos is None:
1836 pos = self.tell()
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001837 return self.buffer.truncate(pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001838
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001839 def detach(self):
1840 if self.buffer is None:
1841 raise ValueError("buffer is already detached")
1842 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001843 buffer = self._buffer
1844 self._buffer = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001845 return buffer
1846
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001847 def seek(self, cookie, whence=0):
1848 if self.closed:
1849 raise ValueError("tell on closed file")
1850 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001851 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001852 if whence == 1: # seek relative to current position
1853 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001854 raise UnsupportedOperation("can't do nonzero cur-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001855 # Seeking to the current position should attempt to
1856 # sync the underlying buffer with the current position.
1857 whence = 0
1858 cookie = self.tell()
1859 if whence == 2: # seek relative to end of file
1860 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001861 raise UnsupportedOperation("can't do nonzero end-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001862 self.flush()
1863 position = self.buffer.seek(0, 2)
1864 self._set_decoded_chars('')
1865 self._snapshot = None
1866 if self._decoder:
1867 self._decoder.reset()
1868 return position
1869 if whence != 0:
Jesus Cea94363612012-06-22 18:32:07 +02001870 raise ValueError("unsupported whence (%r)" % (whence,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001871 if cookie < 0:
1872 raise ValueError("negative seek position %r" % (cookie,))
1873 self.flush()
1874
1875 # The strategy of seek() is to go back to the safe start point
1876 # and replay the effect of read(chars_to_skip) from there.
1877 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
1878 self._unpack_cookie(cookie)
1879
1880 # Seek back to the safe start point.
1881 self.buffer.seek(start_pos)
1882 self._set_decoded_chars('')
1883 self._snapshot = None
1884
1885 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00001886 if cookie == 0 and self._decoder:
1887 self._decoder.reset()
1888 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001889 self._decoder = self._decoder or self._get_decoder()
1890 self._decoder.setstate((b'', dec_flags))
1891 self._snapshot = (dec_flags, b'')
1892
1893 if chars_to_skip:
1894 # Just like _read_chunk, feed the decoder and save a snapshot.
1895 input_chunk = self.buffer.read(bytes_to_feed)
1896 self._set_decoded_chars(
1897 self._decoder.decode(input_chunk, need_eof))
1898 self._snapshot = (dec_flags, input_chunk)
1899
1900 # Skip chars_to_skip of the decoded characters.
1901 if len(self._decoded_chars) < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001902 raise OSError("can't restore logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001903 self._decoded_chars_used = chars_to_skip
1904
Antoine Pitroue4501852009-05-14 18:55:55 +00001905 # Finally, reset the encoder (merely useful for proper BOM handling)
1906 try:
1907 encoder = self._encoder or self._get_encoder()
1908 except LookupError:
1909 # Sometimes the encoder doesn't exist
1910 pass
1911 else:
1912 if cookie != 0:
1913 encoder.setstate(0)
1914 else:
1915 encoder.reset()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001916 return cookie
1917
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001918 def read(self, size=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00001919 self._checkReadable()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001920 if size is None:
1921 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001922 decoder = self._decoder or self._get_decoder()
Florent Xiclunab14930c2010-03-13 15:26:44 +00001923 try:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001924 size.__index__
Florent Xiclunab14930c2010-03-13 15:26:44 +00001925 except AttributeError as err:
1926 raise TypeError("an integer is required") from err
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001927 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001928 # Read everything.
1929 result = (self._get_decoded_chars() +
1930 decoder.decode(self.buffer.read(), final=True))
1931 self._set_decoded_chars('')
1932 self._snapshot = None
1933 return result
1934 else:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001935 # Keep reading chunks until we have size characters to return.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001936 eof = False
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001937 result = self._get_decoded_chars(size)
1938 while len(result) < size and not eof:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001939 eof = not self._read_chunk()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001940 result += self._get_decoded_chars(size - len(result))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001941 return result
1942
1943 def __next__(self):
1944 self._telling = False
1945 line = self.readline()
1946 if not line:
1947 self._snapshot = None
1948 self._telling = self._seekable
1949 raise StopIteration
1950 return line
1951
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001952 def readline(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001953 if self.closed:
1954 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001955 if size is None:
1956 size = -1
1957 elif not isinstance(size, int):
1958 raise TypeError("size must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001959
1960 # Grab all the decoded text (we will rewind any extra bits later).
1961 line = self._get_decoded_chars()
1962
1963 start = 0
1964 # Make the decoder if it doesn't already exist.
1965 if not self._decoder:
1966 self._get_decoder()
1967
1968 pos = endpos = None
1969 while True:
1970 if self._readtranslate:
1971 # Newlines are already translated, only search for \n
1972 pos = line.find('\n', start)
1973 if pos >= 0:
1974 endpos = pos + 1
1975 break
1976 else:
1977 start = len(line)
1978
1979 elif self._readuniversal:
1980 # Universal newline search. Find any of \r, \r\n, \n
1981 # The decoder ensures that \r\n are not split in two pieces
1982
1983 # In C we'd look for these in parallel of course.
1984 nlpos = line.find("\n", start)
1985 crpos = line.find("\r", start)
1986 if crpos == -1:
1987 if nlpos == -1:
1988 # Nothing found
1989 start = len(line)
1990 else:
1991 # Found \n
1992 endpos = nlpos + 1
1993 break
1994 elif nlpos == -1:
1995 # Found lone \r
1996 endpos = crpos + 1
1997 break
1998 elif nlpos < crpos:
1999 # Found \n
2000 endpos = nlpos + 1
2001 break
2002 elif nlpos == crpos + 1:
2003 # Found \r\n
2004 endpos = crpos + 2
2005 break
2006 else:
2007 # Found \r
2008 endpos = crpos + 1
2009 break
2010 else:
2011 # non-universal
2012 pos = line.find(self._readnl)
2013 if pos >= 0:
2014 endpos = pos + len(self._readnl)
2015 break
2016
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002017 if size >= 0 and len(line) >= size:
2018 endpos = size # reached length size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002019 break
2020
2021 # No line ending seen yet - get more data'
2022 while self._read_chunk():
2023 if self._decoded_chars:
2024 break
2025 if self._decoded_chars:
2026 line += self._get_decoded_chars()
2027 else:
2028 # end of file
2029 self._set_decoded_chars('')
2030 self._snapshot = None
2031 return line
2032
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002033 if size >= 0 and endpos > size:
2034 endpos = size # don't exceed size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002035
2036 # Rewind _decoded_chars to just after the line ending we found.
2037 self._rewind_decoded_chars(len(line) - endpos)
2038 return line[:endpos]
2039
2040 @property
2041 def newlines(self):
2042 return self._decoder.newlines if self._decoder else None
2043
2044
2045class StringIO(TextIOWrapper):
2046 """Text I/O implementation using an in-memory buffer.
2047
2048 The initial_value argument sets the value of object. The newline
2049 argument is like the one of TextIOWrapper's constructor.
2050 """
2051
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002052 def __init__(self, initial_value="", newline="\n"):
2053 super(StringIO, self).__init__(BytesIO(),
2054 encoding="utf-8",
2055 errors="strict",
2056 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00002057 # Issue #5645: make universal newlines semantics the same as in the
2058 # C version, even under Windows.
2059 if newline is None:
2060 self._writetranslate = False
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002061 if initial_value is not None:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002062 if not isinstance(initial_value, str):
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002063 raise TypeError("initial_value must be str or None, not {0}"
2064 .format(type(initial_value).__name__))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002065 self.write(initial_value)
2066 self.seek(0)
2067
2068 def getvalue(self):
2069 self.flush()
2070 return self.buffer.getvalue().decode(self._encoding, self._errors)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002071
2072 def __repr__(self):
2073 # TextIOWrapper tells the encoding in its repr. In StringIO,
2074 # that's a implementation detail.
2075 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00002076
2077 @property
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002078 def errors(self):
2079 return None
2080
2081 @property
Benjamin Petersonb487e632009-03-21 03:08:31 +00002082 def encoding(self):
2083 return None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002084
2085 def detach(self):
2086 # This doesn't make sense on StringIO.
2087 self._unsupported("detach")