diff -r 031fc0231f3d Lib/test/test_zipfile.py --- a/Lib/test/test_zipfile.py Thu Jan 15 22:53:21 2015 +0100 +++ b/Lib/test/test_zipfile.py Fri Jan 16 21:30:08 2015 +0200 @@ -1668,6 +1668,34 @@ class LzmaTestsWithRandomBinaryFiles(Abs compression = zipfile.ZIP_LZMA +# Privide the tell() method but not seek() +class Tellable: + def __init__(self, fp): + self.fp = fp + self.offset = 0 + + def write(self, data): + self.offset += self.fp.write(data) + + def tell(self): + return self.offset + + def flush(self): + pass + +class UnseekableTests(unittest.TestCase): + def test_writestr_tellable(self): + f = io.BytesIO() + with zipfile.ZipFile(Tellable(f), 'w', zipfile.ZIP_STORED) as zipfp: + zipfp.writestr('ones', b'111') + zipfp.writestr('twos', b'222') + with zipfile.ZipFile(f, mode='r') as zipf: + with zipf.open('ones') as zopen: + self.assertEqual(zopen.read(), b'111') + with zipf.open('twos') as zopen: + self.assertEqual(zopen.read(), b'222') + + @requires_zlib class TestsWithMultipleOpens(unittest.TestCase): @classmethod diff -r 031fc0231f3d Lib/zipfile.py --- a/Lib/zipfile.py Thu Jan 15 22:53:21 2015 +0100 +++ b/Lib/zipfile.py Fri Jan 16 21:30:08 2015 +0200 @@ -1498,7 +1498,11 @@ class ZipFile: "Attempt to write to ZIP archive that was already closed") zinfo.file_size = len(data) # Uncompressed size - self.fp.seek(self.start_dir, 0) + try: + self.fp.seek(self.start_dir) + except (AttributeError, io.UnsupportedOperation): + # Some file-like objects can provide tell() but not seek() + pass zinfo.header_offset = self.fp.tell() # Start of header data if compress_type is not None: zinfo.compress_type = compress_type @@ -1543,7 +1547,11 @@ class ZipFile: try: if self.mode in ("w", "a") and self._didModify: # write ending records - self.fp.seek(self.start_dir, 0) + try: + self.fp.seek(self.start_dir) + except (AttributeError, io.UnsupportedOperation): + # Some file-like objects can provide tell() but not seek() + pass for zinfo in self.filelist: # write central directory dt = zinfo.date_time dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]