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