[Scipy-svn] r6968 - in trunk/scipy/io/matlab: . tests

scipy-svn@scip... scipy-svn@scip...
Sun Nov 28 09:44:33 CST 2010


Author: ptvirtan
Date: 2010-11-28 09:44:33 -0600 (Sun, 28 Nov 2010)
New Revision: 6968

Modified:
   trunk/scipy/io/matlab/mio.py
   trunk/scipy/io/matlab/tests/test_mio.py
Log:
BUG: io/matlab: close opened stream after reading in loadmat() (fixes #1039)

Modified: trunk/scipy/io/matlab/mio.py
===================================================================
--- trunk/scipy/io/matlab/mio.py	2010-11-28 15:28:17 UTC (rev 6967)
+++ trunk/scipy/io/matlab/mio.py	2010-11-28 15:44:33 UTC (rev 6968)
@@ -154,6 +154,8 @@
         mdict.update(matfile_dict)
     else:
         mdict = matfile_dict
+    if isinstance(file_name, basestring):
+        MR.mat_stream.close()
     return mdict
 
 @docfiller

Modified: trunk/scipy/io/matlab/tests/test_mio.py
===================================================================
--- trunk/scipy/io/matlab/tests/test_mio.py	2010-11-28 15:28:17 UTC (rev 6967)
+++ trunk/scipy/io/matlab/tests/test_mio.py	2010-11-28 15:44:33 UTC (rev 6968)
@@ -5,6 +5,7 @@
 
 '''
 import sys
+import os
 from os.path import join as pjoin, dirname
 from glob import glob
 if sys.version_info[0] >= 3:
@@ -348,7 +349,35 @@
                               expected['x'].todense(),
                               err_msg=repr(actual))
 
+def test_multiple_open():
+    # Ticket #1039, on Windows: check that files are not left open
+    tmpdir = mkdtemp()
+    try:
+        x = dict(x=np.zeros((2, 2)))
 
+        fname = pjoin(tmpdir, "a.mat")
+
+        # Check that file is not left open
+        savemat(fname, x)
+        os.unlink(fname)
+        savemat(fname, x)
+
+        loadmat(fname)
+        os.unlink(fname)
+
+        # Check that stream is left open
+        f = open(fname, 'wb')
+        savemat(f, x)
+        f.seek(0)
+        f.close()
+
+        f = open(fname, 'rb')
+        loadmat(f)
+        f.seek(0)
+        f.close()
+    finally:
+        shutil.rmtree(tmpdir)
+
 def test_mat73():
     # Check any hdf5 files raise an error
     filenames = glob(



More information about the Scipy-svn mailing list