Skip to content

Commit

Permalink
Merge pull request #2951 from minrk/typecheck
Browse files Browse the repository at this point in the history
use istype on lists/tuples
  • Loading branch information
ellisonbg committed Feb 20, 2013
2 parents 2a61853 + 33ad3a8 commit 32c850e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
9 changes: 5 additions & 4 deletions IPython/kernel/zmq/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 32 additions & 1 deletion IPython/kernel/zmq/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#-------------------------------------------------------------------------------

import pickle
from collections import namedtuple

import nose.tools as nt

Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions IPython/parallel/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
import platform
import time
from collections import namedtuple
from tempfile import mktemp
from StringIO import StringIO

Expand All @@ -43,6 +44,8 @@
def setup():
add_engines(3, total=True)

point = namedtuple("point", "x y")

class TestView(ClusterTestCase, ParametricTestCase):

def setUp(self):
Expand Down Expand Up @@ -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))

10 changes: 6 additions & 4 deletions IPython/utils/pickleutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 32c850e

Please sign in to comment.