blob: 78c6dfb4cd846b34dd40685f9d20881e5c97cb85 [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)
65 'U' universal newline mode (for backwards compatibility; unneeded
66 for new code)
67 ========= ===============================================================
68
69 The default mode is 'rt' (open for reading text). For binary random
70 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
Charles-François Natalidc3044c2012-01-09 22:40:02 +010071 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
72 raises an `FileExistsError` if the file already exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000073
74 Python distinguishes between files opened in binary and text modes,
75 even when the underlying operating system doesn't. Files opened in
76 binary mode (appending 'b' to the mode argument) return contents as
77 bytes objects without any decoding. In text mode (the default, or when
78 't' is appended to the mode argument), the contents of the file are
79 returned as strings, the bytes having been first decoded using a
80 platform-dependent encoding or using the specified encoding if given.
81
Antoine Pitroud5587bc2009-12-19 21:08:31 +000082 buffering is an optional integer used to set the buffering policy.
83 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
84 line buffering (only usable in text mode), and an integer > 1 to indicate
85 the size of a fixed-size chunk buffer. When no buffering argument is
86 given, the default buffering policy works as follows:
87
88 * Binary files are buffered in fixed-size chunks; the size of the buffer
89 is chosen using a heuristic trying to determine the underlying device's
90 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
91 On many systems, the buffer will typically be 4096 or 8192 bytes long.
92
93 * "Interactive" text files (files for which isatty() returns True)
94 use line buffering. Other text files use the policy described above
95 for binary files.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000096
Raymond Hettingercbb80892011-01-13 18:15:51 +000097 encoding is the str name of the encoding used to decode or encode the
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000098 file. This should only be used in text mode. The default encoding is
99 platform dependent, but any encoding supported by Python can be
100 passed. See the codecs module for the list of supported encodings.
101
102 errors is an optional string that specifies how encoding errors are to
103 be handled---this argument should not be used in binary mode. Pass
104 'strict' to raise a ValueError exception if there is an encoding error
105 (the default of None has the same effect), or pass 'ignore' to ignore
106 errors. (Note that ignoring encoding errors can lead to data loss.)
107 See the documentation for codecs.register for a list of the permitted
108 encoding error strings.
109
Raymond Hettingercbb80892011-01-13 18:15:51 +0000110 newline is a string controlling how universal newlines works (it only
111 applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
112 as follows:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000113
114 * On input, if newline is None, universal newlines mode is
115 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
116 these are translated into '\n' before being returned to the
117 caller. If it is '', universal newline mode is enabled, but line
118 endings are returned to the caller untranslated. If it has any of
119 the other legal values, input lines are only terminated by the given
120 string, and the line ending is returned to the caller untranslated.
121
122 * On output, if newline is None, any '\n' characters written are
123 translated to the system default line separator, os.linesep. If
124 newline is '', no translation takes place. If newline is any of the
125 other legal values, any '\n' characters written are translated to
126 the given string.
127
Raymond Hettingercbb80892011-01-13 18:15:51 +0000128 closedfd is a bool. If closefd is False, the underlying file descriptor will
129 be kept open when the file is closed. This does not work when a file name is
130 given and must be True in that case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000131
Victor Stinnerdaf45552013-08-28 00:53:59 +0200132 The newly created file is non-inheritable.
133
Ross Lagerwall59142db2011-10-31 20:34:46 +0200134 A custom opener can be used by passing a callable as *opener*. The
135 underlying file descriptor for the file object is then obtained by calling
136 *opener* with (*file*, *flags*). *opener* must return an open file
137 descriptor (passing os.open as *opener* results in functionality similar to
138 passing None).
139
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000140 open() returns a file object whose type depends on the mode, and
141 through which the standard file operations such as reading and writing
142 are performed. When open() is used to open a file in a text mode ('w',
143 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
144 a file in a binary mode, the returned class varies: in read binary
145 mode, it returns a BufferedReader; in write binary and append binary
146 modes, it returns a BufferedWriter, and in read/write mode, it returns
147 a BufferedRandom.
148
149 It is also possible to use a string or bytearray as a file for both
150 reading and writing. For strings StringIO can be used like a file
151 opened in a text mode, and for bytes a BytesIO can be used like a file
152 opened in a binary mode.
153 """
154 if not isinstance(file, (str, bytes, int)):
155 raise TypeError("invalid file: %r" % file)
156 if not isinstance(mode, str):
157 raise TypeError("invalid mode: %r" % mode)
Benjamin Peterson95e392c2010-04-27 21:07:21 +0000158 if not isinstance(buffering, int):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000159 raise TypeError("invalid buffering: %r" % buffering)
160 if encoding is not None and not isinstance(encoding, str):
161 raise TypeError("invalid encoding: %r" % encoding)
162 if errors is not None and not isinstance(errors, str):
163 raise TypeError("invalid errors: %r" % errors)
164 modes = set(mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100165 if modes - set("axrwb+tU") or len(mode) > len(modes):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000166 raise ValueError("invalid mode: %r" % mode)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100167 creating = "x" in modes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168 reading = "r" in modes
169 writing = "w" in modes
170 appending = "a" in modes
171 updating = "+" in modes
172 text = "t" in modes
173 binary = "b" in modes
174 if "U" in modes:
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100175 if creating or writing or appending:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000176 raise ValueError("can't use U and writing mode at once")
177 reading = True
178 if text and binary:
179 raise ValueError("can't have text and binary mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100180 if creating + reading + writing + appending > 1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000181 raise ValueError("can't have read/write/append mode at once")
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100182 if not (creating or reading or writing or appending):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000183 raise ValueError("must have exactly one of read/write/append mode")
184 if binary and encoding is not None:
185 raise ValueError("binary mode doesn't take an encoding argument")
186 if binary and errors is not None:
187 raise ValueError("binary mode doesn't take an errors argument")
188 if binary and newline is not None:
189 raise ValueError("binary mode doesn't take a newline argument")
190 raw = FileIO(file,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100191 (creating and "x" or "") +
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000192 (reading and "r" or "") +
193 (writing and "w" or "") +
194 (appending and "a" or "") +
195 (updating and "+" or ""),
Ross Lagerwall59142db2011-10-31 20:34:46 +0200196 closefd, opener=opener)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000197 line_buffering = False
198 if buffering == 1 or buffering < 0 and raw.isatty():
199 buffering = -1
200 line_buffering = True
201 if buffering < 0:
202 buffering = DEFAULT_BUFFER_SIZE
203 try:
204 bs = os.fstat(raw.fileno()).st_blksize
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200205 except (OSError, AttributeError):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000206 pass
207 else:
208 if bs > 1:
209 buffering = bs
210 if buffering < 0:
211 raise ValueError("invalid buffering size")
212 if buffering == 0:
213 if binary:
214 return raw
215 raise ValueError("can't have unbuffered text I/O")
216 if updating:
217 buffer = BufferedRandom(raw, buffering)
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100218 elif creating or writing or appending:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000219 buffer = BufferedWriter(raw, buffering)
220 elif reading:
221 buffer = BufferedReader(raw, buffering)
222 else:
223 raise ValueError("unknown mode: %r" % mode)
224 if binary:
225 return buffer
226 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
227 text.mode = mode
228 return text
229
230
231class DocDescriptor:
232 """Helper for builtins.open.__doc__
233 """
234 def __get__(self, obj, typ):
235 return (
Benjamin Petersonc3be11a2010-04-27 21:24:03 +0000236 "open(file, mode='r', buffering=-1, encoding=None, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000237 "errors=None, newline=None, closefd=True)\n\n" +
238 open.__doc__)
239
240class OpenWrapper:
241 """Wrapper for builtins.open
242
243 Trick so that open won't become a bound method when stored
244 as a class variable (as dbm.dumb does).
245
246 See initstdio() in Python/pythonrun.c.
247 """
248 __doc__ = DocDescriptor()
249
250 def __new__(cls, *args, **kwargs):
251 return open(*args, **kwargs)
252
253
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000254# In normal operation, both `UnsupportedOperation`s should be bound to the
255# same object.
256try:
257 UnsupportedOperation = io.UnsupportedOperation
258except AttributeError:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200259 class UnsupportedOperation(ValueError, OSError):
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000260 pass
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000261
262
263class IOBase(metaclass=abc.ABCMeta):
264
265 """The abstract base class for all I/O classes, acting on streams of
266 bytes. There is no public constructor.
267
268 This class provides dummy implementations for many methods that
269 derived classes can override selectively; the default implementations
270 represent a file that cannot be read, written or seeked.
271
272 Even though IOBase does not declare read, readinto, or write because
273 their signatures will vary, implementations and clients should
274 consider those methods part of the interface. Also, implementations
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000275 may raise UnsupportedOperation when operations they do not support are
276 called.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000277
278 The basic type used for binary data read from or written to a file is
279 bytes. bytearrays are accepted too, and in some cases (such as
280 readinto) needed. Text I/O classes work with str data.
281
282 Note that calling any method (even inquiries) on a closed stream is
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200283 undefined. Implementations may raise OSError in this case.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000284
285 IOBase (and its subclasses) support the iterator protocol, meaning
286 that an IOBase object can be iterated over yielding the lines in a
287 stream.
288
289 IOBase also supports the :keyword:`with` statement. In this example,
290 fp is closed after the suite of the with statement is complete:
291
292 with open('spam.txt', 'r') as fp:
293 fp.write('Spam and eggs!')
294 """
295
296 ### Internal ###
297
Raymond Hettinger3c940242011-01-12 23:39:31 +0000298 def _unsupported(self, name):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200299 """Internal: raise an OSError exception for unsupported operations."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000300 raise UnsupportedOperation("%s.%s() not supported" %
301 (self.__class__.__name__, name))
302
303 ### Positioning ###
304
Georg Brandl4d73b572011-01-13 07:13:06 +0000305 def seek(self, pos, whence=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000306 """Change stream position.
307
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400308 Change the stream position to byte offset pos. Argument pos is
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000309 interpreted relative to the position indicated by whence. Values
Raymond Hettingercbb80892011-01-13 18:15:51 +0000310 for whence are ints:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000311
312 * 0 -- start of stream (the default); offset should be zero or positive
313 * 1 -- current stream position; offset may be negative
314 * 2 -- end of stream; offset is usually negative
Jesus Cea94363612012-06-22 18:32:07 +0200315 Some operating systems / file systems could provide additional values.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000316
Raymond Hettingercbb80892011-01-13 18:15:51 +0000317 Return an int indicating the new absolute position.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000318 """
319 self._unsupported("seek")
320
Raymond Hettinger3c940242011-01-12 23:39:31 +0000321 def tell(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000322 """Return an int indicating the current stream position."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000323 return self.seek(0, 1)
324
Georg Brandl4d73b572011-01-13 07:13:06 +0000325 def truncate(self, pos=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000326 """Truncate file to size bytes.
327
328 Size defaults to the current IO position as reported by tell(). Return
329 the new size.
330 """
331 self._unsupported("truncate")
332
333 ### Flush and close ###
334
Raymond Hettinger3c940242011-01-12 23:39:31 +0000335 def flush(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000336 """Flush write buffers, if applicable.
337
338 This is not implemented for read-only and non-blocking streams.
339 """
Antoine Pitrou6be88762010-05-03 16:48:20 +0000340 self._checkClosed()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000341 # XXX Should this return the number of bytes written???
342
343 __closed = False
344
Raymond Hettinger3c940242011-01-12 23:39:31 +0000345 def close(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000346 """Flush and close the IO object.
347
348 This method has no effect if the file is already closed.
349 """
350 if not self.__closed:
Benjamin Peterson68623612012-12-20 11:53:11 -0600351 try:
352 self.flush()
353 finally:
354 self.__closed = True
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000355
Raymond Hettinger3c940242011-01-12 23:39:31 +0000356 def __del__(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000357 """Destructor. Calls close()."""
358 # The try/except block is in case this is called at program
359 # exit time, when it's possible that globals have already been
360 # deleted, and then the close() call might fail. Since
361 # there's nothing we can do about such failures and they annoy
362 # the end users, we suppress the traceback.
363 try:
364 self.close()
365 except:
366 pass
367
368 ### Inquiries ###
369
Raymond Hettinger3c940242011-01-12 23:39:31 +0000370 def seekable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000371 """Return a bool indicating whether object supports random access.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000372
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000373 If False, seek(), tell() and truncate() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000374 This method may need to do a test seek().
375 """
376 return False
377
378 def _checkSeekable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000379 """Internal: raise UnsupportedOperation if file is not seekable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000380 """
381 if not self.seekable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000382 raise UnsupportedOperation("File or stream is not seekable."
383 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000384
Raymond Hettinger3c940242011-01-12 23:39:31 +0000385 def readable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000386 """Return a bool indicating whether object was opened for reading.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000387
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000388 If False, read() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000389 """
390 return False
391
392 def _checkReadable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000393 """Internal: raise UnsupportedOperation if file is not readable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000394 """
395 if not self.readable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000396 raise UnsupportedOperation("File or stream is not readable."
397 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000398
Raymond Hettinger3c940242011-01-12 23:39:31 +0000399 def writable(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000400 """Return a bool indicating whether object was opened for writing.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000401
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000402 If False, write() and truncate() will raise UnsupportedOperation.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000403 """
404 return False
405
406 def _checkWritable(self, msg=None):
Amaury Forgeot d'Arcada99482010-09-06 22:23:13 +0000407 """Internal: raise UnsupportedOperation if file is not writable
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000408 """
409 if not self.writable():
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000410 raise UnsupportedOperation("File or stream is not writable."
411 if msg is None else msg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000412
413 @property
414 def closed(self):
415 """closed: bool. True iff the file has been closed.
416
417 For backwards compatibility, this is a property, not a predicate.
418 """
419 return self.__closed
420
421 def _checkClosed(self, msg=None):
422 """Internal: raise an ValueError if file is closed
423 """
424 if self.closed:
425 raise ValueError("I/O operation on closed file."
426 if msg is None else msg)
427
428 ### Context manager ###
429
Raymond Hettinger3c940242011-01-12 23:39:31 +0000430 def __enter__(self): # That's a forward reference
Raymond Hettingercbb80892011-01-13 18:15:51 +0000431 """Context management protocol. Returns self (an instance of IOBase)."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000432 self._checkClosed()
433 return self
434
Raymond Hettinger3c940242011-01-12 23:39:31 +0000435 def __exit__(self, *args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000436 """Context management protocol. Calls close()"""
437 self.close()
438
439 ### Lower-level APIs ###
440
441 # XXX Should these be present even if unimplemented?
442
Raymond Hettinger3c940242011-01-12 23:39:31 +0000443 def fileno(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000444 """Returns underlying file descriptor (an int) if one exists.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000445
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200446 An OSError is raised if the IO object does not use a file descriptor.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000447 """
448 self._unsupported("fileno")
449
Raymond Hettinger3c940242011-01-12 23:39:31 +0000450 def isatty(self):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000451 """Return a bool indicating whether this is an 'interactive' stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000452
453 Return False if it can't be determined.
454 """
455 self._checkClosed()
456 return False
457
458 ### Readline[s] and writelines ###
459
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300460 def readline(self, size=-1):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000461 r"""Read and return a line of bytes from the stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300463 If size is specified, at most size bytes will be read.
464 Size should be an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465
466 The line terminator is always b'\n' for binary files; for text
467 files, the newlines argument to open can be used to select the line
468 terminator(s) recognized.
469 """
470 # For backwards compatibility, a (slowish) readline().
471 if hasattr(self, "peek"):
472 def nreadahead():
473 readahead = self.peek(1)
474 if not readahead:
475 return 1
476 n = (readahead.find(b"\n") + 1) or len(readahead)
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300477 if size >= 0:
478 n = min(n, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000479 return n
480 else:
481 def nreadahead():
482 return 1
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300483 if size is None:
484 size = -1
485 elif not isinstance(size, int):
486 raise TypeError("size must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000487 res = bytearray()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300488 while size < 0 or len(res) < size:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000489 b = self.read(nreadahead())
490 if not b:
491 break
492 res += b
493 if res.endswith(b"\n"):
494 break
495 return bytes(res)
496
497 def __iter__(self):
498 self._checkClosed()
499 return self
500
501 def __next__(self):
502 line = self.readline()
503 if not line:
504 raise StopIteration
505 return line
506
507 def readlines(self, hint=None):
508 """Return a list of lines from the stream.
509
510 hint can be specified to control the number of lines read: no more
511 lines will be read if the total size (in bytes/characters) of all
512 lines so far exceeds hint.
513 """
514 if hint is None or hint <= 0:
515 return list(self)
516 n = 0
517 lines = []
518 for line in self:
519 lines.append(line)
520 n += len(line)
521 if n >= hint:
522 break
523 return lines
524
525 def writelines(self, lines):
526 self._checkClosed()
527 for line in lines:
528 self.write(line)
529
530io.IOBase.register(IOBase)
531
532
533class RawIOBase(IOBase):
534
535 """Base class for raw binary I/O."""
536
537 # The read() method is implemented by calling readinto(); derived
538 # classes that want to support read() only need to implement
539 # readinto() as a primitive operation. In general, readinto() can be
540 # more efficient than read().
541
542 # (It would be tempting to also provide an implementation of
543 # readinto() in terms of read(), in case the latter is a more suitable
544 # primitive operation, but that would lead to nasty recursion in case
545 # a subclass doesn't implement either.)
546
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300547 def read(self, size=-1):
548 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000549
550 Returns an empty bytes object on EOF, or None if the object is
551 set not to block and has no data to read.
552 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300553 if size is None:
554 size = -1
555 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000556 return self.readall()
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300557 b = bytearray(size.__index__())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000558 n = self.readinto(b)
Antoine Pitrou328ec742010-09-14 18:37:24 +0000559 if n is None:
560 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000561 del b[n:]
562 return bytes(b)
563
564 def readall(self):
565 """Read until EOF, using multiple read() call."""
566 res = bytearray()
567 while True:
568 data = self.read(DEFAULT_BUFFER_SIZE)
569 if not data:
570 break
571 res += data
Victor Stinnera80987f2011-05-25 22:47:16 +0200572 if res:
573 return bytes(res)
574 else:
575 # b'' or None
576 return data
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000577
Raymond Hettinger3c940242011-01-12 23:39:31 +0000578 def readinto(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000579 """Read up to len(b) bytes into bytearray b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000580
Raymond Hettingercbb80892011-01-13 18:15:51 +0000581 Returns an int representing the number of bytes read (0 for EOF), or
582 None if the object is set not to block and has no data to read.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000583 """
584 self._unsupported("readinto")
585
Raymond Hettinger3c940242011-01-12 23:39:31 +0000586 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000587 """Write the given buffer to the IO stream.
588
589 Returns the number of bytes written, which may be less than len(b).
590 """
591 self._unsupported("write")
592
593io.RawIOBase.register(RawIOBase)
594from _io import FileIO
595RawIOBase.register(FileIO)
596
597
598class BufferedIOBase(IOBase):
599
600 """Base class for buffered IO objects.
601
602 The main difference with RawIOBase is that the read() method
603 supports omitting the size argument, and does not have a default
604 implementation that defers to readinto().
605
606 In addition, read(), readinto() and write() may raise
607 BlockingIOError if the underlying raw stream is in non-blocking
608 mode and not ready; unlike their raw counterparts, they will never
609 return None.
610
611 A typical implementation should not inherit from a RawIOBase
612 implementation, but wrap one.
613 """
614
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300615 def read(self, size=None):
616 """Read and return up to size bytes, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000617
618 If the argument is omitted, None, or negative, reads and
619 returns all data until EOF.
620
621 If the argument is positive, and the underlying raw stream is
622 not 'interactive', multiple raw reads may be issued to satisfy
623 the byte count (unless EOF is reached first). But for
624 interactive raw streams (XXX and for pipes?), at most one raw
625 read will be issued, and a short result does not imply that
626 EOF is imminent.
627
628 Returns an empty bytes array on EOF.
629
630 Raises BlockingIOError if the underlying raw stream has no
631 data at the moment.
632 """
633 self._unsupported("read")
634
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300635 def read1(self, size=None):
636 """Read up to size bytes with at most one read() system call,
637 where size is an int.
Raymond Hettingercbb80892011-01-13 18:15:51 +0000638 """
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000639 self._unsupported("read1")
640
Raymond Hettinger3c940242011-01-12 23:39:31 +0000641 def readinto(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000642 """Read up to len(b) bytes into bytearray b.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000643
644 Like read(), this may issue multiple reads to the underlying raw
645 stream, unless the latter is 'interactive'.
646
Raymond Hettingercbb80892011-01-13 18:15:51 +0000647 Returns an int representing the number of bytes read (0 for EOF).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000648
649 Raises BlockingIOError if the underlying raw stream has no
650 data at the moment.
651 """
652 # XXX This ought to work with anything that supports the buffer API
653 data = self.read(len(b))
654 n = len(data)
655 try:
656 b[:n] = data
657 except TypeError as err:
658 import array
659 if not isinstance(b, array.array):
660 raise err
661 b[:n] = array.array('b', data)
662 return n
663
Raymond Hettinger3c940242011-01-12 23:39:31 +0000664 def write(self, b):
Raymond Hettingercbb80892011-01-13 18:15:51 +0000665 """Write the given bytes buffer to the IO stream.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000666
667 Return the number of bytes written, which is never less than
668 len(b).
669
670 Raises BlockingIOError if the buffer is full and the
671 underlying raw stream cannot accept more data at the moment.
672 """
673 self._unsupported("write")
674
Raymond Hettinger3c940242011-01-12 23:39:31 +0000675 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000676 """
677 Separate the underlying raw stream from the buffer and return it.
678
679 After the raw stream has been detached, the buffer is in an unusable
680 state.
681 """
682 self._unsupported("detach")
683
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000684io.BufferedIOBase.register(BufferedIOBase)
685
686
687class _BufferedIOMixin(BufferedIOBase):
688
689 """A mixin implementation of BufferedIOBase with an underlying raw stream.
690
691 This passes most requests on to the underlying raw stream. It
692 does *not* provide implementations of read(), readinto() or
693 write().
694 """
695
696 def __init__(self, raw):
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000697 self._raw = raw
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000698
699 ### Positioning ###
700
701 def seek(self, pos, whence=0):
702 new_position = self.raw.seek(pos, whence)
703 if new_position < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200704 raise OSError("seek() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000705 return new_position
706
707 def tell(self):
708 pos = self.raw.tell()
709 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200710 raise OSError("tell() returned an invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000711 return pos
712
713 def truncate(self, pos=None):
714 # Flush the stream. We're mixing buffered I/O with lower-level I/O,
715 # and a flush may be necessary to synch both views of the current
716 # file state.
717 self.flush()
718
719 if pos is None:
720 pos = self.tell()
721 # XXX: Should seek() be used, instead of passing the position
722 # XXX directly to truncate?
723 return self.raw.truncate(pos)
724
725 ### Flush and close ###
726
727 def flush(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000728 if self.closed:
729 raise ValueError("flush of closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000730 self.raw.flush()
731
732 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +0000733 if self.raw is not None and not self.closed:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100734 try:
735 # may raise BlockingIOError or BrokenPipeError etc
736 self.flush()
737 finally:
738 self.raw.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000739
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000740 def detach(self):
741 if self.raw is None:
742 raise ValueError("raw stream already detached")
743 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000744 raw = self._raw
745 self._raw = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000746 return raw
747
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000748 ### Inquiries ###
749
750 def seekable(self):
751 return self.raw.seekable()
752
753 def readable(self):
754 return self.raw.readable()
755
756 def writable(self):
757 return self.raw.writable()
758
759 @property
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000760 def raw(self):
761 return self._raw
762
763 @property
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764 def closed(self):
765 return self.raw.closed
766
767 @property
768 def name(self):
769 return self.raw.name
770
771 @property
772 def mode(self):
773 return self.raw.mode
774
Antoine Pitrou243757e2010-11-05 21:15:39 +0000775 def __getstate__(self):
776 raise TypeError("can not serialize a '{0}' object"
777 .format(self.__class__.__name__))
778
Antoine Pitrou716c4442009-05-23 19:04:03 +0000779 def __repr__(self):
780 clsname = self.__class__.__name__
781 try:
782 name = self.name
783 except AttributeError:
784 return "<_pyio.{0}>".format(clsname)
785 else:
786 return "<_pyio.{0} name={1!r}>".format(clsname, name)
787
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788 ### Lower-level APIs ###
789
790 def fileno(self):
791 return self.raw.fileno()
792
793 def isatty(self):
794 return self.raw.isatty()
795
796
797class BytesIO(BufferedIOBase):
798
799 """Buffered I/O implementation using an in-memory bytes buffer."""
800
801 def __init__(self, initial_bytes=None):
802 buf = bytearray()
803 if initial_bytes is not None:
804 buf += initial_bytes
805 self._buffer = buf
806 self._pos = 0
807
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000808 def __getstate__(self):
809 if self.closed:
810 raise ValueError("__getstate__ on closed file")
811 return self.__dict__.copy()
812
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000813 def getvalue(self):
814 """Return the bytes value (contents) of the buffer
815 """
816 if self.closed:
817 raise ValueError("getvalue on closed file")
818 return bytes(self._buffer)
819
Antoine Pitrou972ee132010-09-06 18:48:21 +0000820 def getbuffer(self):
821 """Return a readable and writable view of the buffer.
822 """
823 return memoryview(self._buffer)
824
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300825 def read(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000826 if self.closed:
827 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300828 if size is None:
829 size = -1
830 if size < 0:
831 size = len(self._buffer)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000832 if len(self._buffer) <= self._pos:
833 return b""
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300834 newpos = min(len(self._buffer), self._pos + size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000835 b = self._buffer[self._pos : newpos]
836 self._pos = newpos
837 return bytes(b)
838
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300839 def read1(self, size):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000840 """This is the same as read.
841 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300842 return self.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000843
844 def write(self, b):
845 if self.closed:
846 raise ValueError("write to closed file")
847 if isinstance(b, str):
848 raise TypeError("can't write str to binary stream")
849 n = len(b)
850 if n == 0:
851 return 0
852 pos = self._pos
853 if pos > len(self._buffer):
854 # Inserts null bytes between the current end of the file
855 # and the new write position.
856 padding = b'\x00' * (pos - len(self._buffer))
857 self._buffer += padding
858 self._buffer[pos:pos + n] = b
859 self._pos += n
860 return n
861
862 def seek(self, pos, whence=0):
863 if self.closed:
864 raise ValueError("seek on closed file")
865 try:
Florent Xiclunab14930c2010-03-13 15:26:44 +0000866 pos.__index__
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000867 except AttributeError as err:
868 raise TypeError("an integer is required") from err
869 if whence == 0:
870 if pos < 0:
871 raise ValueError("negative seek position %r" % (pos,))
872 self._pos = pos
873 elif whence == 1:
874 self._pos = max(0, self._pos + pos)
875 elif whence == 2:
876 self._pos = max(0, len(self._buffer) + pos)
877 else:
Jesus Cea94363612012-06-22 18:32:07 +0200878 raise ValueError("unsupported whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000879 return self._pos
880
881 def tell(self):
882 if self.closed:
883 raise ValueError("tell on closed file")
884 return self._pos
885
886 def truncate(self, pos=None):
887 if self.closed:
888 raise ValueError("truncate on closed file")
889 if pos is None:
890 pos = self._pos
Florent Xiclunab14930c2010-03-13 15:26:44 +0000891 else:
892 try:
893 pos.__index__
894 except AttributeError as err:
895 raise TypeError("an integer is required") from err
896 if pos < 0:
897 raise ValueError("negative truncate position %r" % (pos,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000898 del self._buffer[pos:]
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000899 return pos
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000900
901 def readable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200902 if self.closed:
903 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904 return True
905
906 def writable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200907 if self.closed:
908 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000909 return True
910
911 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +0200912 if self.closed:
913 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000914 return True
915
916
917class BufferedReader(_BufferedIOMixin):
918
919 """BufferedReader(raw[, buffer_size])
920
921 A buffer for a readable, sequential BaseRawIO object.
922
923 The constructor creates a BufferedReader for the given readable raw
924 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE
925 is used.
926 """
927
928 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
929 """Create a new buffered reader using the given readable raw IO object.
930 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000931 if not raw.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200932 raise OSError('"raw" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +0000933
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000934 _BufferedIOMixin.__init__(self, raw)
935 if buffer_size <= 0:
936 raise ValueError("invalid buffer size")
937 self.buffer_size = buffer_size
938 self._reset_read_buf()
939 self._read_lock = Lock()
940
941 def _reset_read_buf(self):
942 self._read_buf = b""
943 self._read_pos = 0
944
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300945 def read(self, size=None):
946 """Read size bytes.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000947
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300948 Returns exactly size bytes of data unless the underlying raw IO
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000949 stream reaches EOF or if the call would block in non-blocking
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300950 mode. If size is negative, read until EOF or until read() would
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000951 block.
952 """
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300953 if size is not None and size < -1:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000954 raise ValueError("invalid number of bytes to read")
955 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +0300956 return self._read_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000957
958 def _read_unlocked(self, n=None):
959 nodata_val = b""
960 empty_values = (b"", None)
961 buf = self._read_buf
962 pos = self._read_pos
963
964 # Special case for when the number of bytes to read is unspecified.
965 if n is None or n == -1:
966 self._reset_read_buf()
Victor Stinnerb57f1082011-05-26 00:19:38 +0200967 if hasattr(self.raw, 'readall'):
968 chunk = self.raw.readall()
969 if chunk is None:
970 return buf[pos:] or None
971 else:
972 return buf[pos:] + chunk
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000973 chunks = [buf[pos:]] # Strip the consumed bytes.
974 current_size = 0
975 while True:
976 # Read until EOF or until read() would block.
Antoine Pitrou707ce822011-02-25 21:24:11 +0000977 try:
978 chunk = self.raw.read()
Antoine Pitrou24d659d2011-10-23 23:49:42 +0200979 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +0000980 continue
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000981 if chunk in empty_values:
982 nodata_val = chunk
983 break
984 current_size += len(chunk)
985 chunks.append(chunk)
986 return b"".join(chunks) or nodata_val
987
988 # The number of bytes to read is specified, return at most n bytes.
989 avail = len(buf) - pos # Length of the available buffered data.
990 if n <= avail:
991 # Fast path: the data to read is fully buffered.
992 self._read_pos += n
993 return buf[pos:pos+n]
994 # Slow path: read from the stream until enough bytes are read,
995 # or until an EOF occurs or until read() would block.
996 chunks = [buf[pos:]]
997 wanted = max(self.buffer_size, n)
998 while avail < n:
Antoine Pitrou707ce822011-02-25 21:24:11 +0000999 try:
1000 chunk = self.raw.read(wanted)
Antoine Pitrou24d659d2011-10-23 23:49:42 +02001001 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +00001002 continue
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001003 if chunk in empty_values:
1004 nodata_val = chunk
1005 break
1006 avail += len(chunk)
1007 chunks.append(chunk)
1008 # n is more then avail only when an EOF occurred or when
1009 # read() would have blocked.
1010 n = min(n, avail)
1011 out = b"".join(chunks)
1012 self._read_buf = out[n:] # Save the extra data in the buffer.
1013 self._read_pos = 0
1014 return out[:n] if out else nodata_val
1015
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001016 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001017 """Returns buffered bytes without advancing the position.
1018
1019 The argument indicates a desired minimal number of bytes; we
1020 do at most one raw read to satisfy it. We never return more
1021 than self.buffer_size.
1022 """
1023 with self._read_lock:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001024 return self._peek_unlocked(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001025
1026 def _peek_unlocked(self, n=0):
1027 want = min(n, self.buffer_size)
1028 have = len(self._read_buf) - self._read_pos
1029 if have < want or have <= 0:
1030 to_read = self.buffer_size - have
Antoine Pitrou707ce822011-02-25 21:24:11 +00001031 while True:
1032 try:
1033 current = self.raw.read(to_read)
Antoine Pitrou24d659d2011-10-23 23:49:42 +02001034 except InterruptedError:
Antoine Pitrou707ce822011-02-25 21:24:11 +00001035 continue
1036 break
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001037 if current:
1038 self._read_buf = self._read_buf[self._read_pos:] + current
1039 self._read_pos = 0
1040 return self._read_buf[self._read_pos:]
1041
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001042 def read1(self, size):
1043 """Reads up to size bytes, with at most one read() system call."""
1044 # Returns up to size bytes. If at least one byte is buffered, we
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001045 # only return buffered bytes. Otherwise, we do one raw read.
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001046 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001047 raise ValueError("number of bytes to read must be positive")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001048 if size == 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001049 return b""
1050 with self._read_lock:
1051 self._peek_unlocked(1)
1052 return self._read_unlocked(
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001053 min(size, len(self._read_buf) - self._read_pos))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001054
1055 def tell(self):
1056 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
1057
1058 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001059 if whence not in valid_seek_flags:
Jesus Cea990eff02012-04-26 17:05:31 +02001060 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001061 with self._read_lock:
1062 if whence == 1:
1063 pos -= len(self._read_buf) - self._read_pos
1064 pos = _BufferedIOMixin.seek(self, pos, whence)
1065 self._reset_read_buf()
1066 return pos
1067
1068class BufferedWriter(_BufferedIOMixin):
1069
1070 """A buffer for a writeable sequential RawIO object.
1071
1072 The constructor creates a BufferedWriter for the given writeable raw
1073 stream. If the buffer_size is not given, it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001074 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001075 """
1076
Florent Xicluna109d5732012-07-07 17:03:22 +02001077 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001078 if not raw.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001079 raise OSError('"raw" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001080
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001081 _BufferedIOMixin.__init__(self, raw)
1082 if buffer_size <= 0:
1083 raise ValueError("invalid buffer size")
1084 self.buffer_size = buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001085 self._write_buf = bytearray()
1086 self._write_lock = Lock()
1087
1088 def write(self, b):
1089 if self.closed:
1090 raise ValueError("write to closed file")
1091 if isinstance(b, str):
1092 raise TypeError("can't write str to binary stream")
1093 with self._write_lock:
1094 # XXX we can implement some more tricks to try and avoid
1095 # partial writes
1096 if len(self._write_buf) > self.buffer_size:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001097 # We're full, so let's pre-flush the buffer. (This may
1098 # raise BlockingIOError with characters_written == 0.)
1099 self._flush_unlocked()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001100 before = len(self._write_buf)
1101 self._write_buf.extend(b)
1102 written = len(self._write_buf) - before
1103 if len(self._write_buf) > self.buffer_size:
1104 try:
1105 self._flush_unlocked()
1106 except BlockingIOError as e:
Benjamin Peterson394ee002009-03-05 22:33:59 +00001107 if len(self._write_buf) > self.buffer_size:
1108 # We've hit the buffer_size. We have to accept a partial
1109 # write and cut back our buffer.
1110 overage = len(self._write_buf) - self.buffer_size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001111 written -= overage
Benjamin Peterson394ee002009-03-05 22:33:59 +00001112 self._write_buf = self._write_buf[:self.buffer_size]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001113 raise BlockingIOError(e.errno, e.strerror, written)
1114 return written
1115
1116 def truncate(self, pos=None):
1117 with self._write_lock:
1118 self._flush_unlocked()
1119 if pos is None:
1120 pos = self.raw.tell()
1121 return self.raw.truncate(pos)
1122
1123 def flush(self):
1124 with self._write_lock:
1125 self._flush_unlocked()
1126
1127 def _flush_unlocked(self):
1128 if self.closed:
1129 raise ValueError("flush of closed file")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001130 while self._write_buf:
1131 try:
1132 n = self.raw.write(self._write_buf)
Antoine Pitrou7fe601c2011-11-21 20:22:01 +01001133 except InterruptedError:
1134 continue
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001135 except BlockingIOError:
1136 raise RuntimeError("self.raw should implement RawIOBase: it "
1137 "should not raise BlockingIOError")
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001138 if n is None:
1139 raise BlockingIOError(
1140 errno.EAGAIN,
1141 "write could not complete without blocking", 0)
1142 if n > len(self._write_buf) or n < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001143 raise OSError("write() returned incorrect number of bytes")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001144 del self._write_buf[:n]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001145
1146 def tell(self):
1147 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1148
1149 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001150 if whence not in valid_seek_flags:
1151 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001152 with self._write_lock:
1153 self._flush_unlocked()
1154 return _BufferedIOMixin.seek(self, pos, whence)
1155
1156
1157class BufferedRWPair(BufferedIOBase):
1158
1159 """A buffered reader and writer object together.
1160
1161 A buffered reader object and buffered writer object put together to
1162 form a sequential IO object that can read and write. This is typically
1163 used with a socket or two-way pipe.
1164
1165 reader and writer are RawIOBase objects that are readable and
1166 writeable respectively. If the buffer_size is omitted it defaults to
Benjamin Peterson59406a92009-03-26 17:10:29 +00001167 DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001168 """
1169
1170 # XXX The usefulness of this (compared to having two separate IO
1171 # objects) is questionable.
1172
Florent Xicluna109d5732012-07-07 17:03:22 +02001173 def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001174 """Constructor.
1175
1176 The arguments are two RawIO instances.
1177 """
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001178 if not reader.readable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001179 raise OSError('"reader" argument must be readable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001180
1181 if not writer.writable():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001182 raise OSError('"writer" argument must be writable.')
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001183
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001184 self.reader = BufferedReader(reader, buffer_size)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001185 self.writer = BufferedWriter(writer, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001186
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001187 def read(self, size=None):
1188 if size is None:
1189 size = -1
1190 return self.reader.read(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001191
1192 def readinto(self, b):
1193 return self.reader.readinto(b)
1194
1195 def write(self, b):
1196 return self.writer.write(b)
1197
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001198 def peek(self, size=0):
1199 return self.reader.peek(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001200
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001201 def read1(self, size):
1202 return self.reader.read1(size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001203
1204 def readable(self):
1205 return self.reader.readable()
1206
1207 def writable(self):
1208 return self.writer.writable()
1209
1210 def flush(self):
1211 return self.writer.flush()
1212
1213 def close(self):
1214 self.writer.close()
1215 self.reader.close()
1216
1217 def isatty(self):
1218 return self.reader.isatty() or self.writer.isatty()
1219
1220 @property
1221 def closed(self):
1222 return self.writer.closed
1223
1224
1225class BufferedRandom(BufferedWriter, BufferedReader):
1226
1227 """A buffered interface to random access streams.
1228
1229 The constructor creates a reader and writer for a seekable stream,
1230 raw, given in the first argument. If the buffer_size is omitted it
Benjamin Peterson59406a92009-03-26 17:10:29 +00001231 defaults to DEFAULT_BUFFER_SIZE.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001232 """
1233
Florent Xicluna109d5732012-07-07 17:03:22 +02001234 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001235 raw._checkSeekable()
1236 BufferedReader.__init__(self, raw, buffer_size)
Florent Xicluna109d5732012-07-07 17:03:22 +02001237 BufferedWriter.__init__(self, raw, buffer_size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001238
1239 def seek(self, pos, whence=0):
Jesus Cea94363612012-06-22 18:32:07 +02001240 if whence not in valid_seek_flags:
1241 raise ValueError("invalid whence value")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001242 self.flush()
1243 if self._read_buf:
1244 # Undo read ahead.
1245 with self._read_lock:
1246 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1247 # First do the raw seek, then empty the read buffer, so that
1248 # if the raw seek fails, we don't lose buffered data forever.
1249 pos = self.raw.seek(pos, whence)
1250 with self._read_lock:
1251 self._reset_read_buf()
1252 if pos < 0:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001253 raise OSError("seek() returned invalid position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001254 return pos
1255
1256 def tell(self):
1257 if self._write_buf:
1258 return BufferedWriter.tell(self)
1259 else:
1260 return BufferedReader.tell(self)
1261
1262 def truncate(self, pos=None):
1263 if pos is None:
1264 pos = self.tell()
1265 # Use seek to flush the read buffer.
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001266 return BufferedWriter.truncate(self, pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001267
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001268 def read(self, size=None):
1269 if size is None:
1270 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001271 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001272 return BufferedReader.read(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001273
1274 def readinto(self, b):
1275 self.flush()
1276 return BufferedReader.readinto(self, b)
1277
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001278 def peek(self, size=0):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001279 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001280 return BufferedReader.peek(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001281
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001282 def read1(self, size):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001283 self.flush()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001284 return BufferedReader.read1(self, size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001285
1286 def write(self, b):
1287 if self._read_buf:
1288 # Undo readahead
1289 with self._read_lock:
1290 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1291 self._reset_read_buf()
1292 return BufferedWriter.write(self, b)
1293
1294
1295class TextIOBase(IOBase):
1296
1297 """Base class for text I/O.
1298
1299 This class provides a character and line based interface to stream
1300 I/O. There is no readinto method because Python's character strings
1301 are immutable. There is no public constructor.
1302 """
1303
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001304 def read(self, size=-1):
1305 """Read at most size characters from stream, where size is an int.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001306
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001307 Read from underlying buffer until we have size characters or we hit EOF.
1308 If size is negative or omitted, read until EOF.
Raymond Hettingercbb80892011-01-13 18:15:51 +00001309
1310 Returns a string.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001311 """
1312 self._unsupported("read")
1313
Raymond Hettinger3c940242011-01-12 23:39:31 +00001314 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001315 """Write string s to stream and returning an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001316 self._unsupported("write")
1317
Georg Brandl4d73b572011-01-13 07:13:06 +00001318 def truncate(self, pos=None):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001319 """Truncate size to pos, where pos is an int."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001320 self._unsupported("truncate")
1321
Raymond Hettinger3c940242011-01-12 23:39:31 +00001322 def readline(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001323 """Read until newline or EOF.
1324
1325 Returns an empty string if EOF is hit immediately.
1326 """
1327 self._unsupported("readline")
1328
Raymond Hettinger3c940242011-01-12 23:39:31 +00001329 def detach(self):
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001330 """
1331 Separate the underlying buffer from the TextIOBase and return it.
1332
1333 After the underlying buffer has been detached, the TextIO is in an
1334 unusable state.
1335 """
1336 self._unsupported("detach")
1337
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001338 @property
1339 def encoding(self):
1340 """Subclasses should override."""
1341 return None
1342
1343 @property
1344 def newlines(self):
1345 """Line endings translated so far.
1346
1347 Only line endings translated during reading are considered.
1348
1349 Subclasses should override.
1350 """
1351 return None
1352
Benjamin Peterson0926ad12009-06-06 18:02:12 +00001353 @property
1354 def errors(self):
1355 """Error setting of the decoder or encoder.
1356
1357 Subclasses should override."""
1358 return None
1359
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001360io.TextIOBase.register(TextIOBase)
1361
1362
1363class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1364 r"""Codec used when reading a file in universal newlines mode. It wraps
1365 another incremental decoder, translating \r\n and \r into \n. It also
1366 records the types of newlines encountered. When used with
1367 translate=False, it ensures that the newline sequence is returned in
1368 one piece.
1369 """
1370 def __init__(self, decoder, translate, errors='strict'):
1371 codecs.IncrementalDecoder.__init__(self, errors=errors)
1372 self.translate = translate
1373 self.decoder = decoder
1374 self.seennl = 0
1375 self.pendingcr = False
1376
1377 def decode(self, input, final=False):
1378 # decode input (with the eventual \r from a previous pass)
1379 if self.decoder is None:
1380 output = input
1381 else:
1382 output = self.decoder.decode(input, final=final)
1383 if self.pendingcr and (output or final):
1384 output = "\r" + output
1385 self.pendingcr = False
1386
1387 # retain last \r even when not translating data:
1388 # then readline() is sure to get \r\n in one pass
1389 if output.endswith("\r") and not final:
1390 output = output[:-1]
1391 self.pendingcr = True
1392
1393 # Record which newlines are read
1394 crlf = output.count('\r\n')
1395 cr = output.count('\r') - crlf
1396 lf = output.count('\n') - crlf
1397 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1398 | (crlf and self._CRLF)
1399
1400 if self.translate:
1401 if crlf:
1402 output = output.replace("\r\n", "\n")
1403 if cr:
1404 output = output.replace("\r", "\n")
1405
1406 return output
1407
1408 def getstate(self):
1409 if self.decoder is None:
1410 buf = b""
1411 flag = 0
1412 else:
1413 buf, flag = self.decoder.getstate()
1414 flag <<= 1
1415 if self.pendingcr:
1416 flag |= 1
1417 return buf, flag
1418
1419 def setstate(self, state):
1420 buf, flag = state
1421 self.pendingcr = bool(flag & 1)
1422 if self.decoder is not None:
1423 self.decoder.setstate((buf, flag >> 1))
1424
1425 def reset(self):
1426 self.seennl = 0
1427 self.pendingcr = False
1428 if self.decoder is not None:
1429 self.decoder.reset()
1430
1431 _LF = 1
1432 _CR = 2
1433 _CRLF = 4
1434
1435 @property
1436 def newlines(self):
1437 return (None,
1438 "\n",
1439 "\r",
1440 ("\r", "\n"),
1441 "\r\n",
1442 ("\n", "\r\n"),
1443 ("\r", "\r\n"),
1444 ("\r", "\n", "\r\n")
1445 )[self.seennl]
1446
1447
1448class TextIOWrapper(TextIOBase):
1449
1450 r"""Character and line based layer over a BufferedIOBase object, buffer.
1451
1452 encoding gives the name of the encoding that the stream will be
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001453 decoded or encoded with. It defaults to locale.getpreferredencoding(False).
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001454
1455 errors determines the strictness of encoding and decoding (see the
1456 codecs.register) and defaults to "strict".
1457
1458 newline can be None, '', '\n', '\r', or '\r\n'. It controls the
1459 handling of line endings. If it is None, universal newlines is
1460 enabled. With this enabled, on input, the lines endings '\n', '\r',
1461 or '\r\n' are translated to '\n' before being returned to the
1462 caller. Conversely, on output, '\n' is translated to the system
Éric Araujo39242302011-11-03 00:08:48 +01001463 default line separator, os.linesep. If newline is any other of its
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001464 legal values, that newline becomes the newline when the file is read
1465 and it is returned untranslated. On output, '\n' is converted to the
1466 newline.
1467
1468 If line_buffering is True, a call to flush is implied when a call to
1469 write contains a newline character.
1470 """
1471
1472 _CHUNK_SIZE = 2048
1473
Andrew Svetlov4e9e9c12012-08-13 16:09:54 +03001474 # The write_through argument has no effect here since this
1475 # implementation always writes through. The argument is present only
1476 # so that the signature can match the signature of the C version.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001477 def __init__(self, buffer, encoding=None, errors=None, newline=None,
Antoine Pitroue96ec682011-07-23 21:46:35 +02001478 line_buffering=False, write_through=False):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001479 if newline is not None and not isinstance(newline, str):
1480 raise TypeError("illegal newline type: %r" % (type(newline),))
1481 if newline not in (None, "", "\n", "\r", "\r\n"):
1482 raise ValueError("illegal newline value: %r" % (newline,))
1483 if encoding is None:
1484 try:
1485 encoding = os.device_encoding(buffer.fileno())
1486 except (AttributeError, UnsupportedOperation):
1487 pass
1488 if encoding is None:
1489 try:
1490 import locale
Brett Cannoncd171c82013-07-04 17:43:24 -04001491 except ImportError:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001492 # Importing locale may fail if Python is being built
1493 encoding = "ascii"
1494 else:
Victor Stinnerf86a5e82012-06-05 13:43:22 +02001495 encoding = locale.getpreferredencoding(False)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001496
1497 if not isinstance(encoding, str):
1498 raise ValueError("invalid encoding: %r" % encoding)
1499
1500 if errors is None:
1501 errors = "strict"
1502 else:
1503 if not isinstance(errors, str):
1504 raise ValueError("invalid errors: %r" % errors)
1505
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001506 self._buffer = buffer
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001507 self._line_buffering = line_buffering
1508 self._encoding = encoding
1509 self._errors = errors
1510 self._readuniversal = not newline
1511 self._readtranslate = newline is None
1512 self._readnl = newline
1513 self._writetranslate = newline != ''
1514 self._writenl = newline or os.linesep
1515 self._encoder = None
1516 self._decoder = None
1517 self._decoded_chars = '' # buffer for text returned from decoder
1518 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1519 self._snapshot = None # info for reconstructing decoder state
1520 self._seekable = self._telling = self.buffer.seekable()
Antoine Pitroue96ec682011-07-23 21:46:35 +02001521 self._has_read1 = hasattr(self.buffer, 'read1')
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001522 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001523
Antoine Pitroue4501852009-05-14 18:55:55 +00001524 if self._seekable and self.writable():
1525 position = self.buffer.tell()
1526 if position != 0:
1527 try:
1528 self._get_encoder().setstate(0)
1529 except LookupError:
1530 # Sometimes the encoder doesn't exist
1531 pass
1532
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001533 # self._snapshot is either None, or a tuple (dec_flags, next_input)
1534 # where dec_flags is the second (integer) item of the decoder state
1535 # and next_input is the chunk of input bytes that comes next after the
1536 # snapshot point. We use this to reconstruct decoder states in tell().
1537
1538 # Naming convention:
1539 # - "bytes_..." for integer variables that count input bytes
1540 # - "chars_..." for integer variables that count decoded characters
1541
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001542 def __repr__(self):
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001543 result = "<_pyio.TextIOWrapper"
Antoine Pitrou716c4442009-05-23 19:04:03 +00001544 try:
1545 name = self.name
1546 except AttributeError:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001547 pass
Antoine Pitrou716c4442009-05-23 19:04:03 +00001548 else:
Antoine Pitroua4815ca2011-01-09 20:38:15 +00001549 result += " name={0!r}".format(name)
1550 try:
1551 mode = self.mode
1552 except AttributeError:
1553 pass
1554 else:
1555 result += " mode={0!r}".format(mode)
1556 return result + " encoding={0!r}>".format(self.encoding)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00001557
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001558 @property
1559 def encoding(self):
1560 return self._encoding
1561
1562 @property
1563 def errors(self):
1564 return self._errors
1565
1566 @property
1567 def line_buffering(self):
1568 return self._line_buffering
1569
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001570 @property
1571 def buffer(self):
1572 return self._buffer
1573
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001574 def seekable(self):
Antoine Pitrou1d857452012-09-05 20:11:49 +02001575 if self.closed:
1576 raise ValueError("I/O operation on closed file.")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001577 return self._seekable
1578
1579 def readable(self):
1580 return self.buffer.readable()
1581
1582 def writable(self):
1583 return self.buffer.writable()
1584
1585 def flush(self):
1586 self.buffer.flush()
1587 self._telling = self._seekable
1588
1589 def close(self):
Antoine Pitrou6be88762010-05-03 16:48:20 +00001590 if self.buffer is not None and not self.closed:
Benjamin Peterson68623612012-12-20 11:53:11 -06001591 try:
1592 self.flush()
1593 finally:
1594 self.buffer.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001595
1596 @property
1597 def closed(self):
1598 return self.buffer.closed
1599
1600 @property
1601 def name(self):
1602 return self.buffer.name
1603
1604 def fileno(self):
1605 return self.buffer.fileno()
1606
1607 def isatty(self):
1608 return self.buffer.isatty()
1609
Raymond Hettinger00fa0392011-01-13 02:52:26 +00001610 def write(self, s):
Raymond Hettingercbb80892011-01-13 18:15:51 +00001611 'Write data, where s is a str'
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001612 if self.closed:
1613 raise ValueError("write to closed file")
1614 if not isinstance(s, str):
1615 raise TypeError("can't write %s to text stream" %
1616 s.__class__.__name__)
1617 length = len(s)
1618 haslf = (self._writetranslate or self._line_buffering) and "\n" in s
1619 if haslf and self._writetranslate and self._writenl != "\n":
1620 s = s.replace("\n", self._writenl)
1621 encoder = self._encoder or self._get_encoder()
1622 # XXX What if we were just reading?
1623 b = encoder.encode(s)
1624 self.buffer.write(b)
1625 if self._line_buffering and (haslf or "\r" in s):
1626 self.flush()
1627 self._snapshot = None
1628 if self._decoder:
1629 self._decoder.reset()
1630 return length
1631
1632 def _get_encoder(self):
1633 make_encoder = codecs.getincrementalencoder(self._encoding)
1634 self._encoder = make_encoder(self._errors)
1635 return self._encoder
1636
1637 def _get_decoder(self):
1638 make_decoder = codecs.getincrementaldecoder(self._encoding)
1639 decoder = make_decoder(self._errors)
1640 if self._readuniversal:
1641 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)
1642 self._decoder = decoder
1643 return decoder
1644
1645 # The following three methods implement an ADT for _decoded_chars.
1646 # Text returned from the decoder is buffered here until the client
1647 # requests it by calling our read() or readline() method.
1648 def _set_decoded_chars(self, chars):
1649 """Set the _decoded_chars buffer."""
1650 self._decoded_chars = chars
1651 self._decoded_chars_used = 0
1652
1653 def _get_decoded_chars(self, n=None):
1654 """Advance into the _decoded_chars buffer."""
1655 offset = self._decoded_chars_used
1656 if n is None:
1657 chars = self._decoded_chars[offset:]
1658 else:
1659 chars = self._decoded_chars[offset:offset + n]
1660 self._decoded_chars_used += len(chars)
1661 return chars
1662
1663 def _rewind_decoded_chars(self, n):
1664 """Rewind the _decoded_chars buffer."""
1665 if self._decoded_chars_used < n:
1666 raise AssertionError("rewind decoded_chars out of bounds")
1667 self._decoded_chars_used -= n
1668
1669 def _read_chunk(self):
1670 """
1671 Read and decode the next chunk of data from the BufferedReader.
1672 """
1673
1674 # The return value is True unless EOF was reached. The decoded
1675 # string is placed in self._decoded_chars (replacing its previous
1676 # value). The entire input chunk is sent to the decoder, though
1677 # some of it may remain buffered in the decoder, yet to be
1678 # converted.
1679
1680 if self._decoder is None:
1681 raise ValueError("no decoder")
1682
1683 if self._telling:
1684 # To prepare for tell(), we need to snapshot a point in the
1685 # file where the decoder's input buffer is empty.
1686
1687 dec_buffer, dec_flags = self._decoder.getstate()
1688 # Given this, we know there was a valid snapshot point
1689 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1690
1691 # Read a chunk, decode it, and put the result in self._decoded_chars.
Antoine Pitroue96ec682011-07-23 21:46:35 +02001692 if self._has_read1:
1693 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1694 else:
1695 input_chunk = self.buffer.read(self._CHUNK_SIZE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001696 eof = not input_chunk
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001697 decoded_chars = self._decoder.decode(input_chunk, eof)
1698 self._set_decoded_chars(decoded_chars)
1699 if decoded_chars:
1700 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
1701 else:
1702 self._b2cratio = 0.0
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001703
1704 if self._telling:
1705 # At the snapshot point, len(dec_buffer) bytes before the read,
1706 # the next input to be decoded is dec_buffer + input_chunk.
1707 self._snapshot = (dec_flags, dec_buffer + input_chunk)
1708
1709 return not eof
1710
1711 def _pack_cookie(self, position, dec_flags=0,
1712 bytes_to_feed=0, need_eof=0, chars_to_skip=0):
1713 # The meaning of a tell() cookie is: seek to position, set the
1714 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them
1715 # into the decoder with need_eof as the EOF flag, then skip
1716 # chars_to_skip characters of the decoded result. For most simple
1717 # decoders, tell() will often just give a byte offset in the file.
1718 return (position | (dec_flags<<64) | (bytes_to_feed<<128) |
1719 (chars_to_skip<<192) | bool(need_eof)<<256)
1720
1721 def _unpack_cookie(self, bigint):
1722 rest, position = divmod(bigint, 1<<64)
1723 rest, dec_flags = divmod(rest, 1<<64)
1724 rest, bytes_to_feed = divmod(rest, 1<<64)
1725 need_eof, chars_to_skip = divmod(rest, 1<<64)
1726 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip
1727
1728 def tell(self):
1729 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001730 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001731 if not self._telling:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001732 raise OSError("telling position disabled by next() call")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001733 self.flush()
1734 position = self.buffer.tell()
1735 decoder = self._decoder
1736 if decoder is None or self._snapshot is None:
1737 if self._decoded_chars:
1738 # This should never happen.
1739 raise AssertionError("pending decoded text")
1740 return position
1741
1742 # Skip backward to the snapshot point (see _read_chunk).
1743 dec_flags, next_input = self._snapshot
1744 position -= len(next_input)
1745
1746 # How many decoded characters have been used up since the snapshot?
1747 chars_to_skip = self._decoded_chars_used
1748 if chars_to_skip == 0:
1749 # We haven't moved from the snapshot point.
1750 return self._pack_cookie(position, dec_flags)
1751
1752 # Starting from the snapshot position, we will walk the decoder
1753 # forward until it gives us enough decoded characters.
1754 saved_state = decoder.getstate()
1755 try:
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001756 # Fast search for an acceptable start point, close to our
1757 # current pos.
1758 # Rationale: calling decoder.decode() has a large overhead
1759 # regardless of chunk size; we want the number of such calls to
1760 # be O(1) in most situations (common decoders, non-crazy input).
1761 # Actually, it will be exactly 1 for fixed-size codecs (all
1762 # 8-bit codecs, also UTF-16 and UTF-32).
1763 skip_bytes = int(self._b2cratio * chars_to_skip)
1764 skip_back = 1
1765 assert skip_bytes <= len(next_input)
1766 while skip_bytes > 0:
1767 decoder.setstate((b'', dec_flags))
1768 # Decode up to temptative start point
1769 n = len(decoder.decode(next_input[:skip_bytes]))
1770 if n <= chars_to_skip:
1771 b, d = decoder.getstate()
1772 if not b:
1773 # Before pos and no bytes buffered in decoder => OK
1774 dec_flags = d
1775 chars_to_skip -= n
1776 break
1777 # Skip back by buffered amount and reset heuristic
1778 skip_bytes -= len(b)
1779 skip_back = 1
1780 else:
1781 # We're too far ahead, skip back a bit
1782 skip_bytes -= skip_back
1783 skip_back = skip_back * 2
1784 else:
1785 skip_bytes = 0
1786 decoder.setstate((b'', dec_flags))
1787
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001788 # Note our initial start point.
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001789 start_pos = position + skip_bytes
1790 start_flags = dec_flags
1791 if chars_to_skip == 0:
1792 # We haven't moved from the start point.
1793 return self._pack_cookie(start_pos, start_flags)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001794
1795 # Feed the decoder one byte at a time. As we go, note the
1796 # nearest "safe start point" before the current location
1797 # (a point where the decoder has nothing buffered, so seek()
1798 # can safely start from there and advance to this location).
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001799 bytes_fed = 0
1800 need_eof = 0
1801 # Chars decoded since `start_pos`
1802 chars_decoded = 0
1803 for i in range(skip_bytes, len(next_input)):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001804 bytes_fed += 1
Antoine Pitrou211b81d2011-02-25 20:27:33 +00001805 chars_decoded += len(decoder.decode(next_input[i:i+1]))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001806 dec_buffer, dec_flags = decoder.getstate()
1807 if not dec_buffer and chars_decoded <= chars_to_skip:
1808 # Decoder buffer is empty, so this is a safe start point.
1809 start_pos += bytes_fed
1810 chars_to_skip -= chars_decoded
1811 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0
1812 if chars_decoded >= chars_to_skip:
1813 break
1814 else:
1815 # We didn't get enough decoded data; signal EOF to get more.
1816 chars_decoded += len(decoder.decode(b'', final=True))
1817 need_eof = 1
1818 if chars_decoded < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001819 raise OSError("can't reconstruct logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001820
1821 # The returned cookie corresponds to the last safe start point.
1822 return self._pack_cookie(
1823 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip)
1824 finally:
1825 decoder.setstate(saved_state)
1826
1827 def truncate(self, pos=None):
1828 self.flush()
1829 if pos is None:
1830 pos = self.tell()
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001831 return self.buffer.truncate(pos)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001832
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001833 def detach(self):
1834 if self.buffer is None:
1835 raise ValueError("buffer is already detached")
1836 self.flush()
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00001837 buffer = self._buffer
1838 self._buffer = None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001839 return buffer
1840
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001841 def seek(self, cookie, whence=0):
1842 if self.closed:
1843 raise ValueError("tell on closed file")
1844 if not self._seekable:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001845 raise UnsupportedOperation("underlying stream is not seekable")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001846 if whence == 1: # seek relative to current position
1847 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001848 raise UnsupportedOperation("can't do nonzero cur-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001849 # Seeking to the current position should attempt to
1850 # sync the underlying buffer with the current position.
1851 whence = 0
1852 cookie = self.tell()
1853 if whence == 2: # seek relative to end of file
1854 if cookie != 0:
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001855 raise UnsupportedOperation("can't do nonzero end-relative seeks")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001856 self.flush()
1857 position = self.buffer.seek(0, 2)
1858 self._set_decoded_chars('')
1859 self._snapshot = None
1860 if self._decoder:
1861 self._decoder.reset()
1862 return position
1863 if whence != 0:
Jesus Cea94363612012-06-22 18:32:07 +02001864 raise ValueError("unsupported whence (%r)" % (whence,))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001865 if cookie < 0:
1866 raise ValueError("negative seek position %r" % (cookie,))
1867 self.flush()
1868
1869 # The strategy of seek() is to go back to the safe start point
1870 # and replay the effect of read(chars_to_skip) from there.
1871 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
1872 self._unpack_cookie(cookie)
1873
1874 # Seek back to the safe start point.
1875 self.buffer.seek(start_pos)
1876 self._set_decoded_chars('')
1877 self._snapshot = None
1878
1879 # Restore the decoder to its state from the safe start point.
Benjamin Peterson9363a652009-03-05 00:42:09 +00001880 if cookie == 0 and self._decoder:
1881 self._decoder.reset()
1882 elif self._decoder or dec_flags or chars_to_skip:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001883 self._decoder = self._decoder or self._get_decoder()
1884 self._decoder.setstate((b'', dec_flags))
1885 self._snapshot = (dec_flags, b'')
1886
1887 if chars_to_skip:
1888 # Just like _read_chunk, feed the decoder and save a snapshot.
1889 input_chunk = self.buffer.read(bytes_to_feed)
1890 self._set_decoded_chars(
1891 self._decoder.decode(input_chunk, need_eof))
1892 self._snapshot = (dec_flags, input_chunk)
1893
1894 # Skip chars_to_skip of the decoded characters.
1895 if len(self._decoded_chars) < chars_to_skip:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001896 raise OSError("can't restore logical file position")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001897 self._decoded_chars_used = chars_to_skip
1898
Antoine Pitroue4501852009-05-14 18:55:55 +00001899 # Finally, reset the encoder (merely useful for proper BOM handling)
1900 try:
1901 encoder = self._encoder or self._get_encoder()
1902 except LookupError:
1903 # Sometimes the encoder doesn't exist
1904 pass
1905 else:
1906 if cookie != 0:
1907 encoder.setstate(0)
1908 else:
1909 encoder.reset()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001910 return cookie
1911
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001912 def read(self, size=None):
Benjamin Petersona1b49012009-03-31 23:11:32 +00001913 self._checkReadable()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001914 if size is None:
1915 size = -1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001916 decoder = self._decoder or self._get_decoder()
Florent Xiclunab14930c2010-03-13 15:26:44 +00001917 try:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001918 size.__index__
Florent Xiclunab14930c2010-03-13 15:26:44 +00001919 except AttributeError as err:
1920 raise TypeError("an integer is required") from err
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001921 if size < 0:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001922 # Read everything.
1923 result = (self._get_decoded_chars() +
1924 decoder.decode(self.buffer.read(), final=True))
1925 self._set_decoded_chars('')
1926 self._snapshot = None
1927 return result
1928 else:
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001929 # Keep reading chunks until we have size characters to return.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001930 eof = False
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001931 result = self._get_decoded_chars(size)
1932 while len(result) < size and not eof:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001933 eof = not self._read_chunk()
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001934 result += self._get_decoded_chars(size - len(result))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001935 return result
1936
1937 def __next__(self):
1938 self._telling = False
1939 line = self.readline()
1940 if not line:
1941 self._snapshot = None
1942 self._telling = self._seekable
1943 raise StopIteration
1944 return line
1945
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001946 def readline(self, size=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001947 if self.closed:
1948 raise ValueError("read from closed file")
Serhiy Storchaka3c411542013-09-16 23:18:10 +03001949 if size is None:
1950 size = -1
1951 elif not isinstance(size, int):
1952 raise TypeError("size must be an integer")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001953
1954 # Grab all the decoded text (we will rewind any extra bits later).
1955 line = self._get_decoded_chars()
1956
1957 start = 0
1958 # Make the decoder if it doesn't already exist.
1959 if not self._decoder:
1960 self._get_decoder()
1961
1962 pos = endpos = None
1963 while True:
1964 if self._readtranslate:
1965 # Newlines are already translated, only search for \n
1966 pos = line.find('\n', start)
1967 if pos >= 0:
1968 endpos = pos + 1
1969 break
1970 else:
1971 start = len(line)
1972
1973 elif self._readuniversal:
1974 # Universal newline search. Find any of \r, \r\n, \n
1975 # The decoder ensures that \r\n are not split in two pieces
1976
1977 # In C we'd look for these in parallel of course.
1978 nlpos = line.find("\n", start)
1979 crpos = line.find("\r", start)
1980 if crpos == -1:
1981 if nlpos == -1:
1982 # Nothing found
1983 start = len(line)
1984 else:
1985 # Found \n
1986 endpos = nlpos + 1
1987 break
1988 elif nlpos == -1:
1989 # Found lone \r
1990 endpos = crpos + 1
1991 break
1992 elif nlpos < crpos:
1993 # Found \n
1994 endpos = nlpos + 1
1995 break
1996 elif nlpos == crpos + 1:
1997 # Found \r\n
1998 endpos = crpos + 2
1999 break
2000 else:
2001 # Found \r
2002 endpos = crpos + 1
2003 break
2004 else:
2005 # non-universal
2006 pos = line.find(self._readnl)
2007 if pos >= 0:
2008 endpos = pos + len(self._readnl)
2009 break
2010
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002011 if size >= 0 and len(line) >= size:
2012 endpos = size # reached length size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002013 break
2014
2015 # No line ending seen yet - get more data'
2016 while self._read_chunk():
2017 if self._decoded_chars:
2018 break
2019 if self._decoded_chars:
2020 line += self._get_decoded_chars()
2021 else:
2022 # end of file
2023 self._set_decoded_chars('')
2024 self._snapshot = None
2025 return line
2026
Serhiy Storchaka3c411542013-09-16 23:18:10 +03002027 if size >= 0 and endpos > size:
2028 endpos = size # don't exceed size
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002029
2030 # Rewind _decoded_chars to just after the line ending we found.
2031 self._rewind_decoded_chars(len(line) - endpos)
2032 return line[:endpos]
2033
2034 @property
2035 def newlines(self):
2036 return self._decoder.newlines if self._decoder else None
2037
2038
2039class StringIO(TextIOWrapper):
2040 """Text I/O implementation using an in-memory buffer.
2041
2042 The initial_value argument sets the value of object. The newline
2043 argument is like the one of TextIOWrapper's constructor.
2044 """
2045
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002046 def __init__(self, initial_value="", newline="\n"):
2047 super(StringIO, self).__init__(BytesIO(),
2048 encoding="utf-8",
2049 errors="strict",
2050 newline=newline)
Antoine Pitrou11446482009-04-04 14:09:30 +00002051 # Issue #5645: make universal newlines semantics the same as in the
2052 # C version, even under Windows.
2053 if newline is None:
2054 self._writetranslate = False
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002055 if initial_value is not None:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002056 if not isinstance(initial_value, str):
Alexandre Vassalottid2bb18b2009-07-22 03:07:33 +00002057 raise TypeError("initial_value must be str or None, not {0}"
2058 .format(type(initial_value).__name__))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002059 self.write(initial_value)
2060 self.seek(0)
2061
2062 def getvalue(self):
2063 self.flush()
2064 return self.buffer.getvalue().decode(self._encoding, self._errors)
Benjamin Peterson9fd459a2009-03-09 00:09:44 +00002065
2066 def __repr__(self):
2067 # TextIOWrapper tells the encoding in its repr. In StringIO,
2068 # that's a implementation detail.
2069 return object.__repr__(self)
Benjamin Petersonb487e632009-03-21 03:08:31 +00002070
2071 @property
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002072 def errors(self):
2073 return None
2074
2075 @property
Benjamin Petersonb487e632009-03-21 03:08:31 +00002076 def encoding(self):
2077 return None
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002078
2079 def detach(self):
2080 # This doesn't make sense on StringIO.
2081 self._unsupported("detach")