1

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:

  1. get the Python script to generate an object that is compatible with C (ie a contiguous block of binary data) and
  2. get a C compatible pointer pointed at it
  3. get that pointer assigned into a structure and passed into the C function (via SWIG)

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.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
kking85743
  • 21
  • 5
  • 3
    What is your question? – Henri Menke Dec 02 '17 at 02:21
  • I'm trying to test a C routine in Python. The C routine needs a pointer to a block of data (C array). This pointer is nested deep in a set of structures. I need to use Python to create a C compatible data block, get a pointer to that data block, assign that pointer into the C data structure so I can properly invoke the C function in the library. Does that help? – kking85743 Dec 02 '17 at 02:24
  • How do you transmit data between _C_ and _Python_? They are 2 different types of objects which are not compatible. Let's say you can create the pointer object in _Python_, then how would you fill it? As an alternative you could use [\[Python\]: ctypes — A foreign function library for Python](https://docs.python.org/2/library/ctypes.html#module-ctypes), but you'd need to redefine all the _C_ structs as descendants of `ctypes.Structure`. – CristiFati Dec 07 '17 at 18:54
  • You could also check [\[SO\]: Pass str as an int array to a Python C extended function (extended using SWIG)](https://stackoverflow.com/questions/47276327/pass-str-as-an-int-array-to-a-python-c-extended-function-extended-using-swig). – CristiFati Dec 07 '17 at 18:59

1 Answers1

1

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.

kking85743
  • 21
  • 5