From 8bf877ac1ed0fddc10546dd7974598584956143f Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Thu, 4 Aug 2016 21:06:31 -0700 Subject: [PATCH] Serialize and Deserialize unicode (#349) --- lib/python/ray/serialization.py | 3 +++ lib/python/ray/worker.py | 2 ++ protos/types.proto | 5 +++++ src/raylib.cc | 17 ++++++++++++++++- test/runtest.py | 2 +- thirdparty/arrow | 2 +- thirdparty/numbuf | 2 +- 7 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/python/ray/serialization.py b/lib/python/ray/serialization.py index 4e10101f8..8cdbfa558 100644 --- a/lib/python/ray/serialization.py +++ b/lib/python/ray/serialization.py @@ -28,6 +28,9 @@ class Tuple(tuple): class Str(str): pass + +class Unicode(unicode): + pass class NDArray(np.ndarray): pass diff --git a/lib/python/ray/worker.py b/lib/python/ray/worker.py index a089f4241..eb0e05672 100644 --- a/lib/python/ray/worker.py +++ b/lib/python/ray/worker.py @@ -489,6 +489,8 @@ class Worker(object): result = serialization.Tuple(result) elif isinstance(result, str): result = serialization.Str(result) + elif isinstance(result, unicode): + result = serialization.Unicode(result) elif isinstance(result, np.ndarray): result = result.view(serialization.NDArray) elif isinstance(result, np.generic): diff --git a/protos/types.proto b/protos/types.proto index 37bbe9da4..753902996 100644 --- a/protos/types.proto +++ b/protos/types.proto @@ -12,6 +12,10 @@ message String { string data = 1; } +message Unicode { + string data = 1; +} + message Double { double data = 1; } @@ -46,6 +50,7 @@ message ReusableVar { // Union of possible object types message Obj { String string_data = 1; + Unicode unicode_data = 13; Int int_data = 2; Long long_data = 12; Double double_data = 3; diff --git a/src/raylib.cc b/src/raylib.cc index 0b4849b6c..f30d0c600 100644 --- a/src/raylib.cc +++ b/src/raylib.cc @@ -289,6 +289,17 @@ int serialize(PyObject* worker_capsule, PyObject* val, Obj* obj, std::vectormutable_string_data()->set_data(buffer, length); + } else if (PyUnicode_Check(val)) { + Py_ssize_t length; + #if PY_MAJOR_VERSION >= 3 + char* data = PyUnicode_AsUTF8AndSize(val, &length); // TODO(pcm): Check if this is correct + #else + PyObject* str = PyUnicode_AsUTF8String(val); + char* data = PyString_AS_STRING(str); + length = PyString_GET_SIZE(str); + #endif + obj->mutable_unicode_data()->set_data(data, length); + Py_XDECREF(str); } else if (val == Py_None) { obj->mutable_empty_data(); // allocate an Empty object, this is a None } else if (PyObject_IsInstance(val, (PyObject*) &PyObjectIDType)) { @@ -343,7 +354,7 @@ int serialize(PyObject* worker_capsule, PyObject* val, Obj* obj, std::vector