Skip to content

Commit 5d61911

Browse files
committed
return bytes not void* from Get
1 parent c8a4f06 commit 5d61911

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

bindings/support/BlockCache.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
namespace py = pybind11;
99

10+
/*
11+
py::bytes Get_wrapper(size_t blockSize) {
12+
void* data = Get(blockSize);
13+
py::buffer buffer;
14+
15+
if (data == nullptr) {
16+
throw std::runtime_error("Failed to get data");
17+
}
18+
return py::bytes(static_cast<const char*>(data), blockSize);
19+
}*/
20+
py::bytes Get_wrapper2(BBlockCache& self, size_t blockSize) {
21+
void* data = self.Get(blockSize);
22+
if (data == nullptr) {
23+
throw std::runtime_error("Failed to get data");
24+
}
25+
py::buffer_info buf_info(static_cast<char*>(data), sizeof(char), py::format_descriptor<char>::format(), 1, {blockSize}, {sizeof(char)} );
26+
//return py::array(buf_info);
27+
return py::bytes(static_cast<const char*>(buf_info.ptr), buf_info.size * buf_info.itemsize); //convert py::buffer to py::bytes
28+
};
1029

1130
PYBIND11_MODULE(BlockCache, m)
1231
{
@@ -15,8 +34,9 @@ m.attr("B_MALLOC_CACHE") = 1;//py::cast(B_MALLOC_CACHE);
1534

1635
py::class_<BBlockCache>(m, "BBlockCache")
1736
.def(py::init<uint32, size_t, uint32>(), "", py::arg("blockCount"), py::arg("blockSize"), py::arg("allocationType"))
18-
.def("Get", &BBlockCache::Get, "", py::arg("blockSize"))
19-
.def("Save", &BBlockCache::Save, "", py::arg("pointer"), py::arg("blockSize"))
37+
//.def("Get", &BBlockCache::Get, "", py::arg("blockSize"))//TODO this returns a void * -- let's return a py::bytes or a py::buffer
38+
.def("Get", &Get_wrapper2, "", py::arg("blockSize"))
39+
.def("Save", &BBlockCache::Save, "", py::arg("pointer"), py::arg("blockSize"))//TODO: pointer is a void * -> python can't use void *
2040
;
2141

2242

0 commit comments

Comments
 (0)