Skip to content

Commit fab35c4

Browse files
committed
fix: buffer protocol
1 parent 4e2eebe commit fab35c4

2 files changed

Lines changed: 56 additions & 38 deletions

File tree

pyWrapper.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ std::map<std::string, const char *> SID_USRTAG;
1919

2020
PYBIND11_EMBEDDED_MODULE(aiges_embed, module) {
2121
module.def("callback", &callBack, py::return_value_policy::automatic_reference);
22-
py::class_<ResponseData> responseData(module, "ResponseData");
22+
py::class_<ResponseData> responseData(module, "ResponseData", py::buffer_protocol());
2323
responseData.def(py::init<>())
24+
.def(py::init<std::string, unsigned int, int, int>())
2425
.def(py::init([](const py::buffer &b) {
2526
py::buffer_info info = b.request();
2627
if (info.format != py::format_descriptor<unsigned char>::format() || info.ndim != 1) {
2728
throw std::runtime_error("Incompatible buffer format! Please Pass Bytes...");
2829
}
29-
auto *v = new ResponseData(info.shape[0]);
30+
auto *v = new ResponseData();
31+
v->len = info.shape[0];
3032
v->data = info.ptr;
3133
// memcpy( v->data, info.ptr, info.shape[0] );
3234
return (v);
@@ -65,9 +67,10 @@ PYBIND11_EMBEDDED_MODULE(aiges_embed, module) {
6567
));
6668
})
6769
.def_readwrite("key", &ResponseData::key, py::return_value_policy::automatic_reference)
68-
.def_read("data", &ResponseData::data, py::return_value_policy::automatic_reference)
70+
.def_property_readonly("data",
71+
py::cpp_function(&ResponseData::get_data, py::return_value_policy::automatic_reference))
6972
.def_readwrite("status", &ResponseData::status, py::return_value_policy::automatic_reference)
70-
.def_read("len", &ResponseData::len, py::return_value_policy::automatic_reference)
73+
.def_property_readonly("len", py::cpp_function(&ResponseData::get_len, py::return_value_policy::automatic_reference))
7174
.def_readwrite("type", &ResponseData::type, py::return_value_policy::automatic_reference);
7275

7376
py::class_<Response> response(module, "Response");
@@ -306,21 +309,20 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
306309
tmpData->type = DataType(itemData.type);
307310
tmpData->desc = nullptr;
308311
// 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
309-
void *pr;
310-
pr = malloc(itemData.len);
311-
if (pr == nullptr) {
312-
int ret = -1;
313-
spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
314-
return ret;
315-
}
316-
ptr = PyBytes_AsString(itemData.data.ptr());
317-
Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
318-
printf("GetSIze, %d", size);
319-
printf("item data len: %d", itemData.len);
320-
memcpy(pr, ptr, itemData.len);
312+
// void *pr;
313+
// pr = malloc(itemData.len);
314+
// if (pr == nullptr) {
315+
// int ret = -1;
316+
// spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
317+
// return ret;
318+
// }
319+
// ptr = PyBytes_AsString(itemData.data.ptr());
320+
// Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
321+
printf("GetSIze data len: %d", itemData.len);
322+
// memcpy(pr, ptr, itemData.len);
321323
//char *data_ = new char[itemData.data.length()+1];
322324
// strdup(.c_str());
323-
tmpData->data = pr;
325+
tmpData->data = itemData.data;
324326
tmpData->status = DataStatus(itemData.status);
325327
if (idx == 0) {
326328
headPtr = tmpData;
@@ -480,18 +482,18 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
480482
tmpData->len = itemData.len;
481483
tmpData->type = DataType(itemData.type);
482484
tmpData->desc = nullptr;
483-
// 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
484-
void *pr;
485-
pr = malloc(itemData.len);
486-
if (pr == nullptr) {
487-
int ret = -1;
488-
spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
489-
return ret;
490-
}
491-
memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
485+
// // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
486+
// void *pr;
487+
// pr = malloc(itemData.len);
488+
// if (pr == nullptr) {
489+
// int ret = -1;
490+
// spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
491+
// return ret;
492+
// }
493+
// memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
492494
//char *data_ = new char[itemData.data.length()+1];
493495
// strdup(.c_str());
494-
tmpData->data = pr;
496+
tmpData->data = itemData.data;
495497
tmpData->status = DataStatus(itemData.status);
496498
if (idx == 0) {
497499
headPtr = tmpData;
@@ -600,20 +602,20 @@ int callBack(Response *resp, std::string sid) {
600602
tmpData->type = DataType(itemData.type);
601603
tmpData->desc = nullptr;
602604
// 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
603-
void *pr;
604-
pr = malloc(itemData.len);
605-
if (pr == nullptr) {
606-
int ret = -1;
607-
spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
608-
return ret;
609-
}
610-
ptr = PyBytes_AsString(itemData.data.ptr());
611-
Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
612-
memcpy(pr, ptr, itemData.len);
605+
// void *pr;
606+
// pr = malloc(itemData.len);
607+
// if (pr == nullptr) {
608+
// int ret = -1;
609+
// spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
610+
// return ret;
611+
// }
612+
// ptr = PyBytes_AsString(itemData.data.ptr());
613+
// Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
614+
// memcpy(pr, ptr, itemData.len);
613615
// 还是有问题::memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
614616
//char *data_ = new char[itemData.data.length()+1];
615617
// strdup(.c_str());
616-
tmpData->data = pr;
618+
tmpData->data = itemData.data;
617619
tmpData->status = DataStatus(itemData.status);
618620
if (idx == 0) {
619621
headPtr = tmpData;

pyWrapper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ class ResponseData {
4747

4848
}
4949

50+
void *get_data() {
51+
return data;
52+
}
53+
54+
void set_data(void *data_) {
55+
data = data_;
56+
}
57+
58+
unsigned int get_len() {
59+
return len;
60+
}
61+
62+
void set_len(unsigned int _len) {
63+
len = _len;
64+
}
65+
5066
std::string key;
5167
void *data;
5268
unsigned int len;

0 commit comments

Comments
 (0)