##// END OF EJS Templates
use istype instead of isinstance for canning tuples/lists...
MinRK -
Show More
@@ -34,7 +34,8 b' except:'
34 from IPython.utils import py3compat
34 from IPython.utils import py3compat
35 from IPython.utils.data import flatten
35 from IPython.utils.data import flatten
36 from IPython.utils.pickleutil import (
36 from IPython.utils.pickleutil import (
37 can, uncan, can_sequence, uncan_sequence, CannedObject
37 can, uncan, can_sequence, uncan_sequence, CannedObject,
38 istype, sequence_types,
38 )
39 )
39
40
40 if py3compat.PY3:
41 if py3compat.PY3:
@@ -91,11 +92,11 b' def serialize_object(obj, buffer_threshold=MAX_BYTES, item_threshold=MAX_ITEMS):'
91 [bufs] : list of buffers representing the serialized object.
92 [bufs] : list of buffers representing the serialized object.
92 """
93 """
93 buffers = []
94 buffers = []
94 if isinstance(obj, (list, tuple)) and len(obj) < item_threshold:
95 if istype(obj, sequence_types) and len(obj) < item_threshold:
95 cobj = can_sequence(obj)
96 cobj = can_sequence(obj)
96 for c in cobj:
97 for c in cobj:
97 buffers.extend(_extract_buffers(c, buffer_threshold))
98 buffers.extend(_extract_buffers(c, buffer_threshold))
98 elif isinstance(obj, dict) and len(obj) < item_threshold:
99 elif istype(obj, dict) and len(obj) < item_threshold:
99 cobj = {}
100 cobj = {}
100 for k in sorted(obj.iterkeys()):
101 for k in sorted(obj.iterkeys()):
101 c = can(obj[k])
102 c = can(obj[k])
@@ -129,7 +130,7 b' def unserialize_object(buffers, g=None):'
129 # a zmq message
130 # a zmq message
130 pobj = bytes(pobj)
131 pobj = bytes(pobj)
131 canned = pickle.loads(pobj)
132 canned = pickle.loads(pobj)
132 if isinstance(canned, (list, tuple)) and len(canned) < MAX_ITEMS:
133 if istype(canned, sequence_types) and len(canned) < MAX_ITEMS:
133 for c in canned:
134 for c in canned:
134 _restore_buffers(c, bufs)
135 _restore_buffers(c, bufs)
135 newobj = uncan_sequence(canned, g)
136 newobj = uncan_sequence(canned, g)
@@ -18,6 +18,7 b' __docformat__ = "restructuredtext en"'
18 import copy
18 import copy
19 import logging
19 import logging
20 import sys
20 import sys
21 from collections import namedtuple
21 from types import FunctionType
22 from types import FunctionType
22
23
23 try:
24 try:
@@ -249,7 +250,7 b' def can_class(obj):'
249
250
250 def can_dict(obj):
251 def can_dict(obj):
251 """can the *values* of a dict"""
252 """can the *values* of a dict"""
252 if isinstance(obj, dict):
253 if istype(obj, dict):
253 newobj = {}
254 newobj = {}
254 for k, v in obj.iteritems():
255 for k, v in obj.iteritems():
255 newobj[k] = can(v)
256 newobj[k] = can(v)
@@ -257,9 +258,11 b' def can_dict(obj):'
257 else:
258 else:
258 return obj
259 return obj
259
260
261 sequence_types = (list, tuple, set)
262
260 def can_sequence(obj):
263 def can_sequence(obj):
261 """can the elements of a sequence"""
264 """can the elements of a sequence"""
262 if isinstance(obj, (list, tuple)):
265 if istype(obj, sequence_types):
263 t = type(obj)
266 t = type(obj)
264 return t([can(i) for i in obj])
267 return t([can(i) for i in obj])
265 else:
268 else:
@@ -285,7 +288,7 b' def uncan(obj, g=None):'
285 return obj
288 return obj
286
289
287 def uncan_dict(obj, g=None):
290 def uncan_dict(obj, g=None):
288 if isinstance(obj, dict):
291 if istype(obj, dict):
289 newobj = {}
292 newobj = {}
290 for k, v in obj.iteritems():
293 for k, v in obj.iteritems():
291 newobj[k] = uncan(v,g)
294 newobj[k] = uncan(v,g)
@@ -294,7 +297,7 b' def uncan_dict(obj, g=None):'
294 return obj
297 return obj
295
298
296 def uncan_sequence(obj, g=None):
299 def uncan_sequence(obj, g=None):
297 if isinstance(obj, (list, tuple)):
300 if istype(obj, sequence_types):
298 t = type(obj)
301 t = type(obj)
299 return t([uncan(i,g) for i in obj])
302 return t([uncan(i,g) for i in obj])
300 else:
303 else:
General Comments 0
You need to be logged in to leave comments. Login now