Author: Alastair McKinstry <mckinstry@debian.org>
Description: Python3 support. Code is python2,3 neutral.
Last-Updated: 2014-04-30
Forwarded: no

Index: gsw-3.0.3/gsw/gibbs/library.py
===================================================================
--- gsw-3.0.3.orig/gsw/gibbs/library.py	2014-04-16 16:44:47.716967955 +0100
+++ gsw-3.0.3/gsw/gibbs/library.py	2014-04-25 14:15:48.702418882 +0100
@@ -143,7 +143,7 @@
         p, lon, lat = np.broadcast_arrays(p, lon, lat)
         if p.ndim > 1:
             shape_in = p.shape
-            p, lon, lat = map(np.ravel, (p, lon, lat))
+            p, lon, lat = list(map(np.ravel, (p, lon, lat)))
             reshaped = True
         else:
             reshaped = False
@@ -432,7 +432,7 @@
         input_mask = input_mask | lon.mask
     if np.ma.is_masked(lat):
         input_mask = input_mask | lat.mask
-    SP, lon, lat = map(np.atleast_1d, (SP, lon, lat))
+    SP, lon, lat = list(map(np.atleast_1d, (SP, lon, lat)))
     SP, lon, lat = np.broadcast_arrays(SP, lon, lat)
     inds_baltic = in_Baltic(lon, lat)
     #SA_baltic = np.ma.masked_all(SP.shape, dtype=np.float)
@@ -488,7 +488,7 @@
     Modifications:
     2010-07-23. David Jackett, Trevor McDougall & Paul Barker
     """
-    SA, lon, lat = map(np.ma.masked_invalid, (SA, lon, lat))
+    SA, lon, lat = list(map(np.ma.masked_invalid, (SA, lon, lat)))
     lon, lat, SA = np.broadcast_arrays(lon, lat, SA)
     inds_baltic = in_Baltic(lon, lat)
     if not inds_baltic.sum():
@@ -543,7 +543,7 @@
     Modifications:
     2010-07-23. David Jackett, Trevor McDougall & Paul Barker
     """
-    SA, lon, lat = map(np.ma.masked_invalid, (SA, lon, lat))
+    SA, lon, lat = list(map(np.ma.masked_invalid, (SA, lon, lat)))
     lon, lat, SA = np.broadcast_arrays(lon, lat, SA)
     xb1, xb2, xb3 = 12.6, 7., 26.
     xb1a, xb3a = 45., 26.
Index: gsw-3.0.3/gsw/gibbs/practical_salinity.py
===================================================================
--- gsw-3.0.3.orig/gsw/gibbs/practical_salinity.py	2014-04-16 16:44:47.716967955 +0100
+++ gsw-3.0.3/gsw/gibbs/practical_salinity.py	2014-04-30 21:33:54.525516302 +0100
@@ -51,7 +51,7 @@
      6.797409608973845e-7, 3.345074990451475e-10, 8.285687652694768e-13)
 k = 0.0162
 
-a, b, c, d, e, P, q, r, u, k = map(np.asarray, (a, b, c, d, e, P, q, r, u, k))
+a, b, c, d, e, P, q, r, u, k = list(map(np.asarray, (a, b, c, d, e, P, q, r, u, k)))
 
 
 def C_from_SP(SP, t, p):
Index: gsw-3.0.3/gsw/gibbs/conversions.py
===================================================================
--- gsw-3.0.3.orig/gsw/gibbs/conversions.py	2014-04-16 16:44:47.716967955 +0100
+++ gsw-3.0.3/gsw/gibbs/conversions.py	2014-04-25 14:14:23.523891133 +0100
@@ -74,11 +74,11 @@
     # needed, it should not just be for the "from_SP" functions.
     if False:
         if ((p < -1.5) | (p > 12000)).any():
-            raise(Exception, 'Sstar_from_SP: pressure is out of range')
+            raise Exception('Sstar_from_SP: pressure is out of range')
         if ((lon < 0) | (lon > 360)).any():
-            raise(Exception, 'Sstar_from_SP: longitude is out of range')
+            raise Exception('Sstar_from_SP: longitude is out of range')
         if (np.abs(lat) > 90).any():
-            raise(Exception, 'Sstar_from_SP: latitude is out of range')
+            raise Exception('Sstar_from_SP: latitude is out of range')
 
     SP = np.maximum(SP, 0)  # Works on masked array also.
 
