Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(259)

Side by Side Diff: Lib/_pyio.py

Issue 10142: Support for SEEK_HOLE/SEEK_DATA
Patch Set: Created 1 year ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/os.rst ('k') | Lib/os.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """ 1 """
2 Python implementation of the io module. 2 Python implementation of the io module.
3 """ 3 """
4 4
5 import os 5 import os
6 import abc 6 import abc
7 import codecs 7 import codecs
8 import warnings 8 import warnings
9 import errno 9 import errno
10 # Import _thread instead of threading to reduce startup cost 10 # Import _thread instead of threading to reduce startup cost
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 def seek(self, pos, whence=0): 299 def seek(self, pos, whence=0):
300 """Change stream position. 300 """Change stream position.
301 301
302 Change the stream position to byte offset offset. offset is 302 Change the stream position to byte offset offset. offset is
303 interpreted relative to the position indicated by whence. Values 303 interpreted relative to the position indicated by whence. Values
304 for whence are ints: 304 for whence are ints:
305 305
306 * 0 -- start of stream (the default); offset should be zero or positive 306 * 0 -- start of stream (the default); offset should be zero or positive
307 * 1 -- current stream position; offset may be negative 307 * 1 -- current stream position; offset may be negative
308 * 2 -- end of stream; offset is usually negative 308 * 2 -- end of stream; offset is usually negative
309 Some operating systems / file systems could provide additional values.
309 310
310 Return an int indicating the new absolute position. 311 Return an int indicating the new absolute position.
311 """ 312 """
312 self._unsupported("seek") 313 self._unsupported("seek")
313 314
314 def tell(self): 315 def tell(self):
315 """Return an int indicating the current stream position.""" 316 """Return an int indicating the current stream position."""
316 return self.seek(0, 1) 317 return self.seek(0, 1)
317 318
318 def truncate(self, pos=None): 319 def truncate(self, pos=None):
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 raise TypeError("an integer is required") from err 860 raise TypeError("an integer is required") from err
860 if whence == 0: 861 if whence == 0:
861 if pos < 0: 862 if pos < 0:
862 raise ValueError("negative seek position %r" % (pos,)) 863 raise ValueError("negative seek position %r" % (pos,))
863 self._pos = pos 864 self._pos = pos
864 elif whence == 1: 865 elif whence == 1:
865 self._pos = max(0, self._pos + pos) 866 self._pos = max(0, self._pos + pos)
866 elif whence == 2: 867 elif whence == 2:
867 self._pos = max(0, len(self._buffer) + pos) 868 self._pos = max(0, len(self._buffer) + pos)
868 else: 869 else:
869 raise ValueError("invalid whence value") 870 raise ValueError("unsupported whence value")
870 return self._pos 871 return self._pos
871 872
872 def tell(self): 873 def tell(self):
873 if self.closed: 874 if self.closed:
874 raise ValueError("tell on closed file") 875 raise ValueError("tell on closed file")
875 return self._pos 876 return self._pos
876 877
877 def truncate(self, pos=None): 878 def truncate(self, pos=None):
878 if self.closed: 879 if self.closed:
879 raise ValueError("truncate on closed file") 880 raise ValueError("truncate on closed file")
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 return b"" 1035 return b""
1035 with self._read_lock: 1036 with self._read_lock:
1036 self._peek_unlocked(1) 1037 self._peek_unlocked(1)
1037 return self._read_unlocked( 1038 return self._read_unlocked(
1038 min(n, len(self._read_buf) - self._read_pos)) 1039 min(n, len(self._read_buf) - self._read_pos))
1039 1040
1040 def tell(self): 1041 def tell(self):
1041 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_po s 1042 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_po s
1042 1043
1043 def seek(self, pos, whence=0): 1044 def seek(self, pos, whence=0):
1044 if not (0 <= whence <= 2):
1045 raise ValueError("invalid whence value")
1046 with self._read_lock: 1045 with self._read_lock:
1047 if whence == 1: 1046 if whence == 1:
1048 pos -= len(self._read_buf) - self._read_pos 1047 pos -= len(self._read_buf) - self._read_pos
1049 pos = _BufferedIOMixin.seek(self, pos, whence) 1048 pos = _BufferedIOMixin.seek(self, pos, whence)
1050 self._reset_read_buf() 1049 self._reset_read_buf()
1051 return pos 1050 return pos
1052 1051
1053 class BufferedWriter(_BufferedIOMixin): 1052 class BufferedWriter(_BufferedIOMixin):
1054 1053
1055 """A buffer for a writeable sequential RawIO object. 1054 """A buffer for a writeable sequential RawIO object.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1131 errno.EAGAIN, 1130 errno.EAGAIN,
1132 "write could not complete without blocking", 0) 1131 "write could not complete without blocking", 0)
1133 if n > len(self._write_buf) or n < 0: 1132 if n > len(self._write_buf) or n < 0:
1134 raise IOError("write() returned incorrect number of bytes") 1133 raise IOError("write() returned incorrect number of bytes")
1135 del self._write_buf[:n] 1134 del self._write_buf[:n]
1136 1135
1137 def tell(self): 1136 def tell(self):
1138 return _BufferedIOMixin.tell(self) + len(self._write_buf) 1137 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1139 1138
1140 def seek(self, pos, whence=0): 1139 def seek(self, pos, whence=0):
1141 if not (0 <= whence <= 2):
1142 raise ValueError("invalid whence")
1143 with self._write_lock: 1140 with self._write_lock:
1144 self._flush_unlocked() 1141 self._flush_unlocked()
1145 return _BufferedIOMixin.seek(self, pos, whence) 1142 return _BufferedIOMixin.seek(self, pos, whence)
1146 1143
1147 1144
1148 class BufferedRWPair(BufferedIOBase): 1145 class BufferedRWPair(BufferedIOBase):
1149 1146
1150 """A buffered reader and writer object together. 1147 """A buffered reader and writer object together.
1151 1148
1152 A buffered reader object and buffered writer object put together to 1149 A buffered reader object and buffered writer object put together to
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 1225
1229 _warning_stack_offset = 3 1226 _warning_stack_offset = 3
1230 1227
1231 def __init__(self, raw, 1228 def __init__(self, raw,
1232 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): 1229 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1233 raw._checkSeekable() 1230 raw._checkSeekable()
1234 BufferedReader.__init__(self, raw, buffer_size) 1231 BufferedReader.__init__(self, raw, buffer_size)
1235 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size) 1232 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
1236 1233
1237 def seek(self, pos, whence=0): 1234 def seek(self, pos, whence=0):
1238 if not (0 <= whence <= 2):
1239 raise ValueError("invalid whence")
1240 self.flush() 1235 self.flush()
1241 if self._read_buf: 1236 if self._read_buf:
1242 # Undo read ahead. 1237 # Undo read ahead.
1243 with self._read_lock: 1238 with self._read_lock:
1244 self.raw.seek(self._read_pos - len(self._read_buf), 1) 1239 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1245 # First do the raw seek, then empty the read buffer, so that 1240 # First do the raw seek, then empty the read buffer, so that
1246 # if the raw seek fails, we don't lose buffered data forever. 1241 # if the raw seek fails, we don't lose buffered data forever.
1247 pos = self.raw.seek(pos, whence) 1242 pos = self.raw.seek(pos, whence)
1248 with self._read_lock: 1243 with self._read_lock:
1249 self._reset_read_buf() 1244 self._reset_read_buf()
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after
1845 if cookie != 0: 1840 if cookie != 0:
1846 raise UnsupportedOperation("can't do nonzero end-relative seeks" ) 1841 raise UnsupportedOperation("can't do nonzero end-relative seeks" )
1847 self.flush() 1842 self.flush()
1848 position = self.buffer.seek(0, 2) 1843 position = self.buffer.seek(0, 2)
1849 self._set_decoded_chars('') 1844 self._set_decoded_chars('')
1850 self._snapshot = None 1845 self._snapshot = None
1851 if self._decoder: 1846 if self._decoder:
1852 self._decoder.reset() 1847 self._decoder.reset()
1853 return position 1848 return position
1854 if whence != 0: 1849 if whence != 0:
1855 raise ValueError("invalid whence (%r, should be 0, 1 or 2)" % 1850 raise ValueError("unsupported whence (%r)" % (whence,))
1856 (whence,))
1857 if cookie < 0: 1851 if cookie < 0:
1858 raise ValueError("negative seek position %r" % (cookie,)) 1852 raise ValueError("negative seek position %r" % (cookie,))
1859 self.flush() 1853 self.flush()
1860 1854
1861 # The strategy of seek() is to go back to the safe start point 1855 # The strategy of seek() is to go back to the safe start point
1862 # and replay the effect of read(chars_to_skip) from there. 1856 # and replay the effect of read(chars_to_skip) from there.
1863 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \ 1857 start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = \
1864 self._unpack_cookie(cookie) 1858 self._unpack_cookie(cookie)
1865 1859
1866 # Seek back to the safe start point. 1860 # Seek back to the safe start point.
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
2065 def errors(self): 2059 def errors(self):
2066 return None 2060 return None
2067 2061
2068 @property 2062 @property
2069 def encoding(self): 2063 def encoding(self):
2070 return None 2064 return None
2071 2065
2072 def detach(self): 2066 def detach(self):
2073 # This doesn't make sense on StringIO. 2067 # This doesn't make sense on StringIO.
2074 self._unsupported("detach") 2068 self._unsupported("detach")
OLDNEW
« no previous file with comments | « Doc/library/os.rst ('k') | Lib/os.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7