Skip to content

Commit

Permalink
Fix bug whereby iter_zip returned closed ZipExtFile instances
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnVinyard committed Mar 7, 2019
1 parent 1ac9cc1 commit fac876a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions featureflow/bytestream.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,22 @@ def iter_zip(fn):
for info in zf.filelist:
if not info.file_size:
continue
with zf.open(info.filename) as f:
yield ZipWrapper(f, info)
f = zf.open(info.filename)
yield ZipWrapper(f, info)


class ZipWrapper(object):
def __init__(self, zipfile, zipinfo):
self.zipinfo = zipinfo
self.zipfile = zipfile

def __enter__(self):
self.zipfile.__enter__()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.zipfile.__exit__(exc_type, exc_val, exc_tb)

@property
def file_size(self):
return self.zipinfo.file_size
Expand Down
16 changes: 15 additions & 1 deletion featureflow/test_bytestream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .bytestream import BytesWithTotalLength, ByteStream, ZipWrapper
from .bytestream import BytesWithTotalLength, ByteStream, ZipWrapper, iter_zip
import unittest2
import sys
import tempfile
Expand Down Expand Up @@ -136,3 +136,17 @@ def test_right_increment(self):
x = b'blah'
x += BytesWithTotalLength(b'fake', 100)
self.assertEqual(b'blahfake', x)


class IterZipTests(unittest2.TestCase):
def test_iter_zip_yields_open_zip_files(self):
bio = BytesIO()
filename = 'test.dat'
with zipfile.ZipFile(bio, mode='w') as zf:
zf.writestr(filename, b'content')
bio.seek(0)

with list(iter_zip(bio))[0] as z:
self.assertFalse(
z.zipfile.closed, 'zipfile should be open, but was closed')
self.assertEqual(z.zipfile.read(), b'content')

0 comments on commit fac876a

Please sign in to comment.