diff --git a/xcsf/pybind_wrapper.cpp b/xcsf/pybind_wrapper.cpp index aee42bc31..5e781b1f5 100644 --- a/xcsf/pybind_wrapper.cpp +++ b/xcsf/pybind_wrapper.cpp @@ -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); @@ -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().c_str(), state.cast().size());