0

I’m trying to pass a structure between C and Python using SWIG. I am completely new to Python and C. I searched for passing structure using SWIG, without success.

I based my code on examples from SWIG Python tutorial, page 55 and 56. It should fetch the input values from Python, multiply them by 2 in C and return the results to Python. I am getting the error AttributeError: 'module' object has no attribute 'new_info.

sample.c

#include <stdio.h>
#include "sample.h"

struct info sample;

void getstruct (struct info *sample);

void getstruct (struct info *sample) {

   int i = 0;
   int j = 0;
   int k = 0;
   int l = 0;

   i = 2 * sample->i;
   j = 2 * sample->j;
   k = 2 * sample->k;
   l = 2 * sample->l;

   sample->i = i;
   sample->j = j;
   sample->k = k;
   sample->l = l;

   return(&sample);

}

sample.i

%module sample
%{
typedef struct
{
   int i;
   int j;
   int k;
   int l;
} info;

extern void getstruct (struct info *sample);

info *new_info(int i, int j, int k, int l) {
    info *in = (info *) malloc(sizeof(info));
    in->i = i;
    in->j = j;
    in->k = k;
    in->l = l;
    return in;
}

void delete_info(info *in) {
    free(in);
}
%}

extern void getstruct (struct info *sample);
typedef struct
{
   int i;
   int j;
   int k;
   int l;
} info;

Command executed to build the wrapper:

swig -python sample.i
gcc -fPIC -c sample.c sample_wrap.c -I/usr/include/python2.7
ld -shared sample.o sample_wrap.o -o _sample.so

Python error:

[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>>
>>> print sample
<module 'sample' from 'sample.pyc'>
>>> print sample.getstruct(1,2,3,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getstruct() takes exactly 1 argument (4 given)
>>> v = new_info(1,2,3,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'new_info' is not defined
>>> v = sample.new_info(1,2,3,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'new_info'
>>>
7
  • Which part of the code works for you? Did you write it from scratch? Commented Mar 5, 2015 at 22:43
  • I write it from scratch. I have referred the link swig.org/papers/PyTutorial98/PyTutorial98.pdf Page no 55, 56. I have updated the question section with updated program. Please let me know what am i doing wrong here. Commented Mar 5, 2015 at 22:57
  • I edited your question to formulate it as succinctly as possible, remove exaggeration (searched everywhere; this harms your credibility) and split the code block to include just one file in each block. Please edit to include what you do understand and how you tried to debug your issue. Commented Mar 5, 2015 at 23:11
  • You C code should not compile at all. You declare the info typedef twice (one error) and then use struct info instead of info (another error). To work with structures, you don’t need typedef. Commented Mar 5, 2015 at 23:17
  • Declaring getstruct immediately before its definition does nothing useful, only duplicates code. Commented Mar 5, 2015 at 23:19

1 Answer 1

1

In the sample.i file, you've added the new_info and delete_info functions to the wrapper code directly by declaring it within %{ and %}, but did not tell SWIG to generate wrappers for those functions. Either repeat the code again outside the %{/%}, or use %inline %{/%}. The latter adds the code directly to the wrapper as well as tells SWIG to wrap it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.