Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | """ |
| 2 | Python implementation of the io module. |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import abc |
| 7 | import codecs |
Antoine Pitrou | 58fcf9f | 2011-11-21 20:16:44 +0100 | [diff] [blame] | 8 | import errno |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 9 | import array |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 10 | import stat |
Serhiy Storchaka | f0f55a0 | 2015-08-28 22:17:04 +0300 | [diff] [blame] | 11 | import sys |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 12 | # Import _thread instead of threading to reduce startup cost |
| 13 | try: |
| 14 | from _thread import allocate_lock as Lock |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 15 | except ImportError: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 16 | from _dummy_thread import allocate_lock as Lock |
Serhiy Storchaka | f0f55a0 | 2015-08-28 22:17:04 +0300 | [diff] [blame] | 17 | if sys.platform in {'win32', 'cygwin'}: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 18 | from msvcrt import setmode as _setmode |
| 19 | else: |
| 20 | _setmode = None |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 21 | |
| 22 | import io |
Benjamin Peterson | c3be11a | 2010-04-27 21:24:03 +0000 | [diff] [blame] | 23 | from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 24 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 25 | valid_seek_flags = {0, 1, 2} # Hardwired values |
| 26 | if hasattr(os, 'SEEK_HOLE') : |
| 27 | valid_seek_flags.add(os.SEEK_HOLE) |
| 28 | valid_seek_flags.add(os.SEEK_DATA) |
| 29 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 30 | # open() uses st_blksize whenever we can |
| 31 | DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes |
| 32 | |
| 33 | # NOTE: Base classes defined here are registered with the "official" ABCs |
Benjamin Peterson | 86fdbf3 | 2015-03-18 21:35:38 -0500 | [diff] [blame] | 34 | # defined in io.py. We don't use real inheritance though, because we don't want |
| 35 | # to inherit the C implementations. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 37 | # Rebind for compatibility |
| 38 | BlockingIOError = BlockingIOError |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 39 | |
| 40 | |
Georg Brandl | 4d73b57 | 2011-01-13 07:13:06 +0000 | [diff] [blame] | 41 | def open(file, mode="r", buffering=-1, encoding=None, errors=None, |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 42 | newline=None, closefd=True, opener=None): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 43 | |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 44 | r"""Open file and return a stream. Raise OSError upon failure. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 45 | |
| 46 | file is either a text or byte string giving the name (and the path |
| 47 | if the file isn't in the current working directory) of the file to |
| 48 | be opened or an integer file descriptor of the file to be |
| 49 | wrapped. (If a file descriptor is given, it is closed when the |
| 50 | returned I/O object is closed, unless closefd is set to False.) |
| 51 | |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 52 | mode is an optional string that specifies the mode in which the file is |
| 53 | opened. It defaults to 'r' which means open for reading in text mode. Other |
| 54 | common values are 'w' for writing (truncating the file if it already |
Charles-François Natali | d612de1 | 2012-01-14 11:51:00 +0100 | [diff] [blame] | 55 | exists), 'x' for exclusive creation of a new file, and 'a' for appending |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 56 | (which on some Unix systems, means that all writes append to the end of the |
| 57 | file regardless of the current seek position). In text mode, if encoding is |
| 58 | not specified the encoding used is platform dependent. (For reading and |
| 59 | writing raw bytes use binary mode and leave encoding unspecified.) The |
| 60 | available modes are: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 61 | |
| 62 | ========= =============================================================== |
| 63 | Character Meaning |
| 64 | --------- --------------------------------------------------------------- |
| 65 | 'r' open for reading (default) |
| 66 | 'w' open for writing, truncating the file first |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 67 | 'x' create a new file and open it for writing |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 68 | 'a' open for writing, appending to the end of the file if it exists |
| 69 | 'b' binary mode |
| 70 | 't' text mode (default) |
| 71 | '+' open a disk file for updating (reading and writing) |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 72 | 'U' universal newline mode (deprecated) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 73 | ========= =============================================================== |
| 74 | |
| 75 | The default mode is 'rt' (open for reading text). For binary random |
| 76 | access, the mode 'w+b' opens and truncates the file to 0 bytes, while |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 77 | 'r+b' opens the file without truncation. The 'x' mode implies 'w' and |
| 78 | raises an `FileExistsError` if the file already exists. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 79 | |
| 80 | Python distinguishes between files opened in binary and text modes, |
| 81 | even when the underlying operating system doesn't. Files opened in |
| 82 | binary mode (appending 'b' to the mode argument) return contents as |
| 83 | bytes objects without any decoding. In text mode (the default, or when |
| 84 | 't' is appended to the mode argument), the contents of the file are |
| 85 | returned as strings, the bytes having been first decoded using a |
| 86 | platform-dependent encoding or using the specified encoding if given. |
| 87 | |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 88 | 'U' mode is deprecated and will raise an exception in future versions |
| 89 | of Python. It has no effect in Python 3. Use newline to control |
| 90 | universal newlines mode. |
| 91 | |
Antoine Pitrou | d5587bc | 2009-12-19 21:08:31 +0000 | [diff] [blame] | 92 | buffering is an optional integer used to set the buffering policy. |
| 93 | Pass 0 to switch buffering off (only allowed in binary mode), 1 to select |
| 94 | line buffering (only usable in text mode), and an integer > 1 to indicate |
| 95 | the size of a fixed-size chunk buffer. When no buffering argument is |
| 96 | given, the default buffering policy works as follows: |
| 97 | |
| 98 | * Binary files are buffered in fixed-size chunks; the size of the buffer |
| 99 | is chosen using a heuristic trying to determine the underlying device's |
| 100 | "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`. |
| 101 | On many systems, the buffer will typically be 4096 or 8192 bytes long. |
| 102 | |
| 103 | * "Interactive" text files (files for which isatty() returns True) |
| 104 | use line buffering. Other text files use the policy described above |
| 105 | for binary files. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 106 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 107 | encoding is the str name of the encoding used to decode or encode the |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 108 | file. This should only be used in text mode. The default encoding is |
| 109 | platform dependent, but any encoding supported by Python can be |
| 110 | passed. See the codecs module for the list of supported encodings. |
| 111 | |
| 112 | errors is an optional string that specifies how encoding errors are to |
| 113 | be handled---this argument should not be used in binary mode. Pass |
| 114 | 'strict' to raise a ValueError exception if there is an encoding error |
| 115 | (the default of None has the same effect), or pass 'ignore' to ignore |
| 116 | errors. (Note that ignoring encoding errors can lead to data loss.) |
| 117 | See the documentation for codecs.register for a list of the permitted |
| 118 | encoding error strings. |
| 119 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 120 | newline is a string controlling how universal newlines works (it only |
| 121 | applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works |
| 122 | as follows: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 123 | |
| 124 | * On input, if newline is None, universal newlines mode is |
| 125 | enabled. Lines in the input can end in '\n', '\r', or '\r\n', and |
| 126 | these are translated into '\n' before being returned to the |
| 127 | caller. If it is '', universal newline mode is enabled, but line |
| 128 | endings are returned to the caller untranslated. If it has any of |
| 129 | the other legal values, input lines are only terminated by the given |
| 130 | string, and the line ending is returned to the caller untranslated. |
| 131 | |
| 132 | * On output, if newline is None, any '\n' characters written are |
| 133 | translated to the system default line separator, os.linesep. If |
| 134 | newline is '', no translation takes place. If newline is any of the |
| 135 | other legal values, any '\n' characters written are translated to |
| 136 | the given string. |
| 137 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 138 | closedfd is a bool. If closefd is False, the underlying file descriptor will |
| 139 | be kept open when the file is closed. This does not work when a file name is |
| 140 | given and must be True in that case. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 141 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 142 | The newly created file is non-inheritable. |
| 143 | |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 144 | A custom opener can be used by passing a callable as *opener*. The |
| 145 | underlying file descriptor for the file object is then obtained by calling |
| 146 | *opener* with (*file*, *flags*). *opener* must return an open file |
| 147 | descriptor (passing os.open as *opener* results in functionality similar to |
| 148 | passing None). |
| 149 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 150 | open() returns a file object whose type depends on the mode, and |
| 151 | through which the standard file operations such as reading and writing |
| 152 | are performed. When open() is used to open a file in a text mode ('w', |
| 153 | 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open |
| 154 | a file in a binary mode, the returned class varies: in read binary |
| 155 | mode, it returns a BufferedReader; in write binary and append binary |
| 156 | modes, it returns a BufferedWriter, and in read/write mode, it returns |
| 157 | a BufferedRandom. |
| 158 | |
| 159 | It is also possible to use a string or bytearray as a file for both |
| 160 | reading and writing. For strings StringIO can be used like a file |
| 161 | opened in a text mode, and for bytes a BytesIO can be used like a file |
| 162 | opened in a binary mode. |
| 163 | """ |
| 164 | if not isinstance(file, (str, bytes, int)): |
| 165 | raise TypeError("invalid file: %r" % file) |
| 166 | if not isinstance(mode, str): |
| 167 | raise TypeError("invalid mode: %r" % mode) |
Benjamin Peterson | 95e392c | 2010-04-27 21:07:21 +0000 | [diff] [blame] | 168 | if not isinstance(buffering, int): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 169 | raise TypeError("invalid buffering: %r" % buffering) |
| 170 | if encoding is not None and not isinstance(encoding, str): |
| 171 | raise TypeError("invalid encoding: %r" % encoding) |
| 172 | if errors is not None and not isinstance(errors, str): |
| 173 | raise TypeError("invalid errors: %r" % errors) |
| 174 | modes = set(mode) |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 175 | if modes - set("axrwb+tU") or len(mode) > len(modes): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 176 | raise ValueError("invalid mode: %r" % mode) |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 177 | creating = "x" in modes |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 178 | reading = "r" in modes |
| 179 | writing = "w" in modes |
| 180 | appending = "a" in modes |
| 181 | updating = "+" in modes |
| 182 | text = "t" in modes |
| 183 | binary = "b" in modes |
| 184 | if "U" in modes: |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 185 | if creating or writing or appending: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 186 | raise ValueError("can't use U and writing mode at once") |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 187 | import warnings |
| 188 | warnings.warn("'U' mode is deprecated", |
| 189 | DeprecationWarning, 2) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 190 | reading = True |
| 191 | if text and binary: |
| 192 | raise ValueError("can't have text and binary mode at once") |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 193 | if creating + reading + writing + appending > 1: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 194 | raise ValueError("can't have read/write/append mode at once") |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 195 | if not (creating or reading or writing or appending): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 196 | raise ValueError("must have exactly one of read/write/append mode") |
| 197 | if binary and encoding is not None: |
| 198 | raise ValueError("binary mode doesn't take an encoding argument") |
| 199 | if binary and errors is not None: |
| 200 | raise ValueError("binary mode doesn't take an errors argument") |
| 201 | if binary and newline is not None: |
| 202 | raise ValueError("binary mode doesn't take a newline argument") |
| 203 | raw = FileIO(file, |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 204 | (creating and "x" or "") + |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 205 | (reading and "r" or "") + |
| 206 | (writing and "w" or "") + |
| 207 | (appending and "a" or "") + |
| 208 | (updating and "+" or ""), |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 209 | closefd, opener=opener) |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 210 | result = raw |
| 211 | try: |
| 212 | line_buffering = False |
| 213 | if buffering == 1 or buffering < 0 and raw.isatty(): |
| 214 | buffering = -1 |
| 215 | line_buffering = True |
| 216 | if buffering < 0: |
| 217 | buffering = DEFAULT_BUFFER_SIZE |
| 218 | try: |
| 219 | bs = os.fstat(raw.fileno()).st_blksize |
| 220 | except (OSError, AttributeError): |
| 221 | pass |
| 222 | else: |
| 223 | if bs > 1: |
| 224 | buffering = bs |
| 225 | if buffering < 0: |
| 226 | raise ValueError("invalid buffering size") |
| 227 | if buffering == 0: |
| 228 | if binary: |
| 229 | return result |
| 230 | raise ValueError("can't have unbuffered text I/O") |
| 231 | if updating: |
| 232 | buffer = BufferedRandom(raw, buffering) |
| 233 | elif creating or writing or appending: |
| 234 | buffer = BufferedWriter(raw, buffering) |
| 235 | elif reading: |
| 236 | buffer = BufferedReader(raw, buffering) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 237 | else: |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 238 | raise ValueError("unknown mode: %r" % mode) |
| 239 | result = buffer |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 240 | if binary: |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 241 | return result |
| 242 | text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) |
| 243 | result = text |
| 244 | text.mode = mode |
| 245 | return result |
| 246 | except: |
| 247 | result.close() |
| 248 | raise |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | class DocDescriptor: |
| 252 | """Helper for builtins.open.__doc__ |
| 253 | """ |
| 254 | def __get__(self, obj, typ): |
| 255 | return ( |
Benjamin Peterson | c3be11a | 2010-04-27 21:24:03 +0000 | [diff] [blame] | 256 | "open(file, mode='r', buffering=-1, encoding=None, " |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 257 | "errors=None, newline=None, closefd=True)\n\n" + |
| 258 | open.__doc__) |
| 259 | |
| 260 | class OpenWrapper: |
| 261 | """Wrapper for builtins.open |
| 262 | |
| 263 | Trick so that open won't become a bound method when stored |
| 264 | as a class variable (as dbm.dumb does). |
| 265 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 266 | See initstdio() in Python/pylifecycle.c. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 267 | """ |
| 268 | __doc__ = DocDescriptor() |
| 269 | |
| 270 | def __new__(cls, *args, **kwargs): |
| 271 | return open(*args, **kwargs) |
| 272 | |
| 273 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 274 | # In normal operation, both `UnsupportedOperation`s should be bound to the |
| 275 | # same object. |
| 276 | try: |
| 277 | UnsupportedOperation = io.UnsupportedOperation |
| 278 | except AttributeError: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 279 | class UnsupportedOperation(ValueError, OSError): |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 280 | pass |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 281 | |
| 282 | |
| 283 | class IOBase(metaclass=abc.ABCMeta): |
| 284 | |
| 285 | """The abstract base class for all I/O classes, acting on streams of |
| 286 | bytes. There is no public constructor. |
| 287 | |
| 288 | This class provides dummy implementations for many methods that |
| 289 | derived classes can override selectively; the default implementations |
| 290 | represent a file that cannot be read, written or seeked. |
| 291 | |
| 292 | Even though IOBase does not declare read, readinto, or write because |
| 293 | their signatures will vary, implementations and clients should |
| 294 | consider those methods part of the interface. Also, implementations |
Amaury Forgeot d'Arc | 616453c | 2010-09-06 22:31:52 +0000 | [diff] [blame] | 295 | may raise UnsupportedOperation when operations they do not support are |
| 296 | called. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 297 | |
| 298 | The basic type used for binary data read from or written to a file is |
| 299 | bytes. bytearrays are accepted too, and in some cases (such as |
| 300 | readinto) needed. Text I/O classes work with str data. |
| 301 | |
| 302 | Note that calling any method (even inquiries) on a closed stream is |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 303 | undefined. Implementations may raise OSError in this case. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 304 | |
| 305 | IOBase (and its subclasses) support the iterator protocol, meaning |
| 306 | that an IOBase object can be iterated over yielding the lines in a |
| 307 | stream. |
| 308 | |
| 309 | IOBase also supports the :keyword:`with` statement. In this example, |
| 310 | fp is closed after the suite of the with statement is complete: |
| 311 | |
| 312 | with open('spam.txt', 'r') as fp: |
| 313 | fp.write('Spam and eggs!') |
| 314 | """ |
| 315 | |
| 316 | ### Internal ### |
| 317 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 318 | def _unsupported(self, name): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 319 | """Internal: raise an OSError exception for unsupported operations.""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 320 | raise UnsupportedOperation("%s.%s() not supported" % |
| 321 | (self.__class__.__name__, name)) |
| 322 | |
| 323 | ### Positioning ### |
| 324 | |
Georg Brandl | 4d73b57 | 2011-01-13 07:13:06 +0000 | [diff] [blame] | 325 | def seek(self, pos, whence=0): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 326 | """Change stream position. |
| 327 | |
Terry Jan Reedy | c30b7b1 | 2013-03-11 17:57:08 -0400 | [diff] [blame] | 328 | Change the stream position to byte offset pos. Argument pos is |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 329 | interpreted relative to the position indicated by whence. Values |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 330 | for whence are ints: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 331 | |
| 332 | * 0 -- start of stream (the default); offset should be zero or positive |
| 333 | * 1 -- current stream position; offset may be negative |
| 334 | * 2 -- end of stream; offset is usually negative |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 335 | Some operating systems / file systems could provide additional values. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 336 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 337 | Return an int indicating the new absolute position. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 338 | """ |
| 339 | self._unsupported("seek") |
| 340 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 341 | def tell(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 342 | """Return an int indicating the current stream position.""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 343 | return self.seek(0, 1) |
| 344 | |
Georg Brandl | 4d73b57 | 2011-01-13 07:13:06 +0000 | [diff] [blame] | 345 | def truncate(self, pos=None): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 346 | """Truncate file to size bytes. |
| 347 | |
| 348 | Size defaults to the current IO position as reported by tell(). Return |
| 349 | the new size. |
| 350 | """ |
| 351 | self._unsupported("truncate") |
| 352 | |
| 353 | ### Flush and close ### |
| 354 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 355 | def flush(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 356 | """Flush write buffers, if applicable. |
| 357 | |
| 358 | This is not implemented for read-only and non-blocking streams. |
| 359 | """ |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 360 | self._checkClosed() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 361 | # XXX Should this return the number of bytes written??? |
| 362 | |
| 363 | __closed = False |
| 364 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 365 | def close(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 366 | """Flush and close the IO object. |
| 367 | |
| 368 | This method has no effect if the file is already closed. |
| 369 | """ |
| 370 | if not self.__closed: |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 371 | try: |
| 372 | self.flush() |
| 373 | finally: |
| 374 | self.__closed = True |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 375 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 376 | def __del__(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 377 | """Destructor. Calls close().""" |
| 378 | # The try/except block is in case this is called at program |
| 379 | # exit time, when it's possible that globals have already been |
| 380 | # deleted, and then the close() call might fail. Since |
| 381 | # there's nothing we can do about such failures and they annoy |
| 382 | # the end users, we suppress the traceback. |
| 383 | try: |
| 384 | self.close() |
| 385 | except: |
| 386 | pass |
| 387 | |
| 388 | ### Inquiries ### |
| 389 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 390 | def seekable(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 391 | """Return a bool indicating whether object supports random access. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 392 | |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 393 | If False, seek(), tell() and truncate() will raise UnsupportedOperation. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 394 | This method may need to do a test seek(). |
| 395 | """ |
| 396 | return False |
| 397 | |
| 398 | def _checkSeekable(self, msg=None): |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 399 | """Internal: raise UnsupportedOperation if file is not seekable |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 400 | """ |
| 401 | if not self.seekable(): |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 402 | raise UnsupportedOperation("File or stream is not seekable." |
| 403 | if msg is None else msg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 404 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 405 | def readable(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 406 | """Return a bool indicating whether object was opened for reading. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 407 | |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 408 | If False, read() will raise UnsupportedOperation. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 409 | """ |
| 410 | return False |
| 411 | |
| 412 | def _checkReadable(self, msg=None): |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 413 | """Internal: raise UnsupportedOperation if file is not readable |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 414 | """ |
| 415 | if not self.readable(): |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 416 | raise UnsupportedOperation("File or stream is not readable." |
| 417 | if msg is None else msg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 418 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 419 | def writable(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 420 | """Return a bool indicating whether object was opened for writing. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 421 | |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 422 | If False, write() and truncate() will raise UnsupportedOperation. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 423 | """ |
| 424 | return False |
| 425 | |
| 426 | def _checkWritable(self, msg=None): |
Amaury Forgeot d'Arc | ada9948 | 2010-09-06 22:23:13 +0000 | [diff] [blame] | 427 | """Internal: raise UnsupportedOperation if file is not writable |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 428 | """ |
| 429 | if not self.writable(): |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 430 | raise UnsupportedOperation("File or stream is not writable." |
| 431 | if msg is None else msg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 432 | |
| 433 | @property |
| 434 | def closed(self): |
| 435 | """closed: bool. True iff the file has been closed. |
| 436 | |
| 437 | For backwards compatibility, this is a property, not a predicate. |
| 438 | """ |
| 439 | return self.__closed |
| 440 | |
| 441 | def _checkClosed(self, msg=None): |
| 442 | """Internal: raise an ValueError if file is closed |
| 443 | """ |
| 444 | if self.closed: |
| 445 | raise ValueError("I/O operation on closed file." |
| 446 | if msg is None else msg) |
| 447 | |
| 448 | ### Context manager ### |
| 449 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 450 | def __enter__(self): # That's a forward reference |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 451 | """Context management protocol. Returns self (an instance of IOBase).""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 452 | self._checkClosed() |
| 453 | return self |
| 454 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 455 | def __exit__(self, *args): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 456 | """Context management protocol. Calls close()""" |
| 457 | self.close() |
| 458 | |
| 459 | ### Lower-level APIs ### |
| 460 | |
| 461 | # XXX Should these be present even if unimplemented? |
| 462 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 463 | def fileno(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 464 | """Returns underlying file descriptor (an int) if one exists. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 465 | |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 466 | An OSError is raised if the IO object does not use a file descriptor. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 467 | """ |
| 468 | self._unsupported("fileno") |
| 469 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 470 | def isatty(self): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 471 | """Return a bool indicating whether this is an 'interactive' stream. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 472 | |
| 473 | Return False if it can't be determined. |
| 474 | """ |
| 475 | self._checkClosed() |
| 476 | return False |
| 477 | |
| 478 | ### Readline[s] and writelines ### |
| 479 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 480 | def readline(self, size=-1): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 481 | r"""Read and return a line of bytes from the stream. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 482 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 483 | If size is specified, at most size bytes will be read. |
| 484 | Size should be an int. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 485 | |
| 486 | The line terminator is always b'\n' for binary files; for text |
| 487 | files, the newlines argument to open can be used to select the line |
| 488 | terminator(s) recognized. |
| 489 | """ |
| 490 | # For backwards compatibility, a (slowish) readline(). |
| 491 | if hasattr(self, "peek"): |
| 492 | def nreadahead(): |
| 493 | readahead = self.peek(1) |
| 494 | if not readahead: |
| 495 | return 1 |
| 496 | n = (readahead.find(b"\n") + 1) or len(readahead) |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 497 | if size >= 0: |
| 498 | n = min(n, size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 499 | return n |
| 500 | else: |
| 501 | def nreadahead(): |
| 502 | return 1 |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 503 | if size is None: |
| 504 | size = -1 |
| 505 | elif not isinstance(size, int): |
| 506 | raise TypeError("size must be an integer") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 507 | res = bytearray() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 508 | while size < 0 or len(res) < size: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 509 | b = self.read(nreadahead()) |
| 510 | if not b: |
| 511 | break |
| 512 | res += b |
| 513 | if res.endswith(b"\n"): |
| 514 | break |
| 515 | return bytes(res) |
| 516 | |
| 517 | def __iter__(self): |
| 518 | self._checkClosed() |
| 519 | return self |
| 520 | |
| 521 | def __next__(self): |
| 522 | line = self.readline() |
| 523 | if not line: |
| 524 | raise StopIteration |
| 525 | return line |
| 526 | |
| 527 | def readlines(self, hint=None): |
| 528 | """Return a list of lines from the stream. |
| 529 | |
| 530 | hint can be specified to control the number of lines read: no more |
| 531 | lines will be read if the total size (in bytes/characters) of all |
| 532 | lines so far exceeds hint. |
| 533 | """ |
| 534 | if hint is None or hint <= 0: |
| 535 | return list(self) |
| 536 | n = 0 |
| 537 | lines = [] |
| 538 | for line in self: |
| 539 | lines.append(line) |
| 540 | n += len(line) |
| 541 | if n >= hint: |
| 542 | break |
| 543 | return lines |
| 544 | |
| 545 | def writelines(self, lines): |
| 546 | self._checkClosed() |
| 547 | for line in lines: |
| 548 | self.write(line) |
| 549 | |
| 550 | io.IOBase.register(IOBase) |
| 551 | |
| 552 | |
| 553 | class RawIOBase(IOBase): |
| 554 | |
| 555 | """Base class for raw binary I/O.""" |
| 556 | |
| 557 | # The read() method is implemented by calling readinto(); derived |
| 558 | # classes that want to support read() only need to implement |
| 559 | # readinto() as a primitive operation. In general, readinto() can be |
| 560 | # more efficient than read(). |
| 561 | |
| 562 | # (It would be tempting to also provide an implementation of |
| 563 | # readinto() in terms of read(), in case the latter is a more suitable |
| 564 | # primitive operation, but that would lead to nasty recursion in case |
| 565 | # a subclass doesn't implement either.) |
| 566 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 567 | def read(self, size=-1): |
| 568 | """Read and return up to size bytes, where size is an int. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 569 | |
| 570 | Returns an empty bytes object on EOF, or None if the object is |
| 571 | set not to block and has no data to read. |
| 572 | """ |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 573 | if size is None: |
| 574 | size = -1 |
| 575 | if size < 0: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 576 | return self.readall() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 577 | b = bytearray(size.__index__()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 578 | n = self.readinto(b) |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 579 | if n is None: |
| 580 | return None |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 581 | del b[n:] |
| 582 | return bytes(b) |
| 583 | |
| 584 | def readall(self): |
| 585 | """Read until EOF, using multiple read() call.""" |
| 586 | res = bytearray() |
| 587 | while True: |
| 588 | data = self.read(DEFAULT_BUFFER_SIZE) |
| 589 | if not data: |
| 590 | break |
| 591 | res += data |
Victor Stinner | a80987f | 2011-05-25 22:47:16 +0200 | [diff] [blame] | 592 | if res: |
| 593 | return bytes(res) |
| 594 | else: |
| 595 | # b'' or None |
| 596 | return data |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 597 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 598 | def readinto(self, b): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 599 | """Read up to len(b) bytes into bytearray b. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 600 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 601 | Returns an int representing the number of bytes read (0 for EOF), or |
| 602 | None if the object is set not to block and has no data to read. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 603 | """ |
| 604 | self._unsupported("readinto") |
| 605 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 606 | def write(self, b): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 607 | """Write the given buffer to the IO stream. |
| 608 | |
| 609 | Returns the number of bytes written, which may be less than len(b). |
| 610 | """ |
| 611 | self._unsupported("write") |
| 612 | |
| 613 | io.RawIOBase.register(RawIOBase) |
| 614 | from _io import FileIO |
| 615 | RawIOBase.register(FileIO) |
| 616 | |
| 617 | |
| 618 | class BufferedIOBase(IOBase): |
| 619 | |
| 620 | """Base class for buffered IO objects. |
| 621 | |
| 622 | The main difference with RawIOBase is that the read() method |
| 623 | supports omitting the size argument, and does not have a default |
| 624 | implementation that defers to readinto(). |
| 625 | |
| 626 | In addition, read(), readinto() and write() may raise |
| 627 | BlockingIOError if the underlying raw stream is in non-blocking |
| 628 | mode and not ready; unlike their raw counterparts, they will never |
| 629 | return None. |
| 630 | |
| 631 | A typical implementation should not inherit from a RawIOBase |
| 632 | implementation, but wrap one. |
| 633 | """ |
| 634 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 635 | def read(self, size=None): |
| 636 | """Read and return up to size bytes, where size is an int. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 637 | |
| 638 | If the argument is omitted, None, or negative, reads and |
| 639 | returns all data until EOF. |
| 640 | |
| 641 | If the argument is positive, and the underlying raw stream is |
| 642 | not 'interactive', multiple raw reads may be issued to satisfy |
| 643 | the byte count (unless EOF is reached first). But for |
| 644 | interactive raw streams (XXX and for pipes?), at most one raw |
| 645 | read will be issued, and a short result does not imply that |
| 646 | EOF is imminent. |
| 647 | |
| 648 | Returns an empty bytes array on EOF. |
| 649 | |
| 650 | Raises BlockingIOError if the underlying raw stream has no |
| 651 | data at the moment. |
| 652 | """ |
| 653 | self._unsupported("read") |
| 654 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 655 | def read1(self, size=None): |
| 656 | """Read up to size bytes with at most one read() system call, |
| 657 | where size is an int. |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 658 | """ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 659 | self._unsupported("read1") |
| 660 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 661 | def readinto(self, b): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 662 | """Read up to len(b) bytes into bytearray b. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 663 | |
| 664 | Like read(), this may issue multiple reads to the underlying raw |
| 665 | stream, unless the latter is 'interactive'. |
| 666 | |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 667 | Returns an int representing the number of bytes read (0 for EOF). |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 668 | |
| 669 | Raises BlockingIOError if the underlying raw stream has no |
| 670 | data at the moment. |
| 671 | """ |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 672 | |
| 673 | return self._readinto(b, read1=False) |
| 674 | |
| 675 | def readinto1(self, b): |
| 676 | """Read up to len(b) bytes into *b*, using at most one system call |
| 677 | |
| 678 | Returns an int representing the number of bytes read (0 for EOF). |
| 679 | |
| 680 | Raises BlockingIOError if the underlying raw stream has no |
| 681 | data at the moment. |
| 682 | """ |
| 683 | |
| 684 | return self._readinto(b, read1=True) |
| 685 | |
| 686 | def _readinto(self, b, read1): |
| 687 | if not isinstance(b, memoryview): |
| 688 | b = memoryview(b) |
| 689 | b = b.cast('B') |
| 690 | |
| 691 | if read1: |
| 692 | data = self.read1(len(b)) |
| 693 | else: |
| 694 | data = self.read(len(b)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 695 | n = len(data) |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 696 | |
| 697 | b[:n] = data |
| 698 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 699 | return n |
| 700 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 701 | def write(self, b): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 702 | """Write the given bytes buffer to the IO stream. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 703 | |
| 704 | Return the number of bytes written, which is never less than |
| 705 | len(b). |
| 706 | |
| 707 | Raises BlockingIOError if the buffer is full and the |
| 708 | underlying raw stream cannot accept more data at the moment. |
| 709 | """ |
| 710 | self._unsupported("write") |
| 711 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 712 | def detach(self): |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 713 | """ |
| 714 | Separate the underlying raw stream from the buffer and return it. |
| 715 | |
| 716 | After the raw stream has been detached, the buffer is in an unusable |
| 717 | state. |
| 718 | """ |
| 719 | self._unsupported("detach") |
| 720 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 721 | io.BufferedIOBase.register(BufferedIOBase) |
| 722 | |
| 723 | |
| 724 | class _BufferedIOMixin(BufferedIOBase): |
| 725 | |
| 726 | """A mixin implementation of BufferedIOBase with an underlying raw stream. |
| 727 | |
| 728 | This passes most requests on to the underlying raw stream. It |
| 729 | does *not* provide implementations of read(), readinto() or |
| 730 | write(). |
| 731 | """ |
| 732 | |
| 733 | def __init__(self, raw): |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 734 | self._raw = raw |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 735 | |
| 736 | ### Positioning ### |
| 737 | |
| 738 | def seek(self, pos, whence=0): |
| 739 | new_position = self.raw.seek(pos, whence) |
| 740 | if new_position < 0: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 741 | raise OSError("seek() returned an invalid position") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 742 | return new_position |
| 743 | |
| 744 | def tell(self): |
| 745 | pos = self.raw.tell() |
| 746 | if pos < 0: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 747 | raise OSError("tell() returned an invalid position") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 748 | return pos |
| 749 | |
| 750 | def truncate(self, pos=None): |
| 751 | # Flush the stream. We're mixing buffered I/O with lower-level I/O, |
| 752 | # and a flush may be necessary to synch both views of the current |
| 753 | # file state. |
| 754 | self.flush() |
| 755 | |
| 756 | if pos is None: |
| 757 | pos = self.tell() |
| 758 | # XXX: Should seek() be used, instead of passing the position |
| 759 | # XXX directly to truncate? |
| 760 | return self.raw.truncate(pos) |
| 761 | |
| 762 | ### Flush and close ### |
| 763 | |
| 764 | def flush(self): |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 765 | if self.closed: |
| 766 | raise ValueError("flush of closed file") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 767 | self.raw.flush() |
| 768 | |
| 769 | def close(self): |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 770 | if self.raw is not None and not self.closed: |
Antoine Pitrou | 58fcf9f | 2011-11-21 20:16:44 +0100 | [diff] [blame] | 771 | try: |
| 772 | # may raise BlockingIOError or BrokenPipeError etc |
| 773 | self.flush() |
| 774 | finally: |
| 775 | self.raw.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 776 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 777 | def detach(self): |
| 778 | if self.raw is None: |
| 779 | raise ValueError("raw stream already detached") |
| 780 | self.flush() |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 781 | raw = self._raw |
| 782 | self._raw = None |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 783 | return raw |
| 784 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 785 | ### Inquiries ### |
| 786 | |
| 787 | def seekable(self): |
| 788 | return self.raw.seekable() |
| 789 | |
| 790 | def readable(self): |
| 791 | return self.raw.readable() |
| 792 | |
| 793 | def writable(self): |
| 794 | return self.raw.writable() |
| 795 | |
| 796 | @property |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 797 | def raw(self): |
| 798 | return self._raw |
| 799 | |
| 800 | @property |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 801 | def closed(self): |
| 802 | return self.raw.closed |
| 803 | |
| 804 | @property |
| 805 | def name(self): |
| 806 | return self.raw.name |
| 807 | |
| 808 | @property |
| 809 | def mode(self): |
| 810 | return self.raw.mode |
| 811 | |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 812 | def __getstate__(self): |
| 813 | raise TypeError("can not serialize a '{0}' object" |
| 814 | .format(self.__class__.__name__)) |
| 815 | |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 816 | def __repr__(self): |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 817 | modname = self.__class__.__module__ |
| 818 | clsname = self.__class__.__qualname__ |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 819 | try: |
| 820 | name = self.name |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 821 | except Exception: |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 822 | return "<{}.{}>".format(modname, clsname) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 823 | else: |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 824 | return "<{}.{} name={!r}>".format(modname, clsname, name) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 825 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 826 | ### Lower-level APIs ### |
| 827 | |
| 828 | def fileno(self): |
| 829 | return self.raw.fileno() |
| 830 | |
| 831 | def isatty(self): |
| 832 | return self.raw.isatty() |
| 833 | |
| 834 | |
| 835 | class BytesIO(BufferedIOBase): |
| 836 | |
| 837 | """Buffered I/O implementation using an in-memory bytes buffer.""" |
| 838 | |
| 839 | def __init__(self, initial_bytes=None): |
| 840 | buf = bytearray() |
| 841 | if initial_bytes is not None: |
| 842 | buf += initial_bytes |
| 843 | self._buffer = buf |
| 844 | self._pos = 0 |
| 845 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 846 | def __getstate__(self): |
| 847 | if self.closed: |
| 848 | raise ValueError("__getstate__ on closed file") |
| 849 | return self.__dict__.copy() |
| 850 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 851 | def getvalue(self): |
| 852 | """Return the bytes value (contents) of the buffer |
| 853 | """ |
| 854 | if self.closed: |
| 855 | raise ValueError("getvalue on closed file") |
| 856 | return bytes(self._buffer) |
| 857 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 858 | def getbuffer(self): |
| 859 | """Return a readable and writable view of the buffer. |
| 860 | """ |
Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 861 | if self.closed: |
| 862 | raise ValueError("getbuffer on closed file") |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 863 | return memoryview(self._buffer) |
| 864 | |
Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 865 | def close(self): |
| 866 | self._buffer.clear() |
| 867 | super().close() |
| 868 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 869 | def read(self, size=None): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 870 | if self.closed: |
| 871 | raise ValueError("read from closed file") |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 872 | if size is None: |
| 873 | size = -1 |
| 874 | if size < 0: |
| 875 | size = len(self._buffer) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 876 | if len(self._buffer) <= self._pos: |
| 877 | return b"" |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 878 | newpos = min(len(self._buffer), self._pos + size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 879 | b = self._buffer[self._pos : newpos] |
| 880 | self._pos = newpos |
| 881 | return bytes(b) |
| 882 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 883 | def read1(self, size): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 884 | """This is the same as read. |
| 885 | """ |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 886 | return self.read(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 887 | |
| 888 | def write(self, b): |
| 889 | if self.closed: |
| 890 | raise ValueError("write to closed file") |
| 891 | if isinstance(b, str): |
| 892 | raise TypeError("can't write str to binary stream") |
| 893 | n = len(b) |
| 894 | if n == 0: |
| 895 | return 0 |
| 896 | pos = self._pos |
| 897 | if pos > len(self._buffer): |
| 898 | # Inserts null bytes between the current end of the file |
| 899 | # and the new write position. |
| 900 | padding = b'\x00' * (pos - len(self._buffer)) |
| 901 | self._buffer += padding |
| 902 | self._buffer[pos:pos + n] = b |
| 903 | self._pos += n |
| 904 | return n |
| 905 | |
| 906 | def seek(self, pos, whence=0): |
| 907 | if self.closed: |
| 908 | raise ValueError("seek on closed file") |
| 909 | try: |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 910 | pos.__index__ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 911 | except AttributeError as err: |
| 912 | raise TypeError("an integer is required") from err |
| 913 | if whence == 0: |
| 914 | if pos < 0: |
| 915 | raise ValueError("negative seek position %r" % (pos,)) |
| 916 | self._pos = pos |
| 917 | elif whence == 1: |
| 918 | self._pos = max(0, self._pos + pos) |
| 919 | elif whence == 2: |
| 920 | self._pos = max(0, len(self._buffer) + pos) |
| 921 | else: |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 922 | raise ValueError("unsupported whence value") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 923 | return self._pos |
| 924 | |
| 925 | def tell(self): |
| 926 | if self.closed: |
| 927 | raise ValueError("tell on closed file") |
| 928 | return self._pos |
| 929 | |
| 930 | def truncate(self, pos=None): |
| 931 | if self.closed: |
| 932 | raise ValueError("truncate on closed file") |
| 933 | if pos is None: |
| 934 | pos = self._pos |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 935 | else: |
| 936 | try: |
| 937 | pos.__index__ |
| 938 | except AttributeError as err: |
| 939 | raise TypeError("an integer is required") from err |
| 940 | if pos < 0: |
| 941 | raise ValueError("negative truncate position %r" % (pos,)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 942 | del self._buffer[pos:] |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 943 | return pos |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 944 | |
| 945 | def readable(self): |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 946 | if self.closed: |
| 947 | raise ValueError("I/O operation on closed file.") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 948 | return True |
| 949 | |
| 950 | def writable(self): |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 951 | if self.closed: |
| 952 | raise ValueError("I/O operation on closed file.") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 953 | return True |
| 954 | |
| 955 | def seekable(self): |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 956 | if self.closed: |
| 957 | raise ValueError("I/O operation on closed file.") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 958 | return True |
| 959 | |
| 960 | |
| 961 | class BufferedReader(_BufferedIOMixin): |
| 962 | |
| 963 | """BufferedReader(raw[, buffer_size]) |
| 964 | |
| 965 | A buffer for a readable, sequential BaseRawIO object. |
| 966 | |
| 967 | The constructor creates a BufferedReader for the given readable raw |
| 968 | stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE |
| 969 | is used. |
| 970 | """ |
| 971 | |
| 972 | def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE): |
| 973 | """Create a new buffered reader using the given readable raw IO object. |
| 974 | """ |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 975 | if not raw.readable(): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 976 | raise OSError('"raw" argument must be readable.') |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 977 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 978 | _BufferedIOMixin.__init__(self, raw) |
| 979 | if buffer_size <= 0: |
| 980 | raise ValueError("invalid buffer size") |
| 981 | self.buffer_size = buffer_size |
| 982 | self._reset_read_buf() |
| 983 | self._read_lock = Lock() |
| 984 | |
| 985 | def _reset_read_buf(self): |
| 986 | self._read_buf = b"" |
| 987 | self._read_pos = 0 |
| 988 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 989 | def read(self, size=None): |
| 990 | """Read size bytes. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 991 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 992 | Returns exactly size bytes of data unless the underlying raw IO |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 993 | stream reaches EOF or if the call would block in non-blocking |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 994 | mode. If size is negative, read until EOF or until read() would |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 995 | block. |
| 996 | """ |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 997 | if size is not None and size < -1: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 998 | raise ValueError("invalid number of bytes to read") |
| 999 | with self._read_lock: |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1000 | return self._read_unlocked(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1001 | |
| 1002 | def _read_unlocked(self, n=None): |
| 1003 | nodata_val = b"" |
| 1004 | empty_values = (b"", None) |
| 1005 | buf = self._read_buf |
| 1006 | pos = self._read_pos |
| 1007 | |
| 1008 | # Special case for when the number of bytes to read is unspecified. |
| 1009 | if n is None or n == -1: |
| 1010 | self._reset_read_buf() |
Victor Stinner | b57f108 | 2011-05-26 00:19:38 +0200 | [diff] [blame] | 1011 | if hasattr(self.raw, 'readall'): |
| 1012 | chunk = self.raw.readall() |
| 1013 | if chunk is None: |
| 1014 | return buf[pos:] or None |
| 1015 | else: |
| 1016 | return buf[pos:] + chunk |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1017 | chunks = [buf[pos:]] # Strip the consumed bytes. |
| 1018 | current_size = 0 |
| 1019 | while True: |
| 1020 | # Read until EOF or until read() would block. |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1021 | chunk = self.raw.read() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1022 | if chunk in empty_values: |
| 1023 | nodata_val = chunk |
| 1024 | break |
| 1025 | current_size += len(chunk) |
| 1026 | chunks.append(chunk) |
| 1027 | return b"".join(chunks) or nodata_val |
| 1028 | |
| 1029 | # The number of bytes to read is specified, return at most n bytes. |
| 1030 | avail = len(buf) - pos # Length of the available buffered data. |
| 1031 | if n <= avail: |
| 1032 | # Fast path: the data to read is fully buffered. |
| 1033 | self._read_pos += n |
| 1034 | return buf[pos:pos+n] |
| 1035 | # Slow path: read from the stream until enough bytes are read, |
| 1036 | # or until an EOF occurs or until read() would block. |
| 1037 | chunks = [buf[pos:]] |
| 1038 | wanted = max(self.buffer_size, n) |
| 1039 | while avail < n: |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1040 | chunk = self.raw.read(wanted) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1041 | if chunk in empty_values: |
| 1042 | nodata_val = chunk |
| 1043 | break |
| 1044 | avail += len(chunk) |
| 1045 | chunks.append(chunk) |
| 1046 | # n is more then avail only when an EOF occurred or when |
| 1047 | # read() would have blocked. |
| 1048 | n = min(n, avail) |
| 1049 | out = b"".join(chunks) |
| 1050 | self._read_buf = out[n:] # Save the extra data in the buffer. |
| 1051 | self._read_pos = 0 |
| 1052 | return out[:n] if out else nodata_val |
| 1053 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1054 | def peek(self, size=0): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1055 | """Returns buffered bytes without advancing the position. |
| 1056 | |
| 1057 | The argument indicates a desired minimal number of bytes; we |
| 1058 | do at most one raw read to satisfy it. We never return more |
| 1059 | than self.buffer_size. |
| 1060 | """ |
| 1061 | with self._read_lock: |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1062 | return self._peek_unlocked(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1063 | |
| 1064 | def _peek_unlocked(self, n=0): |
| 1065 | want = min(n, self.buffer_size) |
| 1066 | have = len(self._read_buf) - self._read_pos |
| 1067 | if have < want or have <= 0: |
| 1068 | to_read = self.buffer_size - have |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1069 | current = self.raw.read(to_read) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1070 | if current: |
| 1071 | self._read_buf = self._read_buf[self._read_pos:] + current |
| 1072 | self._read_pos = 0 |
| 1073 | return self._read_buf[self._read_pos:] |
| 1074 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1075 | def read1(self, size): |
| 1076 | """Reads up to size bytes, with at most one read() system call.""" |
| 1077 | # Returns up to size bytes. If at least one byte is buffered, we |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1078 | # only return buffered bytes. Otherwise, we do one raw read. |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1079 | if size < 0: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1080 | raise ValueError("number of bytes to read must be positive") |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1081 | if size == 0: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1082 | return b"" |
| 1083 | with self._read_lock: |
| 1084 | self._peek_unlocked(1) |
| 1085 | return self._read_unlocked( |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1086 | min(size, len(self._read_buf) - self._read_pos)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1087 | |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 1088 | # Implementing readinto() and readinto1() is not strictly necessary (we |
| 1089 | # could rely on the base class that provides an implementation in terms of |
| 1090 | # read() and read1()). We do it anyway to keep the _pyio implementation |
| 1091 | # similar to the io implementation (which implements the methods for |
| 1092 | # performance reasons). |
| 1093 | def _readinto(self, buf, read1): |
| 1094 | """Read data into *buf* with at most one system call.""" |
| 1095 | |
| 1096 | if len(buf) == 0: |
| 1097 | return 0 |
| 1098 | |
| 1099 | # Need to create a memoryview object of type 'b', otherwise |
| 1100 | # we may not be able to assign bytes to it, and slicing it |
| 1101 | # would create a new object. |
| 1102 | if not isinstance(buf, memoryview): |
| 1103 | buf = memoryview(buf) |
| 1104 | buf = buf.cast('B') |
| 1105 | |
| 1106 | written = 0 |
| 1107 | with self._read_lock: |
| 1108 | while written < len(buf): |
| 1109 | |
| 1110 | # First try to read from internal buffer |
| 1111 | avail = min(len(self._read_buf) - self._read_pos, len(buf)) |
| 1112 | if avail: |
| 1113 | buf[written:written+avail] = \ |
| 1114 | self._read_buf[self._read_pos:self._read_pos+avail] |
| 1115 | self._read_pos += avail |
| 1116 | written += avail |
| 1117 | if written == len(buf): |
| 1118 | break |
| 1119 | |
| 1120 | # If remaining space in callers buffer is larger than |
| 1121 | # internal buffer, read directly into callers buffer |
| 1122 | if len(buf) - written > self.buffer_size: |
| 1123 | n = self.raw.readinto(buf[written:]) |
| 1124 | if not n: |
| 1125 | break # eof |
| 1126 | written += n |
| 1127 | |
| 1128 | # Otherwise refill internal buffer - unless we're |
| 1129 | # in read1 mode and already got some data |
| 1130 | elif not (read1 and written): |
| 1131 | if not self._peek_unlocked(1): |
| 1132 | break # eof |
| 1133 | |
| 1134 | # In readinto1 mode, return as soon as we have some data |
| 1135 | if read1 and written: |
| 1136 | break |
| 1137 | |
| 1138 | return written |
| 1139 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1140 | def tell(self): |
| 1141 | return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos |
| 1142 | |
| 1143 | def seek(self, pos, whence=0): |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 1144 | if whence not in valid_seek_flags: |
Jesus Cea | 990eff0 | 2012-04-26 17:05:31 +0200 | [diff] [blame] | 1145 | raise ValueError("invalid whence value") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1146 | with self._read_lock: |
| 1147 | if whence == 1: |
| 1148 | pos -= len(self._read_buf) - self._read_pos |
| 1149 | pos = _BufferedIOMixin.seek(self, pos, whence) |
| 1150 | self._reset_read_buf() |
| 1151 | return pos |
| 1152 | |
| 1153 | class BufferedWriter(_BufferedIOMixin): |
| 1154 | |
| 1155 | """A buffer for a writeable sequential RawIO object. |
| 1156 | |
| 1157 | The constructor creates a BufferedWriter for the given writeable raw |
| 1158 | stream. If the buffer_size is not given, it defaults to |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1159 | DEFAULT_BUFFER_SIZE. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1160 | """ |
| 1161 | |
Florent Xicluna | 109d573 | 2012-07-07 17:03:22 +0200 | [diff] [blame] | 1162 | def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE): |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1163 | if not raw.writable(): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1164 | raise OSError('"raw" argument must be writable.') |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1165 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1166 | _BufferedIOMixin.__init__(self, raw) |
| 1167 | if buffer_size <= 0: |
| 1168 | raise ValueError("invalid buffer size") |
| 1169 | self.buffer_size = buffer_size |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1170 | self._write_buf = bytearray() |
| 1171 | self._write_lock = Lock() |
| 1172 | |
| 1173 | def write(self, b): |
| 1174 | if self.closed: |
| 1175 | raise ValueError("write to closed file") |
| 1176 | if isinstance(b, str): |
| 1177 | raise TypeError("can't write str to binary stream") |
| 1178 | with self._write_lock: |
| 1179 | # XXX we can implement some more tricks to try and avoid |
| 1180 | # partial writes |
| 1181 | if len(self._write_buf) > self.buffer_size: |
Antoine Pitrou | 58fcf9f | 2011-11-21 20:16:44 +0100 | [diff] [blame] | 1182 | # We're full, so let's pre-flush the buffer. (This may |
| 1183 | # raise BlockingIOError with characters_written == 0.) |
| 1184 | self._flush_unlocked() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1185 | before = len(self._write_buf) |
| 1186 | self._write_buf.extend(b) |
| 1187 | written = len(self._write_buf) - before |
| 1188 | if len(self._write_buf) > self.buffer_size: |
| 1189 | try: |
| 1190 | self._flush_unlocked() |
| 1191 | except BlockingIOError as e: |
Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 1192 | if len(self._write_buf) > self.buffer_size: |
| 1193 | # We've hit the buffer_size. We have to accept a partial |
| 1194 | # write and cut back our buffer. |
| 1195 | overage = len(self._write_buf) - self.buffer_size |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1196 | written -= overage |
Benjamin Peterson | 394ee00 | 2009-03-05 22:33:59 +0000 | [diff] [blame] | 1197 | self._write_buf = self._write_buf[:self.buffer_size] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1198 | raise BlockingIOError(e.errno, e.strerror, written) |
| 1199 | return written |
| 1200 | |
| 1201 | def truncate(self, pos=None): |
| 1202 | with self._write_lock: |
| 1203 | self._flush_unlocked() |
| 1204 | if pos is None: |
| 1205 | pos = self.raw.tell() |
| 1206 | return self.raw.truncate(pos) |
| 1207 | |
| 1208 | def flush(self): |
| 1209 | with self._write_lock: |
| 1210 | self._flush_unlocked() |
| 1211 | |
| 1212 | def _flush_unlocked(self): |
| 1213 | if self.closed: |
| 1214 | raise ValueError("flush of closed file") |
Antoine Pitrou | 58fcf9f | 2011-11-21 20:16:44 +0100 | [diff] [blame] | 1215 | while self._write_buf: |
| 1216 | try: |
| 1217 | n = self.raw.write(self._write_buf) |
| 1218 | except BlockingIOError: |
| 1219 | raise RuntimeError("self.raw should implement RawIOBase: it " |
| 1220 | "should not raise BlockingIOError") |
Antoine Pitrou | 58fcf9f | 2011-11-21 20:16:44 +0100 | [diff] [blame] | 1221 | if n is None: |
| 1222 | raise BlockingIOError( |
| 1223 | errno.EAGAIN, |
| 1224 | "write could not complete without blocking", 0) |
| 1225 | if n > len(self._write_buf) or n < 0: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1226 | raise OSError("write() returned incorrect number of bytes") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1227 | del self._write_buf[:n] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1228 | |
| 1229 | def tell(self): |
| 1230 | return _BufferedIOMixin.tell(self) + len(self._write_buf) |
| 1231 | |
| 1232 | def seek(self, pos, whence=0): |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 1233 | if whence not in valid_seek_flags: |
| 1234 | raise ValueError("invalid whence value") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1235 | with self._write_lock: |
| 1236 | self._flush_unlocked() |
| 1237 | return _BufferedIOMixin.seek(self, pos, whence) |
| 1238 | |
| 1239 | |
| 1240 | class BufferedRWPair(BufferedIOBase): |
| 1241 | |
| 1242 | """A buffered reader and writer object together. |
| 1243 | |
| 1244 | A buffered reader object and buffered writer object put together to |
| 1245 | form a sequential IO object that can read and write. This is typically |
| 1246 | used with a socket or two-way pipe. |
| 1247 | |
| 1248 | reader and writer are RawIOBase objects that are readable and |
| 1249 | writeable respectively. If the buffer_size is omitted it defaults to |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1250 | DEFAULT_BUFFER_SIZE. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1251 | """ |
| 1252 | |
| 1253 | # XXX The usefulness of this (compared to having two separate IO |
| 1254 | # objects) is questionable. |
| 1255 | |
Florent Xicluna | 109d573 | 2012-07-07 17:03:22 +0200 | [diff] [blame] | 1256 | def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1257 | """Constructor. |
| 1258 | |
| 1259 | The arguments are two RawIO instances. |
| 1260 | """ |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1261 | if not reader.readable(): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1262 | raise OSError('"reader" argument must be readable.') |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1263 | |
| 1264 | if not writer.writable(): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1265 | raise OSError('"writer" argument must be writable.') |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1266 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1267 | self.reader = BufferedReader(reader, buffer_size) |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1268 | self.writer = BufferedWriter(writer, buffer_size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1269 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1270 | def read(self, size=None): |
| 1271 | if size is None: |
| 1272 | size = -1 |
| 1273 | return self.reader.read(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1274 | |
| 1275 | def readinto(self, b): |
| 1276 | return self.reader.readinto(b) |
| 1277 | |
| 1278 | def write(self, b): |
| 1279 | return self.writer.write(b) |
| 1280 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1281 | def peek(self, size=0): |
| 1282 | return self.reader.peek(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1283 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1284 | def read1(self, size): |
| 1285 | return self.reader.read1(size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1286 | |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 1287 | def readinto1(self, b): |
| 1288 | return self.reader.readinto1(b) |
| 1289 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1290 | def readable(self): |
| 1291 | return self.reader.readable() |
| 1292 | |
| 1293 | def writable(self): |
| 1294 | return self.writer.writable() |
| 1295 | |
| 1296 | def flush(self): |
| 1297 | return self.writer.flush() |
| 1298 | |
| 1299 | def close(self): |
Serhiy Storchaka | 7665be6 | 2015-03-24 23:21:57 +0200 | [diff] [blame] | 1300 | try: |
| 1301 | self.writer.close() |
| 1302 | finally: |
| 1303 | self.reader.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1304 | |
| 1305 | def isatty(self): |
| 1306 | return self.reader.isatty() or self.writer.isatty() |
| 1307 | |
| 1308 | @property |
| 1309 | def closed(self): |
| 1310 | return self.writer.closed |
| 1311 | |
| 1312 | |
| 1313 | class BufferedRandom(BufferedWriter, BufferedReader): |
| 1314 | |
| 1315 | """A buffered interface to random access streams. |
| 1316 | |
| 1317 | The constructor creates a reader and writer for a seekable stream, |
| 1318 | raw, given in the first argument. If the buffer_size is omitted it |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1319 | defaults to DEFAULT_BUFFER_SIZE. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1320 | """ |
| 1321 | |
Florent Xicluna | 109d573 | 2012-07-07 17:03:22 +0200 | [diff] [blame] | 1322 | def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1323 | raw._checkSeekable() |
| 1324 | BufferedReader.__init__(self, raw, buffer_size) |
Florent Xicluna | 109d573 | 2012-07-07 17:03:22 +0200 | [diff] [blame] | 1325 | BufferedWriter.__init__(self, raw, buffer_size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1326 | |
| 1327 | def seek(self, pos, whence=0): |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 1328 | if whence not in valid_seek_flags: |
| 1329 | raise ValueError("invalid whence value") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1330 | self.flush() |
| 1331 | if self._read_buf: |
| 1332 | # Undo read ahead. |
| 1333 | with self._read_lock: |
| 1334 | self.raw.seek(self._read_pos - len(self._read_buf), 1) |
| 1335 | # First do the raw seek, then empty the read buffer, so that |
| 1336 | # if the raw seek fails, we don't lose buffered data forever. |
| 1337 | pos = self.raw.seek(pos, whence) |
| 1338 | with self._read_lock: |
| 1339 | self._reset_read_buf() |
| 1340 | if pos < 0: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1341 | raise OSError("seek() returned invalid position") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1342 | return pos |
| 1343 | |
| 1344 | def tell(self): |
| 1345 | if self._write_buf: |
| 1346 | return BufferedWriter.tell(self) |
| 1347 | else: |
| 1348 | return BufferedReader.tell(self) |
| 1349 | |
| 1350 | def truncate(self, pos=None): |
| 1351 | if pos is None: |
| 1352 | pos = self.tell() |
| 1353 | # Use seek to flush the read buffer. |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 1354 | return BufferedWriter.truncate(self, pos) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1355 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1356 | def read(self, size=None): |
| 1357 | if size is None: |
| 1358 | size = -1 |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1359 | self.flush() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1360 | return BufferedReader.read(self, size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1361 | |
| 1362 | def readinto(self, b): |
| 1363 | self.flush() |
| 1364 | return BufferedReader.readinto(self, b) |
| 1365 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1366 | def peek(self, size=0): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1367 | self.flush() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1368 | return BufferedReader.peek(self, size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1369 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1370 | def read1(self, size): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1371 | self.flush() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1372 | return BufferedReader.read1(self, size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1373 | |
Benjamin Peterson | a96fea0 | 2014-06-22 14:17:44 -0700 | [diff] [blame] | 1374 | def readinto1(self, b): |
| 1375 | self.flush() |
| 1376 | return BufferedReader.readinto1(self, b) |
| 1377 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1378 | def write(self, b): |
| 1379 | if self._read_buf: |
| 1380 | # Undo readahead |
| 1381 | with self._read_lock: |
| 1382 | self.raw.seek(self._read_pos - len(self._read_buf), 1) |
| 1383 | self._reset_read_buf() |
| 1384 | return BufferedWriter.write(self, b) |
| 1385 | |
| 1386 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 1387 | class FileIO(RawIOBase): |
| 1388 | _fd = -1 |
| 1389 | _created = False |
| 1390 | _readable = False |
| 1391 | _writable = False |
| 1392 | _appending = False |
| 1393 | _seekable = None |
| 1394 | _closefd = True |
| 1395 | |
| 1396 | def __init__(self, file, mode='r', closefd=True, opener=None): |
| 1397 | """Open a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading, |
| 1398 | writing, exclusive creation or appending. The file will be created if it |
| 1399 | doesn't exist when opened for writing or appending; it will be truncated |
| 1400 | when opened for writing. A FileExistsError will be raised if it already |
| 1401 | exists when opened for creating. Opening a file for creating implies |
| 1402 | writing so this mode behaves in a similar way to 'w'. Add a '+' to the mode |
| 1403 | to allow simultaneous reading and writing. A custom opener can be used by |
| 1404 | passing a callable as *opener*. The underlying file descriptor for the file |
| 1405 | object is then obtained by calling opener with (*name*, *flags*). |
| 1406 | *opener* must return an open file descriptor (passing os.open as *opener* |
| 1407 | results in functionality similar to passing None). |
| 1408 | """ |
| 1409 | if self._fd >= 0: |
| 1410 | # Have to close the existing file first. |
| 1411 | try: |
| 1412 | if self._closefd: |
| 1413 | os.close(self._fd) |
| 1414 | finally: |
| 1415 | self._fd = -1 |
| 1416 | |
| 1417 | if isinstance(file, float): |
| 1418 | raise TypeError('integer argument expected, got float') |
| 1419 | if isinstance(file, int): |
| 1420 | fd = file |
| 1421 | if fd < 0: |
| 1422 | raise ValueError('negative file descriptor') |
| 1423 | else: |
| 1424 | fd = -1 |
| 1425 | |
| 1426 | if not isinstance(mode, str): |
| 1427 | raise TypeError('invalid mode: %s' % (mode,)) |
| 1428 | if not set(mode) <= set('xrwab+'): |
| 1429 | raise ValueError('invalid mode: %s' % (mode,)) |
| 1430 | if sum(c in 'rwax' for c in mode) != 1 or mode.count('+') > 1: |
| 1431 | raise ValueError('Must have exactly one of create/read/write/append ' |
| 1432 | 'mode and at most one plus') |
| 1433 | |
| 1434 | if 'x' in mode: |
| 1435 | self._created = True |
| 1436 | self._writable = True |
| 1437 | flags = os.O_EXCL | os.O_CREAT |
| 1438 | elif 'r' in mode: |
| 1439 | self._readable = True |
| 1440 | flags = 0 |
| 1441 | elif 'w' in mode: |
| 1442 | self._writable = True |
| 1443 | flags = os.O_CREAT | os.O_TRUNC |
| 1444 | elif 'a' in mode: |
| 1445 | self._writable = True |
| 1446 | self._appending = True |
| 1447 | flags = os.O_APPEND | os.O_CREAT |
| 1448 | |
| 1449 | if '+' in mode: |
| 1450 | self._readable = True |
| 1451 | self._writable = True |
| 1452 | |
| 1453 | if self._readable and self._writable: |
| 1454 | flags |= os.O_RDWR |
| 1455 | elif self._readable: |
| 1456 | flags |= os.O_RDONLY |
| 1457 | else: |
| 1458 | flags |= os.O_WRONLY |
| 1459 | |
| 1460 | flags |= getattr(os, 'O_BINARY', 0) |
| 1461 | |
| 1462 | noinherit_flag = (getattr(os, 'O_NOINHERIT', 0) or |
| 1463 | getattr(os, 'O_CLOEXEC', 0)) |
| 1464 | flags |= noinherit_flag |
| 1465 | |
| 1466 | owned_fd = None |
| 1467 | try: |
| 1468 | if fd < 0: |
| 1469 | if not closefd: |
| 1470 | raise ValueError('Cannot use closefd=False with file name') |
| 1471 | if opener is None: |
| 1472 | fd = os.open(file, flags, 0o666) |
| 1473 | else: |
| 1474 | fd = opener(file, flags) |
| 1475 | if not isinstance(fd, int): |
| 1476 | raise TypeError('expected integer from opener') |
| 1477 | if fd < 0: |
| 1478 | raise OSError('Negative file descriptor') |
| 1479 | owned_fd = fd |
| 1480 | if not noinherit_flag: |
| 1481 | os.set_inheritable(fd, False) |
| 1482 | |
| 1483 | self._closefd = closefd |
| 1484 | fdfstat = os.fstat(fd) |
| 1485 | try: |
| 1486 | if stat.S_ISDIR(fdfstat.st_mode): |
| 1487 | raise IsADirectoryError(errno.EISDIR, |
| 1488 | os.strerror(errno.EISDIR), file) |
| 1489 | except AttributeError: |
| 1490 | # Ignore the AttribueError if stat.S_ISDIR or errno.EISDIR |
| 1491 | # don't exist. |
| 1492 | pass |
| 1493 | self._blksize = getattr(fdfstat, 'st_blksize', 0) |
| 1494 | if self._blksize <= 1: |
| 1495 | self._blksize = DEFAULT_BUFFER_SIZE |
| 1496 | |
| 1497 | if _setmode: |
| 1498 | # don't translate newlines (\r\n <=> \n) |
| 1499 | _setmode(fd, os.O_BINARY) |
| 1500 | |
| 1501 | self.name = file |
| 1502 | if self._appending: |
| 1503 | # For consistent behaviour, we explicitly seek to the |
| 1504 | # end of file (otherwise, it might be done only on the |
| 1505 | # first write()). |
| 1506 | os.lseek(fd, 0, SEEK_END) |
| 1507 | except: |
| 1508 | if owned_fd is not None: |
| 1509 | os.close(owned_fd) |
| 1510 | raise |
| 1511 | self._fd = fd |
| 1512 | |
| 1513 | def __del__(self): |
| 1514 | if self._fd >= 0 and self._closefd and not self.closed: |
| 1515 | import warnings |
| 1516 | warnings.warn('unclosed file %r' % (self,), ResourceWarning, |
| 1517 | stacklevel=2) |
| 1518 | self.close() |
| 1519 | |
| 1520 | def __getstate__(self): |
| 1521 | raise TypeError("cannot serialize '%s' object", self.__class__.__name__) |
| 1522 | |
| 1523 | def __repr__(self): |
| 1524 | class_name = '%s.%s' % (self.__class__.__module__, |
| 1525 | self.__class__.__qualname__) |
| 1526 | if self.closed: |
| 1527 | return '<%s [closed]>' % class_name |
| 1528 | try: |
| 1529 | name = self.name |
| 1530 | except AttributeError: |
| 1531 | return ('<%s fd=%d mode=%r closefd=%r>' % |
| 1532 | (class_name, self._fd, self.mode, self._closefd)) |
| 1533 | else: |
| 1534 | return ('<%s name=%r mode=%r closefd=%r>' % |
| 1535 | (class_name, name, self.mode, self._closefd)) |
| 1536 | |
| 1537 | def _checkReadable(self): |
| 1538 | if not self._readable: |
| 1539 | raise UnsupportedOperation('File not open for reading') |
| 1540 | |
| 1541 | def _checkWritable(self, msg=None): |
| 1542 | if not self._writable: |
| 1543 | raise UnsupportedOperation('File not open for writing') |
| 1544 | |
| 1545 | def read(self, size=None): |
| 1546 | """Read at most size bytes, returned as bytes. |
| 1547 | |
| 1548 | Only makes one system call, so less data may be returned than requested |
| 1549 | In non-blocking mode, returns None if no data is available. |
| 1550 | Return an empty bytes object at EOF. |
| 1551 | """ |
| 1552 | self._checkClosed() |
| 1553 | self._checkReadable() |
| 1554 | if size is None or size < 0: |
| 1555 | return self.readall() |
| 1556 | try: |
| 1557 | return os.read(self._fd, size) |
| 1558 | except BlockingIOError: |
| 1559 | return None |
| 1560 | |
| 1561 | def readall(self): |
| 1562 | """Read all data from the file, returned as bytes. |
| 1563 | |
| 1564 | In non-blocking mode, returns as much as is immediately available, |
| 1565 | or None if no data is available. Return an empty bytes object at EOF. |
| 1566 | """ |
| 1567 | self._checkClosed() |
| 1568 | self._checkReadable() |
| 1569 | bufsize = DEFAULT_BUFFER_SIZE |
| 1570 | try: |
| 1571 | pos = os.lseek(self._fd, 0, SEEK_CUR) |
| 1572 | end = os.fstat(self._fd).st_size |
| 1573 | if end >= pos: |
| 1574 | bufsize = end - pos + 1 |
| 1575 | except OSError: |
| 1576 | pass |
| 1577 | |
| 1578 | result = bytearray() |
| 1579 | while True: |
| 1580 | if len(result) >= bufsize: |
| 1581 | bufsize = len(result) |
| 1582 | bufsize += max(bufsize, DEFAULT_BUFFER_SIZE) |
| 1583 | n = bufsize - len(result) |
| 1584 | try: |
| 1585 | chunk = os.read(self._fd, n) |
| 1586 | except BlockingIOError: |
| 1587 | if result: |
| 1588 | break |
| 1589 | return None |
| 1590 | if not chunk: # reached the end of the file |
| 1591 | break |
| 1592 | result += chunk |
| 1593 | |
| 1594 | return bytes(result) |
| 1595 | |
| 1596 | def readinto(self, b): |
| 1597 | """Same as RawIOBase.readinto().""" |
| 1598 | m = memoryview(b).cast('B') |
| 1599 | data = self.read(len(m)) |
| 1600 | n = len(data) |
| 1601 | m[:n] = data |
| 1602 | return n |
| 1603 | |
| 1604 | def write(self, b): |
| 1605 | """Write bytes b to file, return number written. |
| 1606 | |
| 1607 | Only makes one system call, so not all of the data may be written. |
| 1608 | The number of bytes actually written is returned. In non-blocking mode, |
| 1609 | returns None if the write would block. |
| 1610 | """ |
| 1611 | self._checkClosed() |
| 1612 | self._checkWritable() |
| 1613 | try: |
| 1614 | return os.write(self._fd, b) |
| 1615 | except BlockingIOError: |
| 1616 | return None |
| 1617 | |
| 1618 | def seek(self, pos, whence=SEEK_SET): |
| 1619 | """Move to new file position. |
| 1620 | |
| 1621 | Argument offset is a byte count. Optional argument whence defaults to |
| 1622 | SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values |
| 1623 | are SEEK_CUR or 1 (move relative to current position, positive or negative), |
| 1624 | and SEEK_END or 2 (move relative to end of file, usually negative, although |
| 1625 | many platforms allow seeking beyond the end of a file). |
| 1626 | |
| 1627 | Note that not all file objects are seekable. |
| 1628 | """ |
| 1629 | if isinstance(pos, float): |
| 1630 | raise TypeError('an integer is required') |
| 1631 | self._checkClosed() |
| 1632 | return os.lseek(self._fd, pos, whence) |
| 1633 | |
| 1634 | def tell(self): |
| 1635 | """tell() -> int. Current file position. |
| 1636 | |
| 1637 | Can raise OSError for non seekable files.""" |
| 1638 | self._checkClosed() |
| 1639 | return os.lseek(self._fd, 0, SEEK_CUR) |
| 1640 | |
| 1641 | def truncate(self, size=None): |
| 1642 | """Truncate the file to at most size bytes. |
| 1643 | |
| 1644 | Size defaults to the current file position, as returned by tell(). |
| 1645 | The current file position is changed to the value of size. |
| 1646 | """ |
| 1647 | self._checkClosed() |
| 1648 | self._checkWritable() |
| 1649 | if size is None: |
| 1650 | size = self.tell() |
| 1651 | os.ftruncate(self._fd, size) |
| 1652 | return size |
| 1653 | |
| 1654 | def close(self): |
| 1655 | """Close the file. |
| 1656 | |
| 1657 | A closed file cannot be used for further I/O operations. close() may be |
| 1658 | called more than once without error. |
| 1659 | """ |
| 1660 | if not self.closed: |
| 1661 | try: |
| 1662 | if self._closefd: |
| 1663 | os.close(self._fd) |
| 1664 | finally: |
| 1665 | super().close() |
| 1666 | |
| 1667 | def seekable(self): |
| 1668 | """True if file supports random-access.""" |
| 1669 | self._checkClosed() |
| 1670 | if self._seekable is None: |
| 1671 | try: |
| 1672 | self.tell() |
| 1673 | except OSError: |
| 1674 | self._seekable = False |
| 1675 | else: |
| 1676 | self._seekable = True |
| 1677 | return self._seekable |
| 1678 | |
| 1679 | def readable(self): |
| 1680 | """True if file was opened in a read mode.""" |
| 1681 | self._checkClosed() |
| 1682 | return self._readable |
| 1683 | |
| 1684 | def writable(self): |
| 1685 | """True if file was opened in a write mode.""" |
| 1686 | self._checkClosed() |
| 1687 | return self._writable |
| 1688 | |
| 1689 | def fileno(self): |
| 1690 | """Return the underlying file descriptor (an integer).""" |
| 1691 | self._checkClosed() |
| 1692 | return self._fd |
| 1693 | |
| 1694 | def isatty(self): |
| 1695 | """True if the file is connected to a TTY device.""" |
| 1696 | self._checkClosed() |
| 1697 | return os.isatty(self._fd) |
| 1698 | |
| 1699 | @property |
| 1700 | def closefd(self): |
| 1701 | """True if the file descriptor will be closed by close().""" |
| 1702 | return self._closefd |
| 1703 | |
| 1704 | @property |
| 1705 | def mode(self): |
| 1706 | """String giving the file mode""" |
| 1707 | if self._created: |
| 1708 | if self._readable: |
| 1709 | return 'xb+' |
| 1710 | else: |
| 1711 | return 'xb' |
| 1712 | elif self._appending: |
| 1713 | if self._readable: |
| 1714 | return 'ab+' |
| 1715 | else: |
| 1716 | return 'ab' |
| 1717 | elif self._readable: |
| 1718 | if self._writable: |
| 1719 | return 'rb+' |
| 1720 | else: |
| 1721 | return 'rb' |
| 1722 | else: |
| 1723 | return 'wb' |
| 1724 | |
| 1725 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1726 | class TextIOBase(IOBase): |
| 1727 | |
| 1728 | """Base class for text I/O. |
| 1729 | |
| 1730 | This class provides a character and line based interface to stream |
| 1731 | I/O. There is no readinto method because Python's character strings |
| 1732 | are immutable. There is no public constructor. |
| 1733 | """ |
| 1734 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1735 | def read(self, size=-1): |
| 1736 | """Read at most size characters from stream, where size is an int. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1737 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 1738 | Read from underlying buffer until we have size characters or we hit EOF. |
| 1739 | If size is negative or omitted, read until EOF. |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 1740 | |
| 1741 | Returns a string. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1742 | """ |
| 1743 | self._unsupported("read") |
| 1744 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 1745 | def write(self, s): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 1746 | """Write string s to stream and returning an int.""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1747 | self._unsupported("write") |
| 1748 | |
Georg Brandl | 4d73b57 | 2011-01-13 07:13:06 +0000 | [diff] [blame] | 1749 | def truncate(self, pos=None): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 1750 | """Truncate size to pos, where pos is an int.""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1751 | self._unsupported("truncate") |
| 1752 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 1753 | def readline(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1754 | """Read until newline or EOF. |
| 1755 | |
| 1756 | Returns an empty string if EOF is hit immediately. |
| 1757 | """ |
| 1758 | self._unsupported("readline") |
| 1759 | |
Raymond Hettinger | 3c94024 | 2011-01-12 23:39:31 +0000 | [diff] [blame] | 1760 | def detach(self): |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1761 | """ |
| 1762 | Separate the underlying buffer from the TextIOBase and return it. |
| 1763 | |
| 1764 | After the underlying buffer has been detached, the TextIO is in an |
| 1765 | unusable state. |
| 1766 | """ |
| 1767 | self._unsupported("detach") |
| 1768 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1769 | @property |
| 1770 | def encoding(self): |
| 1771 | """Subclasses should override.""" |
| 1772 | return None |
| 1773 | |
| 1774 | @property |
| 1775 | def newlines(self): |
| 1776 | """Line endings translated so far. |
| 1777 | |
| 1778 | Only line endings translated during reading are considered. |
| 1779 | |
| 1780 | Subclasses should override. |
| 1781 | """ |
| 1782 | return None |
| 1783 | |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 1784 | @property |
| 1785 | def errors(self): |
| 1786 | """Error setting of the decoder or encoder. |
| 1787 | |
| 1788 | Subclasses should override.""" |
| 1789 | return None |
| 1790 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1791 | io.TextIOBase.register(TextIOBase) |
| 1792 | |
| 1793 | |
| 1794 | class IncrementalNewlineDecoder(codecs.IncrementalDecoder): |
| 1795 | r"""Codec used when reading a file in universal newlines mode. It wraps |
| 1796 | another incremental decoder, translating \r\n and \r into \n. It also |
| 1797 | records the types of newlines encountered. When used with |
| 1798 | translate=False, it ensures that the newline sequence is returned in |
| 1799 | one piece. |
| 1800 | """ |
| 1801 | def __init__(self, decoder, translate, errors='strict'): |
| 1802 | codecs.IncrementalDecoder.__init__(self, errors=errors) |
| 1803 | self.translate = translate |
| 1804 | self.decoder = decoder |
| 1805 | self.seennl = 0 |
| 1806 | self.pendingcr = False |
| 1807 | |
| 1808 | def decode(self, input, final=False): |
| 1809 | # decode input (with the eventual \r from a previous pass) |
| 1810 | if self.decoder is None: |
| 1811 | output = input |
| 1812 | else: |
| 1813 | output = self.decoder.decode(input, final=final) |
| 1814 | if self.pendingcr and (output or final): |
| 1815 | output = "\r" + output |
| 1816 | self.pendingcr = False |
| 1817 | |
| 1818 | # retain last \r even when not translating data: |
| 1819 | # then readline() is sure to get \r\n in one pass |
| 1820 | if output.endswith("\r") and not final: |
| 1821 | output = output[:-1] |
| 1822 | self.pendingcr = True |
| 1823 | |
| 1824 | # Record which newlines are read |
| 1825 | crlf = output.count('\r\n') |
| 1826 | cr = output.count('\r') - crlf |
| 1827 | lf = output.count('\n') - crlf |
| 1828 | self.seennl |= (lf and self._LF) | (cr and self._CR) \ |
| 1829 | | (crlf and self._CRLF) |
| 1830 | |
| 1831 | if self.translate: |
| 1832 | if crlf: |
| 1833 | output = output.replace("\r\n", "\n") |
| 1834 | if cr: |
| 1835 | output = output.replace("\r", "\n") |
| 1836 | |
| 1837 | return output |
| 1838 | |
| 1839 | def getstate(self): |
| 1840 | if self.decoder is None: |
| 1841 | buf = b"" |
| 1842 | flag = 0 |
| 1843 | else: |
| 1844 | buf, flag = self.decoder.getstate() |
| 1845 | flag <<= 1 |
| 1846 | if self.pendingcr: |
| 1847 | flag |= 1 |
| 1848 | return buf, flag |
| 1849 | |
| 1850 | def setstate(self, state): |
| 1851 | buf, flag = state |
| 1852 | self.pendingcr = bool(flag & 1) |
| 1853 | if self.decoder is not None: |
| 1854 | self.decoder.setstate((buf, flag >> 1)) |
| 1855 | |
| 1856 | def reset(self): |
| 1857 | self.seennl = 0 |
| 1858 | self.pendingcr = False |
| 1859 | if self.decoder is not None: |
| 1860 | self.decoder.reset() |
| 1861 | |
| 1862 | _LF = 1 |
| 1863 | _CR = 2 |
| 1864 | _CRLF = 4 |
| 1865 | |
| 1866 | @property |
| 1867 | def newlines(self): |
| 1868 | return (None, |
| 1869 | "\n", |
| 1870 | "\r", |
| 1871 | ("\r", "\n"), |
| 1872 | "\r\n", |
| 1873 | ("\n", "\r\n"), |
| 1874 | ("\r", "\r\n"), |
| 1875 | ("\r", "\n", "\r\n") |
| 1876 | )[self.seennl] |
| 1877 | |
| 1878 | |
| 1879 | class TextIOWrapper(TextIOBase): |
| 1880 | |
| 1881 | r"""Character and line based layer over a BufferedIOBase object, buffer. |
| 1882 | |
| 1883 | encoding gives the name of the encoding that the stream will be |
Victor Stinner | f86a5e8 | 2012-06-05 13:43:22 +0200 | [diff] [blame] | 1884 | decoded or encoded with. It defaults to locale.getpreferredencoding(False). |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1885 | |
| 1886 | errors determines the strictness of encoding and decoding (see the |
| 1887 | codecs.register) and defaults to "strict". |
| 1888 | |
| 1889 | newline can be None, '', '\n', '\r', or '\r\n'. It controls the |
| 1890 | handling of line endings. If it is None, universal newlines is |
| 1891 | enabled. With this enabled, on input, the lines endings '\n', '\r', |
| 1892 | or '\r\n' are translated to '\n' before being returned to the |
| 1893 | caller. Conversely, on output, '\n' is translated to the system |
Éric Araujo | 3924230 | 2011-11-03 00:08:48 +0100 | [diff] [blame] | 1894 | default line separator, os.linesep. If newline is any other of its |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1895 | legal values, that newline becomes the newline when the file is read |
| 1896 | and it is returned untranslated. On output, '\n' is converted to the |
| 1897 | newline. |
| 1898 | |
| 1899 | If line_buffering is True, a call to flush is implied when a call to |
| 1900 | write contains a newline character. |
| 1901 | """ |
| 1902 | |
| 1903 | _CHUNK_SIZE = 2048 |
| 1904 | |
Andrew Svetlov | 4e9e9c1 | 2012-08-13 16:09:54 +0300 | [diff] [blame] | 1905 | # The write_through argument has no effect here since this |
| 1906 | # implementation always writes through. The argument is present only |
| 1907 | # so that the signature can match the signature of the C version. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1908 | def __init__(self, buffer, encoding=None, errors=None, newline=None, |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 1909 | line_buffering=False, write_through=False): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1910 | if newline is not None and not isinstance(newline, str): |
| 1911 | raise TypeError("illegal newline type: %r" % (type(newline),)) |
| 1912 | if newline not in (None, "", "\n", "\r", "\r\n"): |
| 1913 | raise ValueError("illegal newline value: %r" % (newline,)) |
| 1914 | if encoding is None: |
| 1915 | try: |
| 1916 | encoding = os.device_encoding(buffer.fileno()) |
| 1917 | except (AttributeError, UnsupportedOperation): |
| 1918 | pass |
| 1919 | if encoding is None: |
| 1920 | try: |
| 1921 | import locale |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 1922 | except ImportError: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1923 | # Importing locale may fail if Python is being built |
| 1924 | encoding = "ascii" |
| 1925 | else: |
Victor Stinner | f86a5e8 | 2012-06-05 13:43:22 +0200 | [diff] [blame] | 1926 | encoding = locale.getpreferredencoding(False) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1927 | |
| 1928 | if not isinstance(encoding, str): |
| 1929 | raise ValueError("invalid encoding: %r" % encoding) |
| 1930 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1931 | if not codecs.lookup(encoding)._is_text_encoding: |
| 1932 | msg = ("%r is not a text encoding; " |
| 1933 | "use codecs.open() to handle arbitrary codecs") |
| 1934 | raise LookupError(msg % encoding) |
| 1935 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1936 | if errors is None: |
| 1937 | errors = "strict" |
| 1938 | else: |
| 1939 | if not isinstance(errors, str): |
| 1940 | raise ValueError("invalid errors: %r" % errors) |
| 1941 | |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 1942 | self._buffer = buffer |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1943 | self._line_buffering = line_buffering |
| 1944 | self._encoding = encoding |
| 1945 | self._errors = errors |
| 1946 | self._readuniversal = not newline |
| 1947 | self._readtranslate = newline is None |
| 1948 | self._readnl = newline |
| 1949 | self._writetranslate = newline != '' |
| 1950 | self._writenl = newline or os.linesep |
| 1951 | self._encoder = None |
| 1952 | self._decoder = None |
| 1953 | self._decoded_chars = '' # buffer for text returned from decoder |
| 1954 | self._decoded_chars_used = 0 # offset into _decoded_chars for read() |
| 1955 | self._snapshot = None # info for reconstructing decoder state |
| 1956 | self._seekable = self._telling = self.buffer.seekable() |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 1957 | self._has_read1 = hasattr(self.buffer, 'read1') |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 1958 | self._b2cratio = 0.0 |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1960 | if self._seekable and self.writable(): |
| 1961 | position = self.buffer.tell() |
| 1962 | if position != 0: |
| 1963 | try: |
| 1964 | self._get_encoder().setstate(0) |
| 1965 | except LookupError: |
| 1966 | # Sometimes the encoder doesn't exist |
| 1967 | pass |
| 1968 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1969 | # self._snapshot is either None, or a tuple (dec_flags, next_input) |
| 1970 | # where dec_flags is the second (integer) item of the decoder state |
| 1971 | # and next_input is the chunk of input bytes that comes next after the |
| 1972 | # snapshot point. We use this to reconstruct decoder states in tell(). |
| 1973 | |
| 1974 | # Naming convention: |
| 1975 | # - "bytes_..." for integer variables that count input bytes |
| 1976 | # - "chars_..." for integer variables that count decoded characters |
| 1977 | |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 1978 | def __repr__(self): |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 1979 | result = "<{}.{}".format(self.__class__.__module__, |
| 1980 | self.__class__.__qualname__) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1981 | try: |
| 1982 | name = self.name |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1983 | except Exception: |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 1984 | pass |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1985 | else: |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 1986 | result += " name={0!r}".format(name) |
| 1987 | try: |
| 1988 | mode = self.mode |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1989 | except Exception: |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 1990 | pass |
| 1991 | else: |
| 1992 | result += " mode={0!r}".format(mode) |
| 1993 | return result + " encoding={0!r}>".format(self.encoding) |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 1994 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1995 | @property |
| 1996 | def encoding(self): |
| 1997 | return self._encoding |
| 1998 | |
| 1999 | @property |
| 2000 | def errors(self): |
| 2001 | return self._errors |
| 2002 | |
| 2003 | @property |
| 2004 | def line_buffering(self): |
| 2005 | return self._line_buffering |
| 2006 | |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 2007 | @property |
| 2008 | def buffer(self): |
| 2009 | return self._buffer |
| 2010 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2011 | def seekable(self): |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 2012 | if self.closed: |
| 2013 | raise ValueError("I/O operation on closed file.") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2014 | return self._seekable |
| 2015 | |
| 2016 | def readable(self): |
| 2017 | return self.buffer.readable() |
| 2018 | |
| 2019 | def writable(self): |
| 2020 | return self.buffer.writable() |
| 2021 | |
| 2022 | def flush(self): |
| 2023 | self.buffer.flush() |
| 2024 | self._telling = self._seekable |
| 2025 | |
| 2026 | def close(self): |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2027 | if self.buffer is not None and not self.closed: |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 2028 | try: |
| 2029 | self.flush() |
| 2030 | finally: |
| 2031 | self.buffer.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2032 | |
| 2033 | @property |
| 2034 | def closed(self): |
| 2035 | return self.buffer.closed |
| 2036 | |
| 2037 | @property |
| 2038 | def name(self): |
| 2039 | return self.buffer.name |
| 2040 | |
| 2041 | def fileno(self): |
| 2042 | return self.buffer.fileno() |
| 2043 | |
| 2044 | def isatty(self): |
| 2045 | return self.buffer.isatty() |
| 2046 | |
Raymond Hettinger | 00fa039 | 2011-01-13 02:52:26 +0000 | [diff] [blame] | 2047 | def write(self, s): |
Raymond Hettinger | cbb8089 | 2011-01-13 18:15:51 +0000 | [diff] [blame] | 2048 | 'Write data, where s is a str' |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2049 | if self.closed: |
| 2050 | raise ValueError("write to closed file") |
| 2051 | if not isinstance(s, str): |
| 2052 | raise TypeError("can't write %s to text stream" % |
| 2053 | s.__class__.__name__) |
| 2054 | length = len(s) |
| 2055 | haslf = (self._writetranslate or self._line_buffering) and "\n" in s |
| 2056 | if haslf and self._writetranslate and self._writenl != "\n": |
| 2057 | s = s.replace("\n", self._writenl) |
| 2058 | encoder = self._encoder or self._get_encoder() |
| 2059 | # XXX What if we were just reading? |
| 2060 | b = encoder.encode(s) |
| 2061 | self.buffer.write(b) |
| 2062 | if self._line_buffering and (haslf or "\r" in s): |
| 2063 | self.flush() |
| 2064 | self._snapshot = None |
| 2065 | if self._decoder: |
| 2066 | self._decoder.reset() |
| 2067 | return length |
| 2068 | |
| 2069 | def _get_encoder(self): |
| 2070 | make_encoder = codecs.getincrementalencoder(self._encoding) |
| 2071 | self._encoder = make_encoder(self._errors) |
| 2072 | return self._encoder |
| 2073 | |
| 2074 | def _get_decoder(self): |
| 2075 | make_decoder = codecs.getincrementaldecoder(self._encoding) |
| 2076 | decoder = make_decoder(self._errors) |
| 2077 | if self._readuniversal: |
| 2078 | decoder = IncrementalNewlineDecoder(decoder, self._readtranslate) |
| 2079 | self._decoder = decoder |
| 2080 | return decoder |
| 2081 | |
| 2082 | # The following three methods implement an ADT for _decoded_chars. |
| 2083 | # Text returned from the decoder is buffered here until the client |
| 2084 | # requests it by calling our read() or readline() method. |
| 2085 | def _set_decoded_chars(self, chars): |
| 2086 | """Set the _decoded_chars buffer.""" |
| 2087 | self._decoded_chars = chars |
| 2088 | self._decoded_chars_used = 0 |
| 2089 | |
| 2090 | def _get_decoded_chars(self, n=None): |
| 2091 | """Advance into the _decoded_chars buffer.""" |
| 2092 | offset = self._decoded_chars_used |
| 2093 | if n is None: |
| 2094 | chars = self._decoded_chars[offset:] |
| 2095 | else: |
| 2096 | chars = self._decoded_chars[offset:offset + n] |
| 2097 | self._decoded_chars_used += len(chars) |
| 2098 | return chars |
| 2099 | |
| 2100 | def _rewind_decoded_chars(self, n): |
| 2101 | """Rewind the _decoded_chars buffer.""" |
| 2102 | if self._decoded_chars_used < n: |
| 2103 | raise AssertionError("rewind decoded_chars out of bounds") |
| 2104 | self._decoded_chars_used -= n |
| 2105 | |
| 2106 | def _read_chunk(self): |
| 2107 | """ |
| 2108 | Read and decode the next chunk of data from the BufferedReader. |
| 2109 | """ |
| 2110 | |
| 2111 | # The return value is True unless EOF was reached. The decoded |
| 2112 | # string is placed in self._decoded_chars (replacing its previous |
| 2113 | # value). The entire input chunk is sent to the decoder, though |
| 2114 | # some of it may remain buffered in the decoder, yet to be |
| 2115 | # converted. |
| 2116 | |
| 2117 | if self._decoder is None: |
| 2118 | raise ValueError("no decoder") |
| 2119 | |
| 2120 | if self._telling: |
| 2121 | # To prepare for tell(), we need to snapshot a point in the |
| 2122 | # file where the decoder's input buffer is empty. |
| 2123 | |
| 2124 | dec_buffer, dec_flags = self._decoder.getstate() |
| 2125 | # Given this, we know there was a valid snapshot point |
| 2126 | # len(dec_buffer) bytes ago with decoder state (b'', dec_flags). |
| 2127 | |
| 2128 | # Read a chunk, decode it, and put the result in self._decoded_chars. |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 2129 | if self._has_read1: |
| 2130 | input_chunk = self.buffer.read1(self._CHUNK_SIZE) |
| 2131 | else: |
| 2132 | input_chunk = self.buffer.read(self._CHUNK_SIZE) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2133 | eof = not input_chunk |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2134 | decoded_chars = self._decoder.decode(input_chunk, eof) |
| 2135 | self._set_decoded_chars(decoded_chars) |
| 2136 | if decoded_chars: |
| 2137 | self._b2cratio = len(input_chunk) / len(self._decoded_chars) |
| 2138 | else: |
| 2139 | self._b2cratio = 0.0 |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2140 | |
| 2141 | if self._telling: |
| 2142 | # At the snapshot point, len(dec_buffer) bytes before the read, |
| 2143 | # the next input to be decoded is dec_buffer + input_chunk. |
| 2144 | self._snapshot = (dec_flags, dec_buffer + input_chunk) |
| 2145 | |
| 2146 | return not eof |
| 2147 | |
| 2148 | def _pack_cookie(self, position, dec_flags=0, |
| 2149 | bytes_to_feed=0, need_eof=0, chars_to_skip=0): |
| 2150 | # The meaning of a tell() cookie is: seek to position, set the |
| 2151 | # decoder flags to dec_flags, read bytes_to_feed bytes, feed them |
| 2152 | # into the decoder with need_eof as the EOF flag, then skip |
| 2153 | # chars_to_skip characters of the decoded result. For most simple |
| 2154 | # decoders, tell() will often just give a byte offset in the file. |
| 2155 | return (position | (dec_flags<<64) | (bytes_to_feed<<128) | |
| 2156 | (chars_to_skip<<192) | bool(need_eof)<<256) |
| 2157 | |
| 2158 | def _unpack_cookie(self, bigint): |
| 2159 | rest, position = divmod(bigint, 1<<64) |
| 2160 | rest, dec_flags = divmod(rest, 1<<64) |
| 2161 | rest, bytes_to_feed = divmod(rest, 1<<64) |
| 2162 | need_eof, chars_to_skip = divmod(rest, 1<<64) |
| 2163 | return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip |
| 2164 | |
| 2165 | def tell(self): |
| 2166 | if not self._seekable: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2167 | raise UnsupportedOperation("underlying stream is not seekable") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2168 | if not self._telling: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2169 | raise OSError("telling position disabled by next() call") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2170 | self.flush() |
| 2171 | position = self.buffer.tell() |
| 2172 | decoder = self._decoder |
| 2173 | if decoder is None or self._snapshot is None: |
| 2174 | if self._decoded_chars: |
| 2175 | # This should never happen. |
| 2176 | raise AssertionError("pending decoded text") |
| 2177 | return position |
| 2178 | |
| 2179 | # Skip backward to the snapshot point (see _read_chunk). |
| 2180 | dec_flags, next_input = self._snapshot |
| 2181 | position -= len(next_input) |
| 2182 | |
| 2183 | # How many decoded characters have been used up since the snapshot? |
| 2184 | chars_to_skip = self._decoded_chars_used |
| 2185 | if chars_to_skip == 0: |
| 2186 | # We haven't moved from the snapshot point. |
| 2187 | return self._pack_cookie(position, dec_flags) |
| 2188 | |
| 2189 | # Starting from the snapshot position, we will walk the decoder |
| 2190 | # forward until it gives us enough decoded characters. |
| 2191 | saved_state = decoder.getstate() |
| 2192 | try: |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2193 | # Fast search for an acceptable start point, close to our |
| 2194 | # current pos. |
| 2195 | # Rationale: calling decoder.decode() has a large overhead |
| 2196 | # regardless of chunk size; we want the number of such calls to |
| 2197 | # be O(1) in most situations (common decoders, non-crazy input). |
| 2198 | # Actually, it will be exactly 1 for fixed-size codecs (all |
| 2199 | # 8-bit codecs, also UTF-16 and UTF-32). |
| 2200 | skip_bytes = int(self._b2cratio * chars_to_skip) |
| 2201 | skip_back = 1 |
| 2202 | assert skip_bytes <= len(next_input) |
| 2203 | while skip_bytes > 0: |
| 2204 | decoder.setstate((b'', dec_flags)) |
| 2205 | # Decode up to temptative start point |
| 2206 | n = len(decoder.decode(next_input[:skip_bytes])) |
| 2207 | if n <= chars_to_skip: |
| 2208 | b, d = decoder.getstate() |
| 2209 | if not b: |
| 2210 | # Before pos and no bytes buffered in decoder => OK |
| 2211 | dec_flags = d |
| 2212 | chars_to_skip -= n |
| 2213 | break |
| 2214 | # Skip back by buffered amount and reset heuristic |
| 2215 | skip_bytes -= len(b) |
| 2216 | skip_back = 1 |
| 2217 | else: |
| 2218 | # We're too far ahead, skip back a bit |
| 2219 | skip_bytes -= skip_back |
| 2220 | skip_back = skip_back * 2 |
| 2221 | else: |
| 2222 | skip_bytes = 0 |
| 2223 | decoder.setstate((b'', dec_flags)) |
| 2224 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2225 | # Note our initial start point. |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2226 | start_pos = position + skip_bytes |
| 2227 | start_flags = dec_flags |
| 2228 | if chars_to_skip == 0: |
| 2229 | # We haven't moved from the start point. |
| 2230 | return self._pack_cookie(start_pos, start_flags) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2231 | |
| 2232 | # Feed the decoder one byte at a time. As we go, note the |
| 2233 | # nearest "safe start point" before the current location |
| 2234 | # (a point where the decoder has nothing buffered, so seek() |
| 2235 | # can safely start from there and advance to this location). |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2236 | bytes_fed = 0 |
| 2237 | need_eof = 0 |
| 2238 | # Chars decoded since `start_pos` |
| 2239 | chars_decoded = 0 |
| 2240 | for i in range(skip_bytes, len(next_input)): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2241 | bytes_fed += 1 |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2242 | chars_decoded += len(decoder.decode(next_input[i:i+1])) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2243 | dec_buffer, dec_flags = decoder.getstate() |
| 2244 | if not dec_buffer and chars_decoded <= chars_to_skip: |
| 2245 | # Decoder buffer is empty, so this is a safe start point. |
| 2246 | start_pos += bytes_fed |
| 2247 | chars_to_skip -= chars_decoded |
| 2248 | start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0 |
| 2249 | if chars_decoded >= chars_to_skip: |
| 2250 | break |
| 2251 | else: |
| 2252 | # We didn't get enough decoded data; signal EOF to get more. |
| 2253 | chars_decoded += len(decoder.decode(b'', final=True)) |
| 2254 | need_eof = 1 |
| 2255 | if chars_decoded < chars_to_skip: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2256 | raise OSError("can't reconstruct logical file position") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2257 | |
| 2258 | # The returned cookie corresponds to the last safe start point. |
| 2259 | return self._pack_cookie( |
| 2260 | start_pos, start_flags, bytes_fed, need_eof, chars_to_skip) |
| 2261 | finally: |
| 2262 | decoder.setstate(saved_state) |
| 2263 | |
| 2264 | def truncate(self, pos=None): |
| 2265 | self.flush() |
| 2266 | if pos is None: |
| 2267 | pos = self.tell() |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 2268 | return self.buffer.truncate(pos) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2269 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 2270 | def detach(self): |
| 2271 | if self.buffer is None: |
| 2272 | raise ValueError("buffer is already detached") |
| 2273 | self.flush() |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 2274 | buffer = self._buffer |
| 2275 | self._buffer = None |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 2276 | return buffer |
| 2277 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2278 | def seek(self, cookie, whence=0): |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2279 | def _reset_encoder(position): |
| 2280 | """Reset the encoder (merely useful for proper BOM handling)""" |
| 2281 | try: |
| 2282 | encoder = self._encoder or self._get_encoder() |
| 2283 | except LookupError: |
| 2284 | # Sometimes the encoder doesn't exist |
| 2285 | pass |
| 2286 | else: |
| 2287 | if position != 0: |
| 2288 | encoder.setstate(0) |
| 2289 | else: |
| 2290 | encoder.reset() |
| 2291 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2292 | if self.closed: |
| 2293 | raise ValueError("tell on closed file") |
| 2294 | if not self._seekable: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2295 | raise UnsupportedOperation("underlying stream is not seekable") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2296 | if whence == 1: # seek relative to current position |
| 2297 | if cookie != 0: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2298 | raise UnsupportedOperation("can't do nonzero cur-relative seeks") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2299 | # Seeking to the current position should attempt to |
| 2300 | # sync the underlying buffer with the current position. |
| 2301 | whence = 0 |
| 2302 | cookie = self.tell() |
| 2303 | if whence == 2: # seek relative to end of file |
| 2304 | if cookie != 0: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2305 | raise UnsupportedOperation("can't do nonzero end-relative seeks") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2306 | self.flush() |
| 2307 | position = self.buffer.seek(0, 2) |
| 2308 | self._set_decoded_chars('') |
| 2309 | self._snapshot = None |
| 2310 | if self._decoder: |
| 2311 | self._decoder.reset() |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2312 | _reset_encoder(position) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2313 | return position |
| 2314 | if whence != 0: |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 2315 | raise ValueError("unsupported whence (%r)" % (whence,)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2316 | if cookie < 0: |
| 2317 | raise ValueError("negative seek position %r" % (cookie,)) |
| 2318 | self.flush() |
| 2319 | |
| 2320 | # The strategy of seek() is to go back to the safe start point |
| 2321 | # and replay the effect of read(chars_to_skip) from there. |
| 2322 | start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \ |
| 2323 | self._unpack_cookie(cookie) |
| 2324 | |
| 2325 | # Seek back to the safe start point. |
| 2326 | self.buffer.seek(start_pos) |
| 2327 | self._set_decoded_chars('') |
| 2328 | self._snapshot = None |
| 2329 | |
| 2330 | # Restore the decoder to its state from the safe start point. |
Benjamin Peterson | 9363a65 | 2009-03-05 00:42:09 +0000 | [diff] [blame] | 2331 | if cookie == 0 and self._decoder: |
| 2332 | self._decoder.reset() |
| 2333 | elif self._decoder or dec_flags or chars_to_skip: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2334 | self._decoder = self._decoder or self._get_decoder() |
| 2335 | self._decoder.setstate((b'', dec_flags)) |
| 2336 | self._snapshot = (dec_flags, b'') |
| 2337 | |
| 2338 | if chars_to_skip: |
| 2339 | # Just like _read_chunk, feed the decoder and save a snapshot. |
| 2340 | input_chunk = self.buffer.read(bytes_to_feed) |
| 2341 | self._set_decoded_chars( |
| 2342 | self._decoder.decode(input_chunk, need_eof)) |
| 2343 | self._snapshot = (dec_flags, input_chunk) |
| 2344 | |
| 2345 | # Skip chars_to_skip of the decoded characters. |
| 2346 | if len(self._decoded_chars) < chars_to_skip: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2347 | raise OSError("can't restore logical file position") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2348 | self._decoded_chars_used = chars_to_skip |
| 2349 | |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2350 | _reset_encoder(cookie) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2351 | return cookie |
| 2352 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2353 | def read(self, size=None): |
Benjamin Peterson | a1b4901 | 2009-03-31 23:11:32 +0000 | [diff] [blame] | 2354 | self._checkReadable() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2355 | if size is None: |
| 2356 | size = -1 |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2357 | decoder = self._decoder or self._get_decoder() |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 2358 | try: |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2359 | size.__index__ |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 2360 | except AttributeError as err: |
| 2361 | raise TypeError("an integer is required") from err |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2362 | if size < 0: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2363 | # Read everything. |
| 2364 | result = (self._get_decoded_chars() + |
| 2365 | decoder.decode(self.buffer.read(), final=True)) |
| 2366 | self._set_decoded_chars('') |
| 2367 | self._snapshot = None |
| 2368 | return result |
| 2369 | else: |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2370 | # Keep reading chunks until we have size characters to return. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2371 | eof = False |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2372 | result = self._get_decoded_chars(size) |
| 2373 | while len(result) < size and not eof: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2374 | eof = not self._read_chunk() |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2375 | result += self._get_decoded_chars(size - len(result)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2376 | return result |
| 2377 | |
| 2378 | def __next__(self): |
| 2379 | self._telling = False |
| 2380 | line = self.readline() |
| 2381 | if not line: |
| 2382 | self._snapshot = None |
| 2383 | self._telling = self._seekable |
| 2384 | raise StopIteration |
| 2385 | return line |
| 2386 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2387 | def readline(self, size=None): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2388 | if self.closed: |
| 2389 | raise ValueError("read from closed file") |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2390 | if size is None: |
| 2391 | size = -1 |
| 2392 | elif not isinstance(size, int): |
| 2393 | raise TypeError("size must be an integer") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2394 | |
| 2395 | # Grab all the decoded text (we will rewind any extra bits later). |
| 2396 | line = self._get_decoded_chars() |
| 2397 | |
| 2398 | start = 0 |
| 2399 | # Make the decoder if it doesn't already exist. |
| 2400 | if not self._decoder: |
| 2401 | self._get_decoder() |
| 2402 | |
| 2403 | pos = endpos = None |
| 2404 | while True: |
| 2405 | if self._readtranslate: |
| 2406 | # Newlines are already translated, only search for \n |
| 2407 | pos = line.find('\n', start) |
| 2408 | if pos >= 0: |
| 2409 | endpos = pos + 1 |
| 2410 | break |
| 2411 | else: |
| 2412 | start = len(line) |
| 2413 | |
| 2414 | elif self._readuniversal: |
| 2415 | # Universal newline search. Find any of \r, \r\n, \n |
| 2416 | # The decoder ensures that \r\n are not split in two pieces |
| 2417 | |
| 2418 | # In C we'd look for these in parallel of course. |
| 2419 | nlpos = line.find("\n", start) |
| 2420 | crpos = line.find("\r", start) |
| 2421 | if crpos == -1: |
| 2422 | if nlpos == -1: |
| 2423 | # Nothing found |
| 2424 | start = len(line) |
| 2425 | else: |
| 2426 | # Found \n |
| 2427 | endpos = nlpos + 1 |
| 2428 | break |
| 2429 | elif nlpos == -1: |
| 2430 | # Found lone \r |
| 2431 | endpos = crpos + 1 |
| 2432 | break |
| 2433 | elif nlpos < crpos: |
| 2434 | # Found \n |
| 2435 | endpos = nlpos + 1 |
| 2436 | break |
| 2437 | elif nlpos == crpos + 1: |
| 2438 | # Found \r\n |
| 2439 | endpos = crpos + 2 |
| 2440 | break |
| 2441 | else: |
| 2442 | # Found \r |
| 2443 | endpos = crpos + 1 |
| 2444 | break |
| 2445 | else: |
| 2446 | # non-universal |
| 2447 | pos = line.find(self._readnl) |
| 2448 | if pos >= 0: |
| 2449 | endpos = pos + len(self._readnl) |
| 2450 | break |
| 2451 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2452 | if size >= 0 and len(line) >= size: |
| 2453 | endpos = size # reached length size |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2454 | break |
| 2455 | |
| 2456 | # No line ending seen yet - get more data' |
| 2457 | while self._read_chunk(): |
| 2458 | if self._decoded_chars: |
| 2459 | break |
| 2460 | if self._decoded_chars: |
| 2461 | line += self._get_decoded_chars() |
| 2462 | else: |
| 2463 | # end of file |
| 2464 | self._set_decoded_chars('') |
| 2465 | self._snapshot = None |
| 2466 | return line |
| 2467 | |
Serhiy Storchaka | 3c41154 | 2013-09-16 23:18:10 +0300 | [diff] [blame] | 2468 | if size >= 0 and endpos > size: |
| 2469 | endpos = size # don't exceed size |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2470 | |
| 2471 | # Rewind _decoded_chars to just after the line ending we found. |
| 2472 | self._rewind_decoded_chars(len(line) - endpos) |
| 2473 | return line[:endpos] |
| 2474 | |
| 2475 | @property |
| 2476 | def newlines(self): |
| 2477 | return self._decoder.newlines if self._decoder else None |
| 2478 | |
| 2479 | |
| 2480 | class StringIO(TextIOWrapper): |
| 2481 | """Text I/O implementation using an in-memory buffer. |
| 2482 | |
| 2483 | The initial_value argument sets the value of object. The newline |
| 2484 | argument is like the one of TextIOWrapper's constructor. |
| 2485 | """ |
| 2486 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2487 | def __init__(self, initial_value="", newline="\n"): |
| 2488 | super(StringIO, self).__init__(BytesIO(), |
| 2489 | encoding="utf-8", |
Serhiy Storchaka | c92ea76 | 2014-01-29 11:33:26 +0200 | [diff] [blame] | 2490 | errors="surrogatepass", |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2491 | newline=newline) |
Antoine Pitrou | 1144648 | 2009-04-04 14:09:30 +0000 | [diff] [blame] | 2492 | # Issue #5645: make universal newlines semantics the same as in the |
| 2493 | # C version, even under Windows. |
| 2494 | if newline is None: |
| 2495 | self._writetranslate = False |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 2496 | if initial_value is not None: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2497 | if not isinstance(initial_value, str): |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 2498 | raise TypeError("initial_value must be str or None, not {0}" |
| 2499 | .format(type(initial_value).__name__)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2500 | self.write(initial_value) |
| 2501 | self.seek(0) |
| 2502 | |
| 2503 | def getvalue(self): |
| 2504 | self.flush() |
Antoine Pitrou | 57839a6 | 2014-02-02 23:37:29 +0100 | [diff] [blame] | 2505 | decoder = self._decoder or self._get_decoder() |
| 2506 | old_state = decoder.getstate() |
| 2507 | decoder.reset() |
| 2508 | try: |
| 2509 | return decoder.decode(self.buffer.getvalue(), final=True) |
| 2510 | finally: |
| 2511 | decoder.setstate(old_state) |
Benjamin Peterson | 9fd459a | 2009-03-09 00:09:44 +0000 | [diff] [blame] | 2512 | |
| 2513 | def __repr__(self): |
| 2514 | # TextIOWrapper tells the encoding in its repr. In StringIO, |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 2515 | # that's an implementation detail. |
Benjamin Peterson | 9fd459a | 2009-03-09 00:09:44 +0000 | [diff] [blame] | 2516 | return object.__repr__(self) |
Benjamin Peterson | b487e63 | 2009-03-21 03:08:31 +0000 | [diff] [blame] | 2517 | |
| 2518 | @property |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 2519 | def errors(self): |
| 2520 | return None |
| 2521 | |
| 2522 | @property |
Benjamin Peterson | b487e63 | 2009-03-21 03:08:31 +0000 | [diff] [blame] | 2523 | def encoding(self): |
| 2524 | return None |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 2525 | |
| 2526 | def detach(self): |
| 2527 | # This doesn't make sense on StringIO. |
| 2528 | self._unsupported("detach") |