diff options
author | njsmith <njs@pobox.com> | 2012-07-06 14:01:54 -0700 |
---|---|---|
committer | njsmith <njs@pobox.com> | 2012-07-06 14:01:54 -0700 |
commit | 3b9a0fea12ae89fe6ce745d9af0beb3df17260b8 (patch) | |
tree | e5c78b184a72a1ceee5d5dba0351ccd1497b013e | |
parent | dd86cb378fb287e1e45ad8893097dc5a7f796aa8 (diff) | |
parent | 613589e2286b03171829bf4ff8cb5c9c863df4be (diff) | |
download | numpy-3b9a0fea12ae89fe6ce745d9af0beb3df17260b8.tar.gz |
Merge pull request #328 from yarikoptic/master
fix ticket #2178: "own" (to close) file handles in load() only if they were not opened before
-rw-r--r-- | numpy/lib/npyio.py | 3 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 45 |
2 files changed, 46 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 221529929..cb14e4963 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -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 @@ -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) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 8922070df..c539c040a 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -167,6 +167,51 @@ class TestSavezLoad(RoundtripTest, TestCase): 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) |