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

scipy-svn@scip... scipy-svn@scip...
Fri Dec 5 20:14:54 CST 2008


Author: matthew.brett@gmail.com
Date: 2008-12-05 20:14:52 -0600 (Fri, 05 Dec 2008)
New Revision: 5228

Modified:
   trunk/scipy/io/matlab/mio.py
   trunk/scipy/io/matlab/mio5.py
   trunk/scipy/io/matlab/tests/test_mio.py
Log:
Added neater field name length check, with test, thanks to Vebjorn Ljosa and Lee Kamentsky

Modified: trunk/scipy/io/matlab/mio.py
===================================================================
--- trunk/scipy/io/matlab/mio.py	2008-12-05 11:40:20 UTC (rev 5227)
+++ trunk/scipy/io/matlab/mio.py	2008-12-06 02:14:52 UTC (rev 5228)
@@ -150,7 +150,7 @@
     elif format == '5':
         MW = MatFile5Writer(file_stream, unicode_strings=True)
     else:
-        raise ValueError, 'Format should be 4 or 5'
+        raise ValueError("Format should be '4' or '5'")
     MW.put_variables(mdict)
     if file_is_string:
         file_stream.close()

Modified: trunk/scipy/io/matlab/mio5.py
===================================================================
--- trunk/scipy/io/matlab/mio5.py	2008-12-05 11:40:20 UTC (rev 5227)
+++ trunk/scipy/io/matlab/mio5.py	2008-12-06 02:14:52 UTC (rev 5228)
@@ -1,4 +1,10 @@
 ''' Classes for read / write of matlab (TM) 5 files
+
+The matfile specification last found here:
+
+http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf
+
+(as of December 5 2008)
 '''
 
 # Small fragments of current code adapted from matfile.py by Heiko
@@ -170,7 +176,7 @@
 
     We will deprecate this method of holding struct information in a
     future version of scipy, in favor of the recarray method (see
-    loadmat doctstring)
+    loadmat docstring)
     '''
     pass
 
@@ -808,9 +814,14 @@
     def write_fields(self):
         # write fieldnames
         fieldnames = [f[0] for f in self.arr.dtype.descr]
-        self.write_element(np.array([32], dtype='i4'))
-        self.write_element(np.array(fieldnames, dtype='S32'),
-                           mdtype=miINT8)
+        length = max([len(fieldname) for fieldname in fieldnames])+1
+        if length > 32:
+            raise ValueError(
+                "Field names are restricted to 64 characters in Matlab")
+        self.write_element(np.array([length], dtype='i4'))
+        self.write_element(
+            np.array(fieldnames, dtype='S%d'%(length)),
+            mdtype=miINT8)
         A = np.atleast_2d(self.arr).flatten('F')
         MWG = Mat5WriterGetter(self.file_stream,
                                self.unicode_strings)

Modified: trunk/scipy/io/matlab/tests/test_mio.py
===================================================================
--- trunk/scipy/io/matlab/tests/test_mio.py	2008-12-05 11:40:20 UTC (rev 5227)
+++ trunk/scipy/io/matlab/tests/test_mio.py	2008-12-06 02:14:52 UTC (rev 5228)
@@ -337,3 +337,16 @@
 def test_regression_653():
     """Regression test for #653."""
     assert_raises(TypeError, savemat, StringIO(), {'d':{1:2}}, format='5')
+
+def test_structname_len():
+    # Test limit for length of field names in structs
+    lim = 31
+    fldname = 'a' * lim
+    st1 = np.zeros((1,1), dtype=[(fldname, object)])
+    mat_stream = StringIO()
+    savemat(StringIO(), {'longstruct': st1}, format='5')
+    fldname = 'a' * (lim+1)
+    st1 = np.zeros((1,1), dtype=[(fldname, object)])
+    assert_raises(ValueError, savemat, StringIO(), 
+                  {'longstruct': st1}, format='5')
+    



More information about the Scipy-svn mailing list