From 59b9bf3b980579c2774908d6ce853f1ff948cd13 2013-12-24 21:01:10 From: MinRK Date: 2013-12-24 21:01:10 Subject: [PATCH] Backport PR #4217: avoid importing numpy at the module level in utils.pickleutil With this change, numpy is not in sys.modules at kernel startup. --- diff --git a/IPython/utils/pickleutil.py b/IPython/utils/pickleutil.py index 04ac882..61315c1 100644 --- a/IPython/utils/pickleutil.py +++ b/IPython/utils/pickleutil.py @@ -25,11 +25,6 @@ try: except ImportError: import pickle -try: - import numpy -except: - numpy = None - import codeutil # This registers a hook when it's imported import py3compat from importstring import import_item @@ -163,6 +158,7 @@ class CannedClass(CannedObject): class CannedArray(CannedObject): def __init__(self, obj): + from numpy import ascontiguousarray self.shape = obj.shape self.dtype = obj.dtype.descr if obj.dtype.fields else obj.dtype.str if sum(obj.shape) == 0: @@ -170,16 +166,17 @@ class CannedArray(CannedObject): self.buffers = [pickle.dumps(obj, -1)] else: # ensure contiguous - obj = numpy.ascontiguousarray(obj, dtype=None) + obj = ascontiguousarray(obj, dtype=None) self.buffers = [buffer(obj)] def get_object(self, g=None): + from numpy import frombuffer data = self.buffers[0] if sum(self.shape) == 0: # no shape, we just pickled it return pickle.loads(data) else: - return numpy.frombuffer(data, dtype=self.dtype).reshape(self.shape) + return frombuffer(data, dtype=self.dtype).reshape(self.shape) class CannedBytes(CannedObject): @@ -225,7 +222,7 @@ def _import_mapping(mapping, original=None): except Exception: if original and key not in original: # only message on user-added classes - log.error("cannning class not importable: %r", key, exc_info=True) + log.error("canning class not importable: %r", key, exc_info=True) mapping.pop(key) else: mapping[cls] = mapping.pop(key)