From 09a52ed47bb26498c97a579ce1147861df696d84 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 28 Mar 2013 17:13:53 -0600 Subject: 2to3: Apply `imports` fixer. The `imports` fixer deals with the standard packages that have been renamed, removed, or methods that have moved. cPickle -- removed, use pickle commands -- removed, getoutput, getstatusoutput moved to subprocess urlparse -- removed, urlparse moved to urllib.parse cStringIO -- removed, use StringIO or io.StringIO copy_reg -- renamed copyreg _winreg -- renamed winreg ConfigParser -- renamed configparser __builtin__ -- renamed builtins In the case of `cPickle`, it is imported as `pickle` when python < 3 and performance may be a consideration, but otherwise plain old `pickle` is used. Dealing with `StringIO` is a bit tricky. There is an `io.StringIO` function in the `io` module, available since Python 2.6, but it expects unicode whereas `StringIO.StringIO` expects ascii. The Python 3 equivalent is then `io.BytesIO`. What I have done here is used BytesIO for anything that is emulating a file for testing purposes. That is more explicit than using a redefined StringIO as was done before we dropped support for Python 2.4 and 2.5. Closes #3180. --- numpy/lib/format.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'numpy/lib/format.py') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 21b0607e7..dedfabfd2 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -136,13 +136,16 @@ alternatives, is described fully in the "npy-format" NEP. """ from __future__ import division, absolute_import -import cPickle - import numpy import sys from numpy.lib.utils import safe_eval from numpy.compat import asbytes, isfileobj +if sys.version_info[0] >= 3: + import pickle +else: + import cPickle as pickle + MAGIC_PREFIX = asbytes('\x93NUMPY') MAGIC_LEN = len(MAGIC_PREFIX) + 2 @@ -399,7 +402,7 @@ def write_array(fp, array, version=(1,0)): if array.dtype.hasobject: # We contain Python objects so we cannot write out the data directly. # Instead, we will pickle it out with version 2 of the pickle protocol. - cPickle.dump(array, fp, protocol=2) + pickle.dump(array, fp, protocol=2) elif array.flags.f_contiguous and not array.flags.c_contiguous: if isfileobj(fp): array.T.tofile(fp) @@ -447,7 +450,7 @@ def read_array(fp): # Now read the actual data. if dtype.hasobject: # The array contained Python objects. We need to unpickle the data. - array = cPickle.load(fp) + array = pickle.load(fp) else: if isfileobj(fp): # We can use the fast fromfile() function. -- cgit v1.2.1