##// END OF EJS Templates
Merge pull request #7105 from minrk/pickle-np-obj-py2...
Thomas Kluyver -
r19248:e1de45a6 merge
parent child Browse files
Show More
@@ -11,14 +11,14 b' except:'
11 import pickle
11 import pickle
12
12
13 # IPython imports
13 # IPython imports
14 from IPython.utils import py3compat
14 from IPython.utils.py3compat import PY3, buffer_to_bytes_py2
15 from IPython.utils.data import flatten
15 from IPython.utils.data import flatten
16 from IPython.utils.pickleutil import (
16 from IPython.utils.pickleutil import (
17 can, uncan, can_sequence, uncan_sequence, CannedObject,
17 can, uncan, can_sequence, uncan_sequence, CannedObject,
18 istype, sequence_types, PICKLE_PROTOCOL,
18 istype, sequence_types, PICKLE_PROTOCOL,
19 )
19 )
20
20
21 if py3compat.PY3:
21 if PY3:
22 buffer = memoryview
22 buffer = memoryview
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
@@ -105,10 +105,7 b' def deserialize_object(buffers, g=None):'
105 (newobj, bufs) : unpacked object, and the list of remaining unused buffers.
105 (newobj, bufs) : unpacked object, and the list of remaining unused buffers.
106 """
106 """
107 bufs = list(buffers)
107 bufs = list(buffers)
108 pobj = bufs.pop(0)
108 pobj = buffer_to_bytes_py2(bufs.pop(0))
109 if not isinstance(pobj, bytes):
110 # a zmq message
111 pobj = bytes(pobj)
112 canned = pickle.loads(pobj)
109 canned = pickle.loads(pobj)
113 if istype(canned, sequence_types) and len(canned) < MAX_ITEMS:
110 if istype(canned, sequence_types) and len(canned) < MAX_ITEMS:
114 for c in canned:
111 for c in canned:
@@ -161,11 +158,10 b' def unpack_apply_message(bufs, g=None, copy=True):'
161 Returns: original f,args,kwargs"""
158 Returns: original f,args,kwargs"""
162 bufs = list(bufs) # allow us to pop
159 bufs = list(bufs) # allow us to pop
163 assert len(bufs) >= 2, "not enough buffers!"
160 assert len(bufs) >= 2, "not enough buffers!"
164 if not copy:
161 pf = buffer_to_bytes_py2(bufs.pop(0))
165 for i in range(2):
162 f = uncan(pickle.loads(pf), g)
166 bufs[i] = bufs[i].bytes
163 pinfo = buffer_to_bytes_py2(bufs.pop(0))
167 f = uncan(pickle.loads(bufs.pop(0)), g)
164 info = pickle.loads(pinfo)
168 info = pickle.loads(bufs.pop(0))
169 arg_bufs, kwarg_bufs = bufs[:info['narg_bufs']], bufs[info['narg_bufs']:]
165 arg_bufs, kwarg_bufs = bufs[:info['narg_bufs']], bufs[info['narg_bufs']:]
170
166
171 args = []
167 args = []
@@ -17,7 +17,7 b' except ImportError:'
17 from . import codeutil # This registers a hook when it's imported
17 from . import codeutil # This registers a hook when it's imported
18 from . import py3compat
18 from . import py3compat
19 from .importstring import import_item
19 from .importstring import import_item
20 from .py3compat import string_types, iteritems
20 from .py3compat import string_types, iteritems, buffer_to_bytes_py2
21
21
22 from IPython.config import Application
22 from IPython.config import Application
23 from IPython.utils.log import get_logger
23 from IPython.utils.log import get_logger
@@ -260,8 +260,8 b' class CannedArray(CannedObject):'
260 from numpy import frombuffer
260 from numpy import frombuffer
261 data = self.buffers[0]
261 data = self.buffers[0]
262 if self.pickled:
262 if self.pickled:
263 # no shape, we just pickled it
263 # we just pickled it
264 return pickle.loads(data)
264 return pickle.loads(buffer_to_bytes_py2(data))
265 else:
265 else:
266 return frombuffer(data, dtype=self.dtype).reshape(self.shape)
266 return frombuffer(data, dtype=self.dtype).reshape(self.shape)
267
267
@@ -30,6 +30,12 b' def cast_bytes(s, encoding=None):'
30 return encode(s, encoding)
30 return encode(s, encoding)
31 return s
31 return s
32
32
33 def buffer_to_bytes(buf):
34 """Cast a buffer object to bytes"""
35 if not isinstance(buf, bytes):
36 buf = bytes(buf)
37 return buf
38
33 def _modify_str_or_docstring(str_change_func):
39 def _modify_str_or_docstring(str_change_func):
34 @functools.wraps(str_change_func)
40 @functools.wraps(str_change_func)
35 def wrapper(func_or_str):
41 def wrapper(func_or_str):
@@ -86,6 +92,7 b' if sys.version_info[0] >= 3:'
86 bytes_to_str = decode
92 bytes_to_str = decode
87 cast_bytes_py2 = no_code
93 cast_bytes_py2 = no_code
88 cast_unicode_py2 = no_code
94 cast_unicode_py2 = no_code
95 buffer_to_bytes_py2 = no_code
89
96
90 string_types = (str,)
97 string_types = (str,)
91 unicode_type = str
98 unicode_type = str
@@ -151,6 +158,7 b' else:'
151 bytes_to_str = no_code
158 bytes_to_str = no_code
152 cast_bytes_py2 = cast_bytes
159 cast_bytes_py2 = cast_bytes
153 cast_unicode_py2 = cast_unicode
160 cast_unicode_py2 = cast_unicode
161 buffer_to_bytes_py2 = buffer_to_bytes
154
162
155 string_types = (str, unicode)
163 string_types = (str, unicode)
156 unicode_type = unicode
164 unicode_type = unicode
General Comments 0
You need to be logged in to leave comments. Login now