MCPcopy Index your code
hub / github.com/python/cpython / seek

Method seek

Lib/zipfile/__init__.py:1218–1281  ·  view source on GitHub ↗
(self, offset, whence=os.SEEK_SET)

Source from the content-addressed store, hash-verified

1216 return self._seekable
1217
1218 def seek(self, offset, whence=os.SEEK_SET):
1219 if self.closed:
1220 raise ValueError("seek on closed file.")
1221 if not self._seekable:
1222 raise io.UnsupportedOperation("underlying stream is not seekable")
1223 curr_pos = self.tell()
1224 if whence == os.SEEK_SET:
1225 new_pos = offset
1226 elif whence == os.SEEK_CUR:
1227 new_pos = curr_pos + offset
1228 elif whence == os.SEEK_END:
1229 new_pos = self._orig_file_size + offset
1230 else:
1231 raise ValueError("whence must be os.SEEK_SET (0), "
1232 "os.SEEK_CUR (1), or os.SEEK_END (2)")
1233
1234 if new_pos > self._orig_file_size:
1235 new_pos = self._orig_file_size
1236
1237 if new_pos < 0:
1238 new_pos = 0
1239
1240 read_offset = new_pos - curr_pos
1241 buff_offset = read_offset + self._offset
1242
1243 if buff_offset >= 0 and buff_offset < len(self._readbuffer):
1244 # Just move the _offset index if the new position is in the _readbuffer
1245 self._offset = buff_offset
1246 read_offset = 0
1247 # Fast seek uncompressed unencrypted file
1248 elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset != 0:
1249 # disable CRC checking after first seeking - it would be invalid
1250 self._expected_crc = None
1251 # seek actual file taking already buffered data into account
1252 read_offset -= len(self._readbuffer) - self._offset
1253 self._fileobj.seek(read_offset, os.SEEK_CUR)
1254 self._left -= read_offset
1255 self._compress_left -= read_offset
1256 self._eof = self._left <= 0
1257 read_offset = 0
1258 # flush read buffer
1259 self._readbuffer = b''
1260 self._offset = 0
1261 elif read_offset < 0:
1262 # Position is before the current position. Reset the ZipExtFile
1263 self._fileobj.seek(self._orig_compress_start)
1264 self._running_crc = self._orig_start_crc
1265 self._expected_crc = self._orig_crc
1266 self._compress_left = self._orig_compress_size
1267 self._left = self._orig_file_size
1268 self._readbuffer = b''
1269 self._offset = 0
1270 self._decompressor = _get_decompressor(self._compress_type)
1271 self._eof = False
1272 read_offset = new_pos
1273 if self._decrypter is not None:
1274 self._init_decrypter()
1275

Callers 13

_check_zipfileFunction · 0.45
is_zipfileFunction · 0.45
_EndRecData64Function · 0.45
_EndRecDataFunction · 0.45
seekMethod · 0.45
readMethod · 0.45
closeMethod · 0.45
__init__Method · 0.45
openMethod · 0.45
_open_to_writeMethod · 0.45
mkdirMethod · 0.45
closeMethod · 0.45

Calls 4

tellMethod · 0.95
_init_decrypterMethod · 0.95
readMethod · 0.95
_get_decompressorFunction · 0.85

Tested by

no test coverage detected