Commit c212db96 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix graph IO to properly address unicode strings

parent 4a171495
......@@ -94,11 +94,7 @@ public:
std::streamsize read(char* s, std::streamsize n)
{
boost::python::object pbuf = _file.attr("read")(n);
#if (PY_MAJOR_VERSION >= 3)
string buf = boost::python::extract<string>(pbuf.attr("decode")("utf-8"));
#else
string buf = boost::python::extract<string>(pbuf);
#endif
for (size_t i = 0; i < buf.size(); ++i)
s[i] = buf[i];
return buf.size();
......@@ -107,8 +103,15 @@ public:
std::streamsize write(const char* s, std::streamsize n)
{
string buf(s, s+n);
boost::python::object pbuf(buf);
_file.attr("write")(pbuf.attr("encode")("utf-8"));
#if (PY_MAJOR_VERSION >= 3)
// in python 3 we need to construct a 'bytes' instance
PyObject* bytes = PyBytes_FromStringAndSize(s, n);
boost::python::handle<> x(bytes);
boost::python::object pbuf(x);
#else
boost::python::str pbuf(buf);
#endif
_file.attr("write")(pbuf);
return n;
}
......
......@@ -109,7 +109,7 @@ import gzip
import weakref
import copy
from io import BytesIO
from io import BytesIO, StringIO
from .decorators import _wraps, _require, _attrs, _limit_args
from inspect import ismethod
......@@ -1982,7 +1982,7 @@ class Graph(object):
def __getstate__(self):
state = dict()
sio = BytesIO()
stream = gzip.GzipFile(fileobj=sio, mode="wb")
stream = gzip.open(sio, mode="wb")
self.save(stream, "xml")
stream.close()
state["blob"] = sio.getvalue()
......@@ -1993,7 +1993,7 @@ class Graph(object):
blob = state["blob"]
if blob != "":
sio = BytesIO(blob)
stream = gzip.GzipFile(fileobj=sio, mode="rb")
stream = gzip.open(sio, mode="rb")
self.load(stream, "xml")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment