我几周前才开始学习Python(新手)。我们正在使用Python(由公司指定的2.7)作为测试前端,而我正在尝试测试一个C例程,该例程将指向数据二进制块的指针(埋入数据结构中)作为输入参数。我可能做错了很多事情,所以请帮助我理解。
看起来bytearray(size)构成了一个打包的数据块,但是如果这是错误的,请告诉我。我还尝试过使用create_string_buffer(size + 1),它似乎工作得更好。但是我仍然无法使其按我需要的方式工作。
我们将SWIG作为中间因素(也由公司规定,目前无法更改),这可能是一个复杂的因素。我可以对SWIG使用的.i文件进行修改,如果这样做会有所帮助。
关于在SWIG和Python 2.7的约束下如何实现这一目标的任何想法?我愿意使用其他软件包或工具集,只要学习曲线不是很陡峭(这一项功能只需要它)并且它们是免费的(不需要额外工具的预算)。
C数据结构(已精简-现有代码,无法更改)
typedef struct pb_callback_s pb_callback_t;
struct pb_callback_s
{ void *arg;
};
typedef struct _file_page_type
{ uint32_t page_length; //nominal 256 bytes
pb_callback_t data_ptr_handler;
} file_page_type;
//function prototype
void post_software_management_load(file_page_type file_page);
Python代码(向下细分)
myFP = h_api.file_page_type() #SWIG constructor
dataBlock = bytearray(totalUpdateSize)
#here is where we need to create a pointer to the byte array
#here is where we need to put that info into myFP.data_ptr_handler.arg
h_api.post_software_management_load(myFP)
一些数据点:
-print "myFP.data_ptr_handler.arg", type(myFP.data_ptr_handler.arg)
Results: myFP.data_ptr_handler.arg <type 'NoneType'>
-the function `pointer()` seems to work for `create_string_buffer()` objects, but not for `bytearray()` objects (although both the object and the pointer appear to be of type `ctypes.c_char_Array_257`)
-the function `byref()` works for `create_string_buffer()` objects (results in a type of type `CArgObject` back from `byref()`), but not for `bytearray()` objects
我正在尝试按照与此密切相关的顺序来做这些事情:
我一直收到“格式不正确的代码”错误,列出了本节中曾经使用过的信息,因此将其移到了上面代码部分中的“某些数据点”标记中。
[最终,我发现尝试将数据从C转换为Python的痛苦超过了它的价值。提供C接口的公司向我提供了适当的功能(必要的C-Pointer嵌入式Python),并且可以正常工作。对不起,因为我不再在该公司工作,所以无法确切知道调用的辅助代码(C-> Py)。
感谢所有帮助,很抱歉我花了这么长时间才回来回答问题。