Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Tiago Peixoto
graph-tool
Commits
c212db96
Commit
c212db96
authored
May 29, 2014
by
Tiago Peixoto
Browse files
Fix graph IO to properly address unicode strings
parent
4a171495
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/graph/graph_io.cc
View file @
c212db96
...
...
@@ -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
;
}
...
...
src/graph_tool/__init__.py
View file @
c212db96
...
...
@@ -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"
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment