From 073dd3642e1d7d0755a2d492f8e78b7544831daf Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Fri, 3 Jun 2016 12:11:30 -0700 Subject: [PATCH] implement none serialization --- protos/types.proto | 5 +++++ src/orchpylib.cc | 4 ++++ test/runtest.py | 3 +++ 3 files changed, 12 insertions(+) diff --git a/protos/types.proto b/protos/types.proto index 6b08478e6..ee730a6c4 100644 --- a/protos/types.proto +++ b/protos/types.proto @@ -12,6 +12,10 @@ message Double { double data = 1; } +// Empty used to represent a None object +message Empty { +} + message PyObj { bytes data = 1; } @@ -25,6 +29,7 @@ message Obj { List list_data = 4; Dict dict_data = 8; Array array_data = 5; + Empty empty_data = 9; PyObj pyobj_data = 6; } diff --git a/src/orchpylib.cc b/src/orchpylib.cc index 9c2e0b92d..da16655ad 100644 --- a/src/orchpylib.cc +++ b/src/orchpylib.cc @@ -231,6 +231,8 @@ int serialize(PyObject* worker_capsule, PyObject* val, Obj* obj, std::vectormutable_string_data()->set_data(buffer, length); + } else if (val == Py_None) { + obj->mutable_empty_data(); // allocate an Empty object, this is a None } else if (PyArray_Check(val)) { PyArrayObject* array = PyArray_GETCONTIGUOUS((PyArrayObject*) val); Array* data = obj->mutable_array_data(); @@ -347,6 +349,8 @@ PyObject* deserialize(PyObject* worker_capsule, const Obj& obj, std::vector dims; diff --git a/test/runtest.py b/test/runtest.py index f6440327f..02e71a803 100644 --- a/test/runtest.py +++ b/test/runtest.py @@ -38,6 +38,9 @@ class SerializationTest(unittest.TestCase): self.roundTripTest(w, "hello world") self.roundTripTest(w, 42.0) self.roundTripTest(w, (1.0, "hi")) + self.roundTripTest(w, None) + self.roundTripTest(w, (None, None)) + self.roundTripTest(w, ("hello", None)) self.roundTripTest(w, {"hello" : "world", 1: 42, 1.0: 45}) self.roundTripTest(w, {})