Input Python 3 bytes to C char* via SWIG
NickName:banderlog013 Ask DateTime:2018-07-20T00:13:54

Input Python 3 bytes to C char* via SWIG

Im trying to create a wrapper around SWT algorythm written on C.
I found this post and code from there is perfectly working in python 2.7, but when I am trying to run it from python 3, error emerges:
in method 'swt', argument 1 of type 'char *'.

As far as I know, it is because open(img_filename, 'rb').read() in python 2.7 returns string type, but in python 3 it is a bytes type.

I tried to modify ccvwrapper.i with the code below but without success

%typemap(in) char, int, int, int {
     $1 = PyBytes_AS_STRING($1);
}

Functions header: int* swt(char *bytes, int array_length, int width, int height);

How I can pass python3 bytes to that function via SWIG?

Copyright Notice:Content Author:「banderlog013」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51427455/input-python-3-bytes-to-c-char-via-swig

Answers
Henri Menke 2018-07-20T08:24:27

You are using multi-argument typemaps wrong. Multi-argument typemaps have to have concrete parameter names. Otherwise they'd match too greedily in situations where this is not desired. To get the bytes and the length of the buffer from Python use PyBytes_AsStringAndSize.\n\ntest.i\n\n\n\n%module example\n%{\nint* swt(char *bytes, int array_length, int width, int height) {\n printf(\"bytes = %s\\narray_length = %d\\nwidth = %d\\nheight = %d\\n\",\n bytes, array_length, width, height);\n return NULL;\n}\n%}\n\n%typemap(in) (char *bytes, int array_length) {\n Py_ssize_t len;\n PyBytes_AsStringAndSize($input, &$1, &len);\n $2 = (int)len;\n}\n\nint* swt(char *bytes, int array_length, int width, int height);\n\n\ntest.py\n\nfrom example import *\nswt(b\"Hello World!\", 100, 50)\n\n\nExample invocation:\n\n$ swig -python -py3 test.i\n$ clang -Wall -Wextra -Wpedantic -I /usr/include/python3.6/ -fPIC -shared test_wrap.c -o _example.so -lpython3.6m\n$ python3 test.py \nbytes = Hello World!\narray_length = 12\nwidth = 100\nheight = 50\n",


More about “Input Python 3 bytes to C char* via SWIG” related questions

Input Python 3 bytes to C char* via SWIG

Im trying to create a wrapper around SWT algorythm written on C. I found this post and code from there is perfectly working in python 2.7, but when I am trying to run it from python 3, error emerge...

Show Detail

SWIG return bytes instead of string with typemap

I have two functions in my C library: void passBytes(char *data, int size); void returnBytes(char **data, int *size); //dynamically allocates I successfully implement the wrapper for the first fun...

Show Detail

SWIG: passing bytes between Python and C

I have searched online for a while, but what is the correct way to pass bytes to and fro between Python and C? I have leveraged the example from SWIG documentation: C Code: char* foo(char *buff,...

Show Detail

SWIG return unsigned char * from C to Python

I'm running into a problem with generating an interface for python with underlying C code. I have the following pieces of code: prov.h #include<string.h> #include<stdio.h> #include<

Show Detail

python swig string to char* conversion

I am working with a project that uses python SWIG module. The module has a function: void func(char* pin, unsigned long len); and is called from python like this: key = "\x42\x1d\xd7" modul.func...

Show Detail

swig char ** as a pointer to a char *

I'm having trouble with swig and char ** as pointer to a variable char * (not as a list of char *!!!). I couldn't find out a way to wrap the pointer to a char *. The aim is to write the result of ...

Show Detail

Passing mutable char * to Python in SWIG

I have the a purely virtual class I need to implement in Python, so that it's called back from a C++ framework. File example.h: /** Abstract reader to be implemented */ class Reader { public: ...

Show Detail

SWIG, C, Python - Ignoring NULL terminators when passing a char * to python

Put quickly: I want to send a full char * from a C module (build with SWIG) to a python callback function. I can already do most of this BUT: my char array is just binary data and therefore conta...

Show Detail

Proper way of handling char * returning data in swig/python

I have a C++ routine MyClass::myFunction(char * message). which returns a message by writing in the message buffer. This routine is exported to python via SWIG. When the routine is called, I assume...

Show Detail

How to use SWIG Generated C structures in Java as input to C functions via SWIG/JNI

I have a SWIG interface file that exposes some C functions (via JNI) to my Java application and these C structures are used as input into the C function (via SWIG/JNI). SWIG generates the structur...

Show Detail