File tree Expand file tree Collapse file tree 3 files changed +13
-2
lines changed Expand file tree Collapse file tree 3 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -1969,10 +1969,16 @@ def test_is_zip_valid_file(self):
1969
1969
zip_contents = fp .read ()
1970
1970
# - passing a file-like object
1971
1971
fp = io .BytesIO ()
1972
- fp .write (zip_contents )
1972
+ end = fp .write (zip_contents )
1973
+ self .assertEqual (fp .tell (), end )
1974
+ mid = end // 2
1975
+ fp .seek (mid , 0 )
1973
1976
self .assertTrue (zipfile .is_zipfile (fp ))
1974
- fp .seek (0 , 0 )
1977
+ # check that the position is left unchanged after the call
1978
+ # see: https://github.com/python/cpython/issues/122356
1979
+ self .assertEqual (fp .tell (), mid )
1975
1980
self .assertTrue (zipfile .is_zipfile (fp ))
1981
+ self .assertEqual (fp .tell (), mid )
1976
1982
1977
1983
def test_non_existent_file_raises_OSError (self ):
1978
1984
# make sure we don't raise an AttributeError when a partially-constructed
Original file line number Diff line number Diff line change @@ -241,7 +241,9 @@ def is_zipfile(filename):
241
241
result = False
242
242
try :
243
243
if hasattr (filename , "read" ):
244
+ pos = filename .tell ()
244
245
result = _check_zipfile (fp = filename )
246
+ filename .seek (pos )
245
247
else :
246
248
with open (filename , "rb" ) as fp :
247
249
result = _check_zipfile (fp )
Original file line number Diff line number Diff line change
1
+ Guarantee that the position of a file-like object passed to
2
+ :func: `zipfile.is_zipfile ` is left untouched after the call.
3
+ Patch by Bénédikt Tran.
You can’t perform that action at this time.
0 commit comments