Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions xcsf/pybind_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,16 @@ class XCS
py::bytes
serialize() const
{
// Write XCSF to a temporary binary file
const char *filename = "_tmp_pickle.bin";
// Create a unique temporary file in /tmp. The file name is made unique
// in place by mkstemp.
char filename[] = "/tmp/xcsf_pickle_XXXXXX";
int fd = mkstemp(filename);
if (fd == -1) {
throw std::runtime_error(
"Failed to create temporary file in serialize");
}
close(fd);

xcsf_save(&xcs, filename);
// Read the binary file into bytes
std::ifstream file(filename, std::ios::binary);
Expand All @@ -673,8 +681,17 @@ class XCS
static XCS
deserialize(const py::bytes &state)
{
// Write the XCSF bytes to a temporary binary file
const char *filename = "_tmp_pickle.bin";
// Create a unique temporary file in /tmp. The file name is made unique
// in place by mkstemp.
char filename[] = "/tmp/xcsf_pickle_XXXXXX";
int fd = mkstemp(filename);
if (fd == -1) {
throw std::runtime_error(
"Failed to create temporary file in deserialize");
}
close(fd);

// Write the XCSF bytes to the temporary file.
std::ofstream file(filename, std::ios::binary);
file.write(state.cast<std::string>().c_str(),
state.cast<std::string>().size());
Expand Down
Loading