Mercurial > cpython-lru_cache-weakref
annotate Lib/_pyio.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 77130f2fe944 |
| children | e2d41079d059 |
| rev | line source |
|---|---|
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
2 Python implementation of the io module. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
3 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
4 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
5 from __future__ import print_function |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
6 from __future__ import unicode_literals |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
7 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
8 import os |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
9 import abc |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
10 import codecs |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
11 import warnings |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
12 # Import _thread instead of threading to reduce startup cost |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
13 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
14 from thread import allocate_lock as Lock |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
15 except ImportError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
16 from dummy_thread import allocate_lock as Lock |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
17 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
18 import io |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
19 from io import __all__ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
20 from io import SEEK_SET, SEEK_CUR, SEEK_END |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
21 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
22 __metaclass__ = type |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
23 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
24 # open() uses st_blksize whenever we can |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
25 DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
26 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
27 # NOTE: Base classes defined here are registered with the "official" ABCs |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
28 # defined in io.py. We don't use real inheritance though, because we don't |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
29 # want to inherit the C implementations. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
30 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
31 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
32 class BlockingIOError(IOError): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
33 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
34 """Exception raised when I/O would block on a non-blocking I/O stream.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
35 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
36 def __init__(self, errno, strerror, characters_written=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
37 super(IOError, self).__init__(errno, strerror) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
38 if not isinstance(characters_written, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
39 raise TypeError("characters_written must be a integer") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
40 self.characters_written = characters_written |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
41 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
42 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
43 def open(file, mode="r", buffering=None, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
44 encoding=None, errors=None, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
45 newline=None, closefd=True): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
46 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
47 r"""Open file and return a stream. Raise IOError upon failure. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
48 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
49 file is either a text or byte string giving the name (and the path |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
50 if the file isn't in the current working directory) of the file to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
51 be opened or an integer file descriptor of the file to be |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
52 wrapped. (If a file descriptor is given, it is closed when the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
53 returned I/O object is closed, unless closefd is set to False.) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
54 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
55 mode is an optional string that specifies the mode in which the file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
56 is opened. It defaults to 'r' which means open for reading in text |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
57 mode. Other common values are 'w' for writing (truncating the file if |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
58 it already exists), and 'a' for appending (which on some Unix systems, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
59 means that all writes append to the end of the file regardless of the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
60 current seek position). In text mode, if encoding is not specified the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
61 encoding used is platform dependent. (For reading and writing raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
62 bytes use binary mode and leave encoding unspecified.) The available |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
63 modes are: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
64 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
65 ========= =============================================================== |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
66 Character Meaning |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
67 --------- --------------------------------------------------------------- |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
68 'r' open for reading (default) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
69 'w' open for writing, truncating the file first |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
70 'a' open for writing, appending to the end of the file if it exists |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
71 'b' binary mode |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
72 't' text mode (default) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
73 '+' open a disk file for updating (reading and writing) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
74 'U' universal newline mode (for backwards compatibility; unneeded |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
75 for new code) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
76 ========= =============================================================== |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
77 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
78 The default mode is 'rt' (open for reading text). For binary random |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
79 access, the mode 'w+b' opens and truncates the file to 0 bytes, while |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
80 'r+b' opens the file without truncation. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
81 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
82 Python distinguishes between files opened in binary and text modes, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
83 even when the underlying operating system doesn't. Files opened in |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
84 binary mode (appending 'b' to the mode argument) return contents as |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
85 bytes objects without any decoding. In text mode (the default, or when |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
86 't' is appended to the mode argument), the contents of the file are |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
87 returned as strings, the bytes having been first decoded using a |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
88 platform-dependent encoding or using the specified encoding if given. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
89 |
|
57787
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
90 buffering is an optional integer used to set the buffering policy. |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
91 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
92 line buffering (only usable in text mode), and an integer > 1 to indicate |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
93 the size of a fixed-size chunk buffer. When no buffering argument is |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
94 given, the default buffering policy works as follows: |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
95 |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
96 * Binary files are buffered in fixed-size chunks; the size of the buffer |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
97 is chosen using a heuristic trying to determine the underlying device's |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
98 "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`. |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
99 On many systems, the buffer will typically be 4096 or 8192 bytes long. |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
100 |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
101 * "Interactive" text files (files for which isatty() returns True) |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
102 use line buffering. Other text files use the policy described above |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
103 for binary files. |
|
101320899041
Issue #7545: improve documentation of the `buffering` argument in io.open().
Antoine Pitrou <solipsis@pitrou.net>
parents:
56703
diff
changeset
|
104 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
105 encoding is the name of the encoding used to decode or encode the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
106 file. This should only be used in text mode. The default encoding is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
107 platform dependent, but any encoding supported by Python can be |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
108 passed. See the codecs module for the list of supported encodings. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
109 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
110 errors is an optional string that specifies how encoding errors are to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
111 be handled---this argument should not be used in binary mode. Pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
112 'strict' to raise a ValueError exception if there is an encoding error |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
113 (the default of None has the same effect), or pass 'ignore' to ignore |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
114 errors. (Note that ignoring encoding errors can lead to data loss.) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
115 See the documentation for codecs.register for a list of the permitted |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
116 encoding error strings. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
117 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
118 newline controls how universal newlines works (it only applies to text |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
119 mode). It can be None, '', '\n', '\r', and '\r\n'. It works as |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
120 follows: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
121 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
122 * On input, if newline is None, universal newlines mode is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
123 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
124 these are translated into '\n' before being returned to the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
125 caller. If it is '', universal newline mode is enabled, but line |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
126 endings are returned to the caller untranslated. If it has any of |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
127 the other legal values, input lines are only terminated by the given |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
128 string, and the line ending is returned to the caller untranslated. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
129 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
130 * On output, if newline is None, any '\n' characters written are |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
131 translated to the system default line separator, os.linesep. If |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
132 newline is '', no translation takes place. If newline is any of the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
133 other legal values, any '\n' characters written are translated to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
134 the given string. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
135 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
136 If closefd is False, the underlying file descriptor will be kept open |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
137 when the file is closed. This does not work when a file name is given |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
138 and must be True in that case. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
139 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
140 open() returns a file object whose type depends on the mode, and |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
141 through which the standard file operations such as reading and writing |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
142 are performed. When open() is used to open a file in a text mode ('w', |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
143 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
144 a file in a binary mode, the returned class varies: in read binary |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
145 mode, it returns a BufferedReader; in write binary and append binary |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
146 modes, it returns a BufferedWriter, and in read/write mode, it returns |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
147 a BufferedRandom. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
148 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
149 It is also possible to use a string or bytearray as a file for both |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
150 reading and writing. For strings StringIO can be used like a file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
151 opened in a text mode, and for bytes a BytesIO can be used like a file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
152 opened in a binary mode. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
153 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
154 if not isinstance(file, (basestring, int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
155 raise TypeError("invalid file: %r" % file) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
156 if not isinstance(mode, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
157 raise TypeError("invalid mode: %r" % mode) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
158 if buffering is not None and not isinstance(buffering, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
159 raise TypeError("invalid buffering: %r" % buffering) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
160 if encoding is not None and not isinstance(encoding, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
161 raise TypeError("invalid encoding: %r" % encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
162 if errors is not None and not isinstance(errors, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
163 raise TypeError("invalid errors: %r" % errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
164 modes = set(mode) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
165 if modes - set("arwb+tU") or len(mode) > len(modes): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
166 raise ValueError("invalid mode: %r" % mode) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
167 reading = "r" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
168 writing = "w" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
169 appending = "a" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
170 updating = "+" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
171 text = "t" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
172 binary = "b" in modes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
173 if "U" in modes: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
174 if writing or appending: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
175 raise ValueError("can't use U and writing mode at once") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
176 reading = True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
177 if text and binary: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
178 raise ValueError("can't have text and binary mode at once") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
179 if reading + writing + appending > 1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
180 raise ValueError("can't have read/write/append mode at once") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
181 if not (reading or writing or appending): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
182 raise ValueError("must have exactly one of read/write/append mode") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
183 if binary and encoding is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
184 raise ValueError("binary mode doesn't take an encoding argument") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
185 if binary and errors is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
186 raise ValueError("binary mode doesn't take an errors argument") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
187 if binary and newline is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
188 raise ValueError("binary mode doesn't take a newline argument") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
189 raw = FileIO(file, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
190 (reading and "r" or "") + |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
191 (writing and "w" or "") + |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
192 (appending and "a" or "") + |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
193 (updating and "+" or ""), |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
194 closefd) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
195 if buffering is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
196 buffering = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
197 line_buffering = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
198 if buffering == 1 or buffering < 0 and raw.isatty(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
199 buffering = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
200 line_buffering = True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
201 if buffering < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
202 buffering = DEFAULT_BUFFER_SIZE |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
203 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
204 bs = os.fstat(raw.fileno()).st_blksize |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
205 except (os.error, AttributeError): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
206 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
207 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
208 if bs > 1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
209 buffering = bs |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
210 if buffering < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
211 raise ValueError("invalid buffering size") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
212 if buffering == 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
213 if binary: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
214 return raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
215 raise ValueError("can't have unbuffered text I/O") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
216 if updating: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
217 buffer = BufferedRandom(raw, buffering) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
218 elif writing or appending: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
219 buffer = BufferedWriter(raw, buffering) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
220 elif reading: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
221 buffer = BufferedReader(raw, buffering) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
222 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
223 raise ValueError("unknown mode: %r" % mode) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
224 if binary: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
225 return buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
226 text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
227 text.mode = mode |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
228 return text |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
229 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
230 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
231 class DocDescriptor: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
232 """Helper for builtins.open.__doc__ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
233 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
234 def __get__(self, obj, typ): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
235 return ( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
236 "open(file, mode='r', buffering=None, encoding=None, " |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
237 "errors=None, newline=None, closefd=True)\n\n" + |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
238 open.__doc__) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
239 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
240 class OpenWrapper: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
241 """Wrapper for builtins.open |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
242 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
243 Trick so that open won't become a bound method when stored |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
244 as a class variable (as dbm.dumb does). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
245 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
246 See initstdio() in Python/pythonrun.c. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
247 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
248 __doc__ = DocDescriptor() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
249 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
250 def __new__(cls, *args, **kwargs): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
251 return open(*args, **kwargs) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
252 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
253 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
254 class UnsupportedOperation(ValueError, IOError): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
255 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
256 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
257 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
258 class IOBase: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
259 __metaclass__ = abc.ABCMeta |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
260 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
261 """The abstract base class for all I/O classes, acting on streams of |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
262 bytes. There is no public constructor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
263 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
264 This class provides dummy implementations for many methods that |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
265 derived classes can override selectively; the default implementations |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
266 represent a file that cannot be read, written or seeked. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
267 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
268 Even though IOBase does not declare read, readinto, or write because |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
269 their signatures will vary, implementations and clients should |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
270 consider those methods part of the interface. Also, implementations |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
271 may raise a IOError when operations they do not support are called. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
272 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
273 The basic type used for binary data read from or written to a file is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
274 bytes. bytearrays are accepted too, and in some cases (such as |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
275 readinto) needed. Text I/O classes work with str data. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
276 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
277 Note that calling any method (even inquiries) on a closed stream is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
278 undefined. Implementations may raise IOError in this case. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
279 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
280 IOBase (and its subclasses) support the iterator protocol, meaning |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
281 that an IOBase object can be iterated over yielding the lines in a |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
282 stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
283 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
284 IOBase also supports the :keyword:`with` statement. In this example, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
285 fp is closed after the suite of the with statement is complete: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
286 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
287 with open('spam.txt', 'r') as fp: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
288 fp.write('Spam and eggs!') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
289 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
290 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
291 ### Internal ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
292 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
293 def _unsupported(self, name): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
294 """Internal: raise an exception for unsupported operations.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
295 raise UnsupportedOperation("%s.%s() not supported" % |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
296 (self.__class__.__name__, name)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
297 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
298 ### Positioning ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
299 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
300 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
301 """Change stream position. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
302 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
303 Change the stream position to byte offset offset. offset is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
304 interpreted relative to the position indicated by whence. Values |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
305 for whence are: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
306 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
307 * 0 -- start of stream (the default); offset should be zero or positive |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
308 * 1 -- current stream position; offset may be negative |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
309 * 2 -- end of stream; offset is usually negative |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
310 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
311 Return the new absolute position. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
312 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
313 self._unsupported("seek") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
314 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
315 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
316 """Return current stream position.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
317 return self.seek(0, 1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
318 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
319 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
320 """Truncate file to size bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
321 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
322 Size defaults to the current IO position as reported by tell(). Return |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
323 the new size. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
324 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
325 self._unsupported("truncate") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
326 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
327 ### Flush and close ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
328 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
329 def flush(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
330 """Flush write buffers, if applicable. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
331 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
332 This is not implemented for read-only and non-blocking streams. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
333 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
334 # XXX Should this return the number of bytes written??? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
335 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
336 __closed = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
337 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
338 def close(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
339 """Flush and close the IO object. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
340 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
341 This method has no effect if the file is already closed. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
342 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
343 if not self.__closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
344 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
345 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
346 except IOError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
347 pass # If flush() fails, just give up |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
348 self.__closed = True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
349 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
350 def __del__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
351 """Destructor. Calls close().""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
352 # The try/except block is in case this is called at program |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
353 # exit time, when it's possible that globals have already been |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
354 # deleted, and then the close() call might fail. Since |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
355 # there's nothing we can do about such failures and they annoy |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
356 # the end users, we suppress the traceback. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
357 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
358 self.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
359 except: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
360 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
361 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
362 ### Inquiries ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
363 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
364 def seekable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
365 """Return whether object supports random access. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
366 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
367 If False, seek(), tell() and truncate() will raise IOError. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
368 This method may need to do a test seek(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
369 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
370 return False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
371 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
372 def _checkSeekable(self, msg=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
373 """Internal: raise an IOError if file is not seekable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
374 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
375 if not self.seekable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
376 raise IOError("File or stream is not seekable." |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
377 if msg is None else msg) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
378 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
379 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
380 def readable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
381 """Return whether object was opened for reading. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
382 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
383 If False, read() will raise IOError. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
384 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
385 return False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
386 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
387 def _checkReadable(self, msg=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
388 """Internal: raise an IOError if file is not readable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
389 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
390 if not self.readable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
391 raise IOError("File or stream is not readable." |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
392 if msg is None else msg) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
393 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
394 def writable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
395 """Return whether object was opened for writing. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
396 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
397 If False, write() and truncate() will raise IOError. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
398 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
399 return False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
400 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
401 def _checkWritable(self, msg=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
402 """Internal: raise an IOError if file is not writable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
403 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
404 if not self.writable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
405 raise IOError("File or stream is not writable." |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
406 if msg is None else msg) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
407 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
408 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
409 def closed(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
410 """closed: bool. True iff the file has been closed. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
411 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
412 For backwards compatibility, this is a property, not a predicate. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
413 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
414 return self.__closed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
415 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
416 def _checkClosed(self, msg=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
417 """Internal: raise an ValueError if file is closed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
418 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
419 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
420 raise ValueError("I/O operation on closed file." |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
421 if msg is None else msg) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
422 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
423 ### Context manager ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
424 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
425 def __enter__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
426 """Context management protocol. Returns self.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
427 self._checkClosed() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
428 return self |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
429 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
430 def __exit__(self, *args): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
431 """Context management protocol. Calls close()""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
432 self.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
433 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
434 ### Lower-level APIs ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
435 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
436 # XXX Should these be present even if unimplemented? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
437 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
438 def fileno(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
439 """Returns underlying file descriptor if one exists. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
440 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
441 An IOError is raised if the IO object does not use a file descriptor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
442 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
443 self._unsupported("fileno") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
444 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
445 def isatty(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
446 """Return whether this is an 'interactive' stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
447 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
448 Return False if it can't be determined. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
449 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
450 self._checkClosed() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
451 return False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
452 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
453 ### Readline[s] and writelines ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
454 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
455 def readline(self, limit=-1): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
456 r"""Read and return a line from the stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
457 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
458 If limit is specified, at most limit bytes will be read. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
459 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
460 The line terminator is always b'\n' for binary files; for text |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
461 files, the newlines argument to open can be used to select the line |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
462 terminator(s) recognized. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
463 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
464 # For backwards compatibility, a (slowish) readline(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
465 if hasattr(self, "peek"): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
466 def nreadahead(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
467 readahead = self.peek(1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
468 if not readahead: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
469 return 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
470 n = (readahead.find(b"\n") + 1) or len(readahead) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
471 if limit >= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
472 n = min(n, limit) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
473 return n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
474 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
475 def nreadahead(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
476 return 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
477 if limit is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
478 limit = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
479 elif not isinstance(limit, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
480 raise TypeError("limit must be an integer") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
481 res = bytearray() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
482 while limit < 0 or len(res) < limit: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
483 b = self.read(nreadahead()) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
484 if not b: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
485 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
486 res += b |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
487 if res.endswith(b"\n"): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
488 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
489 return bytes(res) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
490 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
491 def __iter__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
492 self._checkClosed() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
493 return self |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
494 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
495 def next(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
496 line = self.readline() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
497 if not line: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
498 raise StopIteration |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
499 return line |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
500 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
501 def readlines(self, hint=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
502 """Return a list of lines from the stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
503 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
504 hint can be specified to control the number of lines read: no more |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
505 lines will be read if the total size (in bytes/characters) of all |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
506 lines so far exceeds hint. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
507 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
508 if hint is not None and not isinstance(hint, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
509 raise TypeError("integer or None expected") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
510 if hint is None or hint <= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
511 return list(self) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
512 n = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
513 lines = [] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
514 for line in self: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
515 lines.append(line) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
516 n += len(line) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
517 if n >= hint: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
518 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
519 return lines |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
520 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
521 def writelines(self, lines): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
522 self._checkClosed() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
523 for line in lines: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
524 self.write(line) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
525 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
526 io.IOBase.register(IOBase) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
527 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
528 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
529 class RawIOBase(IOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
530 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
531 """Base class for raw binary I/O.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
532 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
533 # The read() method is implemented by calling readinto(); derived |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
534 # classes that want to support read() only need to implement |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
535 # readinto() as a primitive operation. In general, readinto() can be |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
536 # more efficient than read(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
537 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
538 # (It would be tempting to also provide an implementation of |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
539 # readinto() in terms of read(), in case the latter is a more suitable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
540 # primitive operation, but that would lead to nasty recursion in case |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
541 # a subclass doesn't implement either.) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
542 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
543 def read(self, n=-1): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
544 """Read and return up to n bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
545 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
546 Returns an empty bytes object on EOF, or None if the object is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
547 set not to block and has no data to read. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
548 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
549 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
550 n = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
551 if n < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
552 return self.readall() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
553 b = bytearray(n.__index__()) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
554 n = self.readinto(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
555 del b[n:] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
556 return bytes(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
557 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
558 def readall(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
559 """Read until EOF, using multiple read() call.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
560 res = bytearray() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
561 while True: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
562 data = self.read(DEFAULT_BUFFER_SIZE) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
563 if not data: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
564 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
565 res += data |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
566 return bytes(res) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
567 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
568 def readinto(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
569 """Read up to len(b) bytes into b. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
570 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
571 Returns number of bytes read (0 for EOF), or None if the object |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
572 is set not to block as has no data to read. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
573 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
574 self._unsupported("readinto") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
575 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
576 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
577 """Write the given buffer to the IO stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
578 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
579 Returns the number of bytes written, which may be less than len(b). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
580 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
581 self._unsupported("write") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
582 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
583 io.RawIOBase.register(RawIOBase) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
584 from _io import FileIO |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
585 RawIOBase.register(FileIO) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
586 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
587 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
588 class BufferedIOBase(IOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
589 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
590 """Base class for buffered IO objects. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
591 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
592 The main difference with RawIOBase is that the read() method |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
593 supports omitting the size argument, and does not have a default |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
594 implementation that defers to readinto(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
595 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
596 In addition, read(), readinto() and write() may raise |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
597 BlockingIOError if the underlying raw stream is in non-blocking |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
598 mode and not ready; unlike their raw counterparts, they will never |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
599 return None. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
600 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
601 A typical implementation should not inherit from a RawIOBase |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
602 implementation, but wrap one. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
603 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
604 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
605 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
606 """Read and return up to n bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
607 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
608 If the argument is omitted, None, or negative, reads and |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
609 returns all data until EOF. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
610 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
611 If the argument is positive, and the underlying raw stream is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
612 not 'interactive', multiple raw reads may be issued to satisfy |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
613 the byte count (unless EOF is reached first). But for |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
614 interactive raw streams (XXX and for pipes?), at most one raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
615 read will be issued, and a short result does not imply that |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
616 EOF is imminent. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
617 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
618 Returns an empty bytes array on EOF. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
619 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
620 Raises BlockingIOError if the underlying raw stream has no |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
621 data at the moment. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
622 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
623 self._unsupported("read") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
624 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
625 def read1(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
626 """Read up to n bytes with at most one read() system call.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
627 self._unsupported("read1") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
628 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
629 def readinto(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
630 """Read up to len(b) bytes into b. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
631 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
632 Like read(), this may issue multiple reads to the underlying raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
633 stream, unless the latter is 'interactive'. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
634 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
635 Returns the number of bytes read (0 for EOF). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
636 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
637 Raises BlockingIOError if the underlying raw stream has no |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
638 data at the moment. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
639 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
640 # XXX This ought to work with anything that supports the buffer API |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
641 data = self.read(len(b)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
642 n = len(data) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
643 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
644 b[:n] = data |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
645 except TypeError as err: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
646 import array |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
647 if not isinstance(b, array.array): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
648 raise err |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
649 b[:n] = array.array(b'b', data) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
650 return n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
651 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
652 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
653 """Write the given buffer to the IO stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
654 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
655 Return the number of bytes written, which is never less than |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
656 len(b). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
657 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
658 Raises BlockingIOError if the buffer is full and the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
659 underlying raw stream cannot accept more data at the moment. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
660 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
661 self._unsupported("write") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
662 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
663 def detach(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
664 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
665 Separate the underlying raw stream from the buffer and return it. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
666 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
667 After the raw stream has been detached, the buffer is in an unusable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
668 state. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
669 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
670 self._unsupported("detach") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
671 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
672 io.BufferedIOBase.register(BufferedIOBase) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
673 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
674 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
675 class _BufferedIOMixin(BufferedIOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
676 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
677 """A mixin implementation of BufferedIOBase with an underlying raw stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
678 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
679 This passes most requests on to the underlying raw stream. It |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
680 does *not* provide implementations of read(), readinto() or |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
681 write(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
682 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
683 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
684 def __init__(self, raw): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
685 self.raw = raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
686 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
687 ### Positioning ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
688 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
689 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
690 new_position = self.raw.seek(pos, whence) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
691 if new_position < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
692 raise IOError("seek() returned an invalid position") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
693 return new_position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
694 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
695 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
696 pos = self.raw.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
697 if pos < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
698 raise IOError("tell() returned an invalid position") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
699 return pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
700 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
701 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
702 # Flush the stream. We're mixing buffered I/O with lower-level I/O, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
703 # and a flush may be necessary to synch both views of the current |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
704 # file state. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
705 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
706 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
707 if pos is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
708 pos = self.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
709 # XXX: Should seek() be used, instead of passing the position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
710 # XXX directly to truncate? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
711 return self.raw.truncate(pos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
712 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
713 ### Flush and close ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
714 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
715 def flush(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
716 self.raw.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
717 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
718 def close(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
719 if not self.closed and self.raw is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
720 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
721 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
722 except IOError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
723 pass # If flush() fails, just give up |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
724 self.raw.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
725 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
726 def detach(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
727 if self.raw is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
728 raise ValueError("raw stream already detached") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
729 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
730 raw = self.raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
731 self.raw = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
732 return raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
733 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
734 ### Inquiries ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
735 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
736 def seekable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
737 return self.raw.seekable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
738 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
739 def readable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
740 return self.raw.readable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
741 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
742 def writable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
743 return self.raw.writable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
744 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
745 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
746 def closed(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
747 return self.raw.closed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
748 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
749 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
750 def name(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
751 return self.raw.name |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
752 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
753 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
754 def mode(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
755 return self.raw.mode |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
756 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
757 def __repr__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
758 clsname = self.__class__.__name__ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
759 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
760 name = self.name |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
761 except AttributeError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
762 return "<_pyio.{0}>".format(clsname) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
763 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
764 return "<_pyio.{0} name={1!r}>".format(clsname, name) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
765 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
766 ### Lower-level APIs ### |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
767 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
768 def fileno(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
769 return self.raw.fileno() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
770 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
771 def isatty(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
772 return self.raw.isatty() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
773 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
774 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
775 class BytesIO(BufferedIOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
776 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
777 """Buffered I/O implementation using an in-memory bytes buffer.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
778 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
779 def __init__(self, initial_bytes=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
780 buf = bytearray() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
781 if initial_bytes is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
782 buf.extend(initial_bytes) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
783 self._buffer = buf |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
784 self._pos = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
785 |
|
56703
264609fbda48
Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO picklable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
786 def __getstate__(self): |
|
264609fbda48
Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO picklable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
787 if self.closed: |
|
264609fbda48
Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO picklable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
788 raise ValueError("__getstate__ on closed file") |
|
264609fbda48
Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO picklable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
789 return self.__dict__.copy() |
|
264609fbda48
Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO picklable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
54717
diff
changeset
|
790 |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
791 def getvalue(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
792 """Return the bytes value (contents) of the buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
793 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
794 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
795 raise ValueError("getvalue on closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
796 return bytes(self._buffer) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
797 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
798 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
799 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
800 raise ValueError("read from closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
801 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
802 n = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
803 if not isinstance(n, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
804 raise TypeError("integer argument expected, got {0!r}".format( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
805 type(n))) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
806 if n < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
807 n = len(self._buffer) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
808 if len(self._buffer) <= self._pos: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
809 return b"" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
810 newpos = min(len(self._buffer), self._pos + n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
811 b = self._buffer[self._pos : newpos] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
812 self._pos = newpos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
813 return bytes(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
814 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
815 def read1(self, n): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
816 """This is the same as read. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
817 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
818 return self.read(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
819 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
820 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
821 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
822 raise ValueError("write to closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
823 if isinstance(b, unicode): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
824 raise TypeError("can't write unicode to binary stream") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
825 n = len(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
826 if n == 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
827 return 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
828 pos = self._pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
829 if pos > len(self._buffer): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
830 # Inserts null bytes between the current end of the file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
831 # and the new write position. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
832 padding = b'\x00' * (pos - len(self._buffer)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
833 self._buffer += padding |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
834 self._buffer[pos:pos + n] = b |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
835 self._pos += n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
836 return n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
837 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
838 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
839 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
840 raise ValueError("seek on closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
841 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
842 pos = pos.__index__() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
843 except AttributeError as err: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
844 raise TypeError("an integer is required") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
845 if whence == 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
846 if pos < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
847 raise ValueError("negative seek position %r" % (pos,)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
848 self._pos = pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
849 elif whence == 1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
850 self._pos = max(0, self._pos + pos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
851 elif whence == 2: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
852 self._pos = max(0, len(self._buffer) + pos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
853 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
854 raise ValueError("invalid whence value") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
855 return self._pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
856 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
857 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
858 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
859 raise ValueError("tell on closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
860 return self._pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
861 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
862 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
863 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
864 raise ValueError("truncate on closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
865 if pos is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
866 pos = self._pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
867 elif pos < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
868 raise ValueError("negative truncate position %r" % (pos,)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
869 del self._buffer[pos:] |
|
58624
77130f2fe944
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents:
57789
diff
changeset
|
870 return pos |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
871 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
872 def readable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
873 return True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
874 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
875 def writable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
876 return True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
877 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
878 def seekable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
879 return True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
880 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
881 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
882 class BufferedReader(_BufferedIOMixin): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
883 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
884 """BufferedReader(raw[, buffer_size]) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
885 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
886 A buffer for a readable, sequential BaseRawIO object. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
887 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
888 The constructor creates a BufferedReader for the given readable raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
889 stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
890 is used. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
891 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
892 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
893 def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
894 """Create a new buffered reader using the given readable raw IO object. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
895 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
896 if not raw.readable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
897 raise IOError('"raw" argument must be readable.') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
898 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
899 _BufferedIOMixin.__init__(self, raw) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
900 if buffer_size <= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
901 raise ValueError("invalid buffer size") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
902 self.buffer_size = buffer_size |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
903 self._reset_read_buf() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
904 self._read_lock = Lock() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
905 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
906 def _reset_read_buf(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
907 self._read_buf = b"" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
908 self._read_pos = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
909 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
910 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
911 """Read n bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
912 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
913 Returns exactly n bytes of data unless the underlying raw IO |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
914 stream reaches EOF or if the call would block in non-blocking |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
915 mode. If n is negative, read until EOF or until read() would |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
916 block. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
917 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
918 if n is not None and n < -1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
919 raise ValueError("invalid number of bytes to read") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
920 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
921 return self._read_unlocked(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
922 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
923 def _read_unlocked(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
924 nodata_val = b"" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
925 empty_values = (b"", None) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
926 buf = self._read_buf |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
927 pos = self._read_pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
928 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
929 # Special case for when the number of bytes to read is unspecified. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
930 if n is None or n == -1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
931 self._reset_read_buf() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
932 chunks = [buf[pos:]] # Strip the consumed bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
933 current_size = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
934 while True: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
935 # Read until EOF or until read() would block. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
936 chunk = self.raw.read() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
937 if chunk in empty_values: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
938 nodata_val = chunk |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
939 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
940 current_size += len(chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
941 chunks.append(chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
942 return b"".join(chunks) or nodata_val |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
943 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
944 # The number of bytes to read is specified, return at most n bytes. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
945 avail = len(buf) - pos # Length of the available buffered data. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
946 if n <= avail: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
947 # Fast path: the data to read is fully buffered. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
948 self._read_pos += n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
949 return buf[pos:pos+n] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
950 # Slow path: read from the stream until enough bytes are read, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
951 # or until an EOF occurs or until read() would block. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
952 chunks = [buf[pos:]] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
953 wanted = max(self.buffer_size, n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
954 while avail < n: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
955 chunk = self.raw.read(wanted) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
956 if chunk in empty_values: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
957 nodata_val = chunk |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
958 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
959 avail += len(chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
960 chunks.append(chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
961 # n is more then avail only when an EOF occurred or when |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
962 # read() would have blocked. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
963 n = min(n, avail) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
964 out = b"".join(chunks) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
965 self._read_buf = out[n:] # Save the extra data in the buffer. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
966 self._read_pos = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
967 return out[:n] if out else nodata_val |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
968 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
969 def peek(self, n=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
970 """Returns buffered bytes without advancing the position. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
971 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
972 The argument indicates a desired minimal number of bytes; we |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
973 do at most one raw read to satisfy it. We never return more |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
974 than self.buffer_size. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
975 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
976 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
977 return self._peek_unlocked(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
978 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
979 def _peek_unlocked(self, n=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
980 want = min(n, self.buffer_size) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
981 have = len(self._read_buf) - self._read_pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
982 if have < want or have <= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
983 to_read = self.buffer_size - have |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
984 current = self.raw.read(to_read) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
985 if current: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
986 self._read_buf = self._read_buf[self._read_pos:] + current |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
987 self._read_pos = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
988 return self._read_buf[self._read_pos:] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
989 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
990 def read1(self, n): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
991 """Reads up to n bytes, with at most one read() system call.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
992 # Returns up to n bytes. If at least one byte is buffered, we |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
993 # only return buffered bytes. Otherwise, we do one raw read. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
994 if n < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
995 raise ValueError("number of bytes to read must be positive") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
996 if n == 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
997 return b"" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
998 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
999 self._peek_unlocked(1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1000 return self._read_unlocked( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1001 min(n, len(self._read_buf) - self._read_pos)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1002 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1003 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1004 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1005 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1006 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1007 if not (0 <= whence <= 2): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1008 raise ValueError("invalid whence value") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1009 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1010 if whence == 1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1011 pos -= len(self._read_buf) - self._read_pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1012 pos = _BufferedIOMixin.seek(self, pos, whence) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1013 self._reset_read_buf() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1014 return pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1015 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1016 class BufferedWriter(_BufferedIOMixin): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1017 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1018 """A buffer for a writeable sequential RawIO object. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1019 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1020 The constructor creates a BufferedWriter for the given writeable raw |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1021 stream. If the buffer_size is not given, it defaults to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1022 DEFAULT_BUFFER_SIZE. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1023 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1024 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1025 _warning_stack_offset = 2 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1026 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1027 def __init__(self, raw, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1028 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1029 if not raw.writable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1030 raise IOError('"raw" argument must be writable.') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1031 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1032 _BufferedIOMixin.__init__(self, raw) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1033 if buffer_size <= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1034 raise ValueError("invalid buffer size") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1035 if max_buffer_size is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1036 warnings.warn("max_buffer_size is deprecated", DeprecationWarning, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1037 self._warning_stack_offset) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1038 self.buffer_size = buffer_size |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1039 self._write_buf = bytearray() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1040 self._write_lock = Lock() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1041 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1042 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1043 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1044 raise ValueError("write to closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1045 if isinstance(b, unicode): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1046 raise TypeError("can't write unicode to binary stream") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1047 with self._write_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1048 # XXX we can implement some more tricks to try and avoid |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1049 # partial writes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1050 if len(self._write_buf) > self.buffer_size: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1051 # We're full, so let's pre-flush the buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1052 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1053 self._flush_unlocked() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1054 except BlockingIOError as e: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1055 # We can't accept anything else. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1056 # XXX Why not just let the exception pass through? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1057 raise BlockingIOError(e.errno, e.strerror, 0) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1058 before = len(self._write_buf) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1059 self._write_buf.extend(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1060 written = len(self._write_buf) - before |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1061 if len(self._write_buf) > self.buffer_size: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1062 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1063 self._flush_unlocked() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1064 except BlockingIOError as e: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1065 if len(self._write_buf) > self.buffer_size: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1066 # We've hit the buffer_size. We have to accept a partial |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1067 # write and cut back our buffer. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1068 overage = len(self._write_buf) - self.buffer_size |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1069 written -= overage |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1070 self._write_buf = self._write_buf[:self.buffer_size] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1071 raise BlockingIOError(e.errno, e.strerror, written) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1072 return written |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1073 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1074 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1075 with self._write_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1076 self._flush_unlocked() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1077 if pos is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1078 pos = self.raw.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1079 return self.raw.truncate(pos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1080 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1081 def flush(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1082 with self._write_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1083 self._flush_unlocked() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1084 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1085 def _flush_unlocked(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1086 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1087 raise ValueError("flush of closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1088 written = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1089 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1090 while self._write_buf: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1091 n = self.raw.write(self._write_buf) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1092 if n > len(self._write_buf) or n < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1093 raise IOError("write() returned incorrect number of bytes") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1094 del self._write_buf[:n] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1095 written += n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1096 except BlockingIOError as e: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1097 n = e.characters_written |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1098 del self._write_buf[:n] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1099 written += n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1100 raise BlockingIOError(e.errno, e.strerror, written) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1101 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1102 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1103 return _BufferedIOMixin.tell(self) + len(self._write_buf) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1104 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1105 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1106 if not (0 <= whence <= 2): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1107 raise ValueError("invalid whence") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1108 with self._write_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1109 self._flush_unlocked() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1110 return _BufferedIOMixin.seek(self, pos, whence) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1111 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1112 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1113 class BufferedRWPair(BufferedIOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1114 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1115 """A buffered reader and writer object together. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1116 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1117 A buffered reader object and buffered writer object put together to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1118 form a sequential IO object that can read and write. This is typically |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1119 used with a socket or two-way pipe. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1120 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1121 reader and writer are RawIOBase objects that are readable and |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1122 writeable respectively. If the buffer_size is omitted it defaults to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1123 DEFAULT_BUFFER_SIZE. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1124 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1125 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1126 # XXX The usefulness of this (compared to having two separate IO |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1127 # objects) is questionable. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1128 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1129 def __init__(self, reader, writer, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1130 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1131 """Constructor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1132 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1133 The arguments are two RawIO instances. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1134 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1135 if max_buffer_size is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1136 warnings.warn("max_buffer_size is deprecated", DeprecationWarning, 2) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1137 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1138 if not reader.readable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1139 raise IOError('"reader" argument must be readable.') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1140 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1141 if not writer.writable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1142 raise IOError('"writer" argument must be writable.') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1143 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1144 self.reader = BufferedReader(reader, buffer_size) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1145 self.writer = BufferedWriter(writer, buffer_size) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1146 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1147 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1148 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1149 n = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1150 return self.reader.read(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1151 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1152 def readinto(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1153 return self.reader.readinto(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1154 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1155 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1156 return self.writer.write(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1157 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1158 def peek(self, n=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1159 return self.reader.peek(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1160 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1161 def read1(self, n): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1162 return self.reader.read1(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1163 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1164 def readable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1165 return self.reader.readable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1166 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1167 def writable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1168 return self.writer.writable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1169 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1170 def flush(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1171 return self.writer.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1172 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1173 def close(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1174 self.writer.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1175 self.reader.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1176 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1177 def isatty(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1178 return self.reader.isatty() or self.writer.isatty() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1179 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1180 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1181 def closed(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1182 return self.writer.closed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1183 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1184 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1185 class BufferedRandom(BufferedWriter, BufferedReader): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1186 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1187 """A buffered interface to random access streams. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1188 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1189 The constructor creates a reader and writer for a seekable stream, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1190 raw, given in the first argument. If the buffer_size is omitted it |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1191 defaults to DEFAULT_BUFFER_SIZE. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1192 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1193 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1194 _warning_stack_offset = 3 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1195 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1196 def __init__(self, raw, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1197 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1198 raw._checkSeekable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1199 BufferedReader.__init__(self, raw, buffer_size) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1200 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1201 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1202 def seek(self, pos, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1203 if not (0 <= whence <= 2): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1204 raise ValueError("invalid whence") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1205 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1206 if self._read_buf: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1207 # Undo read ahead. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1208 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1209 self.raw.seek(self._read_pos - len(self._read_buf), 1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1210 # First do the raw seek, then empty the read buffer, so that |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1211 # if the raw seek fails, we don't lose buffered data forever. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1212 pos = self.raw.seek(pos, whence) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1213 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1214 self._reset_read_buf() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1215 if pos < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1216 raise IOError("seek() returned invalid position") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1217 return pos |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1218 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1219 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1220 if self._write_buf: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1221 return BufferedWriter.tell(self) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1222 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1223 return BufferedReader.tell(self) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1224 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1225 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1226 if pos is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1227 pos = self.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1228 # Use seek to flush the read buffer. |
|
58624
77130f2fe944
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents:
57789
diff
changeset
|
1229 return BufferedWriter.truncate(self, pos) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1230 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1231 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1232 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1233 n = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1234 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1235 return BufferedReader.read(self, n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1236 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1237 def readinto(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1238 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1239 return BufferedReader.readinto(self, b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1240 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1241 def peek(self, n=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1242 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1243 return BufferedReader.peek(self, n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1244 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1245 def read1(self, n): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1246 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1247 return BufferedReader.read1(self, n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1248 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1249 def write(self, b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1250 if self._read_buf: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1251 # Undo readahead |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1252 with self._read_lock: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1253 self.raw.seek(self._read_pos - len(self._read_buf), 1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1254 self._reset_read_buf() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1255 return BufferedWriter.write(self, b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1256 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1257 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1258 class TextIOBase(IOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1259 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1260 """Base class for text I/O. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1261 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1262 This class provides a character and line based interface to stream |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1263 I/O. There is no readinto method because Python's character strings |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1264 are immutable. There is no public constructor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1265 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1266 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1267 def read(self, n=-1): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1268 """Read at most n characters from stream. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1269 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1270 Read from underlying buffer until we have n characters or we hit EOF. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1271 If n is negative or omitted, read until EOF. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1272 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1273 self._unsupported("read") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1274 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1275 def write(self, s): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1276 """Write string s to stream.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1277 self._unsupported("write") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1278 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1279 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1280 """Truncate size to pos.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1281 self._unsupported("truncate") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1282 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1283 def readline(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1284 """Read until newline or EOF. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1285 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1286 Returns an empty string if EOF is hit immediately. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1287 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1288 self._unsupported("readline") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1289 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1290 def detach(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1291 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1292 Separate the underlying buffer from the TextIOBase and return it. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1293 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1294 After the underlying buffer has been detached, the TextIO is in an |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1295 unusable state. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1296 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1297 self._unsupported("detach") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1298 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1299 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1300 def encoding(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1301 """Subclasses should override.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1302 return None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1303 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1304 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1305 def newlines(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1306 """Line endings translated so far. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1307 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1308 Only line endings translated during reading are considered. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1309 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1310 Subclasses should override. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1311 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1312 return None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1313 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1314 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1315 def errors(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1316 """Error setting of the decoder or encoder. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1317 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1318 Subclasses should override.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1319 return None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1320 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1321 io.TextIOBase.register(TextIOBase) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1322 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1323 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1324 class IncrementalNewlineDecoder(codecs.IncrementalDecoder): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1325 r"""Codec used when reading a file in universal newlines mode. It wraps |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1326 another incremental decoder, translating \r\n and \r into \n. It also |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1327 records the types of newlines encountered. When used with |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1328 translate=False, it ensures that the newline sequence is returned in |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1329 one piece. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1330 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1331 def __init__(self, decoder, translate, errors='strict'): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1332 codecs.IncrementalDecoder.__init__(self, errors=errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1333 self.translate = translate |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1334 self.decoder = decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1335 self.seennl = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1336 self.pendingcr = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1337 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1338 def decode(self, input, final=False): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1339 # decode input (with the eventual \r from a previous pass) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1340 if self.decoder is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1341 output = input |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1342 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1343 output = self.decoder.decode(input, final=final) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1344 if self.pendingcr and (output or final): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1345 output = "\r" + output |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1346 self.pendingcr = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1347 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1348 # retain last \r even when not translating data: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1349 # then readline() is sure to get \r\n in one pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1350 if output.endswith("\r") and not final: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1351 output = output[:-1] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1352 self.pendingcr = True |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1353 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1354 # Record which newlines are read |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1355 crlf = output.count('\r\n') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1356 cr = output.count('\r') - crlf |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1357 lf = output.count('\n') - crlf |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1358 self.seennl |= (lf and self._LF) | (cr and self._CR) \ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1359 | (crlf and self._CRLF) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1360 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1361 if self.translate: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1362 if crlf: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1363 output = output.replace("\r\n", "\n") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1364 if cr: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1365 output = output.replace("\r", "\n") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1366 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1367 return output |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1368 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1369 def getstate(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1370 if self.decoder is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1371 buf = b"" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1372 flag = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1373 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1374 buf, flag = self.decoder.getstate() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1375 flag <<= 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1376 if self.pendingcr: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1377 flag |= 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1378 return buf, flag |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1379 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1380 def setstate(self, state): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1381 buf, flag = state |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1382 self.pendingcr = bool(flag & 1) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1383 if self.decoder is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1384 self.decoder.setstate((buf, flag >> 1)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1385 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1386 def reset(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1387 self.seennl = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1388 self.pendingcr = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1389 if self.decoder is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1390 self.decoder.reset() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1391 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1392 _LF = 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1393 _CR = 2 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1394 _CRLF = 4 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1395 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1396 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1397 def newlines(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1398 return (None, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1399 "\n", |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1400 "\r", |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1401 ("\r", "\n"), |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1402 "\r\n", |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1403 ("\n", "\r\n"), |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1404 ("\r", "\r\n"), |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1405 ("\r", "\n", "\r\n") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1406 )[self.seennl] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1407 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1408 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1409 class TextIOWrapper(TextIOBase): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1410 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1411 r"""Character and line based layer over a BufferedIOBase object, buffer. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1412 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1413 encoding gives the name of the encoding that the stream will be |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1414 decoded or encoded with. It defaults to locale.getpreferredencoding. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1415 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1416 errors determines the strictness of encoding and decoding (see the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1417 codecs.register) and defaults to "strict". |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1418 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1419 newline can be None, '', '\n', '\r', or '\r\n'. It controls the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1420 handling of line endings. If it is None, universal newlines is |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1421 enabled. With this enabled, on input, the lines endings '\n', '\r', |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1422 or '\r\n' are translated to '\n' before being returned to the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1423 caller. Conversely, on output, '\n' is translated to the system |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1424 default line seperator, os.linesep. If newline is any other of its |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1425 legal values, that newline becomes the newline when the file is read |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1426 and it is returned untranslated. On output, '\n' is converted to the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1427 newline. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1428 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1429 If line_buffering is True, a call to flush is implied when a call to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1430 write contains a newline character. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1431 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1432 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1433 _CHUNK_SIZE = 2048 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1434 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1435 def __init__(self, buffer, encoding=None, errors=None, newline=None, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1436 line_buffering=False): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1437 if newline is not None and not isinstance(newline, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1438 raise TypeError("illegal newline type: %r" % (type(newline),)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1439 if newline not in (None, "", "\n", "\r", "\r\n"): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1440 raise ValueError("illegal newline value: %r" % (newline,)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1441 if encoding is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1442 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1443 encoding = os.device_encoding(buffer.fileno()) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1444 except (AttributeError, UnsupportedOperation): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1445 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1446 if encoding is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1447 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1448 import locale |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1449 except ImportError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1450 # Importing locale may fail if Python is being built |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1451 encoding = "ascii" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1452 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1453 encoding = locale.getpreferredencoding() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1454 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1455 if not isinstance(encoding, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1456 raise ValueError("invalid encoding: %r" % encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1457 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1458 if errors is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1459 errors = "strict" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1460 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1461 if not isinstance(errors, basestring): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1462 raise ValueError("invalid errors: %r" % errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1463 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1464 self.buffer = buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1465 self._line_buffering = line_buffering |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1466 self._encoding = encoding |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1467 self._errors = errors |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1468 self._readuniversal = not newline |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1469 self._readtranslate = newline is None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1470 self._readnl = newline |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1471 self._writetranslate = newline != '' |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1472 self._writenl = newline or os.linesep |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1473 self._encoder = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1474 self._decoder = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1475 self._decoded_chars = '' # buffer for text returned from decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1476 self._decoded_chars_used = 0 # offset into _decoded_chars for read() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1477 self._snapshot = None # info for reconstructing decoder state |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1478 self._seekable = self._telling = self.buffer.seekable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1479 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1480 if self._seekable and self.writable(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1481 position = self.buffer.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1482 if position != 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1483 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1484 self._get_encoder().setstate(0) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1485 except LookupError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1486 # Sometimes the encoder doesn't exist |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1487 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1488 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1489 # self._snapshot is either None, or a tuple (dec_flags, next_input) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1490 # where dec_flags is the second (integer) item of the decoder state |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1491 # and next_input is the chunk of input bytes that comes next after the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1492 # snapshot point. We use this to reconstruct decoder states in tell(). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1493 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1494 # Naming convention: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1495 # - "bytes_..." for integer variables that count input bytes |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1496 # - "chars_..." for integer variables that count decoded characters |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1497 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1498 def __repr__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1499 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1500 name = self.name |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1501 except AttributeError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1502 return "<_pyio.TextIOWrapper encoding='{0}'>".format(self.encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1503 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1504 return "<_pyio.TextIOWrapper name={0!r} encoding='{1}'>".format( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1505 name, self.encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1506 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1507 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1508 def encoding(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1509 return self._encoding |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1510 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1511 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1512 def errors(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1513 return self._errors |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1514 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1515 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1516 def line_buffering(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1517 return self._line_buffering |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1518 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1519 def seekable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1520 return self._seekable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1521 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1522 def readable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1523 return self.buffer.readable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1524 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1525 def writable(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1526 return self.buffer.writable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1527 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1528 def flush(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1529 self.buffer.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1530 self._telling = self._seekable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1531 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1532 def close(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1533 if self.buffer is not None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1534 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1535 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1536 except IOError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1537 pass # If flush() fails, just give up |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1538 self.buffer.close() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1539 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1540 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1541 def closed(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1542 return self.buffer.closed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1543 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1544 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1545 def name(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1546 return self.buffer.name |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1547 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1548 def fileno(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1549 return self.buffer.fileno() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1550 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1551 def isatty(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1552 return self.buffer.isatty() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1553 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1554 def write(self, s): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1555 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1556 raise ValueError("write to closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1557 if not isinstance(s, unicode): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1558 raise TypeError("can't write %s to text stream" % |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1559 s.__class__.__name__) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1560 length = len(s) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1561 haslf = (self._writetranslate or self._line_buffering) and "\n" in s |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1562 if haslf and self._writetranslate and self._writenl != "\n": |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1563 s = s.replace("\n", self._writenl) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1564 encoder = self._encoder or self._get_encoder() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1565 # XXX What if we were just reading? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1566 b = encoder.encode(s) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1567 self.buffer.write(b) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1568 if self._line_buffering and (haslf or "\r" in s): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1569 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1570 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1571 if self._decoder: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1572 self._decoder.reset() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1573 return length |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1574 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1575 def _get_encoder(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1576 make_encoder = codecs.getincrementalencoder(self._encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1577 self._encoder = make_encoder(self._errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1578 return self._encoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1579 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1580 def _get_decoder(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1581 make_decoder = codecs.getincrementaldecoder(self._encoding) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1582 decoder = make_decoder(self._errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1583 if self._readuniversal: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1584 decoder = IncrementalNewlineDecoder(decoder, self._readtranslate) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1585 self._decoder = decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1586 return decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1587 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1588 # The following three methods implement an ADT for _decoded_chars. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1589 # Text returned from the decoder is buffered here until the client |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1590 # requests it by calling our read() or readline() method. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1591 def _set_decoded_chars(self, chars): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1592 """Set the _decoded_chars buffer.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1593 self._decoded_chars = chars |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1594 self._decoded_chars_used = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1595 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1596 def _get_decoded_chars(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1597 """Advance into the _decoded_chars buffer.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1598 offset = self._decoded_chars_used |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1599 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1600 chars = self._decoded_chars[offset:] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1601 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1602 chars = self._decoded_chars[offset:offset + n] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1603 self._decoded_chars_used += len(chars) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1604 return chars |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1605 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1606 def _rewind_decoded_chars(self, n): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1607 """Rewind the _decoded_chars buffer.""" |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1608 if self._decoded_chars_used < n: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1609 raise AssertionError("rewind decoded_chars out of bounds") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1610 self._decoded_chars_used -= n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1611 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1612 def _read_chunk(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1613 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1614 Read and decode the next chunk of data from the BufferedReader. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1615 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1616 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1617 # The return value is True unless EOF was reached. The decoded |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1618 # string is placed in self._decoded_chars (replacing its previous |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1619 # value). The entire input chunk is sent to the decoder, though |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1620 # some of it may remain buffered in the decoder, yet to be |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1621 # converted. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1622 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1623 if self._decoder is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1624 raise ValueError("no decoder") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1625 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1626 if self._telling: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1627 # To prepare for tell(), we need to snapshot a point in the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1628 # file where the decoder's input buffer is empty. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1629 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1630 dec_buffer, dec_flags = self._decoder.getstate() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1631 # Given this, we know there was a valid snapshot point |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1632 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1633 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1634 # Read a chunk, decode it, and put the result in self._decoded_chars. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1635 input_chunk = self.buffer.read1(self._CHUNK_SIZE) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1636 eof = not input_chunk |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1637 self._set_decoded_chars(self._decoder.decode(input_chunk, eof)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1638 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1639 if self._telling: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1640 # At the snapshot point, len(dec_buffer) bytes before the read, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1641 # the next input to be decoded is dec_buffer + input_chunk. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1642 self._snapshot = (dec_flags, dec_buffer + input_chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1643 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1644 return not eof |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1645 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1646 def _pack_cookie(self, position, dec_flags=0, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1647 bytes_to_feed=0, need_eof=0, chars_to_skip=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1648 # The meaning of a tell() cookie is: seek to position, set the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1649 # decoder flags to dec_flags, read bytes_to_feed bytes, feed them |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1650 # into the decoder with need_eof as the EOF flag, then skip |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1651 # chars_to_skip characters of the decoded result. For most simple |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1652 # decoders, tell() will often just give a byte offset in the file. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1653 return (position | (dec_flags<<64) | (bytes_to_feed<<128) | |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1654 (chars_to_skip<<192) | bool(need_eof)<<256) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1655 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1656 def _unpack_cookie(self, bigint): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1657 rest, position = divmod(bigint, 1<<64) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1658 rest, dec_flags = divmod(rest, 1<<64) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1659 rest, bytes_to_feed = divmod(rest, 1<<64) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1660 need_eof, chars_to_skip = divmod(rest, 1<<64) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1661 return position, dec_flags, bytes_to_feed, need_eof, chars_to_skip |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1662 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1663 def tell(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1664 if not self._seekable: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1665 raise IOError("underlying stream is not seekable") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1666 if not self._telling: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1667 raise IOError("telling position disabled by next() call") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1668 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1669 position = self.buffer.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1670 decoder = self._decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1671 if decoder is None or self._snapshot is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1672 if self._decoded_chars: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1673 # This should never happen. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1674 raise AssertionError("pending decoded text") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1675 return position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1676 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1677 # Skip backward to the snapshot point (see _read_chunk). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1678 dec_flags, next_input = self._snapshot |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1679 position -= len(next_input) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1680 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1681 # How many decoded characters have been used up since the snapshot? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1682 chars_to_skip = self._decoded_chars_used |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1683 if chars_to_skip == 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1684 # We haven't moved from the snapshot point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1685 return self._pack_cookie(position, dec_flags) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1686 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1687 # Starting from the snapshot position, we will walk the decoder |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1688 # forward until it gives us enough decoded characters. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1689 saved_state = decoder.getstate() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1690 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1691 # Note our initial start point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1692 decoder.setstate((b'', dec_flags)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1693 start_pos = position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1694 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1695 need_eof = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1696 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1697 # Feed the decoder one byte at a time. As we go, note the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1698 # nearest "safe start point" before the current location |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1699 # (a point where the decoder has nothing buffered, so seek() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1700 # can safely start from there and advance to this location). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1701 for next_byte in next_input: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1702 bytes_fed += 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1703 chars_decoded += len(decoder.decode(next_byte)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1704 dec_buffer, dec_flags = decoder.getstate() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1705 if not dec_buffer and chars_decoded <= chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1706 # Decoder buffer is empty, so this is a safe start point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1707 start_pos += bytes_fed |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1708 chars_to_skip -= chars_decoded |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1709 start_flags, bytes_fed, chars_decoded = dec_flags, 0, 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1710 if chars_decoded >= chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1711 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1712 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1713 # We didn't get enough decoded data; signal EOF to get more. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1714 chars_decoded += len(decoder.decode(b'', final=True)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1715 need_eof = 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1716 if chars_decoded < chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1717 raise IOError("can't reconstruct logical file position") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1718 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1719 # The returned cookie corresponds to the last safe start point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1720 return self._pack_cookie( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1721 start_pos, start_flags, bytes_fed, need_eof, chars_to_skip) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1722 finally: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1723 decoder.setstate(saved_state) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1724 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1725 def truncate(self, pos=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1726 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1727 if pos is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1728 pos = self.tell() |
|
58624
77130f2fe944
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents:
57789
diff
changeset
|
1729 return self.buffer.truncate(pos) |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1730 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1731 def detach(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1732 if self.buffer is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1733 raise ValueError("buffer is already detached") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1734 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1735 buffer = self.buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1736 self.buffer = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1737 return buffer |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1738 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1739 def seek(self, cookie, whence=0): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1740 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1741 raise ValueError("tell on closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1742 if not self._seekable: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1743 raise IOError("underlying stream is not seekable") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1744 if whence == 1: # seek relative to current position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1745 if cookie != 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1746 raise IOError("can't do nonzero cur-relative seeks") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1747 # Seeking to the current position should attempt to |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1748 # sync the underlying buffer with the current position. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1749 whence = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1750 cookie = self.tell() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1751 if whence == 2: # seek relative to end of file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1752 if cookie != 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1753 raise IOError("can't do nonzero end-relative seeks") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1754 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1755 position = self.buffer.seek(0, 2) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1756 self._set_decoded_chars('') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1757 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1758 if self._decoder: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1759 self._decoder.reset() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1760 return position |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1761 if whence != 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1762 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" % |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1763 (whence,)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1764 if cookie < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1765 raise ValueError("negative seek position %r" % (cookie,)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1766 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1767 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1768 # The strategy of seek() is to go back to the safe start point |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1769 # and replay the effect of read(chars_to_skip) from there. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1770 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1771 self._unpack_cookie(cookie) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1772 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1773 # Seek back to the safe start point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1774 self.buffer.seek(start_pos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1775 self._set_decoded_chars('') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1776 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1777 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1778 # Restore the decoder to its state from the safe start point. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1779 if cookie == 0 and self._decoder: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1780 self._decoder.reset() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1781 elif self._decoder or dec_flags or chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1782 self._decoder = self._decoder or self._get_decoder() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1783 self._decoder.setstate((b'', dec_flags)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1784 self._snapshot = (dec_flags, b'') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1785 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1786 if chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1787 # Just like _read_chunk, feed the decoder and save a snapshot. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1788 input_chunk = self.buffer.read(bytes_to_feed) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1789 self._set_decoded_chars( |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1790 self._decoder.decode(input_chunk, need_eof)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1791 self._snapshot = (dec_flags, input_chunk) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1792 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1793 # Skip chars_to_skip of the decoded characters. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1794 if len(self._decoded_chars) < chars_to_skip: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1795 raise IOError("can't restore logical file position") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1796 self._decoded_chars_used = chars_to_skip |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1797 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1798 # Finally, reset the encoder (merely useful for proper BOM handling) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1799 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1800 encoder = self._encoder or self._get_encoder() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1801 except LookupError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1802 # Sometimes the encoder doesn't exist |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1803 pass |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1804 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1805 if cookie != 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1806 encoder.setstate(0) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1807 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1808 encoder.reset() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1809 return cookie |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1810 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1811 def read(self, n=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1812 self._checkReadable() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1813 if n is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1814 n = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1815 decoder = self._decoder or self._get_decoder() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1816 if n < 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1817 # Read everything. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1818 result = (self._get_decoded_chars() + |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1819 decoder.decode(self.buffer.read(), final=True)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1820 self._set_decoded_chars('') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1821 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1822 return result |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1823 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1824 # Keep reading chunks until we have n characters to return. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1825 eof = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1826 result = self._get_decoded_chars(n) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1827 while len(result) < n and not eof: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1828 eof = not self._read_chunk() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1829 result += self._get_decoded_chars(n - len(result)) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1830 return result |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1831 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1832 def next(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1833 self._telling = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1834 line = self.readline() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1835 if not line: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1836 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1837 self._telling = self._seekable |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1838 raise StopIteration |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1839 return line |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1840 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1841 def readline(self, limit=None): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1842 if self.closed: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1843 raise ValueError("read from closed file") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1844 if limit is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1845 limit = -1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1846 elif not isinstance(limit, (int, long)): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1847 raise TypeError("limit must be an integer") |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1848 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1849 # Grab all the decoded text (we will rewind any extra bits later). |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1850 line = self._get_decoded_chars() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1851 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1852 start = 0 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1853 # Make the decoder if it doesn't already exist. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1854 if not self._decoder: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1855 self._get_decoder() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1856 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1857 pos = endpos = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1858 while True: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1859 if self._readtranslate: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1860 # Newlines are already translated, only search for \n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1861 pos = line.find('\n', start) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1862 if pos >= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1863 endpos = pos + 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1864 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1865 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1866 start = len(line) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1867 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1868 elif self._readuniversal: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1869 # Universal newline search. Find any of \r, \r\n, \n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1870 # The decoder ensures that \r\n are not split in two pieces |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1871 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1872 # In C we'd look for these in parallel of course. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1873 nlpos = line.find("\n", start) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1874 crpos = line.find("\r", start) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1875 if crpos == -1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1876 if nlpos == -1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1877 # Nothing found |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1878 start = len(line) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1879 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1880 # Found \n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1881 endpos = nlpos + 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1882 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1883 elif nlpos == -1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1884 # Found lone \r |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1885 endpos = crpos + 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1886 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1887 elif nlpos < crpos: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1888 # Found \n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1889 endpos = nlpos + 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1890 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1891 elif nlpos == crpos + 1: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1892 # Found \r\n |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1893 endpos = crpos + 2 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1894 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1895 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1896 # Found \r |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1897 endpos = crpos + 1 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1898 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1899 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1900 # non-universal |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1901 pos = line.find(self._readnl) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1902 if pos >= 0: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1903 endpos = pos + len(self._readnl) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1904 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1905 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1906 if limit >= 0 and len(line) >= limit: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1907 endpos = limit # reached length limit |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1908 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1909 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1910 # No line ending seen yet - get more data' |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1911 while self._read_chunk(): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1912 if self._decoded_chars: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1913 break |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1914 if self._decoded_chars: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1915 line += self._get_decoded_chars() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1916 else: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1917 # end of file |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1918 self._set_decoded_chars('') |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1919 self._snapshot = None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1920 return line |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1921 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1922 if limit >= 0 and endpos > limit: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1923 endpos = limit # don't exceed limit |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1924 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1925 # Rewind _decoded_chars to just after the line ending we found. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1926 self._rewind_decoded_chars(len(line) - endpos) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1927 return line[:endpos] |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1928 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1929 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1930 def newlines(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1931 return self._decoder.newlines if self._decoder else None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1932 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1933 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1934 class StringIO(TextIOWrapper): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1935 """Text I/O implementation using an in-memory buffer. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1936 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1937 The initial_value argument sets the value of object. The newline |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1938 argument is like the one of TextIOWrapper's constructor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1939 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1940 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1941 def __init__(self, initial_value="", newline="\n"): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1942 super(StringIO, self).__init__(BytesIO(), |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1943 encoding="utf-8", |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1944 errors="strict", |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1945 newline=newline) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1946 # Issue #5645: make universal newlines semantics the same as in the |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1947 # C version, even under Windows. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1948 if newline is None: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1949 self._writetranslate = False |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1950 if initial_value: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1951 if not isinstance(initial_value, unicode): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1952 initial_value = unicode(initial_value) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1953 self.write(initial_value) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1954 self.seek(0) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1955 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1956 def getvalue(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1957 self.flush() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1958 return self.buffer.getvalue().decode(self._encoding, self._errors) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1959 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1960 def __repr__(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1961 # TextIOWrapper tells the encoding in its repr. In StringIO, |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1962 # that's a implementation detail. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1963 return object.__repr__(self) |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1964 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1965 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1966 def errors(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1967 return None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1968 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1969 @property |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1970 def encoding(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1971 return None |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1972 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1973 def detach(self): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1974 # This doesn't make sense on StringIO. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1975 self._unsupported("detach") |
