1

This is part of the code I wanna wrap:

typedef unsigned char UINT8;
...
void fn(UINT8 data[]);

In a .i file, I included:

From 8.2.2 carrays.i:
%array_class(UINT8, UINT8Array)

To support array write operation:

%typemap(in) UINT8[ANY] ($1_basetype temp[$1_dim0]) {
    int i;
    if (!PySequence_Check($input)) {
        PyErr_SetString(PyExc_ValueError,"Expected a sequence as input");
        return NULL;
    }
    if (PySequence_Length($input) != $1_dim0) {
        PyErr_SetString(PyExc_ValueError,"Input sequence size incorrect, should have $1_dim0 ints");
        return NULL;
    }
    for (i = 0; i < $1_dim0; i++) {
        PyObject *o = PySequence_GetItem($input,i);
        if (PyNumber_Check(o)) {
            temp[i] = ($1_basetype)PyInt_AsLong(o);
            Py_DECREF(o);
        } else {
            Py_XDECREF(o);
            PyErr_SetString(PyExc_ValueError,"Input sequence elements must be numbers");      
            return NULL;
        }
    }
    $1 = temp;
}

then, when compiling, my code fails:

example_wrap.cxx:40:15: error: storage size of ‘temp2’ isn’t known
   UINT8 temp2[] ;

Please note, that the wrapping works if my function is like:

void fn(UINT8 data[10]);

void fn(UINT8* data);

Is there a way I can tell swig, when there is a UINT8[] to trait it as it is a UINT8 *. Something like this:

%typemap(in) UINT8[] ($1_basetype *temp) {

Thanks, Pablo

pablomtz
  • 129
  • 7
  • How is the C code supposed to know how long the array is if you don't pass a size? – Mark Tolonen Jun 11 '19 at 23:49
  • I know what you mean, but this function receives a pointer and iterate in a fixed (constant) amount of iterations. Something like: fn(uint8 data[]) { for (unsigned n=0; n<300; n++) ... }. I know its insecure like any function receiving a pointer (actually It's receiving one), but I cannot modify that func I wanna wrap. Thanks – pablomtz Jun 12 '19 at 00:29

1 Answers1

1

In the comments the OP stated that the function parameter had a fixed size. Let's assume the header file looks like this:

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

typedef unsigned char UINT8;

API void fn(UINT8 data[]); // data must be length 300

The OP's typemap works, but doesn't know the size. To let SWIG know that there is a fixed size and still take advantage of the generic typemap, the following can be used:

%module test

%{
#include "test.h"

// Implementation...this would normally be in a .cpp file and linked in.
// Note to see the result an argout typemap would be needed too...
API void fn(UINT8 data[]) {
    for(int i = 0; i < 300; ++i)
        data[i] += 1;
}
%}

// OP's original generalized array typemap...
%typemap(in) UINT8[ANY] ($1_basetype temp[$1_dim0]) {
    int i;
    if (!PySequence_Check($input)) {
        PyErr_SetString(PyExc_ValueError,"Expected a sequence as input");
        return NULL;
    }
    if (PySequence_Length($input) != $1_dim0) {
        PyErr_SetString(PyExc_ValueError,"Input sequence size incorrect, should have $1_dim0 ints");
        return NULL;
    }
    for (i = 0; i < $1_dim0; i++) {
        PyObject *o = PySequence_GetItem($input,i);
        if (PyNumber_Check(o)) {
            temp[i] = ($1_basetype)PyInt_AsLong(o);
            Py_DECREF(o);
        } else {
            Py_XDECREF(o);
            PyErr_SetString(PyExc_ValueError,"Input sequence elements must be numbers");
            return NULL;
        }
    }
    $1 = temp;
}

// Assume there are lots of other functions in test.h,
// but we don't want to wrap the function that requires
// a fixed-sized array and doesn't declare the size.
%ignore fn;

// Process test.h, but skip wrapping fn.
%include "test.h"

// This "un-ignores" the function and provides a replacement
// that declares the fixed size.
// See http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIG_rename_ignore
%rename("%s") fn;
void fn(UINT8 temp[300]);

Demo:

>>> import test
>>> test.fn([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Input sequence size incorrect, should have 300 ints
>>> test.fn([1]*300)  # This works
>>> test.fn('a'*300)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Input sequence elements must be numbers
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251