从python 3.6.9传递到C-API结构的最佳方法是什么
我有一个 C 库,我正在尝试为它创建一个 Python 接口,
但库希望事先初始化一个结构体,主要问题是结构的一个元素是指向另一个结构的指针。
带有结构的 C 标头部分:
enum filter_type {
BLACK, /* Black list */
WHITE /* White list */
};
struct message {
uint32_t mid; /* the message id */
uint8_t interface; /* the interface */
} __attribute__ ((__packed__));
struct filtering {
struct message *filter;
uint32_t arr_len; /* length of the array
enum filter_type mf_type; /* filter type - black or white list */
} __attribute__ ((__packed__));
蟒蛇代码:
from ctypes import Structure, c_uint32, c_uint8, c_bool
from myC_Module import test_struct
class Message(Structure):
_fields_ = [('mid', c_uint32),
('interface', c_uint8)]
def gen_filter(mids, mf_type):
class Filtering(Structure):
_fields_ = [('filter', Message*len(mids)),
('arr_len', c_uint32),
('mf_type', c_bool)]
c_array = Message * len(mids)
return Filtering(c_array(*mids), len(mids), mf_type)
messages = [ # for testing
Message(int("1af", 16), 3),
Message(int("aaaaaaaa", 16), 100),
Message(int("bbbbbbbb", 16), 200),
]
print(test_struct(gen_filter(messages, True)))
C-API test_struct函数代码:
static PyObject *test_struct(PyObject *self, PyObject *args) {
struct filtering *filtering = NULL;
Py_buffer buffer;
PyObject *result;
if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
return NULL;
printf("buffer.len: %ldn", buffer.len);
filtering = buffer.buf;
printf("recived results: arr_len[%d] mf_type[%d]n", filtering->arr_len, filtering->mf_type);
printf("filter: %dn", filtering->filter);
result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
PyBuffer_Release(&buffer);
return result;
}
结果:
buffer.len: 32
recived results: arr_len[431] mf_type[3]
filter: -1442840576
b'xafx01x00x00x03x00x00x00xaaxaaxaaxaadx00x00x00xbbxbbxbbxbbxc8x00x00x00x03x00x00x00x01x00x00x00'
使用以下方法,在消息的单个值(不是数组,并通过相应地更改过滤结构(的情况下,它可以工作。
知道我做错了什么,或者正确的方法是什么?
谢谢。
编辑 - 额外的想法我想
现在我明白为什么会发生这种情况,但我不知道如何解决它(还(。
我为 buffer.buf 添加了额外的打印件,以查看我实际拥有的内容:
filtering = buffer.buf;
char * buf = (char*)buffer.buf;
for(int i=0; i<buffer.len; i++){
if(i==0)
printf("[%d, ", (uint8_t)buf[i]);
if(i<buffer.len-1)
printf("%d, ", (uint8_t)buf[i]);
else
printf("%d]n", (uint8_t)buf[i]);
}
我得到了以下内容:
[175, 1, 0, 0, 3, 0, 0, 0, 170, 170, 170, 170, 100, 0, 0, 0, 187, 187, 187, 187, 200, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0]
同样的结果在 python 中print("[{}]".format(', '.join(map(str, returned))))
为此,由于
分配的类型(c_uint32、c_uint8(,我希望缓冲区更短,
因为我有一个有 3 条消息的gen_filter,每条消息的大小应该是 5,gen_filter中的额外数据也应该是 5,我希望总大小为 20, 但正如我们所看到的,它更乞丐。
我注意到 c_uint8 型实际上是 4 号而不是 1 号。
为此,我希望得到以下结果:
[175, 1, 0, 0, 3, 170, 170, 170, 170, 100, 187, 187, 187, 187, 200, 3, 0, 0, 0, 1]
由于:
{[<c_uint32,c_uint8>,<c_uint32,c_uint8>,<c_uint32,c_uint8>],<c_uint32,c_uint8>}
缓冲区具有称为 format 的元素,其中包含以下内容:format T{(3)T{<I:mid:<B:interface:}:filter:<I:arr_len:<?:mf_type:}
你的问题是(可能是?(gen_filter
返回的结构与你在 C 中定义的结构不同。与您在gen_filter
中定义的 C 等效项是:
struct filtering {
struct message filter[arr_len]; /* NOT ACTUALLY VALID C SINCE arr_len
ISN'T A CONSTANT */
uint32_t arr_len; /* length of the array
enum filter_type mf_type; /* filter type - black or white list */
} __attribute__ ((__packed__));
这是一个内存块,包含结构内的消息空间。但是,在 C 中,消息列表单独分配给结构,并且filter
仅指向它。
你的Python代码应该是这样的:
class Message(Structure):
_fields_ = [('mid', c_uint32),
('interface', c_uint8)]
_pack_ = 1 # matches "packed" - an important addition!
class Filtering(Structure):
_fields_ = [('filter', POINTER(Message)),
('arr_len', c_uint32),
('mf_type', c_bool)]
_pack_ = 1 # matches "packed" - an important addition!
def __init__(self, messages, mf_type):
self.filter = (Message*len(messages))(*messages)
self.arr_len = len(messages)
self.mf_type = mf_type
请注意,单独分配的消息数组的生存期与 PythonFiltering
实例的生存期相关联。
遵循您的 C 代码有点困难,因为您使用神秘且未指定的属性filtering->int_list
.假设int_list
实际上是filter
,你只是打印指针(解释为有符号的int(,而不是它指向的内容。