Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

use istype on lists/tuples #2951

Merged
merged 4 commits into from

2 participants

@minrk
Owner

avoids catching and mistreating subclasses, such as namedtuples.

@ellisonbg ellisonbg merged commit 32c850e into from
@minrk minrk deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 18, 2013
  1. @minrk

    use istype instead of isinstance for canning tuples/lists

    minrk authored
    and add explicit support for namedtuples
  2. @minrk
  3. @minrk
Commits on Feb 20, 2013
  1. @minrk
This page is out of date. Refresh to see the latest.
View
9 IPython/kernel/zmq/serialize.py
@@ -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)
View
33 IPython/kernel/zmq/tests/test_serialize.py
@@ -12,6 +12,7 @@
#-------------------------------------------------------------------------------
import pickle
+from collections import namedtuple
import nose.tools as nt
@@ -186,12 +187,42 @@ 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
class C(object):
View
22 IPython/parallel/tests/test_view.py
@@ -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))
+
View
10 IPython/utils/pickleutil.py
@@ -249,7 +249,7 @@ 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)
@@ -257,9 +257,11 @@ def can_dict(obj):
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:
Something went wrong with that request. Please try again.