@@ -3463,7 +3463,9 @@ def read(self, size=-1):
3463
3463
3464
3464
3465
3465
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 ):
3467
3469
3468
3470
sio = StatIO ()
3469
3471
# 20000 bytes
@@ -3472,12 +3474,13 @@ def test_random_read(self):
3472
3474
# The seek length must be greater than ZipExtFile.MIN_READ_SIZE
3473
3475
# as `ZipExtFile._read2()` reads in blocks of this size and we
3474
3476
# 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
3478
3480
# The read length must be less than MIN_READ_SIZE, since we assume that
3479
3481
# 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
3481
3484
3482
3485
with zipfile .ZipFile (sio , "w" , compression = zipfile .ZIP_STORED ) as zipf :
3483
3486
zipf .writestr ("foo.txt" , txt )
@@ -3490,33 +3493,42 @@ def test_random_read(self):
3490
3493
3491
3494
# forward seek
3492
3495
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 )
3495
3501
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 ])
3499
3506
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 )
3502
3509
3503
3510
# backward seek
3504
3511
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 )
3507
3516
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 ])
3511
3521
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 )
3514
3524
3515
3525
# eof flags test
3516
3526
fp .seek (0 , os .SEEK_END )
3517
- self .assertTrue (fp ._eof )
3518
3527
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 ])
3520
3532
3521
3533
3522
3534
if __name__ == "__main__" :
0 commit comments