简体   繁体   中英

python pointer to C structure in SWIG — accessing the struct members

I'm trying to get a very basic Python-to-C interface working with SWIG where I can pass a pointer to a structure to a C function and the members get populated, and I can then access the members in python.

In the below example, everything works except when I try to print the structure members:

print swig_test.diags_mem0_get(var)

Results in:

$ ./runpython.py
Traceback (most recent call last):
  File "./runpython.py", line 11, in <module>
    print swig_test.diags_mem0_get(var)
AttributeError: 'module' object has no attribute 'diags_mem0_get'

Whereas this:

print var.mem0

Results in:

$ ./runpython.py
<Swig Object of type 'uint16_t *' at 0x7f8261e15b40>swig/python detected a memory leak of type 'uint16_t *', no destructor found.

I am following the SWIG 3.0 Documentation, specifically section "5.5 Structures and unions" here: http://swig.org/Doc3.0/SWIGDocumentation.html#SWIG_nn31

What am I doing wrong?

I have distilled the example down to bare bones:

swig_test.h

typedef struct diags_t {
    uint16_t mem0;
    uint16_t mem1;
} diags;

diags *malloc_diags(void);
void free_diags(diags *pdiag);

int get_diags(diags *pdiags);

swig_test.c

#include <stdlib.h>  // malloc()
#include <stdint.h>  // uint16_t
#include "swig_test.h"

int main(int argc, char **argv) {
    return 0;
}

int get_diags(diags *pdiags) {
    pdiags->mem0 = 0xdead;
    pdiags->mem1 = 0xbeef;
    return 0;
}

diags *malloc_diags(void) {
    diags *dptr = malloc(sizeof(diags));
    return dptr;
}

void free_diags(diags *pdiag) {
    if (pdiag != NULL) 
        free(pdiag);
}

swig_test.i

%module swig_test

%{
#include "swig_test.h"
%}

%include "swig_test.h"

Makefile

CXX = gcc
INCLUDES = -I./
COMPFLAGS = -c -Wall -fPIC
PYINC = /usr/include/python2.7
SWIG = /usr/bin/swig

all: swig_test _swig_test.so

swig_test: swig_test.o
    $(CXX) -Wall $^ -o $@

swig_test.o: swig_test.c
    $(CXX) $(COMPFLAGS) $(INCLUDES) $^

_swig_test.so: swig_test_wrap.o swig_test.o
    $(CXX) -shared $^ -L$(PYLIB) -lpython2.7 -o $@

swig_test_wrap.o: swig_test_wrap.c
    $(CXX) $(COMPFLAGS) $(INCLUDES) -I$(PYINC) $^

swig_test_wrap.c: swig_test.i
    $(SWIG) -python $(INCLUDES) $^

And finally the simple python example:

runpython.py

#!/usr/bin/python2
import swig_test

var = swig_test.malloc_diags()

if var == 'NULL':
    print "Error, no memory left"
else:
    ret = swig_test.get_diags(var)
    if ret == 0:
        print swig_test.diags_mem0_get(var)
        print var.mem0
    swig_test.free_diags(var)

The functionality you're looking for comes in typemaps. The documentation freely admits that "At first glance, this code will look a little confusing." Here's what worked for me.

In essence, a typemap is a few lines of code that SWIG swaps in when it needs to convert between Python and C. They're separately defined for Python to C ( %typemap(in) ) and C to Python ( %typemap(out) ). SWIG's documentation also defines a few magic variables:

$input refers to an input object that needs to be converted to C/C++.

$result refers to an object that is going to be returned by a wrapper function.

$1 refers to a C/C++ variable that has the same type as specified in the typemap declaration (an int in this example).

For unsigned integer support, you just need in and out maps for uint8_t , uint16_t , and uint32_t

The lines below provide that functionality. They can go into SWIG's .i file, or the main header (with an ifdef SWIG guard around them).

/* uintXX_t mapping: Python -> C */
%typemap(in) uint8_t {
    $1 = (uint8_t) PyInt_AsLong($input);
}
%typemap(in) uint16_t {
    $1 = (uint16_t) PyInt_AsLong($input);
}
%typemap(in) uint32_t {
    $1 = (uint32_t) PyInt_AsLong($input);
}

/* uintXX_t mapping: C -> Python */
%typemap(out) uint8_t {
    $result = PyInt_FromLong((long) $1);
}
%typemap(out) uint16_t {
    $result = PyInt_FromLong((long) $1);
}
%typemap(out) uint32_t {
    $result = PyInt_FromLong((long) $1);
}

Resources I found useful:

One "fix" is to change the uint16_t members to int types. Then this syntax works:

print var.mem0

Clearly SWIG has some problems with non-integer types.

I'd love it if somebody proposed an alternative solution that lets me keep my uint16_t types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM