Commit 84e2ce9b authored by Matthieu Dorier's avatar Matthieu Dorier

moved from boost.python to pybind11

parent e42fea37
# (C) 2018 The University of Chicago # (C) 2018 The University of Chicago
# See COPYRIGHT in top-level directory. # See COPYRIGHT in top-level directory.
import _pybaketarget
import _pybakeserver import _pybakeserver
import pymargo import pymargo
from pybake.target import BakeTargetID from pybake.target import BakeTargetID
......
...@@ -3,14 +3,8 @@ ...@@ -3,14 +3,8 @@
* *
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#define BOOST_NO_AUTO_PTR #include <pybind11/pybind11.h>
#include <boost/python.hpp> #include <pybind11/numpy.h>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
...@@ -18,36 +12,38 @@ ...@@ -18,36 +12,38 @@
#include <margo.h> #include <margo.h>
#include <bake.h> #include <bake.h>
#include <bake-client.h> #include <bake-client.h>
#if HAS_NUMPY
#include <boost/python/numpy.hpp>
namespace np = boost::python::numpy;
#endif
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance) namespace py11 = pybind11;
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_provider_handle) namespace np = py11;
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_client)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(hg_addr)
namespace bpl = boost::python; typedef py11::capsule pymargo_instance_id;
typedef py11::capsule pymargo_addr;
typedef py11::capsule pybake_client_t;
typedef py11::capsule pybake_provider_handle_t;
static bake_client_t pybake_client_init(margo_instance_id mid) { #define MID2CAPSULE(__mid) py11::capsule((void*)(__mid), "margo_instance_id", nullptr)
#define ADDR2CAPSULE(__addr) py11::capsule((void*)(__addr), "hg_addr_t", nullptr)
#define BAKEPH2CAPSULE(__bph) py11::capsule((void*)(__bph), "bake_provider_handle_t", nullptr)
#define BAKECL2CAPSULE(__bcl) py11::capsule((void*)(__bcl), "bake_client_t", nullptr)
static pybake_client_t pybake_client_init(pymargo_instance_id mid) {
bake_client_t result = BAKE_CLIENT_NULL; bake_client_t result = BAKE_CLIENT_NULL;
bake_client_init(mid, &result); bake_client_init(mid, &result);
return result; return BAKECL2CAPSULE(result);
} }
static bake_provider_handle_t pybake_provider_handle_create( static pybake_provider_handle_t pybake_provider_handle_create(
bake_client_t client, pybake_client_t client,
hg_addr_t addr, pymargo_addr addr,
uint8_t provider_id) { uint8_t provider_id) {
bake_provider_handle_t providerHandle = BAKE_PROVIDER_HANDLE_NULL; bake_provider_handle_t providerHandle = BAKE_PROVIDER_HANDLE_NULL;
bake_provider_handle_create(client, addr, provider_id, &providerHandle); bake_provider_handle_create(client, addr, provider_id, &providerHandle);
return providerHandle; return BAKEPH2CAPSULE(providerHandle);
} }
static uint64_t pybake_get_eager_limit( static uint64_t pybake_get_eager_limit(
bake_provider_handle_t ph) pybake_provider_handle_t ph)
{ {
uint64_t limit; uint64_t limit;
int ret = bake_provider_handle_get_eager_limit(ph, &limit); int ret = bake_provider_handle_get_eager_limit(ph, &limit);
...@@ -55,26 +51,26 @@ static uint64_t pybake_get_eager_limit( ...@@ -55,26 +51,26 @@ static uint64_t pybake_get_eager_limit(
return limit; return limit;
} }
static bpl::object pybake_probe( static py11::object pybake_probe(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
uint64_t max_targets) uint64_t max_targets)
{ {
bpl::list result; py11::list result;
std::vector<bake_target_id_t> targets(max_targets); std::vector<bake_target_id_t> targets(max_targets);
uint64_t num_targets; uint64_t num_targets;
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_probe(ph, max_targets, targets.data(), &num_targets); ret = bake_probe(ph, max_targets, targets.data(), &num_targets);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != 0) return bpl::object(); if(ret != 0) return py11::object();
for(uint64_t i=0; i < num_targets; i++) { for(uint64_t i=0; i < num_targets; i++) {
result.append(bpl::object(targets[i])); result.append(py11::cast(targets[i]));
} }
return result; return result;
} }
static bpl::object pybake_create( static py11::object pybake_create(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
bake_target_id_t bti, bake_target_id_t bti,
size_t region_size) size_t region_size)
{ {
...@@ -84,104 +80,107 @@ static bpl::object pybake_create( ...@@ -84,104 +80,107 @@ static bpl::object pybake_create(
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_create(ph, bti, region_size, &rid); ret = bake_create(ph, bti, region_size, &rid);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != 0) return bpl::object(); if(ret != 0) return py11::none();
else return bpl::object(rid); else return py11::cast(rid);
} }
static bpl::object pybake_write( static py11::object pybake_write(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid, const bake_region_id_t& rid,
uint64_t offset, uint64_t offset,
const std::string& data) const py11::bytes& bdata)
{ {
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
std::string data = (std::string)bdata;
ret = bake_write(ph, rid, offset, (const void*)data.data(), data.size()); ret = bake_write(ph, rid, offset, (const void*)data.data(), data.size());
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret == 0) return bpl::object(true); if(ret == 0) return py11::cast(true);
else return bpl::object(false); else return py11::cast(false);
} }
#if HAS_NUMPY #if HAS_NUMPY
static bpl::object pybake_write_numpy( static py11::object pybake_write_numpy(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid, const bake_region_id_t& rid,
uint64_t offset, uint64_t offset,
const np::ndarray& data) const np::array& data)
{ {
if(!(data.get_flags() & np::ndarray::bitflag::V_CONTIGUOUS)) { if(!(data.flags() &
(np::array::f_style | np::array::c_style))) {
std::cerr << "[pyBAKE error]: non-contiguous numpy arrays not yet supported" << std::endl; std::cerr << "[pyBAKE error]: non-contiguous numpy arrays not yet supported" << std::endl;
return bpl::object(false); return py11::cast(false);
} }
size_t size = data.get_dtype().get_itemsize(); size_t size = data.dtype().itemsize();
for(int i = 0; i < data.get_nd(); i++) { for(int i = 0; i < data.ndim(); i++) {
size *= data.shape(i); size *= data.shape(i);
} }
void* buffer = data.get_data(); const void* buffer = data.data();
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_write(ph, rid, offset, buffer, size); ret = bake_write(ph, rid, offset, buffer, size);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != 0) return bpl::object(false); if(ret != 0) return py11::cast(false);
else return bpl::object(true); else return py11::cast(true);
} }
#endif #endif
static bpl::object pybake_persist( static py11::object pybake_persist(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid) const bake_region_id_t& rid)
{ {
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_persist(ph, rid); ret = bake_persist(ph, rid);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret == 0) return bpl::object(true); if(ret == 0) return py11::cast(true);
else return bpl::object(false); else return py11::cast(false);
} }
static bpl::object pybake_create_write_persist( static py11::object pybake_create_write_persist(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
bake_target_id_t tid, bake_target_id_t tid,
const std::string& data) const py11::bytes& bdata)
{ {
bake_region_id_t rid; bake_region_id_t rid;
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
std::string data = (std::string)bdata;
ret = bake_create_write_persist(ph, tid, ret = bake_create_write_persist(ph, tid,
data.data(), data.size(), &rid); data.data(), data.size(), &rid);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret == 0) return bpl::object(rid); if(ret == 0) return py11::cast(rid);
else return bpl::object(); else return py11::none();
} }
#if HAS_NUMPY #if HAS_NUMPY
static bpl::object pybake_create_write_persist_numpy( static py11::object pybake_create_write_persist_numpy(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
bake_target_id_t tid, bake_target_id_t tid,
const np::ndarray& data) const np::array& data)
{ {
bake_region_id_t rid; bake_region_id_t rid;
if(!(data.get_flags() & np::ndarray::bitflag::V_CONTIGUOUS)) { if(!(data.flags() & (np::array::f_style | np::array::c_style))) {
std::cerr << "[pyBAKE error]: non-contiguous numpy arrays not yet supported" << std::endl; std::cerr << "[pyBAKE error]: non-contiguous numpy arrays not yet supported" << std::endl;
return bpl::object(); return py11::none();
} }
size_t size = data.get_dtype().get_itemsize(); size_t size = data.dtype().itemsize();
for(int i = 0; i < data.get_nd(); i++) { for(int i = 0; i < data.ndim(); i++) {
size *= data.shape(i); size *= data.shape(i);
} }
void* buffer = data.get_data(); const void* buffer = data.data();
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_create_write_persist(ph, tid, ret = bake_create_write_persist(ph, tid,
buffer, size, &rid); buffer, size, &rid);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret == 0) return bpl::object(rid); if(ret == 0) return py11::cast(rid);
else return bpl::object(); else return py11::none();
} }
#endif #endif
static bpl::object pybake_get_size( static py11::object pybake_get_size(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid) const bake_region_id_t& rid)
{ {
uint64_t size; uint64_t size;
...@@ -189,12 +188,12 @@ static bpl::object pybake_get_size( ...@@ -189,12 +188,12 @@ static bpl::object pybake_get_size(
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_get_size(ph, rid, &size); ret = bake_get_size(ph, rid, &size);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret == 0) return bpl::object(size); if(ret == 0) return py11::cast(size);
else return bpl::object(); else return py11::none();
} }
static bpl::object pybake_read( static py11::object pybake_read(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid, const bake_region_id_t& rid,
uint64_t offset, uint64_t offset,
size_t size) size_t size)
...@@ -205,13 +204,13 @@ static bpl::object pybake_read( ...@@ -205,13 +204,13 @@ static bpl::object pybake_read(
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_read(ph, rid, offset, (void*)result.data(), size, &bytes_read); ret = bake_read(ph, rid, offset, (void*)result.data(), size, &bytes_read);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != 0) return bpl::object(); if(ret != 0) return py11::none();
result.resize(bytes_read); result.resize(bytes_read);
return bpl::object(result); return py11::bytes(result);
} }
static bpl::object pybake_migrate( static py11::object pybake_migrate(
bake_provider_handle_t source_ph, pybake_provider_handle_t source_ph,
const bake_region_id_t& source_rid, const bake_region_id_t& source_rid,
bool remove_source, bool remove_source,
const std::string& dest_addr, const std::string& dest_addr,
...@@ -224,64 +223,71 @@ static bpl::object pybake_migrate( ...@@ -224,64 +223,71 @@ static bpl::object pybake_migrate(
remove_source, dest_addr.c_str(), dest_provider_id, remove_source, dest_addr.c_str(), dest_provider_id,
dest_target_id, &dest_rid); dest_target_id, &dest_rid);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != BAKE_SUCCESS) return bpl::object(); if(ret != BAKE_SUCCESS) return py11::none();
return bpl::object(dest_rid); return py11::cast(dest_rid);
} }
#if HAS_NUMPY #if HAS_NUMPY
static bpl::object pybake_read_numpy( static py11::object pybake_read_numpy(
bake_provider_handle_t ph, pybake_provider_handle_t ph,
const bake_region_id_t& rid, const bake_region_id_t& rid,
uint64_t offset, uint64_t offset,
const bpl::tuple& shape, const py11::tuple& shape,
const np::dtype& dtype) const np::dtype& dtype)
{ {
np::ndarray result = np::empty(shape, dtype); std::vector<ssize_t> sshape(shape.size());
size_t size = dtype.get_itemsize(); for(unsigned int i=0; i<sshape.size(); i++) sshape[i] = shape[i].cast<ssize_t>();
for(int i=0; i < result.get_nd(); i++) np::array result(dtype, sshape);
size_t size = dtype.itemsize();
for(int i=0; i < result.ndim(); i++)
size *= result.shape(i); size *= result.shape(i);
uint64_t bytes_read; uint64_t bytes_read;
int ret; int ret;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
ret = bake_read(ph, rid, offset, (void*)result.get_data(), size, &bytes_read); ret = bake_read(ph, rid, offset, (void*)result.data(), size, &bytes_read);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if(ret != 0) return bpl::object(); if(ret != 0) return py11::none();
if(bytes_read != size) return bpl::object(); if(bytes_read != size) return py11::none();
else return result; else return result;
} }
#endif #endif
BOOST_PYTHON_MODULE(_pybakeclient) PYBIND11_MODULE(_pybakeclient, m)
{ {
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>()
#if HAS_NUMPY #if HAS_NUMPY
np::initialize(); try { py11::module::import("numpy"); }
catch (...) {
std::cerr << "[Py-BAKE] Error: could not import numpy at C++ level" << std::endl;
exit(-1);
}
#endif #endif
bpl::import("_pybaketarget"); py11::module::import("_pybaketarget");
bpl::opaque<bake_client>(); m.def("client_init", &pybake_client_init);
bpl::opaque<bake_provider_handle>(); m.def("client_finalize", [](pybake_client_t clt) {
bpl::def("client_init", &pybake_client_init, ret_policy_opaque); return bake_client_finalize(clt);} );
bpl::def("client_finalize", &bake_client_finalize); m.def("provider_handle_create", &pybake_provider_handle_create);
bpl::def("provider_handle_create", &pybake_provider_handle_create, ret_policy_opaque); m.def("provider_handle_ref_incr", [](pybake_provider_handle_t pbph) {
bpl::def("provider_handle_ref_incr", &bake_provider_handle_ref_incr); return bake_provider_handle_ref_incr(pbph); });
bpl::def("provider_handle_release", &bake_provider_handle_release); m.def("provider_handle_release", [](pybake_provider_handle_t pbph) {
bpl::def("get_eager_limit", &pybake_get_eager_limit); return bake_provider_handle_release(pbph); });
bpl::def("set_eager_limit", &bake_provider_handle_set_eager_limit); m.def("get_eager_limit", &pybake_get_eager_limit);
bpl::def("probe", &pybake_probe); m.def("set_eager_limit", [](pybake_provider_handle_t pbph, uint64_t lim) {
bpl::def("create", &pybake_create); return bake_provider_handle_set_eager_limit(pbph, lim); });
bpl::def("write", &pybake_write); m.def("probe", &pybake_probe);
bpl::def("persist", &pybake_persist); m.def("create", &pybake_create);
bpl::def("create_write_persist", &pybake_create_write_persist); m.def("write", &pybake_write);
bpl::def("get_size", &pybake_get_size); m.def("persist", &pybake_persist);
bpl::def("read", &pybake_read); m.def("create_write_persist", &pybake_create_write_persist);
bpl::def("remove", &bake_remove); m.def("get_size", &pybake_get_size);
bpl::def("migrate", &pybake_migrate); m.def("read", &pybake_read);
bpl::def("shutdown_service", &bake_shutdown_service); m.def("remove", [](pybake_provider_handle_t pbph, bake_region_id_t rid) {
return bake_remove(pbph, rid);} );
m.def("migrate", &pybake_migrate);
m.def("shutdown_service", [](pybake_client_t client, pymargo_addr addr) {
return bake_shutdown_service(client, addr); });
#if HAS_NUMPY #if HAS_NUMPY
bpl::def("write_numpy", &pybake_write_numpy); m.def("write_numpy", &pybake_write_numpy);
bpl::def("create_write_persist_numpy", &pybake_create_write_persist_numpy); m.def("create_write_persist_numpy", &pybake_create_write_persist_numpy);
bpl::def("read_numpy", &pybake_read_numpy); m.def("read_numpy", &pybake_read_numpy);
#endif #endif
#undef ret_policy_opaque
} }
...@@ -3,14 +3,7 @@ ...@@ -3,14 +3,7 @@
* *
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#define BOOST_NO_AUTO_PTR #include <pybind11/pybind11.h>
#include <boost/python.hpp>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
...@@ -19,59 +12,66 @@ ...@@ -19,59 +12,66 @@
#include <bake.h> #include <bake.h>
#include <bake-server.h> #include <bake-server.h>
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance) namespace py11 = pybind11;
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_server_context_t)
namespace bpl = boost::python; typedef py11::capsule pymargo_instance_id;
typedef py11::capsule pymargo_addr;
typedef py11::capsule pybake_provider_t;
static bake_provider_t pybake_provider_register(margo_instance_id mid, uint8_t provider_id) { #define MID2CAPSULE(__mid) py11::capsule((void*)(__mid), "margo_instance_id", nullptr)
#define ADDR2CAPSULE(__addr) py11::capsule((void*)(__addr), "hg_addr_t", nullptr)
#define BAKEPR2CAPSULE(__bpr) py11::capsule((void*)(__bpr), "bake_provider_t", nullptr)
static pybake_provider_t pybake_provider_register(pymargo_instance_id mid, uint8_t provider_id) {
bake_provider_t provider; bake_provider_t provider;
int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider); int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider);
if(ret != 0) return NULL; if(ret != 0) return py11::none();
else return provider; else return BAKEPR2CAPSULE(provider);
} }
static bpl::object pybake_provider_add_storage_target( static py11::object pybake_provider_add_storage_target(
bake_provider_t provider, pybake_provider_t provider,
const std::string& target_name) { const std::string& target_name) {
bake_target_id_t target_id; bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(target_id)); std::memset(&target_id, 0, sizeof(target_id));
int ret = bake_provider_add_storage_target( int ret = bake_provider_add_storage_target(
provider, target_name.c_str(), &target_id); provider, target_name.c_str(), &target_id);
if(ret != 0) return bpl::object(); if(ret != 0) {
return bpl::object(target_id); return py11::none();
}
return py11::cast(target_id);
} }
static bool pybake_provider_remove_storage_target( static bool pybake_provider_remove_storage_target(
bake_provider_t provider, pybake_provider_t provider,
bake_target_id_t target_id) bake_target_id_t target_id)
{ {
return 0 == bake_provider_remove_storage_target(provider, target_id); return 0 == bake_provider_remove_storage_target(provider, target_id);
} }
static bool pybake_provider_remove_all_storage_targets( static bool pybake_provider_remove_all_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
return 0 == bake_provider_remove_all_storage_targets(provider); return 0 == bake_provider_remove_all_storage_targets(provider);
} }
static uint64_t pybake_provider_count_storage_targets( static uint64_t pybake_provider_count_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
uint64_t n = 0; uint64_t n = 0;
bake_provider_count_storage_targets(provider, &n); bake_provider_count_storage_targets(provider, &n);
return n; return n;
} }
static bpl::list pybake_provider_list_storage_targets( static py11::list pybake_provider_list_storage_targets(
bake_provider_t provider) pybake_provider_t provider)
{ {
std::vector<bake_target_id_t> result; std::vector<bake_target_id_t> result;
uint64_t n = pybake_provider_count_storage_targets(provider); uint64_t n = pybake_provider_count_storage_targets(provider);
if(n == 0) return bpl::list(); if(n == 0) return py11::list();
result.resize(n); result.resize(n);
bake_provider_list_storage_targets(provider, result.data()); bake_provider_list_storage_targets(provider, result.data());
bpl::list list_result; py11::list list_result;
for(const auto& t : result) list_result.append(t); for(const auto& t : result) list_result.append(t);
return list_result; return list_result;
} }
...@@ -81,19 +81,14 @@ static bool pybake_make_pool(const std::string& pool_name, ...@@ -81,19 +81,14 @@ static bool pybake_make_pool(const std::string& pool_name,
return 0 == bake_makepool(pool_name.c_str(), pool_size, mode); return 0 == bake_makepool(pool_name.c_str(), pool_size, mode);
} }
BOOST_PYTHON_MODULE(_pybakeserver) PYBIND11_MODULE(_pybakeserver, m)
{ {
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>() py11::module::import("_pybaketarget");
m.def("register", &pybake_provider_register);
bpl::import("_pybaketarget"); m.def("add_storage_target", &pybake_provider_add_storage_target);
bpl::opaque<bake_server_context_t>(); m.def("remove_storage_target", &pybake_provider_remove_storage_target);
bpl::def("register", &pybake_provider_register, ret_policy_opaque); m.def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
bpl::def("add_storage_target", &pybake_provider_add_storage_target); m.def("count_storage_targets", &pybake_provider_count_storage_targets);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target); m.def("list_storage_targets", &pybake_provider_list_storage_targets);
bpl::def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets); m.def("make_pool", &pybake_make_pool);
bpl::def("count_storage_targets", &pybake_provider_count_storage_targets);
bpl::def("list_storage_targets", &pybake_provider_list_storage_targets);
bpl::def("make_pool", &pybake_make_pool);
#undef ret_policy_opaque
} }
...@@ -3,14 +3,7 @@ ...@@ -3,14 +3,7 @@
* *
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#define BOOST_NO_AUTO_PTR #include <pybind11/pybind11.h>
#include <boost/python.hpp>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
...@@ -19,43 +12,45 @@ ...@@ -19,43 +12,45 @@
#include <bake.h> #include <bake.h>
#include <bake-server.h> #include <bake-server.h>
namespace bpl = boost::python; namespace py11 = pybind11;
static std::string pybake_target_id_to_string(bake_target_id_t tid) { static py11::bytes pybake_target_id_to_string(bake_target_id_t tid) {
char id[37]; char id[37];
uuid_unparse(tid.id, id); uuid_unparse(tid.id, id);
return std::string(id); return py11::bytes(std::string(id));
} }
static bpl::object pybake_target_id_from_string(const std::string& tidstr) { static py11::object pybake_target_id_from_string(const py11::bytes& btidstr) {
bake_target_id_t tid; bake_target_id_t tid;
memset(tid.id, 0, sizeof(uuid_t)); memset(tid.id, 0, sizeof(uuid_t));
if(tidstr.size() != 36) return bpl::object(); std::string tidstr = (std::string)btidstr;
if(tidstr.size() != 36) return py11::none();
int ret = uuid_parse((char*)tidstr.c_str(), tid.id); int ret = uuid_parse((char*)tidstr.c_str(), tid.id);
if(ret == 0) return bpl::object(tid); if(ret == 0) return py11::cast(tid);
else return bpl::object(); else return py11::none();
}