Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
56ccf786
Commit
56ccf786
authored
Nov 26, 2014
by
Davis King
Browse files
Changed python pickling code so it works with Python 3
parent
1f868695
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
6 deletions
+30
-6
dlib/python/serialize_pickle.h
dlib/python/serialize_pickle.h
+30
-6
No files found.
dlib/python/serialize_pickle.h
View file @
56ccf786
...
...
@@ -6,6 +6,7 @@
#include <dlib/serialize.h>
#include <boost/python.hpp>
#include <sstream>
#include <dlib/vectorstream.h>
template
<
typename
T
>
struct
serialize_pickle
:
boost
::
python
::
pickle_suite
...
...
@@ -15,9 +16,12 @@ struct serialize_pickle : boost::python::pickle_suite
)
{
using
namespace
dlib
;
std
::
ostringstream
sout
;
std
::
vector
<
char
>
buf
;
buf
.
reserve
(
5000
);
vectorstream
sout
(
buf
);
serialize
(
item
,
sout
);
return
boost
::
python
::
make_tuple
(
sout
.
str
());
return
boost
::
python
::
make_tuple
(
boost
::
python
::
handle
<>
(
PyBytes_FromStringAndSize
(
buf
.
size
()
?&
buf
[
0
]
:
0
,
buf
.
size
())));
}
static
void
setstate
(
...
...
@@ -36,11 +40,31 @@ struct serialize_pickle : boost::python::pickle_suite
throw_error_already_set
();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
if
(
extract
<
str
>
(
state
[
0
]).
check
())
{
str
data
=
extract
<
str
>
(
state
[
0
]);
std
::
string
temp
(
extract
<
const
char
*>
(
data
),
len
(
data
));
std
::
istringstream
sin
(
temp
);
deserialize
(
item
,
sin
);
}
else
if
(
PyBytes_Check
(
object
(
state
[
0
]).
ptr
()))
{
object
obj
=
state
[
0
];
char
*
data
=
PyBytes_AsString
(
obj
.
ptr
());
unsigned
long
num
=
PyBytes_Size
(
obj
.
ptr
());
std
::
istringstream
sin
(
std
::
string
(
data
,
num
));
deserialize
(
item
,
sin
);
}
else
{
throw
error
(
"Unable to unpickle, error in input file."
);
}
}
};
#endif // DLIB_SERIALIZE_PiCKLE_Hh_
...
...
Write
Preview
Markdown
is supported
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