From 487466d7ce6e3e7adf430d9970d610ec2135b0a2 2011-08-16 03:49:44 From: MinRK Date: 2011-08-16 03:49:44 Subject: [PATCH] don't special case for py3k+numpy py3k+numpy non-copying recv works fine now, with released pyzmq. There was no need to make any changes in pyzmq. closes gh-478, closes gh-587 (rebased) --- diff --git a/IPython/parallel/tests/test_newserialized.py b/IPython/parallel/tests/test_newserialized.py index f321acf..b9228f1 100644 --- a/IPython/parallel/tests/test_newserialized.py +++ b/IPython/parallel/tests/test_newserialized.py @@ -16,6 +16,8 @@ Authors: # Imports #------------------------------------------------------------------------------- +import sys + from unittest import TestCase from IPython.testing.decorators import parametric @@ -23,6 +25,8 @@ from IPython.utils import newserialized as ns from IPython.utils.pickleutil import can, uncan, CannedObject, CannedFunction from IPython.parallel.tests.clienttest import skip_without +if sys.version_info[0] >= 3: + buffer = memoryview class CanningTestCase(TestCase): def test_canning(self): @@ -88,10 +92,10 @@ class CanningTestCase(TestCase): self.assertEquals(md['shape'], a.shape) self.assertEquals(md['dtype'], a.dtype.str) buff = ser1.getData() - self.assertEquals(buff, numpy.getbuffer(a)) + self.assertEquals(buff, buffer(a)) s = ns.Serialized(buff, td, md) final = ns.unserialize(s) - self.assertEquals(numpy.getbuffer(a), numpy.getbuffer(final)) + self.assertEquals(buffer(a), buffer(final)) self.assertTrue((a==final).all()) self.assertEquals(a.dtype.str, final.dtype.str) self.assertEquals(a.shape, final.shape) diff --git a/IPython/utils/newserialized.py b/IPython/utils/newserialized.py index d9259d2..1c4d9fb 100644 --- a/IPython/utils/newserialized.py +++ b/IPython/utils/newserialized.py @@ -35,6 +35,8 @@ if sys.version_info[0] >= 3: py3k = True else: py3k = False + if sys.version_info[:2] <= (2,6): + memoryview = buffer #----------------------------------------------------------------------------- # Classes and functions @@ -101,10 +103,7 @@ class SerializeIt(object): self.data = None self.obj = unSerialized.getObject() if numpy is not None and isinstance(self.obj, numpy.ndarray): - if py3k or len(self.obj.shape) == 0: # length 0 arrays are just pickled - # FIXME: - # also use pickle for numpy arrays on py3k, since - # pyzmq doesn't rebuild from memoryviews properly + if len(self.obj.shape) == 0: # length 0 arrays are just pickled self.typeDescriptor = 'pickle' self.metadata = {} else: @@ -125,7 +124,7 @@ class SerializeIt(object): def _generateData(self): if self.typeDescriptor == 'ndarray': - self.data = numpy.getbuffer(self.obj) + self.data = buffer(self.obj) elif self.typeDescriptor in ('bytes', 'buffer'): self.data = self.obj elif self.typeDescriptor == 'pickle': @@ -158,11 +157,10 @@ class UnSerializeIt(UnSerialized): typeDescriptor = self.serialized.getTypeDescriptor() if numpy is not None and typeDescriptor == 'ndarray': buf = self.serialized.getData() - if isinstance(buf, (bytes, buffer)): + if isinstance(buf, (bytes, buffer, memoryview)): result = numpy.frombuffer(buf, dtype = self.serialized.metadata['dtype']) else: - # memoryview - result = numpy.array(buf, dtype = self.serialized.metadata['dtype']) + raise TypeError("Expected bytes or buffer/memoryview, but got %r"%type(buf)) result.shape = self.serialized.metadata['shape'] elif typeDescriptor == 'pickle': result = pickle.loads(self.serialized.getData())