implement none serialization

This commit is contained in:
Philipp Moritz 2016-06-03 12:11:30 -07:00
parent 069f5add3a
commit 073dd3642e
3 changed files with 12 additions and 0 deletions

View file

@ -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;
}

View file

@ -231,6 +231,8 @@ int serialize(PyObject* worker_capsule, PyObject* val, Obj* obj, std::vector<Obj
Py_ssize_t length;
PyString_AsStringAndSize(val, &buffer, &length); // creates pointer to internal buffer
obj->mutable_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<ObjR
const char* buffer = obj.string_data().data().data();
Py_ssize_t length = obj.string_data().data().size();
return PyString_FromStringAndSize(buffer, length);
} else if (obj.has_empty_data()) {
Py_RETURN_NONE;
} else if (obj.has_array_data()) {
const Array& array = obj.array_data();
std::vector<npy_intp> dims;

View file

@ -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, {})