Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2fe175

Browse files
committed
use istype instead of isinstance for canning tuples/lists
and add explicit support for namedtuples
1 parent e20ba07 commit a2fe175

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

IPython/kernel/zmq/serialize.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
from IPython.utils import py3compat
3535
from IPython.utils.data import flatten
3636
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,
3839
)
3940

4041
if py3compat.PY3:
@@ -91,11 +92,11 @@ def serialize_object(obj, buffer_threshold=MAX_BYTES, item_threshold=MAX_ITEMS):
9192
[bufs] : list of buffers representing the serialized object.
9293
"""
9394
buffers = []
94-
if isinstance(obj, (list, tuple)) and len(obj) < item_threshold:
95+
if istype(obj, sequence_types) and len(obj) < item_threshold:
9596
cobj = can_sequence(obj)
9697
for c in cobj:
9798
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:
99100
cobj = {}
100101
for k in sorted(obj.iterkeys()):
101102
c = can(obj[k])
@@ -129,7 +130,7 @@ def unserialize_object(buffers, g=None):
129130
# a zmq message
130131
pobj = bytes(pobj)
131132
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:
133134
for c in canned:
134135
_restore_buffers(c, bufs)
135136
newobj = uncan_sequence(canned, g)

IPython/utils/pickleutil.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import copy
1919
import logging
2020
import sys
21+
from collections import namedtuple
2122
from types import FunctionType
2223

2324
try:
@@ -249,17 +250,19 @@ def can_class(obj):
249250

250251
def can_dict(obj):
251252
"""can the *values* of a dict"""
252-
if isinstance(obj, dict):
253+
if istype(obj, dict):
253254
newobj = {}
254255
for k, v in obj.iteritems():
255256
newobj[k] = can(v)
256257
return newobj
257258
else:
258259
return obj
259260

261+
sequence_types = (list, tuple, set)
262+
260263
def can_sequence(obj):
261264
"""can the elements of a sequence"""
262-
if isinstance(obj, (list, tuple)):
265+
if istype(obj, sequence_types):
263266
t = type(obj)
264267
return t([can(i) for i in obj])
265268
else:
@@ -285,7 +288,7 @@ def uncan(obj, g=None):
285288
return obj
286289

287290
def uncan_dict(obj, g=None):
288-
if isinstance(obj, dict):
291+
if istype(obj, dict):
289292
newobj = {}
290293
for k, v in obj.iteritems():
291294
newobj[k] = uncan(v,g)
@@ -294,7 +297,7 @@ def uncan_dict(obj, g=None):
294297
return obj
295298

296299
def uncan_sequence(obj, g=None):
297-
if isinstance(obj, (list, tuple)):
300+
if istype(obj, sequence_types):
298301
t = type(obj)
299302
return t([uncan(i,g) for i in obj])
300303
else:

0 commit comments

Comments
 (0)