[Scipy-svn] r6768 - trunk/scipy/io/matlab

scipy-svn@scip... scipy-svn@scip...
Sat Sep 11 20:02:36 CDT 2010


Author: ptvirtan
Date: 2010-09-11 20:02:35 -0500 (Sat, 11 Sep 2010)
New Revision: 6768

Modified:
   trunk/scipy/io/matlab/mio.py
   trunk/scipy/io/matlab/mio4.py
   trunk/scipy/io/matlab/mio5.py
   trunk/scipy/io/matlab/mio5_utils.pyx
   trunk/scipy/io/matlab/miobase.py
Log:
3K: io/matlab: port matlab modules to Py3 (mainly str vs. bytes issues)

Modified: trunk/scipy/io/matlab/mio.py
===================================================================
--- trunk/scipy/io/matlab/mio.py	2010-09-12 01:02:17 UTC (rev 6767)
+++ trunk/scipy/io/matlab/mio.py	2010-09-12 01:02:35 UTC (rev 6768)
@@ -8,6 +8,8 @@
 import sys
 import warnings
 
+from numpy.compat import asbytes
+
 from miobase import get_matfile_version, docfiller
 from mio4 import MatFile4Reader, MatFile4Writer
 from mio5 import MatFile5Reader, MatFile5Writer
@@ -178,7 +180,7 @@
         file_stream = open(file_name, 'wb')
     else:
         try:
-            file_name.write('')
+            file_name.write(asbytes(''))
         except AttributeError:
             raise IOError, 'Writer needs file name or writeable '\
                            'file-like object'

Modified: trunk/scipy/io/matlab/mio4.py
===================================================================
--- trunk/scipy/io/matlab/mio4.py	2010-09-12 01:02:17 UTC (rev 6767)
+++ trunk/scipy/io/matlab/mio4.py	2010-09-12 01:02:35 UTC (rev 6768)
@@ -4,6 +4,7 @@
 import warnings
 
 import numpy as np
+from numpy.compat import asbytes, asstr
 
 import scipy.sparse
 
@@ -96,7 +97,7 @@
     def read_header(self):
         ''' Reads and return header for variable '''
         data = read_dtype(self.mat_stream, self.dtypes['header'])
-        name = self.mat_stream.read(int(data['namlen'])).strip('\x00')
+        name = self.mat_stream.read(int(data['namlen'])).strip(asbytes('\x00'))
         if data['mopt'] < 0 or  data['mopt'] > 5000:
             ValueError, 'Mat 4 mopt wrong format, byteswapping problem?'
         M,rest = divmod(data['mopt'], 1000)
@@ -149,7 +150,7 @@
             num_bytes *= d
         arr = np.ndarray(shape=dims,
                          dtype=dt,
-                         buffer=self.mat_stream.read(num_bytes),
+                         buffer=self.mat_stream.read(int(num_bytes)),
                          order='F')
         if copy:
             arr = arr.copy()
@@ -295,7 +296,7 @@
         mdict = {}
         while not self.end_of_stream():
             hdr, next_position = self.read_var_header()
-            name = hdr.name
+            name = asstr(hdr.name)
             if variable_names and name not in variable_names:
                 self.mat_stream.seek(next_position)
                 continue
@@ -376,7 +377,7 @@
         header['imagf'] = imagf
         header['namlen'] = len(name) + 1
         self.write_bytes(header)
