Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1 | """New I/O library conforming to PEP 3116. |
| 2 | |
| 3 | This is a prototype; hopefully eventually some of this will be |
| 4 | reimplemented in C. |
| 5 | |
| 6 | Conformance of alternative implementations: all arguments are intended |
| 7 | to be positional-only except the arguments of the open() function. |
| 8 | Argument names except those of the open() function are not part of the |
| 9 | specification. Instance variables and methods whose name starts with |
| 10 | a leading underscore are not part of the specification (except "magic" |
| 11 | names like __iter__). Only the top-level names listed in the __all__ |
| 12 | variable are part of the specification. |
| 13 | |
| 14 | XXX edge cases when switching between reading/writing |
| 15 | XXX need to support 1 meaning line-buffered |
| 16 | XXX whenever an argument is None, use the default value |
| 17 | XXX read/write ops should check readable/writable |
| 18 | XXX buffered readinto should work with arbitrary buffer objects |
| 19 | XXX use incremental encoder for text output, at least for UTF-16 and UTF-8-SIG |
| 20 | XXX check writable, readable and seekable in appropriate places |
| 21 | """ |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 22 | from __future__ import print_function |
| 23 | from __future__ import unicode_literals |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 24 | |
| 25 | __author__ = ("Guido van Rossum <guido@python.org>, " |
| 26 | "Mike Verdone <mike.verdone@gmail.com>, " |
| 27 | "Mark Russell <mark.russell@zen.co.uk>") |
| 28 | |
| 29 | __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO", |
| 30 | "BytesIO", "StringIO", "BufferedIOBase", |
| 31 | "BufferedReader", "BufferedWriter", "BufferedRWPair", |
| 32 | "BufferedRandom", "TextIOBase", "TextIOWrapper"] |
| 33 | |
| 34 | import os |
| 35 | import abc |
| 36 | import sys |
| 37 | import codecs |
| 38 | import _fileio |
| 39 | import warnings |
| 40 | |
| 41 | # open() uses st_blksize whenever we can |
| 42 | DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes |
| 43 | |
| 44 | # py3k has only new style classes |
| 45 | __metaclass__ = type |
| 46 | |
| 47 | class BlockingIOError(IOError): |
| 48 | |
| 49 | """Exception raised when I/O would block on a non-blocking I/O stream.""" |
| 50 | |
| 51 | def __init__(self, errno, strerror, characters_written=0): |
| 52 | IOError.__init__(self, errno, strerror) |
| 53 | self.characters_written = characters_written |
| 54 | |
| 55 | |
| 56 | def open(file, mode="r", buffering=None, encoding=None, errors=None, |
| 57 | newline=None, closefd=True): |
| 58 | r"""Replacement for the built-in open function. |
| 59 | |
| 60 | Args: |
| 61 | file: string giving the name of the file to be opened; |
| 62 | or integer file descriptor of the file to be wrapped (*). |
| 63 | mode: optional mode string; see below. |
| 64 | buffering: optional int >= 0 giving the buffer size; values |
| 65 | can be: 0 = unbuffered, 1 = line buffered, |
| 66 | larger = fully buffered. |
| 67 | encoding: optional string giving the text encoding. |
| 68 | errors: optional string giving the encoding error handling. |
| 69 | newline: optional newlines specifier; must be None, '', '\n', '\r' |
| 70 | or '\r\n'; all other values are illegal. It controls the |
| 71 | handling of line endings. It works as follows: |
| 72 | |
| 73 | * On input, if `newline` is `None`, universal newlines |
| 74 | mode is enabled. Lines in the input can end in `'\n'`, |
| 75 | `'\r'`, or `'\r\n'`, and these are translated into |
| 76 | `'\n'` before being returned to the caller. If it is |
| 77 | `''`, universal newline mode is enabled, but line endings |
| 78 | are returned to the caller untranslated. If it has any of |
| 79 | the other legal values, input lines are only terminated by |
| 80 | the given string, and the line ending is returned to the |
| 81 | caller untranslated. |
| 82 | |
| 83 | * On output, if `newline` is `None`, any `'\n'` |
| 84 | characters written are translated to the system default |
| 85 | line separator, `os.linesep`. If `newline` is `''`, |
| 86 | no translation takes place. If `newline` is any of the |
| 87 | other legal values, any `'\n'` characters written are |
| 88 | translated to the given string. |
| 89 | |
| 90 | closefd: optional argument to keep the underlying file descriptor |
| 91 | open when the file is closed. It must not be false when |
| 92 | a filename is given. |
| 93 | |
| 94 | (*) If a file descriptor is given, it is closed when the returned |
| 95 | I/O object is closed, unless closefd=False is given. |
| 96 | |
| 97 | Mode strings characters: |
| 98 | 'r': open for reading (default) |
| 99 | 'w': open for writing, truncating the file first |
| 100 | 'a': open for writing, appending to the end if the file exists |
| 101 | 'b': binary mode |
| 102 | 't': text mode (default) |
| 103 | '+': open a disk file for updating (implies reading and writing) |
| 104 | 'U': universal newline mode (for backwards compatibility) |
| 105 | |
| 106 | Constraints: |
| 107 | - encoding or errors must not be given when a binary mode is given |
| 108 | - buffering must not be zero when a text mode is given |
| 109 | |
| 110 | Returns: |
| 111 | Depending on the mode and buffering arguments, either a raw |
| 112 | binary stream, a buffered binary stream, or a buffered text |
| 113 | stream, open for reading and/or writing. |
| 114 | """ |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 115 | if not isinstance(file, (basestring, int)): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 116 | raise TypeError("invalid file: %r" % file) |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 117 | if not isinstance(mode, basestring): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 118 | raise TypeError("invalid mode: %r" % mode) |
| 119 | if buffering is not None and not isinstance(buffering, int): |
| 120 | raise TypeError("invalid buffering: %r" % buffering) |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 121 | if encoding is not None and not isinstance(encoding, basestring): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 122 | raise TypeError("invalid encoding: %r" % encoding) |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 123 | if errors is not None and not isinstance(errors, basestring): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 124 | raise TypeError("invalid errors: %r" % errors) |
| 125 | modes = set(mode) |
| 126 | if modes - set("arwb+tU") or len(mode) > len(modes): |
| 127 | raise ValueError("invalid mode: %r" % mode) |
| 128 | reading = "r" in modes |
| 129 | writing = "w" in modes |
| 130 | appending = "a" in modes |
| 131 | updating = "+" in modes |
| 132 | text = "t" in modes |
| 133 | binary = "b" in modes |
| 134 | if "U" in modes: |
| 135 | if writing or appending: |
| 136 | raise ValueError("can't use U and writing mode at once") |
| 137 | reading = True |
| 138 | if text and binary: |
| 139 | raise ValueError("can't have text and binary mode at once") |
| 140 | if reading + writing + appending > 1: |
| 141 | raise ValueError("can't have read/write/append mode at once") |
| 142 | if not (reading or writing or appending): |
| 143 | raise ValueError("must have exactly one of read/write/append mode") |
| 144 | if binary and encoding is not None: |
| 145 | raise ValueError("binary mode doesn't take an encoding argument") |
| 146 | if binary and errors is not None: |
| 147 | raise ValueError("binary mode doesn't take an errors argument") |
| 148 | if binary and newline is not None: |
| 149 | raise ValueError("binary mode doesn't take a newline argument") |
| 150 | raw = FileIO(file, |
| 151 | (reading and "r" or "") + |
| 152 | (writing and "w" or "") + |
| 153 | (appending and "a" or "") + |
| 154 | (updating and "+" or ""), |
| 155 | closefd) |
| 156 | if buffering is None: |
| 157 | buffering = -1 |
| 158 | line_buffering = False |
| 159 | if buffering == 1 or buffering < 0 and raw.isatty(): |
| 160 | buffering = -1 |
| 161 | line_buffering = True |
| 162 | if buffering < 0: |
| 163 | buffering = DEFAULT_BUFFER_SIZE |
| 164 | try: |
| 165 | bs = os.fstat(raw.fileno()).st_blksize |
| 166 | except (os.error, AttributeError): |
| 167 | pass |
| 168 | else: |
| 169 | if bs > 1: |
| 170 | buffering = bs |
| 171 | if buffering < 0: |
| 172 | raise ValueError("invalid buffering size") |
| 173 | if buffering == 0: |
| 174 | if binary: |
| 175 | raw._name = file |
| 176 | raw._mode = mode |
| 177 | return raw |
| 178 | raise ValueError("can't have unbuffered text I/O") |
| 179 | if updating: |
| 180 | buffer = BufferedRandom(raw, buffering) |
| 181 | elif writing or appending: |
| 182 | buffer = BufferedWriter(raw, buffering) |
| 183 | elif reading: |
| 184 | buffer = BufferedReader(raw, buffering) |
| 185 | else: |
| 186 | raise ValueError("unknown mode: %r" % mode) |
| 187 | if binary: |
| 188 | buffer.name = file |
| 189 | buffer.mode = mode |
| 190 | return buffer |
| 191 | text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) |
| 192 | text.name = file |
| 193 | text.mode = mode |
| 194 | return text |
| 195 | |
| 196 | class _DocDescriptor: |
| 197 | """Helper for builtins.open.__doc__ |
| 198 | """ |
| 199 | def __get__(self, obj, typ): |
| 200 | return ( |
| 201 | "open(file, mode='r', buffering=None, encoding=None, " |
| 202 | "errors=None, newline=None, closefd=True)\n\n" + |
| 203 | open.__doc__) |
| 204 | |
| 205 | class OpenWrapper: |
| 206 | """Wrapper for builtins.open |
| 207 | |
| 208 | Trick so that open won't become a bound method when stored |
| 209 | as a class variable (as dumbdbm does). |
| 210 | |
| 211 | See initstdio() in Python/pythonrun.c. |
| 212 | """ |
| 213 | __doc__ = _DocDescriptor() |
| 214 | |
| 215 | def __new__(cls, *args, **kwargs): |
| 216 | return open(*args, **kwargs) |
| 217 | |
| 218 | |
| 219 | class UnsupportedOperation(ValueError, IOError): |
| 220 | pass |
| 221 | |
| 222 | |
| 223 | class IOBase(object): |
| 224 | |
| 225 | """Base class for all I/O classes. |
| 226 | |
| 227 | This class provides dummy implementations for many methods that |
| 228 | derived classes can override selectively; the default |
| 229 | implementations represent a file that cannot be read, written or |
| 230 | seeked. |
| 231 | |
| 232 | This does not define read(), readinto() and write(), nor |
| 233 | readline() and friends, since their signatures vary per layer. |
| 234 | |
| 235 | Not that calling any method (even inquiries) on a closed file is |
| 236 | undefined. Implementations may raise IOError in this case. |
| 237 | """ |
| 238 | |
| 239 | __metaclass__ = abc.ABCMeta |
| 240 | |
| 241 | ### Internal ### |
| 242 | |
| 243 | def _unsupported(self, name): |
| 244 | """Internal: raise an exception for unsupported operations.""" |
| 245 | raise UnsupportedOperation("%s.%s() not supported" % |
| 246 | (self.__class__.__name__, name)) |
| 247 | |
| 248 | ### Positioning ### |
| 249 | |
| 250 | def seek(self, pos, whence = 0): |
| 251 | """seek(pos: int, whence: int = 0) -> int. Change stream position. |
| 252 | |
| 253 | Seek to byte offset pos relative to position indicated by whence: |
| 254 | 0 Start of stream (the default). pos should be >= 0; |
| 255 | 1 Current position - whence may be negative; |
| 256 | 2 End of stream - whence usually negative. |
| 257 | Returns the new absolute position. |
| 258 | """ |
| 259 | self._unsupported("seek") |
| 260 | |
| 261 | def tell(self): |
| 262 | """tell() -> int. Return current stream position.""" |
| 263 | return self.seek(0, 1) |
| 264 | |
| 265 | def truncate(self, pos = None): |
| 266 | """truncate(size: int = None) -> int. Truncate file to size bytes. |
| 267 | |
| 268 | Size defaults to the current IO position as reported by tell(). |
| 269 | Returns the new size. |
| 270 | """ |
| 271 | self._unsupported("truncate") |
| 272 | |
| 273 | ### Flush and close ### |
| 274 | |
| 275 | def flush(self): |
| 276 | """flush() -> None. Flushes write buffers, if applicable. |
| 277 | |
| 278 | This is a no-op for read-only and non-blocking streams. |
| 279 | """ |
| 280 | # XXX Should this return the number of bytes written??? |
| 281 | |
| 282 | __closed = False |
| 283 | |
| 284 | def close(self): |
| 285 | """close() -> None. Flushes and closes the IO object. |
| 286 | |
| 287 | This must be idempotent. It should also set a flag for the |
| 288 | 'closed' property (see below) to test. |
| 289 | """ |
| 290 | if not self.__closed: |
| 291 | try: |
| 292 | self.flush() |
| 293 | except IOError: |
| 294 | pass # If flush() fails, just give up |
| 295 | self.__closed = True |
| 296 | |
| 297 | def __del__(self): |
| 298 | """Destructor. Calls close().""" |
| 299 | # The try/except block is in case this is called at program |
| 300 | # exit time, when it's possible that globals have already been |
| 301 | # deleted, and then the close() call might fail. Since |
| 302 | # there's nothing we can do about such failures and they annoy |
| 303 | # the end users, we suppress the traceback. |
| 304 | try: |
| 305 | self.close() |
| 306 | except: |
| 307 | pass |
| 308 | |
| 309 | ### Inquiries ### |
| 310 | |
| 311 | def seekable(self): |
| 312 | """seekable() -> bool. Return whether object supports random access. |
| 313 | |
| 314 | If False, seek(), tell() and truncate() will raise IOError. |
| 315 | This method may need to do a test seek(). |
| 316 | """ |
| 317 | return False |
| 318 | |
| 319 | def _checkSeekable(self, msg=None): |
| 320 | """Internal: raise an IOError if file is not seekable |
| 321 | """ |
| 322 | if not self.seekable(): |
| 323 | raise IOError("File or stream is not seekable." |
| 324 | if msg is None else msg) |
| 325 | |
| 326 | |
| 327 | def readable(self): |
| 328 | """readable() -> bool. Return whether object was opened for reading. |
| 329 | |
| 330 | If False, read() will raise IOError. |
| 331 | """ |
| 332 | return False |
| 333 | |
| 334 | def _checkReadable(self, msg=None): |
| 335 | """Internal: raise an IOError if file is not readable |
| 336 | """ |
| 337 | if not self.readable(): |
| 338 | raise IOError("File or stream is not readable." |
| 339 | if msg is None else msg) |
| 340 | |
| 341 | def writable(self): |
| 342 | """writable() -> bool. Return whether object was opened for writing. |
| 343 | |
| 344 | If False, write() and truncate() will raise IOError. |
| 345 | """ |
| 346 | return False |
| 347 | |
| 348 | def _checkWritable(self, msg=None): |
| 349 | """Internal: raise an IOError if file is not writable |
| 350 | """ |
| 351 | if not self.writable(): |
| 352 | raise IOError("File or stream is not writable." |
| 353 | if msg is None else msg) |
| 354 | |
| 355 | @property |
| 356 | def closed(self): |
| 357 | """closed: bool. True iff the file has been closed. |
| 358 | |
| 359 | For backwards compatibility, this is a property, not a predicate. |
| 360 | """ |
| 361 | return self.__closed |
| 362 | |
| 363 | def _checkClosed(self, msg=None): |
| 364 | """Internal: raise an ValueError if file is closed |
| 365 | """ |
| 366 | if self.closed: |
| 367 | raise ValueError("I/O operation on closed file." |
| 368 | if msg is None else msg) |
| 369 | |
| 370 | ### Context manager ### |
| 371 | |
| 372 | def __enter__(self): |
| 373 | """Context management protocol. Returns self.""" |
| 374 | self._checkClosed() |
| 375 | return self |
| 376 | |
| 377 | def __exit__(self, *args): |
| 378 | """Context management protocol. Calls close()""" |
| 379 | self.close() |
| 380 | |
| 381 | ### Lower-level APIs ### |
| 382 | |
| 383 | # XXX Should these be present even if unimplemented? |
| 384 | |
| 385 | def fileno(self): |
| 386 | """fileno() -> int. Returns underlying file descriptor if one exists. |
| 387 | |
| 388 | Raises IOError if the IO object does not use a file descriptor. |
| 389 | """ |
| 390 | self._unsupported("fileno") |
| 391 | |
| 392 | def isatty(self): |
| 393 | """isatty() -> int. Returns whether this is an 'interactive' stream. |
| 394 | |
| 395 | Returns False if we don't know. |
| 396 | """ |
| 397 | self._checkClosed() |
| 398 | return False |
| 399 | |
| 400 | ### Readline[s] and writelines ### |
| 401 | |
| 402 | def readline(self, limit = -1): |
| 403 | """For backwards compatibility, a (slowish) readline().""" |
| 404 | if hasattr(self, "peek"): |
| 405 | def nreadahead(): |
| 406 | readahead = self.peek(1) |
| 407 | if not readahead: |
| 408 | return 1 |
| 409 | n = (readahead.find(b"\n") + 1) or len(readahead) |
| 410 | if limit >= 0: |
| 411 | n = min(n, limit) |
| 412 | return n |
| 413 | else: |
| 414 | def nreadahead(): |
| 415 | return 1 |
| 416 | if limit is None: |
| 417 | limit = -1 |
| 418 | res = bytearray() |
| 419 | while limit < 0 or len(res) < limit: |
| 420 | b = self.read(nreadahead()) |
| 421 | if not b: |
| 422 | break |
| 423 | res += b |
| 424 | if res.endswith(b"\n"): |
| 425 | break |
| 426 | return bytes(res) |
| 427 | |
| 428 | def __iter__(self): |
| 429 | self._checkClosed() |
| 430 | return self |
| 431 | |
| 432 | def next(self): |
| 433 | line = self.readline() |
| 434 | if not line: |
| 435 | raise StopIteration |
| 436 | return line |
| 437 | |
| 438 | def readlines(self, hint=None): |
| 439 | if hint is None: |
| 440 | return list(self) |
| 441 | n = 0 |
| 442 | lines = [] |
| 443 | for line in self: |
| 444 | lines.append(line) |
| 445 | n += len(line) |
| 446 | if n >= hint: |
| 447 | break |
| 448 | return lines |
| 449 | |
| 450 | def writelines(self, lines): |
| 451 | self._checkClosed() |
| 452 | for line in lines: |
| 453 | self.write(line) |
| 454 | |
| 455 | |
| 456 | class RawIOBase(IOBase): |
| 457 | |
| 458 | """Base class for raw binary I/O. |
| 459 | |
| 460 | The read() method is implemented by calling readinto(); derived |
| 461 | classes that want to support read() only need to implement |
| 462 | readinto() as a primitive operation. In general, readinto() |
| 463 | can be more efficient than read(). |
| 464 | |
| 465 | (It would be tempting to also provide an implementation of |
| 466 | readinto() in terms of read(), in case the latter is a more |
| 467 | suitable primitive operation, but that would lead to nasty |
| 468 | recursion in case a subclass doesn't implement either.) |
| 469 | """ |
| 470 | |
| 471 | def read(self, n = -1): |
| 472 | """read(n: int) -> bytes. Read and return up to n bytes. |
| 473 | |
| 474 | Returns an empty bytes array on EOF, or None if the object is |
| 475 | set not to block and has no data to read. |
| 476 | """ |
| 477 | if n is None: |
| 478 | n = -1 |
| 479 | if n < 0: |
| 480 | return self.readall() |
| 481 | b = bytearray(n.__index__()) |
| 482 | n = self.readinto(b) |
| 483 | del b[n:] |
| 484 | return bytes(b) |
| 485 | |
| 486 | def readall(self): |
| 487 | """readall() -> bytes. Read until EOF, using multiple read() call.""" |
| 488 | res = bytearray() |
| 489 | while True: |
| 490 | data = self.read(DEFAULT_BUFFER_SIZE) |
| 491 | if not data: |
| 492 | break |
| 493 | res += data |
| 494 | return bytes(res) |
| 495 | |
| 496 | def readinto(self, b): |
| 497 | """readinto(b: bytes) -> int. Read up to len(b) bytes into b. |
| 498 | |
| 499 | Returns number of bytes read (0 for EOF), or None if the object |
| 500 | is set not to block as has no data to read. |
| 501 | """ |
| 502 | self._unsupported("readinto") |
| 503 | |
| 504 | def write(self, b): |
| 505 | """write(b: bytes) -> int. Write the given buffer to the IO stream. |
| 506 | |
| 507 | Returns the number of bytes written, which may be less than len(b). |
| 508 | """ |
| 509 | self._unsupported("write") |
| 510 | |
| 511 | |
| 512 | class FileIO(_fileio._FileIO, RawIOBase): |
| 513 | |
| 514 | """Raw I/O implementation for OS files. |
| 515 | |
| 516 | This multiply inherits from _FileIO and RawIOBase to make |
| 517 | isinstance(io.FileIO(), io.RawIOBase) return True without |
| 518 | requiring that _fileio._FileIO inherits from io.RawIOBase (which |
| 519 | would be hard to do since _fileio.c is written in C). |
| 520 | """ |
| 521 | |
| 522 | def close(self): |
| 523 | _fileio._FileIO.close(self) |
| 524 | RawIOBase.close(self) |
| 525 | |
| 526 | @property |
| 527 | def name(self): |
| 528 | return self._name |
| 529 | |
| 530 | @property |
| 531 | def mode(self): |
| 532 | return self._mode |
| 533 | |
| 534 | |
| 535 | class BufferedIOBase(IOBase): |
| 536 | |
| 537 | """Base class for buffered IO objects. |
| 538 | |
| 539 | The main difference with RawIOBase is that the read() method |
| 540 | supports omitting the size argument, and does not have a default |
| 541 | implementation that defers to readinto(). |
| 542 | |
| 543 | In addition, read(), readinto() and write() may raise |
| 544 | BlockingIOError if the underlying raw stream is in non-blocking |
| 545 | mode and not ready; unlike their raw counterparts, they will never |
| 546 | return None. |
| 547 | |
| 548 | A typical implementation should not inherit from a RawIOBase |
| 549 | implementation, but wrap one. |
| 550 | """ |
| 551 | |
| 552 | def read(self, n = None): |
| 553 | """read(n: int = None) -> bytes. Read and return up to n bytes. |
| 554 | |
| 555 | If the argument is omitted, None, or negative, reads and |
| 556 | returns all data until EOF. |
| 557 | |
| 558 | If the argument is positive, and the underlying raw stream is |
| 559 | not 'interactive', multiple raw reads may be issued to satisfy |
| 560 | the byte count (unless EOF is reached first). But for |
| 561 | interactive raw streams (XXX and for pipes?), at most one raw |
| 562 | read will be issued, and a short result does not imply that |
| 563 | EOF is imminent. |
| 564 | |
| 565 | Returns an empty bytes array on EOF. |
| 566 | |
| 567 | Raises BlockingIOError if the underlying raw stream has no |
| 568 | data at the moment. |
| 569 | """ |
| 570 | self._unsupported("read") |
| 571 | |
| 572 | def readinto(self, b): |
| 573 | """readinto(b: bytes) -> int. Read up to len(b) bytes into b. |
| 574 | |
| 575 | Like read(), this may issue multiple reads to the underlying |
| 576 | raw stream, unless the latter is 'interactive' (XXX or a |
| 577 | pipe?). |
| 578 | |
| 579 | Returns the number of bytes read (0 for EOF). |
| 580 | |
| 581 | Raises BlockingIOError if the underlying raw stream has no |
| 582 | data at the moment. |
| 583 | """ |
| 584 | # XXX This ought to work with anything that supports the buffer API |
| 585 | data = self.read(len(b)) |
| 586 | n = len(data) |
| 587 | try: |
| 588 | b[:n] = data |
| 589 | except TypeError as err: |
| 590 | import array |
| 591 | if not isinstance(b, array.array): |
| 592 | raise err |
| 593 | b[:n] = array.array('b', data) |
| 594 | return n |
| 595 | |
| 596 | def write(self, b): |
| 597 | """write(b: bytes) -> int. Write the given buffer to the IO stream. |
| 598 | |
| 599 | Returns the number of bytes written, which is never less than |
| 600 | len(b). |
| 601 | |
| 602 | Raises BlockingIOError if the buffer is full and the |
| 603 | underlying raw stream cannot accept more data at the moment. |
| 604 | """ |
| 605 | self._unsupported("write") |
| 606 | |
| 607 | |
| 608 | class _BufferedIOMixin(BufferedIOBase): |
| 609 | |
| 610 | """A mixin implementation of BufferedIOBase with an underlying raw stream. |
| 611 | |
| 612 | This passes most requests on to the underlying raw stream. It |
| 613 | does *not* provide implementations of read(), readinto() or |
| 614 | write(). |
| 615 | """ |
| 616 | |
| 617 | def __init__(self, raw): |
| 618 | self.raw = raw |
| 619 | |
| 620 | ### Positioning ### |
| 621 | |
| 622 | def seek(self, pos, whence=0): |
| 623 | return self.raw.seek(pos, whence) |
| 624 | |
| 625 | def tell(self): |
| 626 | return self.raw.tell() |
| 627 | |
| 628 | def truncate(self, pos=None): |
| 629 | # Flush the stream. We're mixing buffered I/O with lower-level I/O, |
| 630 | # and a flush may be necessary to synch both views of the current |
| 631 | # file state. |
| 632 | self.flush() |
| 633 | |
| 634 | if pos is None: |
| 635 | pos = self.tell() |
| 636 | return self.raw.truncate(pos) |
| 637 | |
| 638 | ### Flush and close ### |
| 639 | |
| 640 | def flush(self): |
| 641 | self.raw.flush() |
| 642 | |
| 643 | def close(self): |
| 644 | if not self.closed: |
| 645 | try: |
| 646 | self.flush() |
| 647 | except IOError: |
| 648 | pass # If flush() fails, just give up |
| 649 | self.raw.close() |
| 650 | |
| 651 | ### Inquiries ### |
| 652 | |
| 653 | def seekable(self): |
| 654 | return self.raw.seekable() |
| 655 | |
| 656 | def readable(self): |
| 657 | return self.raw.readable() |
| 658 | |
| 659 | def writable(self): |
| 660 | return self.raw.writable() |
| 661 | |
| 662 | @property |
| 663 | def closed(self): |
| 664 | return self.raw.closed |
| 665 | |
| 666 | ### Lower-level APIs ### |
| 667 | |
| 668 | def fileno(self): |
| 669 | return self.raw.fileno() |
| 670 | |
| 671 | def isatty(self): |
| 672 | return self.raw.isatty() |
| 673 | |
| 674 | |
| 675 | class BytesIO(BufferedIOBase): |
| 676 | |
| 677 | """Buffered I/O implementation using an in-memory bytes buffer.""" |
| 678 | |
| 679 | # XXX More docs |
| 680 | |
| 681 | def __init__(self, initial_bytes=None): |
| 682 | buf = bytearray() |
| 683 | if initial_bytes is not None: |
| 684 | buf += initial_bytes |
| 685 | self._buffer = buf |
| 686 | self._pos = 0 |
| 687 | |
| 688 | def getvalue(self): |
| 689 | return bytes(self._buffer) |
| 690 | |
| 691 | def read(self, n=None): |
| 692 | if n is None: |
| 693 | n = -1 |
| 694 | if n < 0: |
| 695 | n = len(self._buffer) |
| 696 | newpos = min(len(self._buffer), self._pos + n) |
| 697 | b = self._buffer[self._pos : newpos] |
| 698 | self._pos = newpos |
| 699 | return bytes(b) |
| 700 | |
| 701 | def read1(self, n): |
| 702 | return self.read(n) |
| 703 | |
| 704 | def write(self, b): |
| 705 | if self.closed: |
| 706 | raise ValueError("write to closed file") |
| 707 | if isinstance(b, unicode): |
| 708 | raise TypeError("can't write unicode to binary stream") |
| 709 | n = len(b) |
| 710 | newpos = self._pos + n |
| 711 | if newpos > len(self._buffer): |
| 712 | # Inserts null bytes between the current end of the file |
| 713 | # and the new write position. |
| 714 | padding = b'\x00' * (newpos - len(self._buffer) - n) |
| 715 | self._buffer[self._pos:newpos - n] = padding |
| 716 | self._buffer[self._pos:newpos] = b |
| 717 | self._pos = newpos |
| 718 | return n |
| 719 | |
| 720 | def seek(self, pos, whence=0): |
| 721 | try: |
| 722 | pos = pos.__index__() |
| 723 | except AttributeError as err: |
| 724 | raise TypeError("an integer is required") # from err |
| 725 | if whence == 0: |
| 726 | self._pos = max(0, pos) |
| 727 | elif whence == 1: |
| 728 | self._pos = max(0, self._pos + pos) |
| 729 | elif whence == 2: |
| 730 | self._pos = max(0, len(self._buffer) + pos) |
| 731 | else: |
| 732 | raise IOError("invalid whence value") |
| 733 | return self._pos |
| 734 | |
| 735 | def tell(self): |
| 736 | return self._pos |
| 737 | |
| 738 | def truncate(self, pos=None): |
| 739 | if pos is None: |
| 740 | pos = self._pos |
| 741 | del self._buffer[pos:] |
| 742 | return pos |
| 743 | |
| 744 | def readable(self): |
| 745 | return True |
| 746 | |
| 747 | def writable(self): |
| 748 | return True |
| 749 | |
| 750 | def seekable(self): |
| 751 | return True |
| 752 | |
| 753 | |
| 754 | class BufferedReader(_BufferedIOMixin): |
| 755 | |
| 756 | """Buffer for a readable sequential RawIO object.""" |
| 757 | |
| 758 | def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE): |
| 759 | """Create a new buffered reader using the given readable raw IO object. |
| 760 | """ |
| 761 | raw._checkReadable() |
| 762 | _BufferedIOMixin.__init__(self, raw) |
| 763 | self._read_buf = b"" |
| 764 | self.buffer_size = buffer_size |
| 765 | |
| 766 | def read(self, n=None): |
| 767 | """Read n bytes. |
| 768 | |
| 769 | Returns exactly n bytes of data unless the underlying raw IO |
| 770 | stream reaches EOF or if the call would block in non-blocking |
| 771 | mode. If n is negative, read until EOF or until read() would |
| 772 | block. |
| 773 | """ |
| 774 | if n is None: |
| 775 | n = -1 |
| 776 | nodata_val = b"" |
| 777 | while n < 0 or len(self._read_buf) < n: |
| 778 | to_read = max(self.buffer_size, |
| 779 | n if n is not None else 2*len(self._read_buf)) |
| 780 | current = self.raw.read(to_read) |
| 781 | if current in (b"", None): |
| 782 | nodata_val = current |
| 783 | break |
| 784 | self._read_buf += current |
| 785 | if self._read_buf: |
| 786 | if n < 0: |
| 787 | n = len(self._read_buf) |
| 788 | out = self._read_buf[:n] |
| 789 | self._read_buf = self._read_buf[n:] |
| 790 | else: |
| 791 | out = nodata_val |
| 792 | return out |
| 793 | |
| 794 | def peek(self, n=0): |
| 795 | """Returns buffered bytes without advancing the position. |
| 796 | |
| 797 | The argument indicates a desired minimal number of bytes; we |
| 798 | do at most one raw read to satisfy it. We never return more |
| 799 | than self.buffer_size. |
| 800 | """ |
| 801 | want = min(n, self.buffer_size) |
| 802 | have = len(self._read_buf) |
| 803 | if have < want: |
| 804 | to_read = self.buffer_size - have |
| 805 | current = self.raw.read(to_read) |
| 806 | if current: |
| 807 | self._read_buf += current |
| 808 | return self._read_buf |
| 809 | |
| 810 | def read1(self, n): |
| 811 | """Reads up to n bytes, with at most one read() system call. |
| 812 | |
| 813 | Returns up to n bytes. If at least one byte is buffered, we |
| 814 | only return buffered bytes. Otherwise, we do one raw read. |
| 815 | """ |
| 816 | if n <= 0: |
| 817 | return b"" |
| 818 | self.peek(1) |
| 819 | return self.read(min(n, len(self._read_buf))) |
| 820 | |
| 821 | def tell(self): |
| 822 | return self.raw.tell() - len(self._read_buf) |
| 823 | |
| 824 | def seek(self, pos, whence=0): |
| 825 | if whence == 1: |
| 826 | pos -= len(self._read_buf) |
| 827 | pos = self.raw.seek(pos, whence) |
| 828 | self._read_buf = b"" |
| 829 | return pos |
| 830 | |
| 831 | |
| 832 | class BufferedWriter(_BufferedIOMixin): |
| 833 | |
| 834 | # XXX docstring |
| 835 | |
| 836 | def __init__(self, raw, |
| 837 | buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
| 838 | raw._checkWritable() |
| 839 | _BufferedIOMixin.__init__(self, raw) |
| 840 | self.buffer_size = buffer_size |
| 841 | self.max_buffer_size = (2*buffer_size |
| 842 | if max_buffer_size is None |
| 843 | else max_buffer_size) |
| 844 | self._write_buf = bytearray() |
| 845 | |
| 846 | def write(self, b): |
| 847 | if self.closed: |
| 848 | raise ValueError("write to closed file") |
| 849 | if isinstance(b, unicode): |
| 850 | raise TypeError("can't write unicode to binary stream") |
| 851 | # XXX we can implement some more tricks to try and avoid partial writes |
| 852 | if len(self._write_buf) > self.buffer_size: |
| 853 | # We're full, so let's pre-flush the buffer |
| 854 | try: |
| 855 | self.flush() |
| 856 | except BlockingIOError as e: |
| 857 | # We can't accept anything else. |
| 858 | # XXX Why not just let the exception pass through? |
| 859 | raise BlockingIOError(e.errno, e.strerror, 0) |
| 860 | before = len(self._write_buf) |
| 861 | self._write_buf.extend(b) |
| 862 | written = len(self._write_buf) - before |
| 863 | if len(self._write_buf) > self.buffer_size: |
| 864 | try: |
| 865 | self.flush() |
| 866 | except BlockingIOError as e: |
| 867 | if (len(self._write_buf) > self.max_buffer_size): |
| 868 | # We've hit max_buffer_size. We have to accept a partial |
| 869 | # write and cut back our buffer. |
| 870 | overage = len(self._write_buf) - self.max_buffer_size |
| 871 | self._write_buf = self._write_buf[:self.max_buffer_size] |
| 872 | raise BlockingIOError(e.errno, e.strerror, overage) |
| 873 | return written |
| 874 | |
| 875 | def flush(self): |
| 876 | if self.closed: |
| 877 | raise ValueError("flush of closed file") |
| 878 | written = 0 |
| 879 | try: |
| 880 | while self._write_buf: |
| 881 | n = self.raw.write(self._write_buf) |
| 882 | del self._write_buf[:n] |
| 883 | written += n |
| 884 | except BlockingIOError as e: |
| 885 | n = e.characters_written |
| 886 | del self._write_buf[:n] |
| 887 | written += n |
| 888 | raise BlockingIOError(e.errno, e.strerror, written) |
| 889 | |
| 890 | def tell(self): |
| 891 | return self.raw.tell() + len(self._write_buf) |
| 892 | |
| 893 | def seek(self, pos, whence=0): |
| 894 | self.flush() |
| 895 | return self.raw.seek(pos, whence) |
| 896 | |
| 897 | |
| 898 | class BufferedRWPair(BufferedIOBase): |
| 899 | |
| 900 | """A buffered reader and writer object together. |
| 901 | |
| 902 | A buffered reader object and buffered writer object put together |
| 903 | to form a sequential IO object that can read and write. |
| 904 | |
| 905 | This is typically used with a socket or two-way pipe. |
| 906 | |
| 907 | XXX The usefulness of this (compared to having two separate IO |
| 908 | objects) is questionable. |
| 909 | """ |
| 910 | |
| 911 | def __init__(self, reader, writer, |
| 912 | buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
| 913 | """Constructor. |
| 914 | |
| 915 | The arguments are two RawIO instances. |
| 916 | """ |
| 917 | reader._checkReadable() |
| 918 | writer._checkWritable() |
| 919 | self.reader = BufferedReader(reader, buffer_size) |
| 920 | self.writer = BufferedWriter(writer, buffer_size, max_buffer_size) |
| 921 | |
| 922 | def read(self, n=None): |
| 923 | if n is None: |
| 924 | n = -1 |
| 925 | return self.reader.read(n) |
| 926 | |
| 927 | def readinto(self, b): |
| 928 | return self.reader.readinto(b) |
| 929 | |
| 930 | def write(self, b): |
| 931 | return self.writer.write(b) |
| 932 | |
| 933 | def peek(self, n=0): |
| 934 | return self.reader.peek(n) |
| 935 | |
| 936 | def read1(self, n): |
| 937 | return self.reader.read1(n) |
| 938 | |
| 939 | def readable(self): |
| 940 | return self.reader.readable() |
| 941 | |
| 942 | def writable(self): |
| 943 | return self.writer.writable() |
| 944 | |
| 945 | def flush(self): |
| 946 | return self.writer.flush() |
| 947 | |
| 948 | def close(self): |
| 949 | self.writer.close() |
| 950 | self.reader.close() |
| 951 | |
| 952 | def isatty(self): |
| 953 | return self.reader.isatty() or self.writer.isatty() |
| 954 | |
| 955 | @property |
| 956 | def closed(self): |
| 957 | return self.writer.closed() |
| 958 | |
| 959 | |
| 960 | class BufferedRandom(BufferedWriter, BufferedReader): |
| 961 | |
| 962 | # XXX docstring |
| 963 | |
| 964 | def __init__(self, raw, |
| 965 | buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
| 966 | raw._checkSeekable() |
| 967 | BufferedReader.__init__(self, raw, buffer_size) |
| 968 | BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size) |
| 969 | |
| 970 | def seek(self, pos, whence=0): |
| 971 | self.flush() |
| 972 | # First do the raw seek, then empty the read buffer, so that |
| 973 | # if the raw seek fails, we don't lose buffered data forever. |
| 974 | pos = self.raw.seek(pos, whence) |
| 975 | self._read_buf = b"" |
| 976 | return pos |
| 977 | |
| 978 | def tell(self): |
| 979 | if (self._write_buf): |
| 980 | return self.raw.tell() + len(self._write_buf) |
| 981 | else: |
| 982 | return self.raw.tell() - len(self._read_buf) |
| 983 | |
| 984 | def read(self, n=None): |
| 985 | if n is None: |
| 986 | n = -1 |
| 987 | self.flush() |
| 988 | return BufferedReader.read(self, n) |
| 989 | |
| 990 | def readinto(self, b): |
| 991 | self.flush() |
| 992 | return BufferedReader.readinto(self, b) |
| 993 | |
| 994 | def peek(self, n=0): |
| 995 | self.flush() |
| 996 | return BufferedReader.peek(self, n) |
| 997 | |
| 998 | def read1(self, n): |
| 999 | self.flush() |
| 1000 | return BufferedReader.read1(self, n) |
| 1001 | |
| 1002 | def write(self, b): |
| 1003 | if self._read_buf: |
| 1004 | self.raw.seek(-len(self._read_buf), 1) # Undo readahead |
| 1005 | self._read_buf = b"" |
| 1006 | return BufferedWriter.write(self, b) |
| 1007 | |
| 1008 | |
| 1009 | class TextIOBase(IOBase): |
| 1010 | |
| 1011 | """Base class for text I/O. |
| 1012 | |
| 1013 | This class provides a character and line based interface to stream I/O. |
| 1014 | |
| 1015 | There is no readinto() method, as character strings are immutable. |
| 1016 | """ |
| 1017 | |
| 1018 | def read(self, n = -1): |
| 1019 | """read(n: int = -1) -> unicode. Read at most n characters from stream. |
| 1020 | |
| 1021 | Read from underlying buffer until we have n characters or we hit EOF. |
| 1022 | If n is negative or omitted, read until EOF. |
| 1023 | """ |
| 1024 | self._unsupported("read") |
| 1025 | |
| 1026 | def write(self, s): |
| 1027 | """write(s: unicode) -> int. Write string s to stream.""" |
| 1028 | self._unsupported("write") |
| 1029 | |
| 1030 | def truncate(self, pos = None): |
| 1031 | """truncate(pos: int = None) -> int. Truncate size to pos.""" |
| 1032 | self.flush() |
| 1033 | if pos is None: |
| 1034 | pos = self.tell() |
| 1035 | self.seek(pos) |
| 1036 | return self.buffer.truncate() |
| 1037 | |
| 1038 | def readline(self): |
| 1039 | """readline() -> unicode. Read until newline or EOF. |
| 1040 | |
| 1041 | Returns an empty string if EOF is hit immediately. |
| 1042 | """ |
| 1043 | self._unsupported("readline") |
| 1044 | |
| 1045 | @property |
| 1046 | def encoding(self): |
| 1047 | """Subclasses should override.""" |
| 1048 | return None |
| 1049 | |
| 1050 | @property |
| 1051 | def newlines(self): |
| 1052 | """newlines -> None | unicode | tuple of unicode. Line endings translated |
| 1053 | so far. |
| 1054 | |
| 1055 | Only line endings translated during reading are considered. |
| 1056 | |
| 1057 | Subclasses should override. |
| 1058 | """ |
| 1059 | return None |
| 1060 | |
| 1061 | |
| 1062 | class IncrementalNewlineDecoder(codecs.IncrementalDecoder): |
| 1063 | """Codec used when reading a file in universal newlines mode. |
| 1064 | It wraps another incremental decoder, translating \\r\\n and \\r into \\n. |
| 1065 | It also records the types of newlines encountered. |
| 1066 | When used with translate=False, it ensures that the newline sequence is |
| 1067 | returned in one piece. |
| 1068 | """ |
| 1069 | def __init__(self, decoder, translate, errors='strict'): |
| 1070 | codecs.IncrementalDecoder.__init__(self, errors=errors) |
| 1071 | self.buffer = b'' |
| 1072 | self.translate = translate |
| 1073 | self.decoder = decoder |
| 1074 | self.seennl = 0 |
| 1075 | |
| 1076 | def decode(self, input, final=False): |
| 1077 | # decode input (with the eventual \r from a previous pass) |
| 1078 | if self.buffer: |
| 1079 | input = self.buffer + input |
| 1080 | |
| 1081 | output = self.decoder.decode(input, final=final) |
| 1082 | |
| 1083 | # retain last \r even when not translating data: |
| 1084 | # then readline() is sure to get \r\n in one pass |
| 1085 | if output.endswith("\r") and not final: |
| 1086 | output = output[:-1] |
| 1087 | self.buffer = b'\r' |
| 1088 | else: |
| 1089 | self.buffer = b'' |
| 1090 | |
| 1091 | # Record which newlines are read |
| 1092 | crlf = output.count('\r\n') |
| 1093 | cr = output.count('\r') - crlf |
| 1094 | lf = output.count('\n') - crlf |
| 1095 | self.seennl |= (lf and self._LF) | (cr and self._CR) \ |
| 1096 | | (crlf and self._CRLF) |
| 1097 | |
| 1098 | if self.translate: |
| 1099 | if crlf: |
| 1100 | output = output.replace("\r\n", "\n") |
| 1101 | if cr: |
| 1102 | output = output.replace("\r", "\n") |
| 1103 | |
| 1104 | return output |
| 1105 | |
| 1106 | def getstate(self): |
| 1107 | buf, flag = self.decoder.getstate() |
| 1108 | return buf + self.buffer, flag |
| 1109 | |
| 1110 | def setstate(self, state): |
| 1111 | buf, flag = state |
| 1112 | if buf.endswith(b'\r'): |
| 1113 | self.buffer = b'\r' |
| 1114 | buf = buf[:-1] |
| 1115 | else: |
| 1116 | self.buffer = b'' |
| 1117 | self.decoder.setstate((buf, flag)) |
| 1118 | |
| 1119 | def reset(self): |
| 1120 | self.seennl = 0 |
| 1121 | self.buffer = b'' |
| 1122 | self.decoder.reset() |
| 1123 | |
| 1124 | _LF = 1 |
| 1125 | _CR = 2 |
| 1126 | _CRLF = 4 |
| 1127 | |
| 1128 | @property |
| 1129 | def newlines(self): |
| 1130 | return (None, |
| 1131 | "\n", |
| 1132 | "\r", |
| 1133 | ("\r", "\n"), |
| 1134 | "\r\n", |
| 1135 | ("\n", "\r\n"), |
| 1136 | ("\r", "\r\n"), |
| 1137 | ("\r", "\n", "\r\n") |
| 1138 | )[self.seennl] |
| 1139 | |
| 1140 | |
| 1141 | class TextIOWrapper(TextIOBase): |
| 1142 | |
| 1143 | """Buffered text stream. |
| 1144 | |
| 1145 | Character and line based layer over a BufferedIOBase object. |
| 1146 | """ |
| 1147 | |
| 1148 | _CHUNK_SIZE = 128 |
| 1149 | |
| 1150 | def __init__(self, buffer, encoding=None, errors=None, newline=None, |
| 1151 | line_buffering=False): |
| 1152 | if newline not in (None, "", "\n", "\r", "\r\n"): |
| 1153 | raise ValueError("illegal newline value: %r" % (newline,)) |
| 1154 | if encoding is None: |
| 1155 | try: |
| 1156 | encoding = os.device_encoding(buffer.fileno()) |
| 1157 | except (AttributeError, UnsupportedOperation): |
| 1158 | pass |
| 1159 | if encoding is None: |
| 1160 | try: |
| 1161 | import locale |
| 1162 | except ImportError: |
| 1163 | # Importing locale may fail if Python is being built |
| 1164 | encoding = "ascii" |
| 1165 | else: |
| 1166 | encoding = locale.getpreferredencoding() |
| 1167 | |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 1168 | if not isinstance(encoding, basestring): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1169 | raise ValueError("invalid encoding: %r" % encoding) |
| 1170 | |
| 1171 | if errors is None: |
| 1172 | errors = "strict" |
| 1173 | else: |
Christian Heimes | 3784c6b | 2008-03-26 23:13:59 +0000 | [diff] [blame] | 1174 | if not isinstance(errors, basestring): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1175 | raise ValueError("invalid errors: %r" % errors) |
| 1176 | |
| 1177 | self.buffer = buffer |
| 1178 | self._line_buffering = line_buffering |
| 1179 | self._encoding = encoding |
| 1180 | self._errors = errors |
| 1181 | self._readuniversal = not newline |
| 1182 | self._readtranslate = newline is None |
| 1183 | self._readnl = newline |
| 1184 | self._writetranslate = newline != '' |
| 1185 | self._writenl = newline or os.linesep |
| 1186 | self._encoder = None |
| 1187 | self._decoder = None |
| 1188 | self._decoded_chars = '' # buffer for text returned from decoder |
| 1189 | self._decoded_chars_used = 0 # offset into _decoded_chars for read() |
| 1190 | self._snapshot = None # info for reconstructing decoder state |
| 1191 | self._seekable = self._telling = self.buffer.seekable() |
| 1192 | |
| 1193 | # self._snapshot is either None, or a tuple (dec_flags, next_input) |
| 1194 | # where dec_flags is the second (integer) item of the decoder state |
| 1195 | # and next_input is the chunk of input bytes that comes next after the |
| 1196 | # snapshot point. We use this to reconstruct decoder states in tell(). |
| 1197 | |
| 1198 | # Naming convention: |
| 1199 | # - "bytes_..." for integer variables that count input bytes |
| 1200 | # - "chars_..." for integer variables that count decoded characters |
| 1201 | |
| 1202 | def __repr__(self): |
| 1203 | return '<TIOW %x>' % id(self) |
| 1204 | |
| 1205 | @property |
| 1206 | def encoding(self): |
| 1207 | return self._encoding |
| 1208 | |
| 1209 | @property |
| 1210 | def errors(self): |
| 1211 | return self._errors |
| 1212 | |
| 1213 | @property |
| 1214 | def line_buffering(self): |
| 1215 | return self._line_buffering |
| 1216 | |
| 1217 | def seekable(self): |
| 1218 | return self._seekable |
| 1219 | |
| 1220 | def flush(self): |
| 1221 | self.buffer.flush() |
| 1222 | self._telling = self._seekable |
| 1223 | |
| 1224 | def close(self): |
| 1225 | try: |
| 1226 | self.flush() |
| 1227 | except: |
| 1228 | pass # If flush() fails, just give up |
| 1229 | self.buffer.close() |
| 1230 | |
| 1231 | @property |
| 1232 | def closed(self): |
| 1233 | return self.buffer.closed |
| 1234 | |
| 1235 | def fileno(self): |
| 1236 | return self.buffer.fileno() |
| 1237 | |
| 1238 | def isatty(self): |
| 1239 | return self.buffer.isatty() |
| 1240 | |
| 1241 | def write(self, s): |
| 1242 | if self.closed: |
| 1243 | raise ValueError("write to closed file") |
| 1244 | if not isinstance(s, unicode): |
| 1245 | raise TypeError("can't write %s to text stream" % |
| 1246 | s.__class__.__name__) |
| 1247 | length = len(s) |
| 1248 | haslf = (self._writetranslate or self._line_buffering) and "\n" in s |
| 1249 | if haslf and self._writetranslate and self._writenl != "\n": |
| 1250 | s = s.replace("\n", self._writenl) |
| 1251 | encoder = self._encoder or self._get_encoder() |
| 1252 | # XXX What if we were just reading? |
| 1253 | b = encoder.encode(s) |
| 1254 | self.buffer.write(b) |
| 1255 | if self._line_buffering and (haslf or "\r" in s): |
| 1256 | self.flush() |
| 1257 | self._snapshot = None |
| 1258 | if self._decoder: |
| 1259 | self._decoder.reset() |
| 1260 | return length |
| 1261 | |
| 1262 | def _get_encoder(self): |
| 1263 | make_encoder = codecs.getincrementalencoder(self._encoding) |
| 1264 | self._encoder = make_encoder(self._errors) |
| 1265 | return self._encoder |
| 1266 | |
| 1267 | def _get_decoder(self): |
| 1268 | make_decoder = codecs.getincrementaldecoder(self._encoding) |
| 1269 | decoder = make_decoder(self._errors) |
| 1270 | if self._readuniversal: |
| 1271 | decoder = IncrementalNewlineDecoder(decoder, self._readtranslate) |
| 1272 | self._decoder = decoder |
| 1273 | return decoder |
| 1274 | |
| 1275 | # The following three methods implement an ADT for _decoded_chars. |
| 1276 | # Text returned from the decoder is buffered here until the client |
| 1277 | # requests it by calling our read() or readline() method. |
| 1278 | def _set_decoded_chars(self, chars): |
| 1279 | """Set the _decoded_chars buffer.""" |
| 1280 | self._decoded_chars = chars |
| 1281 | self._decoded_chars_used = 0 |
| 1282 | |
| 1283 | def _get_decoded_chars(self, n=None): |
| 1284 | """Advance into the _decoded_chars buffer.""" |
| 1285 | offset = self._decoded_chars_used |
| 1286 | if n is None: |
| 1287 | chars = self._decoded_chars[offset:] |
| 1288 | else: |
| 1289 | chars = self._decoded_chars[offset:offset + n] |
| 1290 | self._decoded_chars_used += len(chars) |
| 1291 | return chars |
| 1292 | |
| 1293 | def _rewind_decoded_chars(self, n): |
| 1294 | """Rewind the _decoded_chars buffer.""" |
| 1295 | if self._decoded_chars_used < n: |
| 1296 | raise AssertionError("rewind decoded_chars out of bounds") |
| 1297 | self._decoded_chars_used -= n |
| 1298 | |
| 1299 | def _read_chunk(self): |
| 1300 | """ |
| 1301 | Read and decode the next chunk of data from the BufferedReader. |
| 1302 | |
| 1303 | The return value is True unless EOF was reached. The decoded string |
| 1304 | is placed in self._decoded_chars (replacing its previous value). |
| 1305 | The entire input chunk is sent to the decoder, though some of it |
| 1306 | may remain buffered in the decoder, yet to be converted. |
| 1307 | """ |
| 1308 | |
| 1309 | if self._decoder is None: |
| 1310 | raise ValueError("no decoder") |
| 1311 | |
| 1312 | if self._telling: |
| 1313 | # To prepare for tell(), we need to snapshot a point in the |
| 1314 | # file where the decoder's input buffer is empty. |
| 1315 | |
| 1316 | dec_buffer, dec_flags = self._decoder.getstate() |
| 1317 | # Given this, we know there was a valid snapshot point |
| 1318 | # len(dec_buffer) bytes ago with decoder state (b'', dec_flags). |
| 1319 | |
| 1320 | # Read a chunk, decode it, and put the result in self._decoded_chars. |
| 1321 | input_chunk = self.buffer.read1(self._CHUNK_SIZE) |
| 1322 | eof = not input_chunk |
| 1323 | self._set_decoded_chars(self._decoder.decode(input_chunk, eof)) |
| 1324 | |
| 1325 | if self._telling: |
| 1326 | # At the snapshot point, len(dec_buffer) bytes before the read, |
| 1327 | # the next input to be decoded is dec_buffer + input_chunk. |
| 1328 | self._snapshot = (dec_flags, dec_buffer + input_chunk) |
| 1329 | |
| 1330 | return not eof |
| 1331 | |
| 1332 | def _pack_cookie(self, position, dec_flags=0, |
| 1333 | bytes_to_feed=0, need_eof=0, chars_to_skip=0): |
| 1334 | # The meaning of a tell() cookie is: seek to position, set the |
| 1335 | # decoder flags to dec_flags, read bytes_to_feed bytes, feed them |
| 1336 | # into the decoder with need_eof as the EOF flag, then skip |
| 1337 | # chars_to_skip characters of the decoded result. For most simple |
| 1338 | # decoders, tell() will often just give a byte offset in the file. |
| 1339 | return (position | (dec_flags<<64) | (bytes_to_feed<<128) | |
| 1340 | (chars_to_skip<<192) | bool(need_eof)<<256) |
| 1341 | |
| 1342 | def _unpack_cookie(self, bigint): |
| 1343 | rest, position = divmod(bigint, 1<<64) |
| 1344 | rest, dec_flags = divmod(rest, 1<<64) |
| 1345 | rest, bytes_to_feed = divmod(rest, 1<<64) |
| 1346 | need_eof, chars_to_skip = divmod(rest, 1<<64) |
| 1347 | return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip |
| 1348 | |
| 1349 | def tell(self): |
| 1350 | if not self._seekable: |
| 1351 | raise IOError("underlying stream is not seekable") |
| 1352 | if not self._telling: |
| 1353 | raise IOError("telling position disabled by next() call") |
| 1354 | self.flush() |
| 1355 | position = self.buffer.tell() |
| 1356 | decoder = self._decoder |
| 1357 | if decoder is None or self._snapshot is None: |
| 1358 | if self._decoded_chars: |
| 1359 | # This should never happen. |
| 1360 | raise AssertionError("pending decoded text") |
| 1361 | return position |
| 1362 | |
| 1363 | # Skip backward to the snapshot point (see _read_chunk). |
| 1364 | dec_flags, next_input = self._snapshot |
| 1365 | position -= len(next_input) |
| 1366 | |
| 1367 | # How many decoded characters have been used up since the snapshot? |
| 1368 | chars_to_skip = self._decoded_chars_used |
| 1369 | if chars_to_skip == 0: |
| 1370 | # We haven't moved from the snapshot point. |
| 1371 | return self._pack_cookie(position, dec_flags) |
| 1372 | |
| 1373 | # Starting from the snapshot position, we will walk the decoder |
| 1374 | # forward until it gives us enough decoded characters. |
| 1375 | saved_state = decoder.getstate() |
| 1376 | try: |
| 1377 | # Note our initial start point. |
| 1378 | decoder.setstate((b'', dec_flags)) |
| 1379 | start_pos = position |
| 1380 | start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0 |
| 1381 | need_eof = 0 |
| 1382 | |
| 1383 | # Feed the decoder one byte at a time. As we go, note the |
| 1384 | # nearest "safe start point" before the current location |
| 1385 | # (a point where the decoder has nothing buffered, so seek() |
| 1386 | # can safely start from there and advance to this location). |
| 1387 | next_byte = bytearray(1) |
| 1388 | for next_byte[0] in next_input: |
| 1389 | bytes_fed += 1 |
| 1390 | chars_decoded += len(decoder.decode(next_byte)) |
| 1391 | dec_buffer, dec_flags = decoder.getstate() |
| 1392 | if not dec_buffer and chars_decoded <= chars_to_skip: |
| 1393 | # Decoder buffer is empty, so this is a safe start point. |
| 1394 | start_pos += bytes_fed |
| 1395 | chars_to_skip -= chars_decoded |
| 1396 | start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0 |
| 1397 | if chars_decoded >= chars_to_skip: |
| 1398 | break |
| 1399 | else: |
| 1400 | # We didn't get enough decoded data; signal EOF to get more. |
| 1401 | chars_decoded += len(decoder.decode(b'', final=True)) |
| 1402 | need_eof = 1 |
| 1403 | if chars_decoded < chars_to_skip: |
| 1404 | raise IOError("can't reconstruct logical file position") |
| 1405 | |
| 1406 | # The returned cookie corresponds to the last safe start point. |
| 1407 | return self._pack_cookie( |
| 1408 | start_pos, start_flags, bytes_fed, need_eof, chars_to_skip) |
| 1409 | finally: |
| 1410 | decoder.setstate(saved_state) |
| 1411 | |
| 1412 | def seek(self, cookie, whence=0): |
| 1413 | if not self._seekable: |
| 1414 | raise IOError("underlying stream is not seekable") |
| 1415 | if whence == 1: # seek relative to current position |
| 1416 | if cookie != 0: |
| 1417 | raise IOError("can't do nonzero cur-relative seeks") |
| 1418 | # Seeking to the current position should attempt to |
| 1419 | # sync the underlying buffer with the current position. |
| 1420 | whence = 0 |
| 1421 | cookie = self.tell() |
| 1422 | if whence == 2: # seek relative to end of file |
| 1423 | if cookie != 0: |
| 1424 | raise IOError("can't do nonzero end-relative seeks") |
| 1425 | self.flush() |
| 1426 | position = self.buffer.seek(0, 2) |
| 1427 | self._set_decoded_chars('') |
| 1428 | self._snapshot = None |
| 1429 | if self._decoder: |
| 1430 | self._decoder.reset() |
| 1431 | return position |
| 1432 | if whence != 0: |
| 1433 | raise ValueError("invalid whence (%r, should be 0, 1 or 2)" % |
| 1434 | (whence,)) |
| 1435 | if cookie < 0: |
| 1436 | raise ValueError("negative seek position %r" % (cookie,)) |
| 1437 | self.flush() |
| 1438 | |
| 1439 | # The strategy of seek() is to go back to the safe start point |
| 1440 | # and replay the effect of read(chars_to_skip) from there. |
| 1441 | start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \ |
| 1442 | self._unpack_cookie(cookie) |
| 1443 | |
| 1444 | # Seek back to the safe start point. |
| 1445 | self.buffer.seek(start_pos) |
| 1446 | self._set_decoded_chars('') |
| 1447 | self._snapshot = None |
| 1448 | |
| 1449 | # Restore the decoder to its state from the safe start point. |
| 1450 | if self._decoder or dec_flags or chars_to_skip: |
| 1451 | self._decoder = self._decoder or self._get_decoder() |
| 1452 | self._decoder.setstate((b'', dec_flags)) |
| 1453 | self._snapshot = (dec_flags, b'') |
| 1454 | |
| 1455 | if chars_to_skip: |
| 1456 | # Just like _read_chunk, feed the decoder and save a snapshot. |
| 1457 | input_chunk = self.buffer.read(bytes_to_feed) |
| 1458 | self._set_decoded_chars( |
| 1459 | self._decoder.decode(input_chunk, need_eof)) |
| 1460 | self._snapshot = (dec_flags, input_chunk) |
| 1461 | |
| 1462 | # Skip chars_to_skip of the decoded characters. |
| 1463 | if len(self._decoded_chars) < chars_to_skip: |
| 1464 | raise IOError("can't restore logical file position") |
| 1465 | self._decoded_chars_used = chars_to_skip |
| 1466 | |
| 1467 | return cookie |
| 1468 | |
| 1469 | def read(self, n=None): |
| 1470 | if n is None: |
| 1471 | n = -1 |
| 1472 | decoder = self._decoder or self._get_decoder() |
| 1473 | if n < 0: |
| 1474 | # Read everything. |
| 1475 | result = (self._get_decoded_chars() + |
| 1476 | decoder.decode(self.buffer.read(), final=True)) |
| 1477 | self._set_decoded_chars('') |
| 1478 | self._snapshot = None |
| 1479 | return result |
| 1480 | else: |
| 1481 | # Keep reading chunks until we have n characters to return. |
| 1482 | eof = False |
| 1483 | result = self._get_decoded_chars(n) |
| 1484 | while len(result) < n and not eof: |
| 1485 | eof = not self._read_chunk() |
| 1486 | result += self._get_decoded_chars(n - len(result)) |
| 1487 | return result |
| 1488 | |
| 1489 | def next(self): |
| 1490 | self._telling = False |
| 1491 | line = self.readline() |
| 1492 | if not line: |
| 1493 | self._snapshot = None |
| 1494 | self._telling = self._seekable |
| 1495 | raise StopIteration |
| 1496 | return line |
| 1497 | |
| 1498 | def readline(self, limit=None): |
| 1499 | if limit is None: |
| 1500 | limit = -1 |
| 1501 | |
| 1502 | # Grab all the decoded text (we will rewind any extra bits later). |
| 1503 | line = self._get_decoded_chars() |
| 1504 | |
| 1505 | start = 0 |
| 1506 | decoder = self._decoder or self._get_decoder() |
| 1507 | |
| 1508 | pos = endpos = None |
| 1509 | while True: |
| 1510 | if self._readtranslate: |
| 1511 | # Newlines are already translated, only search for \n |
| 1512 | pos = line.find('\n', start) |
| 1513 | if pos >= 0: |
| 1514 | endpos = pos + 1 |
| 1515 | break |
| 1516 | else: |
| 1517 | start = len(line) |
| 1518 | |
| 1519 | elif self._readuniversal: |
| 1520 | # Universal newline search. Find any of \r, \r\n, \n |
| 1521 | # The decoder ensures that \r\n are not split in two pieces |
| 1522 | |
| 1523 | # In C we'd look for these in parallel of course. |
| 1524 | nlpos = line.find("\n", start) |
| 1525 | crpos = line.find("\r", start) |
| 1526 | if crpos == -1: |
| 1527 | if nlpos == -1: |
| 1528 | # Nothing found |
| 1529 | start = len(line) |
| 1530 | else: |
| 1531 | # Found \n |
| 1532 | endpos = nlpos + 1 |
| 1533 | break |
| 1534 | elif nlpos == -1: |
| 1535 | # Found lone \r |
| 1536 | endpos = crpos + 1 |
| 1537 | break |
| 1538 | elif nlpos < crpos: |
| 1539 | # Found \n |
| 1540 | endpos = nlpos + 1 |
| 1541 | break |
| 1542 | elif nlpos == crpos + 1: |
| 1543 | # Found \r\n |
| 1544 | endpos = crpos + 2 |
| 1545 | break |
| 1546 | else: |
| 1547 | # Found \r |
| 1548 | endpos = crpos + 1 |
| 1549 | break |
| 1550 | else: |
| 1551 | # non-universal |
| 1552 | pos = line.find(self._readnl) |
| 1553 | if pos >= 0: |
| 1554 | endpos = pos + len(self._readnl) |
| 1555 | break |
| 1556 | |
| 1557 | if limit >= 0 and len(line) >= limit: |
| 1558 | endpos = limit # reached length limit |
| 1559 | break |
| 1560 | |
| 1561 | # No line ending seen yet - get more data |
| 1562 | more_line = '' |
| 1563 | while self._read_chunk(): |
| 1564 | if self._decoded_chars: |
| 1565 | break |
| 1566 | if self._decoded_chars: |
| 1567 | line += self._get_decoded_chars() |
| 1568 | else: |
| 1569 | # end of file |
| 1570 | self._set_decoded_chars('') |
| 1571 | self._snapshot = None |
| 1572 | return line |
| 1573 | |
| 1574 | if limit >= 0 and endpos > limit: |
| 1575 | endpos = limit # don't exceed limit |
| 1576 | |
| 1577 | # Rewind _decoded_chars to just after the line ending we found. |
| 1578 | self._rewind_decoded_chars(len(line) - endpos) |
| 1579 | return line[:endpos] |
| 1580 | |
| 1581 | @property |
| 1582 | def newlines(self): |
| 1583 | return self._decoder.newlines if self._decoder else None |
| 1584 | |
| 1585 | class StringIO(TextIOWrapper): |
| 1586 | |
| 1587 | # XXX This is really slow, but fully functional |
| 1588 | |
| 1589 | def __init__(self, initial_value="", encoding="utf-8", |
| 1590 | errors="strict", newline="\n"): |
| 1591 | super(StringIO, self).__init__(BytesIO(), |
| 1592 | encoding=encoding, |
| 1593 | errors=errors, |
| 1594 | newline=newline) |
| 1595 | if initial_value: |
| 1596 | if not isinstance(initial_value, unicode): |
| 1597 | initial_value = unicode(initial_value) |
| 1598 | self.write(initial_value) |
| 1599 | self.seek(0) |
| 1600 | |
| 1601 | def getvalue(self): |
| 1602 | self.flush() |
| 1603 | return self.buffer.getvalue().decode(self._encoding, self._errors) |