Reputation: 21
I’ve just started learning Python a few weeks ago (noob). We are using Python (2.7 dictated by the company) as test front-end, and I’m trying to test a C routine that takes a pointer (buried within a data structure) to a binary block of data as an input parameter. I'm probably doing many things wrong, so please help me understand.
It looks like bytearray(size) makes a packed block of data, but let me know if that’s wrong. I also tried using a create_string_buffer(size+1), which seems to work a little better. However I still cannot get it to work the way I need.
We are using SWIG as the go-between (also dictated by company, no option to change at the moment), which may be a complicating factor. I can make modifications to the .i files used by SWIG, if that is something that will help.
Any thoughts on how to make this happen within the constraints of SWIG & Python 2.7? I am open to using other packages or toolsets, so long as the learning curve isn’t terribly steep (just need it for this one capability) and they are free (no budget for extra tools).
The C data structures (stripped down - existing code, can't change)
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 code (stripped down)
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)
Some data points:
-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
I'm trying to do these things, in this order which are all closely related:
I kept getting "code that is not properly formatted" errors, listing the info that used to be in this section, so it was moved to the "some data points" tag in the code section, above.
Upvotes: 1
Views: 250
Reputation: 21
Ultimately, I found that trying to convert the data from C to Python was more pain than it was worth. The company that was providing the C interface provided the proper functionality (the necessary C-Pointer-embedded-in-Python) to me, and that worked. Sorry for not knowing exactly what the helper code (C->Py) is called as I am no longer at that company.
Thanks for all the help, and sorry it took me so long to get back to answer the question.
Upvotes: 1