-        self.write_string(name + '\0')
+        self.write_string(asbytes(name + '\0'))
 
     def write(self, arr, name):
         ''' Write matrix `arr`, with name `name`

Modified: trunk/scipy/io/matlab/mio5.py
===================================================================
--- trunk/scipy/io/matlab/mio5.py	2010-09-12 01:02:17 UTC (rev 6767)
+++ trunk/scipy/io/matlab/mio5.py	2010-09-12 01:02:35 UTC (rev 6768)
@@ -75,10 +75,14 @@
 import time
 import sys
 import zlib
-from cStringIO import StringIO
+if sys.version_info[0] >= 3:
+    from io import BytesIO
+else:
+    from cStringIO import StringIO as BytesIO
 import warnings
 
 import numpy as np
+from numpy.compat import asbytes, asstr
 
 import scipy.sparse
 
@@ -291,13 +295,13 @@
         self.mat_stream.seek(126)
         mi = self.mat_stream.read(2)
         self.mat_stream.seek(0)
-        return mi == 'IM' and '<' or '>'
+        return mi == asbytes('IM') and '<' or '>'
 
     def read_file_header(self):
         ''' Read in mat 5 file header '''
         hdict = {}
         hdr = read_dtype(self.mat_stream, self.dtypes['file_header'])
-        hdict['__header__'] = hdr['description'].item().strip(' \t\n\000')
+        hdict['__header__'] = hdr['description'].item().strip(asbytes(' \t\n\000'))
         v_major = hdr['version'] >> 8
         v_minor = hdr['version'] & 0xFF
         hdict['__version__'] = '%d.%d' % (v_major, v_minor)
@@ -353,9 +357,9 @@
             # incomplete stream.  See discussion at
             # http://bugs.python.org/issue8672
             dcor = zlib.decompressobj()
-            stream = StringIO(dcor.decompress(data))
+            stream = BytesIO(dcor.decompress(data))
             # Check the stream is not so broken as to leave cruft behind
-            assert dcor.flush() == ''
+            assert dcor.flush() == asbytes('')
             del data
             self._matrix_reader.set_stream(stream)
             mdtype, byte_count = self._matrix_reader.read_full_tag()
@@ -402,7 +406,7 @@
         mdict['__globals__'] = []
         while not self.end_of_stream():
             hdr, next_position = self.read_var_header()
-            name = hdr.name
+            name = asstr(hdr.name)
             if name == '':
                 # can only be a matlab 7 function workspace
                 name = '__function_workspace__'
@@ -569,7 +573,7 @@
         # pad to next 64-bit boundary
         bc_mod_8 = byte_count % 8
         if bc_mod_8:
-            self.file_stream.write('\x00' * (8-bc_mod_8))
+            self.file_stream.write(asbytes('\x00') * (8-bc_mod_8))
 
     def write_header(self,
                      shape, 
@@ -876,13 +880,13 @@
                 continue
             is_global = name in self.global_vars
             if self.do_compression:
-                stream = StringIO()
+                stream = BytesIO()
                 self._matrix_writer.file_stream = stream
-                self._matrix_writer.write_top(var, name, is_global)
+                self._matrix_writer.write_top(var, asbytes(name), is_global)
                 out_str = zlib.compress(stream.getvalue())
                 tag = np.empty((), mdtypes_template['tag_full'])
                 tag['mdtype'] = miCOMPRESSED
                 tag['byte_count'] = len(out_str)
                 self.file_stream.write(tag.tostring() + out_str)
             else: # not compressing
-                self._matrix_writer.write_top(var, name, is_global)
+                self._matrix_writer.write_top(var, asbytes(name), is_global)

Modified: trunk/scipy/io/matlab/mio5_utils.pyx
===================================================================
--- trunk/scipy/io/matlab/mio5_utils.pyx	2010-09-12 01:02:17 UTC (rev 6767)
+++ trunk/scipy/io/matlab/mio5_utils.pyx	2010-09-12 01:02:35 UTC (rev 6768)
@@ -27,6 +27,7 @@
     PyBytes_FromStringAndSize
 
 import numpy as np
+from numpy.compat import asbytes, asstr
 cimport numpy as cnp
 
 cdef extern from "numpy/arrayobject.h":
@@ -641,7 +642,7 @@
         elif mc == mxSTRUCT_CLASS:
             arr = self.read_struct(header)
         elif mc == mxOBJECT_CLASS: # like structs, but with classname
-            classname = self.read_int8_string()
+            classname = asstr(self.read_int8_string())
             arr = self.read_struct(header)
             arr = mio5p.MatlabObject(arr, classname)
         elif mc == mxFUNCTION_CLASS: # just a matrix of struct type
@@ -757,7 +758,7 @@
                              % mdtype)
         uc_str = data.decode(codec)
         # cast to array to deal with 2, 4 byte width characters
-        arr = np.array(uc_str)
+        arr = np.array(uc_str, dtype='U')
         dt = self.U1_dtype
         # could take this to numpy C-API level, but probably not worth
         # it
@@ -802,7 +803,7 @@
         cdef char *n_ptr = names
         for i in range(n_names):
             name = PyBytes_FromString(n_ptr)
-            field_names.append(name)
+            field_names.append(asstr(name))
             n_ptr += namelength
         n_names_ptr[0] = n_names
         return field_names

Modified: trunk/scipy/io/matlab/miobase.py
===================================================================
--- trunk/scipy/io/matlab/miobase.py	2010-09-12 01:02:17 UTC (rev 6767)
+++ trunk/scipy/io/matlab/miobase.py	2010-09-12 01:02:35 UTC (rev 6768)
@@ -3,8 +3,15 @@
 """
 Base classes for matlab (TM) file stream reading
 """
+import sys
 import numpy as np
+from numpy.compat import asbytes
 
+if sys.version_info[0] >= 3:
+    byteord = int
+else:
+    byteord = ord
+
 from scipy.misc import doccer
 
 import byteordercodes as boc
@@ -201,9 +208,9 @@
     fileobj.seek(124)
     tst_str = fileobj.read(4)
     fileobj.seek(0)
-    maj_ind = int(tst_str[2] == 'I')
-    maj_val = ord(tst_str[maj_ind])
-    min_val = ord(tst_str[1-maj_ind])
+    maj_ind = int(tst_str[2] == asbytes('I')[0])
+    maj_val = byteord(tst_str[maj_ind])
+    min_val = byteord(tst_str[1-maj_ind])
     ret = (maj_val, min_val)
     if maj_val in (1, 2):
         return ret



More information about the Scipy-svn mailing list