##// END OF EJS Templates
Backport PR #4217: avoid importing numpy at the module level...
MinRK -
Show More
@@ -25,11 +25,6 b' try:'
25 except ImportError:
25 except ImportError:
26 import pickle
26 import pickle
27
27
28 try:
29 import numpy
30 except:
31 numpy = None
32
33 import codeutil # This registers a hook when it's imported
28 import codeutil # This registers a hook when it's imported
34 import py3compat
29 import py3compat
35 from importstring import import_item
30 from importstring import import_item
@@ -163,6 +158,7 b' class CannedClass(CannedObject):'
163
158
164 class CannedArray(CannedObject):
159 class CannedArray(CannedObject):
165 def __init__(self, obj):
160 def __init__(self, obj):
161 from numpy import ascontiguousarray
166 self.shape = obj.shape
162 self.shape = obj.shape
167 self.dtype = obj.dtype.descr if obj.dtype.fields else obj.dtype.str
163 self.dtype = obj.dtype.descr if obj.dtype.fields else obj.dtype.str
168 if sum(obj.shape) == 0:
164 if sum(obj.shape) == 0:
@@ -170,16 +166,17 b' class CannedArray(CannedObject):'
170 self.buffers = [pickle.dumps(obj, -1)]
166 self.buffers = [pickle.dumps(obj, -1)]
171 else:
167 else:
172 # ensure contiguous
168 # ensure contiguous
173 obj = numpy.ascontiguousarray(obj, dtype=None)
169 obj = ascontiguousarray(obj, dtype=None)
174 self.buffers = [buffer(obj)]
170 self.buffers = [buffer(obj)]
175
171
176 def get_object(self, g=None):
172 def get_object(self, g=None):
173 from numpy import frombuffer
177 data = self.buffers[0]
174 data = self.buffers[0]
178 if sum(self.shape) == 0:
175 if sum(self.shape) == 0:
179 # no shape, we just pickled it
176 # no shape, we just pickled it
180 return pickle.loads(data)
177 return pickle.loads(data)
181 else:
178 else:
182 return numpy.frombuffer(data, dtype=self.dtype).reshape(self.shape)
179 return frombuffer(data, dtype=self.dtype).reshape(self.shape)
183
180
184
181
185 class CannedBytes(CannedObject):
182 class CannedBytes(CannedObject):
@@ -225,7 +222,7 b' def _import_mapping(mapping, original=None):'
225 except Exception:
222 except Exception:
226 if original and key not in original:
223 if original and key not in original:
227 # only message on user-added classes
224 # only message on user-added classes
228 log.error("cannning class not importable: %r", key, exc_info=True)
225 log.error("canning class not importable: %r", key, exc_info=True)
229 mapping.pop(key)
226 mapping.pop(key)
230 else:
227 else:
231 mapping[cls] = mapping.pop(key)
228 mapping[cls] = mapping.pop(key)
General Comments 0
You need to be logged in to leave comments. Login now