From 7022b9a84692e2ff68c39a5eaabcaba43a7f5f79 Mon Sep 17 00:00:00 2001 From: leandor Date: Sat, 5 Nov 2016 20:17:15 -0300 Subject: [PATCH] (#18) Moved `compress` from lambda to named func `compress` was implemented inlined as a lambda in the module initialization. Since it wasn't a "trivial" implementation, I think it's better for it to be a "real" function. --- src/papi/lz4/module.cpp | 36 ++++++++++++++++++------------------ src/papi/lz4/module.h | 6 ++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/papi/lz4/module.cpp b/src/papi/lz4/module.cpp index 6ee5e2f..716e95b 100644 --- a/src/papi/lz4/module.cpp +++ b/src/papi/lz4/module.cpp @@ -11,33 +11,33 @@ static test_initializer modules([](py::module &m) { - using namespace gsl; using namespace lz4; m.def_submodule("lz4") .def("VERSION_NUMBER", VERSION_NUMBER) .def("VERSION", [](){ - return py::make_tuple(VERSION_MAJOR(), VERSION_MINOR(), VERSION_RELEASE()); - }) + return py::make_tuple(VERSION_MAJOR(), VERSION_MINOR(), VERSION_RELEASE()); + }) .def("__version__", [](){ - return py::str(VERSION_STRING()); - }) - .def("compress", [](const py::bytes& buffer){ - - std::string source{buffer}; - auto size = lz4::api::compressBound(source.length()); + return py::str(VERSION_STRING()); + }) + .def("compress", compress) + ; +}); +py::bytes lz4::compress(const py::bytes& buffer) +{ + std::string source{buffer}; + auto size = api::compressBound(source.length()); - std::vector dest(size); - auto res = lz4::api::compress_default(source, dest); + std::vector dest(size); - if (res == 0) { - throw std::exception("compression failed"); - } + auto res = api::compress_default(source, dest); + if (res == 0) { + throw std::exception("compression failed"); + } - return py::bytes(dest.data(), res); - }) - ; -}); + return py::bytes(dest.data(), res); +}; diff --git a/src/papi/lz4/module.h b/src/papi/lz4/module.h index 9c9bc60..0bbdc0d 100644 --- a/src/papi/lz4/module.h +++ b/src/papi/lz4/module.h @@ -6,12 +6,18 @@ #include "api.h" +namespace py = pybind11; +using namespace pybind11::literals; + namespace lz4 { + class context { context(); ~context(); }; + + py::bytes compress(const py::bytes& buffer); }; #endif // __CINT_LZ4_MODULE__ \ No newline at end of file