Skip to content
3 changes: 1 addition & 2 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ def load(file, mmap_mode=None):
own_fid = True
elif isinstance(file, gzip.GzipFile):
fid = seek_gzip_factory(file)
own_fid = True
else:
fid = file

Expand All @@ -371,7 +370,7 @@ def load(file, mmap_mode=None):
fid.seek(-N, 1) # back-up
if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz)
own_fid = False
return NpzFile(fid, own_fid=True)
return NpzFile(fid, own_fid=own_fid)
elif magic == format.MAGIC_PREFIX: # .npy file
if mmap_mode:
return format.open_memmap(file, mode=mmap_mode)
Expand Down
45 changes: 45 additions & 0 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,51 @@ def writer(error_list):
if errors:
raise AssertionError(errors)

def test_not_closing_opened_fid(self):
# Test that issue #2178 is fixed:
# verify could seek on 'loaded' file

fd, tmp = mkstemp(suffix='.npz')
os.close(fd)
try:
fp = open(tmp, 'wb')
np.savez(fp, data='LOVELY LOAD')
fp.close()

fp = open(tmp, 'rb', 10000)
fp.seek(0)
assert_(not fp.closed)
_ = np.load(fp)['data']
assert_(not fp.closed) # must not get closed by .load(opened fp)
fp.seek(0)
assert_(not fp.closed)

finally:
os.remove(tmp)

def test_closing_fid(self):
# Test that issue #1517 (too many opened files) remains closed
# It might be a "week" test since failed to get triggered on
# e.g. Debian sid of 2012 Jul 05 but was reported to
# trigger the failure on Ubuntu 10.04:
# http://projects.scipy.org/numpy/ticket/1517#comment:2
fd, tmp = mkstemp(suffix='.npz')
os.close(fd)

try:
fp = open(tmp, 'wb')
np.savez(fp, data='LOVELY LOAD')
fp.close()

for i in range(1, 1025):
try:
np.load(tmp)["data"]
except Exception, e:
raise AssertionError("Failed to load data from a file: %s" % e)
finally:
os.remove(tmp)


class TestSaveTxt(TestCase):
def test_array(self):
a = np.array([[1, 2], [3, 4]], float)
Expand Down