Hello,

I need to wrap a structure for python that can take a pointer to a list of structures and the size, in bytes, of this list.  For example,

struct a {
    int a;
};

struct my_struct {
    unsigned char *struct_list;
    unsigned int struct_length_bytes;
};

This could be used as follows,

void generic_function_for_a(my_struct *input) {
    if (input->struct_length_bytes % sizeof(struct a) != 0)
        // Invalid parms
}

This is used for an external library so I can't change this behavior.  I can write a typemap to get a pointer to each structure from a python list but, since they have to be contiguous in memory, I have to calloc a list but don't know the size of the structure.

%typemap(in) unsigned char * {
    if (PySequence_Check($input)) {
        int i;
        void *temp_ptr = NULL;

        PyObject *o;

        int input_size = PyList_Size($input);

        // Need the structure size for calloc
        int struct_size = ??
        $1 = calloc(input_size, struct_size);
        
        for (i = 0; i < input_size; ++i) {
            o = PySequence_GetItem($input, i);
            
            // Get generic pointer
            if ((SWIG_ConvertPtr(o, (void **) &temp_ptr, NULL, SWIG_POINTER_EXCEPTION)) == -1)
                return NULL;

            memcopy($1 + struct_size * i, temp_ptr, struct_size);
    }

    else
        if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1)
            return NULL; 
}

I've looked through the documentation and the generated wrapper code but it seems like swig doesn't save the size of the structures anywhere.  The only solution I can think of is to extend every structure to include a size but I have hundreds of structures to work with.  Surely there must be a better solution?

Thank you.

Best regards,
Diego Montemayor