Reputation: 43
I'm using swig to wrap a c++ function to python like this:
int getBytes(unsigned char *& result, int length)
I have learnt from this question how to wrap a function returned char* by reference:
%typemap(in,numinputs=0) char*& (char* tmp) %{
$1 = &tmp;
%}
%typemap(argout) char*& (PyObject* obj) %{
obj = PyUnicode_FromString(*$1);
$result = SWIG_Python_AppendOutput($result,obj);
%}
%typemap(freearg) char*& %{
free(*$1);
%}
I tried to apply this to unsigned char* but failed and get the error that SWIG_Python_AppendOutput
cannot convert an unsigned char** to signed char**. I searched python doc and didn't find a function which can convert an unsigned char** to unsigned char**.
Anyone can help? Thanks!
Upvotes: 2
Views: 614
Reputation: 43
It's long time ago that I have solved this question, the following is my solution:
%typemap(in, numinputs= 0) (unsigned char *&result, int &length) (unsigned char *temp, int len) %{
$1 = &temp;
$2 = &len;
%}
%typemap(argout) (unsigned char *&result, int &length) %{
PyObject* arg5 = 0;
arg5 = SWIG_FromCharPtrAndSize((const char *)(*$1), *$2);
PyObject* temp = NULL;
temp = $result;
$result = PyList_New(1);
PyList_SetItem($result, 0, temp);
PyList_Append($result, (PyObject*)arg5);//unsigned char *&
PyList_Append($result, PyInt_FromLong(*$2));
Py_DECREF(temp);
%}
Upvotes: 2