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