blob: 4dfab22ee5590039fa87006edebb54dc8e57992e [file] [log] [blame]
Guido van Rossum53807da2007-04-10 19:01:47 +00001"""New I/O library conforming to PEP 3116.
Guido van Rossum28524c72007-02-27 05:47:44 +00002
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00003This is a prototype; hopefully eventually some of this will be
4reimplemented in C.
Guido van Rossum17e43e52007-02-27 15:45:13 +00005
Guido van Rossum53807da2007-04-10 19:01:47 +00006Conformance of alternative implementations: all arguments are intended
7to be positional-only except the arguments of the open() function.
8Argument names except those of the open() function are not part of the
9specification. Instance variables and methods whose name starts with
10a leading underscore are not part of the specification (except "magic"
11names like __iter__). Only the top-level names listed in the __all__
12variable are part of the specification.
Guido van Rossumc819dea2007-03-15 18:59:31 +000013
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +000014XXX edge cases when switching between reading/writing
Guido van Rossumc819dea2007-03-15 18:59:31 +000015XXX need to default buffer size to 1 if isatty()
16XXX need to support 1 meaning line-buffered
Guido van Rossum76c5d4d2007-04-06 19:10:29 +000017XXX don't use assert to validate input requirements
Guido van Rossum9b76da62007-04-11 01:09:03 +000018XXX whenever an argument is None, use the default value
19XXX read/write ops should check readable/writable
Guido van Rossumd4103952007-04-12 05:44:49 +000020XXX buffered readinto should work with arbitrary buffer objects
Guido van Rossumd76e7792007-04-17 02:38:04 +000021XXX use incremental encoder for text output, at least for UTF-16 and UTF-8-SIG
Guido van Rossum28524c72007-02-27 05:47:44 +000022"""
23
Guido van Rossum68bbcd22007-02-27 17:19:33 +000024__author__ = ("Guido van Rossum <guido@python.org>, "
Guido van Rossum78892e42007-04-06 17:31:18 +000025 "Mike Verdone <mike.verdone@gmail.com>, "
26 "Mark Russell <mark.russell@zen.co.uk>")
Guido van Rossum28524c72007-02-27 05:47:44 +000027
Guido van Rossum141f7672007-04-10 00:22:16 +000028__all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
29 "SocketIO", "BytesIO", "StringIO", "BufferedIOBase",
Guido van Rossum01a27522007-03-07 01:00:12 +000030 "BufferedReader", "BufferedWriter", "BufferedRWPair",
Guido van Rossum141f7672007-04-10 00:22:16 +000031 "BufferedRandom", "TextIOBase", "TextIOWrapper"]
Guido van Rossum28524c72007-02-27 05:47:44 +000032
33import os
Guido van Rossum78892e42007-04-06 17:31:18 +000034import sys
35import codecs
Guido van Rossum141f7672007-04-10 00:22:16 +000036import _fileio
Guido van Rossum78892e42007-04-06 17:31:18 +000037import warnings
Guido van Rossum28524c72007-02-27 05:47:44 +000038
Guido van Rossum9b76da62007-04-11 01:09:03 +000039# XXX Shouldn't we use st_blksize whenever we can?
Guido van Rossum4f0db6e2007-04-08 23:59:06 +000040DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
Guido van Rossum01a27522007-03-07 01:00:12 +000041
42
Guido van Rossum141f7672007-04-10 00:22:16 +000043class BlockingIOError(IOError):
Guido van Rossum78892e42007-04-06 17:31:18 +000044
Guido van Rossum141f7672007-04-10 00:22:16 +000045 """Exception raised when I/O would block on a non-blocking I/O stream."""
46
47 def __init__(self, errno, strerror, characters_written=0):
Guido van Rossum01a27522007-03-07 01:00:12 +000048 IOError.__init__(self, errno, strerror)
49 self.characters_written = characters_written
50
Guido van Rossum68bbcd22007-02-27 17:19:33 +000051
Guido van Rossum9cbfffd2007-06-07 00:54:15 +000052def open(file, mode="r", buffering=None, encoding=None, newline=None):
Guido van Rossum17e43e52007-02-27 15:45:13 +000053 """Replacement for the built-in open function.
54
55 Args:
Guido van Rossum4f0db6e2007-04-08 23:59:06 +000056 file: string giving the name of the file to be opened;
Guido van Rossum9b76da62007-04-11 01:09:03 +000057 or integer file descriptor of the file to be wrapped (*).
58 mode: optional mode string; see below.
Guido van Rossum17e43e52007-02-27 15:45:13 +000059 buffering: optional int >= 0 giving the buffer size; values
60 can be: 0 = unbuffered, 1 = line buffered,
Guido van Rossum9b76da62007-04-11 01:09:03 +000061 larger = fully buffered.
Guido van Rossum9b76da62007-04-11 01:09:03 +000062 encoding: optional string giving the text encoding.
63 newline: optional newlines specifier; must be None, '\n' or '\r\n';
64 specifies the line ending expected on input and written on
65 output. If None, use universal newlines on input and
66 use os.linesep on output.
Guido van Rossum17e43e52007-02-27 15:45:13 +000067
Guido van Rossum4f0db6e2007-04-08 23:59:06 +000068 (*) If a file descriptor is given, it is closed when the returned
69 I/O object is closed. If you don't want this to happen, use
70 os.dup() to create a duplicate file descriptor.
71
Guido van Rossum17e43e52007-02-27 15:45:13 +000072 Mode strings characters:
73 'r': open for reading (default)
74 'w': open for writing, truncating the file first
75 'a': open for writing, appending to the end if the file exists
76 'b': binary mode
77 't': text mode (default)
78 '+': open a disk file for updating (implies reading and writing)
Guido van Rossum9be55972007-04-07 02:59:27 +000079 'U': universal newline mode (for backwards compatibility)
Guido van Rossum17e43e52007-02-27 15:45:13 +000080
81 Constraints:
82 - encoding must not be given when a binary mode is given
83 - buffering must not be zero when a text mode is given
84
85 Returns:
86 Depending on the mode and buffering arguments, either a raw
87 binary stream, a buffered binary stream, or a buffered text
88 stream, open for reading and/or writing.
89 """
Guido van Rossum9b76da62007-04-11 01:09:03 +000090 # XXX Don't use asserts for these checks; raise TypeError or ValueError
Guido van Rossum4f0db6e2007-04-08 23:59:06 +000091 assert isinstance(file, (basestring, int)), repr(file)
92 assert isinstance(mode, basestring), repr(mode)
93 assert buffering is None or isinstance(buffering, int), repr(buffering)
94 assert encoding is None or isinstance(encoding, basestring), repr(encoding)
Guido van Rossum28524c72007-02-27 05:47:44 +000095 modes = set(mode)
Guido van Rossum9be55972007-04-07 02:59:27 +000096 if modes - set("arwb+tU") or len(mode) > len(modes):
Guido van Rossum28524c72007-02-27 05:47:44 +000097 raise ValueError("invalid mode: %r" % mode)
98 reading = "r" in modes
Guido van Rossum17e43e52007-02-27 15:45:13 +000099 writing = "w" in modes
Guido van Rossum28524c72007-02-27 05:47:44 +0000100 appending = "a" in modes
101 updating = "+" in modes
Guido van Rossum17e43e52007-02-27 15:45:13 +0000102 text = "t" in modes
103 binary = "b" in modes
Guido van Rossum7165cb12007-07-10 06:54:34 +0000104 if "U" in modes:
105 if writing or appending:
106 raise ValueError("can't use U and writing mode at once")
Guido van Rossum9be55972007-04-07 02:59:27 +0000107 reading = True
Guido van Rossum28524c72007-02-27 05:47:44 +0000108 if text and binary:
109 raise ValueError("can't have text and binary mode at once")
110 if reading + writing + appending > 1:
111 raise ValueError("can't have read/write/append mode at once")
112 if not (reading or writing or appending):
113 raise ValueError("must have exactly one of read/write/append mode")
114 if binary and encoding is not None:
Guido van Rossum9b76da62007-04-11 01:09:03 +0000115 raise ValueError("binary mode doesn't take an encoding argument")
116 if binary and newline is not None:
117 raise ValueError("binary mode doesn't take a newline argument")
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000118 raw = FileIO(file,
Guido van Rossum28524c72007-02-27 05:47:44 +0000119 (reading and "r" or "") +
120 (writing and "w" or "") +
121 (appending and "a" or "") +
122 (updating and "+" or ""))
123 if buffering is None:
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000124 buffering = -1
125 if buffering < 0:
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000126 buffering = DEFAULT_BUFFER_SIZE
127 # XXX Should default to line buffering if os.isatty(raw.fileno())
Guido van Rossum17e43e52007-02-27 15:45:13 +0000128 try:
129 bs = os.fstat(raw.fileno()).st_blksize
130 except (os.error, AttributeError):
Guido van Rossumbb09b212007-03-18 03:36:28 +0000131 pass
132 else:
Guido van Rossum17e43e52007-02-27 15:45:13 +0000133 if bs > 1:
134 buffering = bs
Guido van Rossum28524c72007-02-27 05:47:44 +0000135 if buffering < 0:
136 raise ValueError("invalid buffering size")
137 if buffering == 0:
138 if binary:
Guido van Rossum13633bb2007-04-13 18:42:35 +0000139 raw._name = file
140 raw._mode = mode
Guido van Rossum28524c72007-02-27 05:47:44 +0000141 return raw
142 raise ValueError("can't have unbuffered text I/O")
143 if updating:
144 buffer = BufferedRandom(raw, buffering)
Guido van Rossum17e43e52007-02-27 15:45:13 +0000145 elif writing or appending:
Guido van Rossum28524c72007-02-27 05:47:44 +0000146 buffer = BufferedWriter(raw, buffering)
147 else:
148 assert reading
149 buffer = BufferedReader(raw, buffering)
150 if binary:
Guido van Rossum13633bb2007-04-13 18:42:35 +0000151 buffer.name = file
152 buffer.mode = mode
Guido van Rossum28524c72007-02-27 05:47:44 +0000153 return buffer
Guido van Rossum13633bb2007-04-13 18:42:35 +0000154 text = TextIOWrapper(buffer, encoding, newline)
155 text.name = file
156 text.mode = mode
157 return text
Guido van Rossum28524c72007-02-27 05:47:44 +0000158
159
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000160class UnsupportedOperation(ValueError, IOError):
161 pass
162
163
Guido van Rossum141f7672007-04-10 00:22:16 +0000164class IOBase:
Guido van Rossum28524c72007-02-27 05:47:44 +0000165
Guido van Rossum141f7672007-04-10 00:22:16 +0000166 """Base class for all I/O classes.
Guido van Rossum17e43e52007-02-27 15:45:13 +0000167
Guido van Rossum141f7672007-04-10 00:22:16 +0000168 This class provides dummy implementations for many methods that
Guido van Rossum17e43e52007-02-27 15:45:13 +0000169 derived classes can override selectively; the default
170 implementations represent a file that cannot be read, written or
171 seeked.
172
Guido van Rossum141f7672007-04-10 00:22:16 +0000173 This does not define read(), readinto() and write(), nor
174 readline() and friends, since their signatures vary per layer.
Guido van Rossum53807da2007-04-10 19:01:47 +0000175
176 Not that calling any method (even inquiries) on a closed file is
177 undefined. Implementations may raise IOError in this case.
Guido van Rossum17e43e52007-02-27 15:45:13 +0000178 """
179
Guido van Rossum141f7672007-04-10 00:22:16 +0000180 ### Internal ###
181
182 def _unsupported(self, name: str) -> IOError:
183 """Internal: raise an exception for unsupported operations."""
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000184 raise UnsupportedOperation("%s.%s() not supported" %
185 (self.__class__.__name__, name))
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000186
Guido van Rossum141f7672007-04-10 00:22:16 +0000187 ### Positioning ###
188
Guido van Rossum53807da2007-04-10 19:01:47 +0000189 def seek(self, pos: int, whence: int = 0) -> int:
190 """seek(pos: int, whence: int = 0) -> int. Change stream position.
Guido van Rossum141f7672007-04-10 00:22:16 +0000191
192 Seek to byte offset pos relative to position indicated by whence:
193 0 Start of stream (the default). pos should be >= 0;
194 1 Current position - whence may be negative;
195 2 End of stream - whence usually negative.
Guido van Rossum53807da2007-04-10 19:01:47 +0000196 Returns the new absolute position.
Guido van Rossum141f7672007-04-10 00:22:16 +0000197 """
198 self._unsupported("seek")
199
200 def tell(self) -> int:
201 """tell() -> int. Return current stream position."""
Guido van Rossum53807da2007-04-10 19:01:47 +0000202 return self.seek(0, 1)
Guido van Rossum141f7672007-04-10 00:22:16 +0000203
Guido van Rossum87429772007-04-10 21:06:59 +0000204 def truncate(self, pos: int = None) -> int:
205 """truncate(size: int = None) -> int. Truncate file to size bytes.
Guido van Rossum141f7672007-04-10 00:22:16 +0000206
207 Size defaults to the current IO position as reported by tell().
Guido van Rossum87429772007-04-10 21:06:59 +0000208 Returns the new size.
Guido van Rossum141f7672007-04-10 00:22:16 +0000209 """
210 self._unsupported("truncate")
211
212 ### Flush and close ###
213
214 def flush(self) -> None:
215 """flush() -> None. Flushes write buffers, if applicable.
216
217 This is a no-op for read-only and non-blocking streams.
218 """
Guido van Rossumd4103952007-04-12 05:44:49 +0000219 # XXX Should this return the number of bytes written???
Guido van Rossum141f7672007-04-10 00:22:16 +0000220
221 __closed = False
222
223 def close(self) -> None:
224 """close() -> None. Flushes and closes the IO object.
225
226 This must be idempotent. It should also set a flag for the
227 'closed' property (see below) to test.
228 """
229 if not self.__closed:
Guido van Rossum469734b2007-07-10 12:00:45 +0000230 try:
231 self.flush()
Guido van Rossum33e7a8e2007-07-22 20:38:07 +0000232 except IOError:
233 pass # If flush() fails, just give up
234 self.__closed = True
Guido van Rossum141f7672007-04-10 00:22:16 +0000235
236 def __del__(self) -> None:
237 """Destructor. Calls close()."""
238 # The try/except block is in case this is called at program
239 # exit time, when it's possible that globals have already been
240 # deleted, and then the close() call might fail. Since
241 # there's nothing we can do about such failures and they annoy
242 # the end users, we suppress the traceback.
243 try:
244 self.close()
245 except:
246 pass
247
248 ### Inquiries ###
249
250 def seekable(self) -> bool:
251 """seekable() -> bool. Return whether object supports random access.
252
253 If False, seek(), tell() and truncate() will raise IOError.
254 This method may need to do a test seek().
255 """
256 return False
257
258 def readable(self) -> bool:
259 """readable() -> bool. Return whether object was opened for reading.
260
261 If False, read() will raise IOError.
262 """
263 return False
264
265 def writable(self) -> bool:
266 """writable() -> bool. Return whether object was opened for writing.
267
268 If False, write() and truncate() will raise IOError.
269 """
270 return False
271
272 @property
273 def closed(self):
274 """closed: bool. True iff the file has been closed.
275
276 For backwards compatibility, this is a property, not a predicate.
277 """
278 return self.__closed
279
280 ### Context manager ###
281
282 def __enter__(self) -> "IOBase": # That's a forward reference
283 """Context management protocol. Returns self."""
284 return self
285
286 def __exit__(self, *args) -> None:
287 """Context management protocol. Calls close()"""
288 self.close()
289
290 ### Lower-level APIs ###
291
292 # XXX Should these be present even if unimplemented?
293
294 def fileno(self) -> int:
295 """fileno() -> int. Returns underlying file descriptor if one exists.
296
297 Raises IOError if the IO object does not use a file descriptor.
298 """
299 self._unsupported("fileno")
300
301 def isatty(self) -> bool:
302 """isatty() -> int. Returns whether this is an 'interactive' stream.
303
304 Returns False if we don't know.
305 """
306 return False
307
Guido van Rossum7165cb12007-07-10 06:54:34 +0000308 ### Readline[s] and writelines ###
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000309
Guido van Rossum48fc58a2007-06-07 23:45:37 +0000310 def readline(self, limit: int = -1) -> bytes:
311 """For backwards compatibility, a (slowish) readline()."""
Guido van Rossum2bf71382007-06-08 00:07:57 +0000312 if hasattr(self, "peek"):
313 def nreadahead():
314 readahead = self.peek(1, unsafe=True)
315 if not readahead:
316 return 1
317 n = (readahead.find(b"\n") + 1) or len(readahead)
318 if limit >= 0:
319 n = min(n, limit)
320 return n
321 else:
322 def nreadahead():
323 return 1
Guido van Rossum48fc58a2007-06-07 23:45:37 +0000324 if limit is None:
325 limit = -1
326 res = bytes()
327 while limit < 0 or len(res) < limit:
Guido van Rossum2bf71382007-06-08 00:07:57 +0000328 b = self.read(nreadahead())
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000329 if not b:
330 break
331 res += b
Guido van Rossum48fc58a2007-06-07 23:45:37 +0000332 if res.endswith(b"\n"):
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000333 break
334 return res
335
Guido van Rossum7165cb12007-07-10 06:54:34 +0000336 def __iter__(self):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000337 if self.closed:
338 raise ValueError("__iter__ on closed file")
Guido van Rossum7165cb12007-07-10 06:54:34 +0000339 return self
340
341 def __next__(self):
342 line = self.readline()
343 if not line:
344 raise StopIteration
345 return line
346
347 def readlines(self, hint=None):
348 if hint is None:
349 return list(self)
350 n = 0
351 lines = []
352 for line in self:
353 lines.append(line)
354 n += len(line)
355 if n >= hint:
356 break
357 return lines
358
359 def writelines(self, lines):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000360 if self.closed:
361 raise ValueError("write to closed file")
Guido van Rossum7165cb12007-07-10 06:54:34 +0000362 for line in lines:
363 self.write(line)
364
Guido van Rossum141f7672007-04-10 00:22:16 +0000365
366class RawIOBase(IOBase):
367
368 """Base class for raw binary I/O.
369
370 The read() method is implemented by calling readinto(); derived
371 classes that want to support read() only need to implement
372 readinto() as a primitive operation. In general, readinto()
373 can be more efficient than read().
374
375 (It would be tempting to also provide an implementation of
376 readinto() in terms of read(), in case the latter is a more
377 suitable primitive operation, but that would lead to nasty
378 recursion in case a subclass doesn't implement either.)
379 """
380
Guido van Rossum7165cb12007-07-10 06:54:34 +0000381 def read(self, n: int = -1) -> bytes:
Guido van Rossum78892e42007-04-06 17:31:18 +0000382 """read(n: int) -> bytes. Read and return up to n bytes.
Guido van Rossum01a27522007-03-07 01:00:12 +0000383
384 Returns an empty bytes array on EOF, or None if the object is
385 set not to block and has no data to read.
386 """
Guido van Rossum7165cb12007-07-10 06:54:34 +0000387 if n is None:
388 n = -1
389 if n < 0:
390 return self.readall()
Guido van Rossum28524c72007-02-27 05:47:44 +0000391 b = bytes(n.__index__())
Guido van Rossum00efead2007-03-07 05:23:25 +0000392 n = self.readinto(b)
393 del b[n:]
Guido van Rossum28524c72007-02-27 05:47:44 +0000394 return b
395
Guido van Rossum7165cb12007-07-10 06:54:34 +0000396 def readall(self):
397 """readall() -> bytes. Read until EOF, using multiple read() call."""
398 res = bytes()
399 while True:
400 data = self.read(DEFAULT_BUFFER_SIZE)
401 if not data:
402 break
403 res += data
404 return res
405
Guido van Rossum141f7672007-04-10 00:22:16 +0000406 def readinto(self, b: bytes) -> int:
407 """readinto(b: bytes) -> int. Read up to len(b) bytes into b.
Guido van Rossum78892e42007-04-06 17:31:18 +0000408
409 Returns number of bytes read (0 for EOF), or None if the object
410 is set not to block as has no data to read.
411 """
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000412 self._unsupported("readinto")
Guido van Rossum28524c72007-02-27 05:47:44 +0000413
Guido van Rossum141f7672007-04-10 00:22:16 +0000414 def write(self, b: bytes) -> int:
Guido van Rossum78892e42007-04-06 17:31:18 +0000415 """write(b: bytes) -> int. Write the given buffer to the IO stream.
Guido van Rossum01a27522007-03-07 01:00:12 +0000416
Guido van Rossum78892e42007-04-06 17:31:18 +0000417 Returns the number of bytes written, which may be less than len(b).
Guido van Rossum01a27522007-03-07 01:00:12 +0000418 """
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000419 self._unsupported("write")
Guido van Rossum28524c72007-02-27 05:47:44 +0000420
Guido van Rossum78892e42007-04-06 17:31:18 +0000421
Guido van Rossum141f7672007-04-10 00:22:16 +0000422class FileIO(_fileio._FileIO, RawIOBase):
Guido van Rossum28524c72007-02-27 05:47:44 +0000423
Guido van Rossum141f7672007-04-10 00:22:16 +0000424 """Raw I/O implementation for OS files.
Guido van Rossum28524c72007-02-27 05:47:44 +0000425
Guido van Rossum141f7672007-04-10 00:22:16 +0000426 This multiply inherits from _FileIO and RawIOBase to make
427 isinstance(io.FileIO(), io.RawIOBase) return True without
428 requiring that _fileio._FileIO inherits from io.RawIOBase (which
429 would be hard to do since _fileio.c is written in C).
430 """
Guido van Rossuma9e20242007-03-08 00:43:48 +0000431
Guido van Rossum87429772007-04-10 21:06:59 +0000432 def close(self):
433 _fileio._FileIO.close(self)
434 RawIOBase.close(self)
435
Guido van Rossum13633bb2007-04-13 18:42:35 +0000436 @property
437 def name(self):
438 return self._name
439
440 @property
441 def mode(self):
442 return self._mode
443
Guido van Rossuma9e20242007-03-08 00:43:48 +0000444
Guido van Rossum28524c72007-02-27 05:47:44 +0000445class SocketIO(RawIOBase):
446
447 """Raw I/O implementation for stream sockets."""
448
Guido van Rossum17e43e52007-02-27 15:45:13 +0000449 # XXX More docs
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000450
Guido van Rossum28524c72007-02-27 05:47:44 +0000451 def __init__(self, sock, mode):
452 assert mode in ("r", "w", "rw")
Guido van Rossum141f7672007-04-10 00:22:16 +0000453 RawIOBase.__init__(self)
Guido van Rossum28524c72007-02-27 05:47:44 +0000454 self._sock = sock
455 self._mode = mode
Guido van Rossum28524c72007-02-27 05:47:44 +0000456
457 def readinto(self, b):
458 return self._sock.recv_into(b)
459
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000460 def read(self, n: int = None) -> bytes:
461 """read(n: int) -> bytes. Read and return up to n bytes.
462
463 Returns an empty bytes array on EOF, or None if the object is
464 set not to block and has no data to read.
465 """
466 if n is None:
467 n = -1
468 if n >= 0:
469 return RawIOBase.read(self, n)
470 # Support reading until the end.
471 # XXX Why doesn't RawIOBase support this?
472 data = b""
473 while True:
474 more = RawIOBase.read(self, DEFAULT_BUFFER_SIZE)
475 if not more:
476 break
477 data += more
478 return data
479
Guido van Rossum28524c72007-02-27 05:47:44 +0000480 def write(self, b):
481 return self._sock.send(b)
482
483 def close(self):
Guido van Rossum141f7672007-04-10 00:22:16 +0000484 if not self.closed:
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000485 RawIOBase.close(self)
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000486
Guido van Rossum28524c72007-02-27 05:47:44 +0000487 def readable(self):
488 return "r" in self._mode
489
490 def writable(self):
491 return "w" in self._mode
492
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000493 def fileno(self):
494 return self._sock.fileno()
Neal Norwitz8b41c3d2007-02-27 06:26:14 +0000495
Guido van Rossum28524c72007-02-27 05:47:44 +0000496
Guido van Rossumcce92b22007-04-10 14:41:39 +0000497class BufferedIOBase(IOBase):
Guido van Rossum141f7672007-04-10 00:22:16 +0000498
499 """Base class for buffered IO objects.
500
501 The main difference with RawIOBase is that the read() method
502 supports omitting the size argument, and does not have a default
503 implementation that defers to readinto().
504
505 In addition, read(), readinto() and write() may raise
506 BlockingIOError if the underlying raw stream is in non-blocking
507 mode and not ready; unlike their raw counterparts, they will never
508 return None.
509
510 A typical implementation should not inherit from a RawIOBase
511 implementation, but wrap one.
512 """
513
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000514 def read(self, n: int = None) -> bytes:
515 """read(n: int = None) -> bytes. Read and return up to n bytes.
Guido van Rossum141f7672007-04-10 00:22:16 +0000516
Guido van Rossum024da5c2007-05-17 23:59:11 +0000517 If the argument is omitted, None, or negative, reads and
518 returns all data until EOF.
Guido van Rossum141f7672007-04-10 00:22:16 +0000519
520 If the argument is positive, and the underlying raw stream is
521 not 'interactive', multiple raw reads may be issued to satisfy
522 the byte count (unless EOF is reached first). But for
523 interactive raw streams (XXX and for pipes?), at most one raw
524 read will be issued, and a short result does not imply that
525 EOF is imminent.
526
527 Returns an empty bytes array on EOF.
528
529 Raises BlockingIOError if the underlying raw stream has no
530 data at the moment.
531 """
532 self._unsupported("read")
533
534 def readinto(self, b: bytes) -> int:
535 """readinto(b: bytes) -> int. Read up to len(b) bytes into b.
536
537 Like read(), this may issue multiple reads to the underlying
538 raw stream, unless the latter is 'interactive' (XXX or a
539 pipe?).
540
541 Returns the number of bytes read (0 for EOF).
542
543 Raises BlockingIOError if the underlying raw stream has no
544 data at the moment.
545 """
Guido van Rossumd4103952007-04-12 05:44:49 +0000546 # XXX This ought to work with anything that supports the buffer API
Guido van Rossum87429772007-04-10 21:06:59 +0000547 data = self.read(len(b))
548 n = len(data)
Guido van Rossum7165cb12007-07-10 06:54:34 +0000549 try:
550 b[:n] = data
551 except TypeError as err:
552 import array
553 if not isinstance(b, array.array):
554 raise err
555 b[:n] = array.array('b', data)
Guido van Rossum87429772007-04-10 21:06:59 +0000556 return n
Guido van Rossum141f7672007-04-10 00:22:16 +0000557
558 def write(self, b: bytes) -> int:
559 """write(b: bytes) -> int. Write the given buffer to the IO stream.
560
561 Returns the number of bytes written, which is never less than
562 len(b).
563
564 Raises BlockingIOError if the buffer is full and the
565 underlying raw stream cannot accept more data at the moment.
566 """
567 self._unsupported("write")
568
569
570class _BufferedIOMixin(BufferedIOBase):
571
572 """A mixin implementation of BufferedIOBase with an underlying raw stream.
573
574 This passes most requests on to the underlying raw stream. It
575 does *not* provide implementations of read(), readinto() or
576 write().
577 """
578
579 def __init__(self, raw):
580 self.raw = raw
581
582 ### Positioning ###
583
584 def seek(self, pos, whence=0):
Guido van Rossum53807da2007-04-10 19:01:47 +0000585 return self.raw.seek(pos, whence)
Guido van Rossum141f7672007-04-10 00:22:16 +0000586
587 def tell(self):
588 return self.raw.tell()
589
590 def truncate(self, pos=None):
Guido van Rossum7165cb12007-07-10 06:54:34 +0000591 if pos is None:
592 pos = self.tell()
Guido van Rossum87429772007-04-10 21:06:59 +0000593 return self.raw.truncate(pos)
Guido van Rossum141f7672007-04-10 00:22:16 +0000594
595 ### Flush and close ###
596
597 def flush(self):
598 self.raw.flush()
599
600 def close(self):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000601 if not self.closed:
Guido van Rossum33e7a8e2007-07-22 20:38:07 +0000602 try:
603 self.flush()
604 except IOError:
605 pass # If flush() fails, just give up
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000606 self.raw.close()
Guido van Rossum141f7672007-04-10 00:22:16 +0000607
608 ### Inquiries ###
609
610 def seekable(self):
611 return self.raw.seekable()
612
613 def readable(self):
614 return self.raw.readable()
615
616 def writable(self):
617 return self.raw.writable()
618
619 @property
620 def closed(self):
621 return self.raw.closed
622
623 ### Lower-level APIs ###
624
625 def fileno(self):
626 return self.raw.fileno()
627
628 def isatty(self):
629 return self.raw.isatty()
630
631
Guido van Rossum024da5c2007-05-17 23:59:11 +0000632class BytesIO(BufferedIOBase):
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000633
Guido van Rossum024da5c2007-05-17 23:59:11 +0000634 """Buffered I/O implementation using an in-memory bytes buffer."""
Guido van Rossum28524c72007-02-27 05:47:44 +0000635
Guido van Rossum024da5c2007-05-17 23:59:11 +0000636 # XXX More docs
637
638 def __init__(self, initial_bytes=None):
639 buffer = b""
640 if initial_bytes is not None:
641 buffer += initial_bytes
Guido van Rossum78892e42007-04-06 17:31:18 +0000642 self._buffer = buffer
Guido van Rossum28524c72007-02-27 05:47:44 +0000643 self._pos = 0
Guido van Rossum28524c72007-02-27 05:47:44 +0000644
645 def getvalue(self):
646 return self._buffer
647
Guido van Rossum024da5c2007-05-17 23:59:11 +0000648 def read(self, n=None):
649 if n is None:
650 n = -1
Guido van Rossum141f7672007-04-10 00:22:16 +0000651 if n < 0:
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000652 n = len(self._buffer)
Guido van Rossum28524c72007-02-27 05:47:44 +0000653 newpos = min(len(self._buffer), self._pos + n)
654 b = self._buffer[self._pos : newpos]
655 self._pos = newpos
656 return b
657
Guido van Rossum024da5c2007-05-17 23:59:11 +0000658 def read1(self, n):
659 return self.read(n)
660
Guido van Rossum28524c72007-02-27 05:47:44 +0000661 def write(self, b):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000662 if self.closed:
663 raise ValueError("write to closed file")
Guido van Rossum28524c72007-02-27 05:47:44 +0000664 n = len(b)
665 newpos = self._pos + n
Guido van Rossumb972a782007-07-21 00:25:15 +0000666 if newpos > len(self._buffer):
667 # Inserts null bytes between the current end of the file
668 # and the new write position.
669 padding = '\x00' * (newpos - len(self._buffer) - n)
670 self._buffer[self._pos:newpos - n] = padding
Guido van Rossum28524c72007-02-27 05:47:44 +0000671 self._buffer[self._pos:newpos] = b
672 self._pos = newpos
673 return n
674
675 def seek(self, pos, whence=0):
676 if whence == 0:
677 self._pos = max(0, pos)
678 elif whence == 1:
679 self._pos = max(0, self._pos + pos)
680 elif whence == 2:
681 self._pos = max(0, len(self._buffer) + pos)
682 else:
683 raise IOError("invalid whence value")
Guido van Rossum53807da2007-04-10 19:01:47 +0000684 return self._pos
Guido van Rossum28524c72007-02-27 05:47:44 +0000685
686 def tell(self):
687 return self._pos
688
689 def truncate(self, pos=None):
690 if pos is None:
691 pos = self._pos
Guido van Rossum28524c72007-02-27 05:47:44 +0000692 del self._buffer[pos:]
Guido van Rossum87429772007-04-10 21:06:59 +0000693 return pos
Guido van Rossum28524c72007-02-27 05:47:44 +0000694
695 def readable(self):
696 return True
697
698 def writable(self):
699 return True
700
701 def seekable(self):
702 return True
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000703
704
Guido van Rossum141f7672007-04-10 00:22:16 +0000705class BufferedReader(_BufferedIOMixin):
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000706
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000707 """Buffer for a readable sequential RawIO object."""
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000708
Guido van Rossum78892e42007-04-06 17:31:18 +0000709 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
Guido van Rossum01a27522007-03-07 01:00:12 +0000710 """Create a new buffered reader using the given readable raw IO object.
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000711 """
712 assert raw.readable()
Guido van Rossum141f7672007-04-10 00:22:16 +0000713 _BufferedIOMixin.__init__(self, raw)
Guido van Rossum01a27522007-03-07 01:00:12 +0000714 self._read_buf = b""
Guido van Rossum78892e42007-04-06 17:31:18 +0000715 self.buffer_size = buffer_size
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000716
Guido van Rossum024da5c2007-05-17 23:59:11 +0000717 def read(self, n=None):
Guido van Rossum01a27522007-03-07 01:00:12 +0000718 """Read n bytes.
719
720 Returns exactly n bytes of data unless the underlying raw IO
Walter Dörwalda3270002007-05-29 19:13:29 +0000721 stream reaches EOF or if the call would block in non-blocking
Guido van Rossum141f7672007-04-10 00:22:16 +0000722 mode. If n is negative, read until EOF or until read() would
Guido van Rossum01a27522007-03-07 01:00:12 +0000723 block.
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000724 """
Guido van Rossum024da5c2007-05-17 23:59:11 +0000725 if n is None:
726 n = -1
Guido van Rossum78892e42007-04-06 17:31:18 +0000727 nodata_val = b""
Guido van Rossum141f7672007-04-10 00:22:16 +0000728 while n < 0 or len(self._read_buf) < n:
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000729 to_read = max(self.buffer_size,
730 n if n is not None else 2*len(self._read_buf))
Guido van Rossum78892e42007-04-06 17:31:18 +0000731 current = self.raw.read(to_read)
Guido van Rossum78892e42007-04-06 17:31:18 +0000732 if current in (b"", None):
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000733 nodata_val = current
734 break
Guido van Rossum01a27522007-03-07 01:00:12 +0000735 self._read_buf += current
736 if self._read_buf:
Guido van Rossum141f7672007-04-10 00:22:16 +0000737 if n < 0:
Guido van Rossum01a27522007-03-07 01:00:12 +0000738 n = len(self._read_buf)
739 out = self._read_buf[:n]
740 self._read_buf = self._read_buf[n:]
741 else:
742 out = nodata_val
743 return out
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000744
Guido van Rossum13633bb2007-04-13 18:42:35 +0000745 def peek(self, n=0, *, unsafe=False):
746 """Returns buffered bytes without advancing the position.
747
748 The argument indicates a desired minimal number of bytes; we
749 do at most one raw read to satisfy it. We never return more
750 than self.buffer_size.
751
752 Unless unsafe=True is passed, we return a copy.
753 """
754 want = min(n, self.buffer_size)
755 have = len(self._read_buf)
756 if have < want:
757 to_read = self.buffer_size - have
758 current = self.raw.read(to_read)
759 if current:
760 self._read_buf += current
761 result = self._read_buf
762 if unsafe:
763 result = result[:]
764 return result
765
766 def read1(self, n):
767 """Reads up to n bytes.
768
769 Returns up to n bytes. If at least one byte is buffered,
770 we only return buffered bytes. Otherwise, we do one
771 raw read.
772 """
773 if n <= 0:
774 return b""
775 self.peek(1, unsafe=True)
776 return self.read(min(n, len(self._read_buf)))
777
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000778 def tell(self):
779 return self.raw.tell() - len(self._read_buf)
780
781 def seek(self, pos, whence=0):
782 if whence == 1:
783 pos -= len(self._read_buf)
Guido van Rossum53807da2007-04-10 19:01:47 +0000784 pos = self.raw.seek(pos, whence)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000785 self._read_buf = b""
Guido van Rossum53807da2007-04-10 19:01:47 +0000786 return pos
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000787
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000788
Guido van Rossum141f7672007-04-10 00:22:16 +0000789class BufferedWriter(_BufferedIOMixin):
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000790
Guido van Rossum78892e42007-04-06 17:31:18 +0000791 # XXX docstring
792
Guido van Rossum141f7672007-04-10 00:22:16 +0000793 def __init__(self, raw,
794 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
Guido van Rossum01a27522007-03-07 01:00:12 +0000795 assert raw.writable()
Guido van Rossum141f7672007-04-10 00:22:16 +0000796 _BufferedIOMixin.__init__(self, raw)
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000797 self.buffer_size = buffer_size
Guido van Rossum141f7672007-04-10 00:22:16 +0000798 self.max_buffer_size = (2*buffer_size
799 if max_buffer_size is None
800 else max_buffer_size)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000801 self._write_buf = b""
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000802
803 def write(self, b):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000804 if self.closed:
805 raise ValueError("write to closed file")
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000806 if not isinstance(b, bytes):
Guido van Rossum7165cb12007-07-10 06:54:34 +0000807 if hasattr(b, "__index__"):
808 raise TypeError("Can't write object of type %s" %
809 type(b).__name__)
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000810 b = bytes(b)
Guido van Rossum01a27522007-03-07 01:00:12 +0000811 # XXX we can implement some more tricks to try and avoid partial writes
Guido van Rossum01a27522007-03-07 01:00:12 +0000812 if len(self._write_buf) > self.buffer_size:
813 # We're full, so let's pre-flush the buffer
814 try:
815 self.flush()
Guido van Rossum141f7672007-04-10 00:22:16 +0000816 except BlockingIOError as e:
Guido van Rossum01a27522007-03-07 01:00:12 +0000817 # We can't accept anything else.
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000818 # XXX Why not just let the exception pass through?
Guido van Rossum141f7672007-04-10 00:22:16 +0000819 raise BlockingIOError(e.errno, e.strerror, 0)
Guido van Rossumd4103952007-04-12 05:44:49 +0000820 before = len(self._write_buf)
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000821 self._write_buf.extend(b)
Guido van Rossumd4103952007-04-12 05:44:49 +0000822 written = len(self._write_buf) - before
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000823 if len(self._write_buf) > self.buffer_size:
Guido van Rossum01a27522007-03-07 01:00:12 +0000824 try:
825 self.flush()
Guido van Rossum141f7672007-04-10 00:22:16 +0000826 except BlockingIOError as e:
Guido van Rossum01a27522007-03-07 01:00:12 +0000827 if (len(self._write_buf) > self.max_buffer_size):
828 # We've hit max_buffer_size. We have to accept a partial
829 # write and cut back our buffer.
830 overage = len(self._write_buf) - self.max_buffer_size
831 self._write_buf = self._write_buf[:self.max_buffer_size]
Guido van Rossum141f7672007-04-10 00:22:16 +0000832 raise BlockingIOError(e.errno, e.strerror, overage)
Guido van Rossumd4103952007-04-12 05:44:49 +0000833 return written
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000834
835 def flush(self):
Guido van Rossum4b5386f2007-07-10 09:12:49 +0000836 if self.closed:
837 raise ValueError("flush of closed file")
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000838 written = 0
Guido van Rossum01a27522007-03-07 01:00:12 +0000839 try:
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000840 while self._write_buf:
841 n = self.raw.write(self._write_buf)
842 del self._write_buf[:n]
843 written += n
Guido van Rossum141f7672007-04-10 00:22:16 +0000844 except BlockingIOError as e:
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000845 n = e.characters_written
846 del self._write_buf[:n]
847 written += n
Guido van Rossum141f7672007-04-10 00:22:16 +0000848 raise BlockingIOError(e.errno, e.strerror, written)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000849
850 def tell(self):
851 return self.raw.tell() + len(self._write_buf)
852
853 def seek(self, pos, whence=0):
854 self.flush()
Guido van Rossum53807da2007-04-10 19:01:47 +0000855 return self.raw.seek(pos, whence)
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000856
Guido van Rossum01a27522007-03-07 01:00:12 +0000857
Guido van Rossum141f7672007-04-10 00:22:16 +0000858class BufferedRWPair(BufferedIOBase):
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000859
Guido van Rossum01a27522007-03-07 01:00:12 +0000860 """A buffered reader and writer object together.
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000861
Guido van Rossum141f7672007-04-10 00:22:16 +0000862 A buffered reader object and buffered writer object put together
863 to form a sequential IO object that can read and write.
Guido van Rossum78892e42007-04-06 17:31:18 +0000864
865 This is typically used with a socket or two-way pipe.
Guido van Rossum141f7672007-04-10 00:22:16 +0000866
867 XXX The usefulness of this (compared to having two separate IO
868 objects) is questionable.
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000869 """
870
Guido van Rossum141f7672007-04-10 00:22:16 +0000871 def __init__(self, reader, writer,
872 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
873 """Constructor.
874
875 The arguments are two RawIO instances.
876 """
Guido van Rossum01a27522007-03-07 01:00:12 +0000877 assert reader.readable()
878 assert writer.writable()
Guido van Rossum141f7672007-04-10 00:22:16 +0000879 self.reader = BufferedReader(reader, buffer_size)
880 self.writer = BufferedWriter(writer, buffer_size, max_buffer_size)
Guido van Rossum01a27522007-03-07 01:00:12 +0000881
Guido van Rossum024da5c2007-05-17 23:59:11 +0000882 def read(self, n=None):
883 if n is None:
884 n = -1
Guido van Rossum01a27522007-03-07 01:00:12 +0000885 return self.reader.read(n)
886
Guido van Rossum141f7672007-04-10 00:22:16 +0000887 def readinto(self, b):
888 return self.reader.readinto(b)
889
Guido van Rossum01a27522007-03-07 01:00:12 +0000890 def write(self, b):
891 return self.writer.write(b)
892
Guido van Rossum13633bb2007-04-13 18:42:35 +0000893 def peek(self, n=0, *, unsafe=False):
894 return self.reader.peek(n, unsafe=unsafe)
895
896 def read1(self, n):
897 return self.reader.read1(n)
898
Guido van Rossum01a27522007-03-07 01:00:12 +0000899 def readable(self):
900 return self.reader.readable()
901
902 def writable(self):
903 return self.writer.writable()
904
905 def flush(self):
906 return self.writer.flush()
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000907
Guido van Rossum01a27522007-03-07 01:00:12 +0000908 def close(self):
Guido van Rossum01a27522007-03-07 01:00:12 +0000909 self.writer.close()
Guido van Rossum141f7672007-04-10 00:22:16 +0000910 self.reader.close()
911
912 def isatty(self):
913 return self.reader.isatty() or self.writer.isatty()
Guido van Rossum01a27522007-03-07 01:00:12 +0000914
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000915 @property
916 def closed(self):
Guido van Rossum141f7672007-04-10 00:22:16 +0000917 return self.writer.closed()
Guido van Rossum01a27522007-03-07 01:00:12 +0000918
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000919
Guido van Rossum141f7672007-04-10 00:22:16 +0000920class BufferedRandom(BufferedWriter, BufferedReader):
Guido van Rossum01a27522007-03-07 01:00:12 +0000921
Guido van Rossum78892e42007-04-06 17:31:18 +0000922 # XXX docstring
923
Guido van Rossum141f7672007-04-10 00:22:16 +0000924 def __init__(self, raw,
925 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
Guido van Rossum01a27522007-03-07 01:00:12 +0000926 assert raw.seekable()
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000927 BufferedReader.__init__(self, raw, buffer_size)
Guido van Rossum01a27522007-03-07 01:00:12 +0000928 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
929
Guido van Rossum01a27522007-03-07 01:00:12 +0000930 def seek(self, pos, whence=0):
931 self.flush()
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000932 # First do the raw seek, then empty the read buffer, so that
933 # if the raw seek fails, we don't lose buffered data forever.
Guido van Rossum53807da2007-04-10 19:01:47 +0000934 pos = self.raw.seek(pos, whence)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +0000935 self._read_buf = b""
Guido van Rossum53807da2007-04-10 19:01:47 +0000936 return pos
Guido van Rossum01a27522007-03-07 01:00:12 +0000937
938 def tell(self):
939 if (self._write_buf):
940 return self.raw.tell() + len(self._write_buf)
941 else:
942 return self.raw.tell() - len(self._read_buf)
943
Guido van Rossum024da5c2007-05-17 23:59:11 +0000944 def read(self, n=None):
945 if n is None:
946 n = -1
Guido van Rossum01a27522007-03-07 01:00:12 +0000947 self.flush()
948 return BufferedReader.read(self, n)
949
Guido van Rossum141f7672007-04-10 00:22:16 +0000950 def readinto(self, b):
951 self.flush()
952 return BufferedReader.readinto(self, b)
953
Guido van Rossum13633bb2007-04-13 18:42:35 +0000954 def peek(self, n=0, *, unsafe=False):
955 self.flush()
956 return BufferedReader.peek(self, n, unsafe=unsafe)
957
958 def read1(self, n):
959 self.flush()
960 return BufferedReader.read1(self, n)
961
Guido van Rossum01a27522007-03-07 01:00:12 +0000962 def write(self, b):
Guido van Rossum78892e42007-04-06 17:31:18 +0000963 if self._read_buf:
964 self.raw.seek(-len(self._read_buf), 1) # Undo readahead
965 self._read_buf = b""
Guido van Rossum01a27522007-03-07 01:00:12 +0000966 return BufferedWriter.write(self, b)
967
Guido van Rossum78892e42007-04-06 17:31:18 +0000968
Guido van Rossumcce92b22007-04-10 14:41:39 +0000969class TextIOBase(IOBase):
Guido van Rossum78892e42007-04-06 17:31:18 +0000970
971 """Base class for text I/O.
972
973 This class provides a character and line based interface to stream I/O.
Guido van Rossum9b76da62007-04-11 01:09:03 +0000974
975 There is no readinto() method, as character strings are immutable.
Guido van Rossum78892e42007-04-06 17:31:18 +0000976 """
977
978 def read(self, n: int = -1) -> str:
979 """read(n: int = -1) -> str. Read at most n characters from stream.
980
981 Read from underlying buffer until we have n characters or we hit EOF.
982 If n is negative or omitted, read until EOF.
983 """
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000984 self._unsupported("read")
Guido van Rossum78892e42007-04-06 17:31:18 +0000985
Guido van Rossum9b76da62007-04-11 01:09:03 +0000986 def write(self, s: str) -> int:
987 """write(s: str) -> int. Write string s to stream."""
Guido van Rossum4f0db6e2007-04-08 23:59:06 +0000988 self._unsupported("write")
Guido van Rossum78892e42007-04-06 17:31:18 +0000989
Guido van Rossum9b76da62007-04-11 01:09:03 +0000990 def truncate(self, pos: int = None) -> int:
991 """truncate(pos: int = None) -> int. Truncate size to pos."""
992 self.flush()
993 if pos is None:
994 pos = self.tell()
995 self.seek(pos)
996 return self.buffer.truncate()
997
Guido van Rossum78892e42007-04-06 17:31:18 +0000998 def readline(self) -> str:
999 """readline() -> str. Read until newline or EOF.
1000
1001 Returns an empty string if EOF is hit immediately.
1002 """
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001003 self._unsupported("readline")
Guido van Rossum78892e42007-04-06 17:31:18 +00001004
Guido van Rossumfc3436b2007-05-24 17:58:06 +00001005 @property
1006 def encoding(self):
1007 """Subclasses should override."""
1008 return None
1009
Guido van Rossum78892e42007-04-06 17:31:18 +00001010
1011class TextIOWrapper(TextIOBase):
1012
1013 """Buffered text stream.
1014
1015 Character and line based layer over a BufferedIOBase object.
1016 """
1017
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001018 _CHUNK_SIZE = 128
Guido van Rossum78892e42007-04-06 17:31:18 +00001019
1020 def __init__(self, buffer, encoding=None, newline=None):
Guido van Rossum9b76da62007-04-11 01:09:03 +00001021 if newline not in (None, "\n", "\r\n"):
1022 raise ValueError("illegal newline value: %r" % (newline,))
Guido van Rossum78892e42007-04-06 17:31:18 +00001023 if encoding is None:
1024 # XXX This is questionable
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001025 encoding = sys.getfilesystemencoding() or "latin-1"
Guido van Rossum78892e42007-04-06 17:31:18 +00001026
1027 self.buffer = buffer
1028 self._encoding = encoding
1029 self._newline = newline or os.linesep
1030 self._fix_newlines = newline is None
1031 self._decoder = None
Guido van Rossum9b76da62007-04-11 01:09:03 +00001032 self._pending = ""
1033 self._snapshot = None
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001034 self._seekable = self._telling = self.buffer.seekable()
Guido van Rossum9b76da62007-04-11 01:09:03 +00001035
Guido van Rossumfc3436b2007-05-24 17:58:06 +00001036 @property
1037 def encoding(self):
1038 return self._encoding
1039
Guido van Rossum9b76da62007-04-11 01:09:03 +00001040 # A word about _snapshot. This attribute is either None, or a
Guido van Rossumd76e7792007-04-17 02:38:04 +00001041 # tuple (decoder_state, readahead, pending) where decoder_state is
1042 # the second (integer) item of the decoder state, readahead is the
1043 # chunk of bytes that was read, and pending is the characters that
1044 # were rendered by the decoder after feeding it those bytes. We
1045 # use this to reconstruct intermediate decoder states in tell().
Guido van Rossum9b76da62007-04-11 01:09:03 +00001046
1047 def _seekable(self):
1048 return self._seekable
Guido van Rossum78892e42007-04-06 17:31:18 +00001049
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001050 def flush(self):
1051 self.buffer.flush()
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001052 self._telling = self._seekable
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001053
1054 def close(self):
Guido van Rossum33e7a8e2007-07-22 20:38:07 +00001055 try:
1056 self.flush()
1057 except:
1058 pass # If flush() fails, just give up
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001059 self.buffer.close()
1060
1061 @property
1062 def closed(self):
1063 return self.buffer.closed
1064
Guido van Rossum9be55972007-04-07 02:59:27 +00001065 def fileno(self):
1066 return self.buffer.fileno()
1067
Guido van Rossum859b5ec2007-05-27 09:14:51 +00001068 def isatty(self):
1069 return self.buffer.isatty()
1070
Guido van Rossum78892e42007-04-06 17:31:18 +00001071 def write(self, s: str):
Guido van Rossum4b5386f2007-07-10 09:12:49 +00001072 if self.closed:
1073 raise ValueError("write to closed file")
Guido van Rossum9b76da62007-04-11 01:09:03 +00001074 # XXX What if we were just reading?
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001075 b = s.encode(self._encoding)
1076 if isinstance(b, str):
1077 b = bytes(b)
1078 n = self.buffer.write(b)
1079 if "\n" in s:
Guido van Rossum13633bb2007-04-13 18:42:35 +00001080 # XXX only if isatty
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001081 self.flush()
Guido van Rossum9b76da62007-04-11 01:09:03 +00001082 self._snapshot = self._decoder = None
1083 return len(s)
Guido van Rossum78892e42007-04-06 17:31:18 +00001084
1085 def _get_decoder(self):
1086 make_decoder = codecs.getincrementaldecoder(self._encoding)
1087 if make_decoder is None:
Guido van Rossum9b76da62007-04-11 01:09:03 +00001088 raise IOError("Can't find an incremental decoder for encoding %s" %
Guido van Rossum78892e42007-04-06 17:31:18 +00001089 self._encoding)
1090 decoder = self._decoder = make_decoder() # XXX: errors
Guido van Rossum78892e42007-04-06 17:31:18 +00001091 return decoder
1092
Guido van Rossum9b76da62007-04-11 01:09:03 +00001093 def _read_chunk(self):
Guido van Rossum9b76da62007-04-11 01:09:03 +00001094 assert self._decoder is not None
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001095 if not self._telling:
Guido van Rossum13633bb2007-04-13 18:42:35 +00001096 readahead = self.buffer.read1(self._CHUNK_SIZE)
Guido van Rossumcba608c2007-04-11 14:19:59 +00001097 pending = self._decoder.decode(readahead, not readahead)
1098 return readahead, pending
Guido van Rossumd76e7792007-04-17 02:38:04 +00001099 decoder_buffer, decoder_state = self._decoder.getstate()
Guido van Rossum13633bb2007-04-13 18:42:35 +00001100 readahead = self.buffer.read1(self._CHUNK_SIZE)
Guido van Rossumcba608c2007-04-11 14:19:59 +00001101 pending = self._decoder.decode(readahead, not readahead)
Guido van Rossumd76e7792007-04-17 02:38:04 +00001102 self._snapshot = (decoder_state, decoder_buffer + readahead, pending)
Guido van Rossumcba608c2007-04-11 14:19:59 +00001103 return readahead, pending
Guido van Rossum9b76da62007-04-11 01:09:03 +00001104
1105 def _encode_decoder_state(self, ds, pos):
Guido van Rossum9b76da62007-04-11 01:09:03 +00001106 x = 0
1107 for i in bytes(ds):
1108 x = x<<8 | i
1109 return (x<<64) | pos
1110
1111 def _decode_decoder_state(self, pos):
1112 x, pos = divmod(pos, 1<<64)
1113 if not x:
1114 return None, pos
1115 b = b""
1116 while x:
1117 b.append(x&0xff)
1118 x >>= 8
1119 return str(b[::-1]), pos
1120
1121 def tell(self):
1122 if not self._seekable:
1123 raise IOError("Underlying stream is not seekable")
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001124 if not self._telling:
1125 raise IOError("Telling position disabled by next() call")
Guido van Rossum9b76da62007-04-11 01:09:03 +00001126 self.flush()
Guido van Rossumcba608c2007-04-11 14:19:59 +00001127 position = self.buffer.tell()
Guido van Rossumd76e7792007-04-17 02:38:04 +00001128 decoder = self._decoder
1129 if decoder is None or self._snapshot is None:
Guido van Rossum9b76da62007-04-11 01:09:03 +00001130 assert self._pending == ""
Guido van Rossumcba608c2007-04-11 14:19:59 +00001131 return position
1132 decoder_state, readahead, pending = self._snapshot
1133 position -= len(readahead)
1134 needed = len(pending) - len(self._pending)
1135 if not needed:
1136 return self._encode_decoder_state(decoder_state, position)
Guido van Rossumd76e7792007-04-17 02:38:04 +00001137 saved_state = decoder.getstate()
1138 try:
Guido van Rossum2b08b382007-05-08 20:18:39 +00001139 decoder.setstate((b"", decoder_state))
Guido van Rossumd76e7792007-04-17 02:38:04 +00001140 n = 0
1141 bb = bytes(1)
1142 for i, bb[0] in enumerate(readahead):
1143 n += len(decoder.decode(bb))
1144 if n >= needed:
1145 decoder_buffer, decoder_state = decoder.getstate()
1146 return self._encode_decoder_state(
1147 decoder_state,
1148 position + (i+1) - len(decoder_buffer))
1149 raise IOError("Can't reconstruct logical file position")
1150 finally:
1151 decoder.setstate(saved_state)
Guido van Rossum9b76da62007-04-11 01:09:03 +00001152
1153 def seek(self, pos, whence=0):
1154 if not self._seekable:
1155 raise IOError("Underlying stream is not seekable")
1156 if whence == 1:
1157 if pos != 0:
1158 raise IOError("Can't do nonzero cur-relative seeks")
Guido van Rossumaa43ed92007-04-12 05:24:24 +00001159 pos = self.tell()
1160 whence = 0
Guido van Rossum9b76da62007-04-11 01:09:03 +00001161 if whence == 2:
1162 if pos != 0:
1163 raise IOError("Can't do nonzero end-relative seeks")
1164 self.flush()
1165 pos = self.buffer.seek(0, 2)
1166 self._snapshot = None
1167 self._pending = ""
1168 self._decoder = None
1169 return pos
1170 if whence != 0:
1171 raise ValueError("Invalid whence (%r, should be 0, 1 or 2)" %
1172 (whence,))
1173 if pos < 0:
1174 raise ValueError("Negative seek position %r" % (pos,))
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001175 self.flush()
Guido van Rossum9b76da62007-04-11 01:09:03 +00001176 orig_pos = pos
1177 ds, pos = self._decode_decoder_state(pos)
1178 if not ds:
1179 self.buffer.seek(pos)
1180 self._snapshot = None
1181 self._pending = ""
1182 self._decoder = None
1183 return pos
Guido van Rossumd76e7792007-04-17 02:38:04 +00001184 decoder = self._decoder or self._get_decoder()
1185 decoder.set_state(("", ds))
Guido van Rossum9b76da62007-04-11 01:09:03 +00001186 self.buffer.seek(pos)
Guido van Rossumcba608c2007-04-11 14:19:59 +00001187 self._snapshot = (ds, b"", "")
Guido van Rossum9b76da62007-04-11 01:09:03 +00001188 self._pending = ""
Guido van Rossumcba608c2007-04-11 14:19:59 +00001189 self._decoder = decoder
Guido van Rossum9b76da62007-04-11 01:09:03 +00001190 return orig_pos
1191
Guido van Rossum024da5c2007-05-17 23:59:11 +00001192 def read(self, n=None):
1193 if n is None:
1194 n = -1
Guido van Rossum78892e42007-04-06 17:31:18 +00001195 decoder = self._decoder or self._get_decoder()
1196 res = self._pending
1197 if n < 0:
1198 res += decoder.decode(self.buffer.read(), True)
Guido van Rossum141f7672007-04-10 00:22:16 +00001199 self._pending = ""
Guido van Rossum9b76da62007-04-11 01:09:03 +00001200 self._snapshot = None
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001201 return res.replace("\r\n", "\n")
Guido van Rossum78892e42007-04-06 17:31:18 +00001202 else:
1203 while len(res) < n:
Guido van Rossumcba608c2007-04-11 14:19:59 +00001204 readahead, pending = self._read_chunk()
1205 res += pending
1206 if not readahead:
Guido van Rossum78892e42007-04-06 17:31:18 +00001207 break
1208 self._pending = res[n:]
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001209 return res[:n].replace("\r\n", "\n")
Guido van Rossum78892e42007-04-06 17:31:18 +00001210
Guido van Rossum024da5c2007-05-17 23:59:11 +00001211 def __next__(self):
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00001212 self._telling = False
1213 line = self.readline()
1214 if not line:
1215 self._snapshot = None
1216 self._telling = self._seekable
1217 raise StopIteration
1218 return line
1219
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001220 def readline(self, limit=None):
1221 if limit is not None:
Guido van Rossum9b76da62007-04-11 01:09:03 +00001222 # XXX Hack to support limit argument, for backwards compatibility
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001223 line = self.readline()
1224 if len(line) <= limit:
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001225 return line
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001226 line, self._pending = line[:limit], line[limit:] + self._pending
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001227 return line
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001228
Guido van Rossum78892e42007-04-06 17:31:18 +00001229 line = self._pending
1230 start = 0
1231 decoder = self._decoder or self._get_decoder()
1232
1233 while True:
1234 # In C we'd look for these in parallel of course.
1235 nlpos = line.find("\n", start)
1236 crpos = line.find("\r", start)
1237 if nlpos >= 0 and crpos >= 0:
1238 endpos = min(nlpos, crpos)
1239 else:
1240 endpos = nlpos if nlpos >= 0 else crpos
1241
1242 if endpos != -1:
1243 endc = line[endpos]
1244 if endc == "\n":
1245 ending = "\n"
1246 break
1247
1248 # We've seen \r - is it standalone, \r\n or \r at end of line?
1249 if endpos + 1 < len(line):
Guido van Rossum9b76da62007-04-11 01:09:03 +00001250 if line[endpos+1] == "\n":
Guido van Rossum78892e42007-04-06 17:31:18 +00001251 ending = "\r\n"
1252 else:
1253 ending = "\r"
1254 break
1255 # There might be a following \n in the next block of data ...
1256 start = endpos
1257 else:
1258 start = len(line)
1259
1260 # No line ending seen yet - get more data
1261 while True:
Guido van Rossumcba608c2007-04-11 14:19:59 +00001262 readahead, pending = self._read_chunk()
1263 more_line = pending
1264 if more_line or not readahead:
Guido van Rossum78892e42007-04-06 17:31:18 +00001265 break
1266
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001267 if not more_line:
1268 ending = ""
Guido van Rossum78892e42007-04-06 17:31:18 +00001269 endpos = len(line)
1270 break
1271
1272 line += more_line
1273
1274 nextpos = endpos + len(ending)
1275 self._pending = line[nextpos:]
1276
1277 # XXX Update self.newlines here if we want to support that
1278
Guido van Rossum4f0db6e2007-04-08 23:59:06 +00001279 if self._fix_newlines and ending not in ("\n", ""):
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001280 return line[:endpos] + "\n"
Guido van Rossum78892e42007-04-06 17:31:18 +00001281 else:
Guido van Rossumfa0054a2007-05-24 04:05:35 +00001282 return line[:nextpos]
Guido van Rossum024da5c2007-05-17 23:59:11 +00001283
1284
1285class StringIO(TextIOWrapper):
1286
1287 # XXX This is really slow, but fully functional
1288
1289 def __init__(self, initial_value=""):
1290 super(StringIO, self).__init__(BytesIO(), "utf-8")
1291 if initial_value:
1292 self.write(initial_value)
1293 self.seek(0)
1294
1295 def getvalue(self):
1296 return self.buffer.getvalue().decode("utf-8")