Permalink
Browse files

Merge pull request #2951 from minrk/typecheck

use istype on lists/tuples
  • Loading branch information...
ellisonbg committed Feb 20, 2013
2 parents b1c179b + 8bc38f2 commit 77a4c163ddbf5947c503b5f0e449509c79a007d3
@@ -34,7 +34,8 @@
from IPython.utils import py3compat
from IPython.utils.data import flatten
from IPython.utils.pickleutil import (
can, uncan, can_sequence, uncan_sequence, CannedObject
can, uncan, can_sequence, uncan_sequence, CannedObject,
istype, sequence_types,
)
if py3compat.PY3:
@@ -91,11 +92,11 @@ def serialize_object(obj, buffer_threshold=MAX_BYTES, item_threshold=MAX_ITEMS):
[bufs] : list of buffers representing the serialized object.
"""
buffers = []
if isinstance(obj, (list, tuple)) and len(obj) < item_threshold:
if istype(obj, sequence_types) and len(obj) < item_threshold:
cobj = can_sequence(obj)
for c in cobj:
buffers.extend(_extract_buffers(c, buffer_threshold))
elif isinstance(obj, dict) and len(obj) < item_threshold:
elif istype(obj, dict) and len(obj) < item_threshold:
cobj = {}
for k in sorted(obj.iterkeys()):
c = can(obj[k])
@@ -129,7 +130,7 @@ def unserialize_object(buffers, g=None):
# a zmq message
pobj = bytes(pobj)
canned = pickle.loads(pobj)
if isinstance(canned, (list, tuple)) and len(canned) < MAX_ITEMS:
if istype(canned, sequence_types) and len(canned) < MAX_ITEMS:
for c in canned:
_restore_buffers(c, bufs)
newobj = uncan_sequence(canned, g)
@@ -12,6 +12,7 @@
#-------------------------------------------------------------------------------
import pickle
from collections import namedtuple
import nose.tools as nt
@@ -186,11 +187,41 @@ class C:
bufs = serialize_object(dict(C=C))
canned = pickle.loads(bufs[0])
yield nt.assert_true(canned['C'], CannedClass)
yield nt.assert_true(isinstance(canned['C'], CannedClass))
d, r = unserialize_object(bufs)
C2 = d['C']
yield nt.assert_equal(C2.a, C.a)
@dec.parametric
def test_tuple():
tup = (lambda x:x, 1)
bufs = serialize_object(tup)
canned = pickle.loads(bufs[0])
yield nt.assert_true(isinstance(canned, tuple))
t2, r = unserialize_object(bufs)
yield nt.assert_equal(t2[0](t2[1]), tup[0](tup[1]))
point = namedtuple('point', 'x y')
@dec.parametric
def test_namedtuple():
p = point(1,2)
bufs = serialize_object(p)
canned = pickle.loads(bufs[0])
yield nt.assert_true(isinstance(canned, point))
p2, r = unserialize_object(bufs, globals())
yield nt.assert_equal(p2.x, p.x)
yield nt.assert_equal(p2.y, p.y)
@dec.parametric
def test_list():
lis = [lambda x:x, 1]
bufs = serialize_object(lis)
canned = pickle.loads(bufs[0])
yield nt.assert_true(isinstance(canned, list))
l2, r = unserialize_object(bufs)
yield nt.assert_equal(l2[0](l2[1]), lis[0](lis[1]))
@dec.parametric
def test_class_inheritance():
@interactive
@@ -19,6 +19,7 @@
import sys
import platform
import time
from collections import namedtuple
from tempfile import mktemp
from StringIO import StringIO
@@ -43,6 +44,8 @@
def setup():
add_engines(3, total=True)
point = namedtuple("point", "x y")
class TestView(ClusterTestCase, ParametricTestCase):
def setUp(self):
@@ -735,3 +738,22 @@ def test_nested_getitem_setitem(self):
r = view.apply_sync(lambda x: x.b, ra)
self.assertEqual(r, 0)
self.assertEqual(view['a.b'], 0)
def test_return_namedtuple(self):
def namedtuplify(x, y):
from IPython.parallel.tests.test_view import point
return point(x, y)
view = self.client[-1]
p = view.apply_sync(namedtuplify, 1, 2)
self.assertEqual(p.x, 1)
self.assertEqual(p.y, 2)
def test_apply_namedtuple(self):
def echoxy(p):
return p.y, p.x
view = self.client[-1]
tup = view.apply_sync(echoxy, point(1, 2))
self.assertEqual(tup, (2,1))
@@ -249,17 +249,19 @@ def can_class(obj):
def can_dict(obj):
"""can the *values* of a dict"""
if isinstance(obj, dict):
if istype(obj, dict):
newobj = {}
for k, v in obj.iteritems():
newobj[k] = can(v)
return newobj
else:
return obj
sequence_types = (list, tuple, set)
def can_sequence(obj):
"""can the elements of a sequence"""
if isinstance(obj, (list, tuple)):
if istype(obj, sequence_types):
t = type(obj)
return t([can(i) for i in obj])
else:
@@ -285,7 +287,7 @@ def uncan(obj, g=None):
return obj
def uncan_dict(obj, g=None):
if isinstance(obj, dict):
if istype(obj, dict):
newobj = {}
for k, v in obj.iteritems():
newobj[k] = uncan(v,g)
@@ -294,7 +296,7 @@ def uncan_dict(obj, g=None):
return obj
def uncan_sequence(obj, g=None):
if isinstance(obj, (list, tuple)):
if istype(obj, sequence_types):
t = type(obj)
return t([uncan(i,g) for i in obj])
else:

0 comments on commit 77a4c16

Please sign in to comment.