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

Delta Between Two Patch Sets: Lib/_pyio.py

Issue 10142: Support for SEEK_HOLE/SEEK_DATA
Left Patch Set: Created 1 year, 1 month ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/os.rst ('k') | Lib/os.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 try: 11 try:
12 from _thread import allocate_lock as Lock 12 from _thread import allocate_lock as Lock
13 except ImportError: 13 except ImportError:
14 from _dummy_thread import allocate_lock as Lock 14 from _dummy_thread import allocate_lock as Lock
15 15
16 import io 16 import io
17 from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END) 17 from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
18
19 valid_seek_flags = {0, 1, 2} # Hardwired values
20 if hasattr(os, 'SEEK_HOLE') :
21 valid_seek_flags.add(os.SEEK_HOLE)
22 valid_seek_flags.add(os.SEEK_DATA)
18 23
19 # open() uses st_blksize whenever we can 24 # open() uses st_blksize whenever we can
20 DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes 25 DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
21 26
22 # NOTE: Base classes defined here are registered with the "official" ABCs 27 # NOTE: Base classes defined here are registered with the "official" ABCs
23 # defined in io.py. We don't use real inheritance though, because we don't 28 # defined in io.py. We don't use real inheritance though, because we don't
24 # want to inherit the C implementations. 29 # want to inherit the C implementations.
25 30
26 # Rebind for compatibility 31 # Rebind for compatibility
27 BlockingIOError = BlockingIOError 32 BlockingIOError = BlockingIOError
(...skipping 1007 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 return b"" 1040 return b""
1036 with self._read_lock: 1041 with self._read_lock:
1037 self._peek_unlocked(1) 1042 self._peek_unlocked(1)
1038 return self._read_unlocked( 1043 return self._read_unlocked(
1039 min(n, len(self._read_buf) - self._read_pos)) 1044 min(n, len(self._read_buf) - self._read_pos))
1040 1045
1041 def tell(self): 1046 def tell(self):
1042 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_po s 1047 return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_po s
1043 1048
1044 def seek(self, pos, whence=0): 1049 def seek(self, pos, whence=0):
1050 if whence not in valid_seek_flags:
1051 raise ValueError("invalid whence value")
1045 with self._read_lock: 1052 with self._read_lock:
1046 if whence == 1: 1053 if whence == 1:
1047 pos -= len(self._read_buf) - self._read_pos 1054 pos -= len(self._read_buf) - self._read_pos
1048 pos = _BufferedIOMixin.seek(self, pos, whence) 1055 pos = _BufferedIOMixin.seek(self, pos, whence)
1049 self._reset_read_buf() 1056 self._reset_read_buf()
1050 return pos 1057 return pos
1051 1058
1052 class BufferedWriter(_BufferedIOMixin): 1059 class BufferedWriter(_BufferedIOMixin):
1053 1060
1054 """A buffer for a writeable sequential RawIO object. 1061 """A buffer for a writeable sequential RawIO object.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 errno.EAGAIN, 1137 errno.EAGAIN,
1131 "write could not complete without blocking", 0) 1138 "write could not complete without blocking", 0)
1132 if n > len(self._write_buf) or n < 0: 1139 if n > len(self._write_buf) or n < 0:
1133 raise IOError("write() returned incorrect number of bytes") 1140 raise IOError("write() returned incorrect number of bytes")
1134 del self._write_buf[:n] 1141 del self._write_buf[:n]
1135 1142
1136 def tell(self): 1143 def tell(self):
1137 return _BufferedIOMixin.tell(self) + len(self._write_buf) 1144 return _BufferedIOMixin.tell(self) + len(self._write_buf)
1138 1145
1139 def seek(self, pos, whence=0): 1146 def seek(self, pos, whence=0):
1147 if whence not in valid_seek_flags:
1148 raise ValueError("invalid whence value")
1140 with self._write_lock: 1149 with self._write_lock:
1141 self._flush_unlocked() 1150 self._flush_unlocked()
1142 return _BufferedIOMixin.seek(self, pos, whence) 1151 return _BufferedIOMixin.seek(self, pos, whence)
1143 1152
1144 1153
1145 class BufferedRWPair(BufferedIOBase): 1154 class BufferedRWPair(BufferedIOBase):
1146 1155
1147 """A buffered reader and writer object together. 1156 """A buffered reader and writer object together.
1148 1157
1149 A buffered reader object and buffered writer object put together to 1158 A buffered reader object and buffered writer object put together to
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 1234
1226 _warning_stack_offset = 3 1235 _warning_stack_offset = 3
1227 1236
1228 def __init__(self, raw, 1237 def __init__(self, raw,
1229 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): 1238 buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
1230 raw._checkSeekable() 1239 raw._checkSeekable()
1231 BufferedReader.__init__(self, raw, buffer_size) 1240 BufferedReader.__init__(self, raw, buffer_size)
1232 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size) 1241 BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
1233 1242
1234 def seek(self, pos, whence=0): 1243 def seek(self, pos, whence=0):
1244 if whence not in valid_seek_flags:
1245 raise ValueError("invalid whence value")
1235 self.flush() 1246 self.flush()
1236 if self._read_buf: 1247 if self._read_buf:
1237 # Undo read ahead. 1248 # Undo read ahead.
1238 with self._read_lock: 1249 with self._read_lock:
1239 self.raw.seek(self._read_pos - len(self._read_buf), 1) 1250 self.raw.seek(self._read_pos - len(self._read_buf), 1)
1240 # First do the raw seek, then empty the read buffer, so that 1251 # First do the raw seek, then empty the read buffer, so that
1241 # if the raw seek fails, we don't lose buffered data forever. 1252 # if the raw seek fails, we don't lose buffered data forever.
1242 pos = self.raw.seek(pos, whence) 1253 pos = self.raw.seek(pos, whence)
1243 with self._read_lock: 1254 with self._read_lock:
1244 self._reset_read_buf() 1255 self._reset_read_buf()
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after
2059 def errors(self): 2070 def errors(self):
2060 return None 2071 return None
2061 2072
2062 @property 2073 @property
2063 def encoding(self): 2074 def encoding(self):
2064 return None 2075 return None
2065 2076
2066 def detach(self): 2077 def detach(self):
2067 # This doesn't make sense on StringIO. 2078 # This doesn't make sense on StringIO.
2068 self._unsupported("detach") 2079 self._unsupported("detach")
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7