Index: gsw-3.0.3/gsw/test/check_functions.py
===================================================================
--- gsw-3.0.3.orig/gsw/test/check_functions.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/test/check_functions.py	2014-04-25 14:23:33.914081079 +0100
@@ -1,3 +1,6 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import division
 import os
 import sys
 import logging
@@ -161,17 +164,17 @@
              isinstance(f.exception, exc)]
     ex_dict[exc] = elist
 
-print "\n%s tests were translated from gsw_check_functions.m" % len(checks)
-print "\n%s tests ran with no error and with correct output" % len(passes)
-print "\n%s tests had an output mismatch:" % len(failures)
-print " ", "\n  ".join(failures)
+print("\n%s tests were translated from gsw_check_functions.m" % len(checks))
+print("\n%s tests ran with no error and with correct output" % len(passes))
+print("\n%s tests had an output mismatch:" % len(failures))
+print(" ", "\n  ".join(failures))
 
-print "\n%s exceptions were raised as follows:" % len(run_problems)
+print("\n%s exceptions were raised as follows:" % len(run_problems))
 for exc in etypes:
-    print "  ", exc.__name__
+    print("  ", exc.__name__)
     strings = ["     %s : %s" % e for e in ex_dict[exc]]
-    print "\n".join(strings)
-    print ""
+    print("\n".join(strings))
+    print("")
 
 checkbunch = Bunch([(c.name, c) for c in checks])
 
Index: gsw-3.0.3/gsw/test/signatures.py
===================================================================
--- gsw-3.0.3.orig/gsw/test/signatures.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/test/signatures.py	2014-04-25 14:25:16.433941753 +0100
@@ -161,8 +161,8 @@
 
 def help_chunk(helptuple, sect):
     lines, secdict = helptuple
-    sections = secdict.keys()
-    ind = secdict.values()
+    sections = list(secdict.keys())
+    ind = list(secdict.values())
     try:
         i0 = sections.index(sect)
     except ValueError:
Index: gsw-3.0.3/gsw/test/test_octave.py
===================================================================
--- gsw-3.0.3.orig/gsw/test/test_octave.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/test/test_octave.py	2014-04-25 14:28:18.259883380 +0100
@@ -188,12 +188,18 @@
     # absolute_salinity_sstar_ct.py
     'SA_from_SP': (gsw.SA_from_SP, ('SP', 'p', 'lon', 'lat'))})
 
+def iteritems(d):
+    'Factor-out Py2-to-3 differences in dictionary item iterator methods'
+    try:
+         return d.iteritems()
+    except AttributeError:
+         return d.items()
 
 if __name__ == '__main__':
     outcomes = ['passed', 'no_octave', 'no_python', 'failed', 'no_comparison']
     results = dict([(k, list()) for k in outcomes])
 
-    for name, (function, args) in library.iteritems():
+    for name, (function, args) in iteritems(library):
         ret = compare_results(name=name, function=function, args=args)
         results[ret].append(name)
 
Index: gsw-3.0.3/gsw/test/test_tuples.py
===================================================================
--- gsw-3.0.3.orig/gsw/test/test_tuples.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/test/test_tuples.py	2014-04-30 21:38:30.534673085 +0100
@@ -2,6 +2,8 @@
 
 """Unit check for standard profiles for the Gibbs Sea Water python package."""
 
+from __future__ import division
+
 import os
 import sys
 import unittest
@@ -58,7 +60,7 @@
     maxdiff = np.nanmax(abs(out - getattr(cv, func)))
     try:
         self.assertTrue(maxdiff < getattr(cv, func + '_ca'))
-    except AssertionError, e:
+    except AssertionError as e:
         raise AssertionError("Error in %s %s" % (func, e.args))
 
 
@@ -95,5 +97,5 @@
                                   (gsw_cv.CT_pt - gsw_cf.CT_pt) >=
                                   gsw_cv.CT_pt_ca)
 if gsw_cf.ICT_first_deriv:
-    print(2, 'gsw_CT_first_derivatives:   Failed\n')
+    print((2, 'gsw_CT_first_derivatives:   Failed\n'))
     gsw_cf.gsw_chks = 0
