Skip to content

Commit 1239005

Browse files
committed
update
1 parent 6a55aad commit 1239005

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,7 +3463,9 @@ def read(self, size=-1):
34633463

34643464

34653465
class StoredZipExtFileRandomReadTest(unittest.TestCase):
3466-
def test_random_read(self):
3466+
"""Tests whether an uncompressed, unencrypted zip entry can be randomly
3467+
seek and read without reading redundant bytes."""
3468+
def test_stored_seek_and_read(self):
34673469

34683470
sio = StatIO()
34693471
# 20000 bytes
@@ -3472,12 +3474,13 @@ def test_random_read(self):
34723474
# The seek length must be greater than ZipExtFile.MIN_READ_SIZE
34733475
# as `ZipExtFile._read2()` reads in blocks of this size and we
34743476
# need to seek out of the buffered data
3475-
min_size = zipfile.ZipExtFile.MIN_READ_SIZE
3476-
self.assertGreaterEqual(10002, min_size) # for forward seek test
3477-
self.assertGreaterEqual(5003, min_size) # for backward seek test
3477+
read_buffer_size = zipfile.ZipExtFile.MIN_READ_SIZE
3478+
self.assertGreaterEqual(10002, read_buffer_size) # for forward seek test
3479+
self.assertGreaterEqual(5003, read_buffer_size) # for backward seek test
34783480
# The read length must be less than MIN_READ_SIZE, since we assume that
34793481
# only 1 block is read in the test.
3480-
self.assertGreaterEqual(min_size, 100) # for read() calls
3482+
read_length = 100
3483+
self.assertGreaterEqual(read_buffer_size, read_length) # for read() calls
34813484

34823485
with zipfile.ZipFile(sio, "w", compression=zipfile.ZIP_STORED) as zipf:
34833486
zipf.writestr("foo.txt", txt)
@@ -3490,33 +3493,42 @@ def test_random_read(self):
34903493

34913494
# forward seek
34923495
old_count = sio.bytes_read
3493-
fp.seek(10002, os.SEEK_CUR)
3494-
self.assertEqual(fp.tell(), 10002)
3496+
forward_seek_len = 10002
3497+
current_pos = 0
3498+
fp.seek(forward_seek_len, os.SEEK_CUR)
3499+
current_pos += forward_seek_len
3500+
self.assertEqual(fp.tell(), current_pos)
34953501
self.assertEqual(fp._left, fp._compress_left)
3496-
arr = fp.read(100)
3497-
self.assertEqual(fp.tell(), 10102)
3498-
self.assertEqual(arr, txt[10002:10102])
3502+
arr = fp.read(read_length)
3503+
current_pos += read_length
3504+
self.assertEqual(fp.tell(), current_pos)
3505+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
34993506
self.assertEqual(fp._left, fp._compress_left)
3500-
d = sio.bytes_read - old_count
3501-
self.assertLessEqual(d, min_size)
3507+
read_count = sio.bytes_read - old_count
3508+
self.assertLessEqual(read_count, read_buffer_size)
35023509

35033510
# backward seek
35043511
old_count = sio.bytes_read
3505-
fp.seek(-5003, os.SEEK_CUR)
3506-
self.assertEqual(fp.tell(), 5099) # 5099 = 10102 - 5003
3512+
backward_seek_len = 5003
3513+
fp.seek(-backward_seek_len, os.SEEK_CUR)
3514+
current_pos -= backward_seek_len
3515+
self.assertEqual(fp.tell(), current_pos)
35073516
self.assertEqual(fp._left, fp._compress_left)
3508-
arr = fp.read(100)
3509-
self.assertEqual(fp.tell(), 5199)
3510-
self.assertEqual(arr, txt[5099:5199])
3517+
arr = fp.read(read_length)
3518+
current_pos += read_length
3519+
self.assertEqual(fp.tell(), current_pos)
3520+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
35113521
self.assertEqual(fp._left, fp._compress_left)
3512-
d = sio.bytes_read - old_count
3513-
self.assertLessEqual(d, min_size)
3522+
read_count = sio.bytes_read - old_count
3523+
self.assertLessEqual(read_count, read_buffer_size)
35143524

35153525
# eof flags test
35163526
fp.seek(0, os.SEEK_END)
3517-
self.assertTrue(fp._eof)
35183527
fp.seek(12345, os.SEEK_SET)
3519-
self.assertFalse(fp._eof)
3528+
current_pos = 12345
3529+
arr = fp.read(read_length)
3530+
current_pos += read_length
3531+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
35203532

35213533

35223534
if __name__ == "__main__":

Lib/zipfile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ def seek(self, offset, whence=os.SEEK_SET):
11621162
self._offset = buff_offset
11631163
read_offset = 0
11641164
# Fast seek uncompressed unencrypted file
1165-
elif self._compress_type == ZIP_STORED and self._decrypter is None:
1165+
elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset != 0:
11661166
# disable CRC checking after first seeking - it would be invalid
11671167
self._expected_crc = None
11681168
# seek actual file taking already buffered data into account

0 commit comments

Comments
 (0)