Index: gsw-3.0.3/gsw/utilities/list_npz.py
===================================================================
--- gsw-3.0.3.orig/gsw/utilities/list_npz.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/utilities/list_npz.py	2014-04-30 21:39:25.946920917 +0100
@@ -4,14 +4,14 @@
 
 The filename is the sole command-line argument.
 """
-
+from __future__ import division
 import sys
 import numpy as np
 
 fname = sys.argv[1]
 
 dat = np.load(fname)
-keys = dat.keys()
+keys = list(dat.keys())
 keys.sort()
 klens = [len(str(k)) for k in keys]
 klen = max(klens)
@@ -21,6 +21,6 @@
 slist = [str_fmt.format(k, dat[k].dtype, dat[k].shape, klen=klen)
          for k in keys]
 
-print ''.join(slist)
+print (''.join(slist))
 
 
Index: gsw-3.0.3/gsw/utilities/mat2npz.py
===================================================================
--- gsw-3.0.3.orig/gsw/utilities/mat2npz.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/utilities/mat2npz.py	2014-04-30 21:41:56.606470519 +0100
@@ -12,7 +12,7 @@
 #
 # obs:
 #
-
+from __future__ import division
 import numpy as np
 
 from gsw.utilities import loadmatbunch
Index: gsw-3.0.3/gsw/utilities/utilities.py
===================================================================
--- gsw-3.0.3.orig/gsw/utilities/utilities.py	2014-04-16 16:44:27.000000000 +0100
+++ gsw-3.0.3/gsw/utilities/utilities.py	2014-04-30 21:49:14.660050352 +0100
@@ -69,11 +69,11 @@
         """
         if fmt is None:
             fmt = self.str_fmt
-        klens = [len(str(k)) for k in self.keys()]
-        vlens = [len(str(v)) for v in self.values()]
+        klens = [len(str(k)) for k in list(self.keys())]
+        vlens = [len(str(v)) for v in list(self.values())]
         klen = min(20, max(klens))
         vlen = min(40, max(vlens))
-        items = self.items()
+        items = list(self.items())
         items.sort()
         slist = [fmt.format(k, v, klen=klen, vlen=vlen) for k, v in items]
         return ''.join(slist)
@@ -94,7 +94,7 @@
             newkw.update(d)
         newkw.update(kw)
         self._check_strict(strict, newkw)
-        dsub = dict([(k, v) for (k, v) in newkw.items() if k in self])
+        dsub = dict([(k, v) for (k, v) in list(newkw.items()) if k in self])
         self.update(dsub)
 
     def update_None(self, *args, **kw):
@@ -108,7 +108,7 @@
             newkw.update(d)
         newkw.update(kw)
         self._check_strict(strict, newkw)
-        dsub = dict([(k, v) for (k, v) in newkw.items() if k in self and
+        dsub = dict([(k, v) for (k, v) in list(newkw.items()) if k in self and
                      self[k] is None])
         self.update(dsub)
 
@@ -118,7 +118,7 @@
             if bad:
                 bk = list(bad)
                 bk.sort()
-                ek = self.keys()
+                ek = list(self.keys())
                 ek.sort()
                 raise KeyError(
                     "Update keys %s don't match existing keys %s" % (bk, ek))
@@ -145,6 +145,13 @@
 _npz_cache = Cache_npz()
 
 
+def iteritems(d):
+    'Factor-out Py2-to-3 differences in dictionary item iterator methods'
+    try:
+         return d.iteritems()
+    except AttributeError:
+         return d.items()
+
 def repair_npzfile_with_objects(infile, outfile):
     """
     Read an npz file written based on scipy.io.loadmat,
@@ -158,7 +165,7 @@
     """
     dat = np.load(infile)
     out = dict()
-    for k, v in dat.iteritems():
+    for k, v in iteritems(dat):
         if v.dtype.kind == 'O':
             v = v.item()
         out[k] = v
@@ -276,6 +283,8 @@
     except UnicodeEncodeError:
         dt = arr.dtype.newbyteorder('S')
         return unicode(arr.view(dt))
+    except NameError:
+        return str(arr)
 
 
 def crunch(arr, masked=True):
@@ -326,7 +335,7 @@
     out = Bunch()
     fobj = open(fname, 'rb')
     xx = loadmat(fobj)
-    keys = [k for k in xx.keys() if not k.startswith("__")]
+    keys = [k for k in list(xx.keys()) if not k.startswith("__")]
     for k in keys:
         out[k] = structured_to_bunch(xx[k], masked=masked)
     fobj.close()
