7

I'm trying interface a small C function I made into python using SWIG and the Numpy typemaps

This function is defined as follows

void nw(int* D, int Dx, int Dy, int* mat, int mx, int my, char *xstr, int xL,char *ystr, int yL);

And my interface file is as follows

%module nw

%{
    #define SWIG_FILE_WITH_INIT
    #include "nw.h"
%}

%include "numpy.i"

%init %{
  import_array();
%}
/*type maps for input arrays and strings*/
%apply (int* INPLACE_ARRAY2, int DIM1, int DIM2) {(int* D, int Dx, int Dy)}
%apply (int* IN_ARRAY2, int DIM1, int DIM2) {(int* mat, int mx, int my)}
%apply (char* IN_ARRAY, int DIM1){(char *xstr, int xL),(char *ystr, int yL)}

%include "nw.h"

To test it, I used the following input

D = numpy.zeros((5,5),numpy.int)
mat = numpy.array([[1, 0, 0, 0, 0, 0],
                   [0, 1, 0, 0, 0, 0],
                   [0, 0, 1, 0, 0, 0],
                   [0, 0, 0, 1, 0, 0],
                   [0, 0, 0, 0, 1, 0],
                   [0, 0, 0, 0, 0, 1]],numpy.int)
x = numpy.array(list("ABCD"))
y = numpy.array(list("ABCD"))
import nw
nw.nw(D,mat,x,y)

But when I run it, I get the following

TypeError: nw() takes exactly 6 arguments (4 given)

I am really confused about how these arguments are defined. Does anyone here have an idea why there are 6 arguments and what these arguments are? Thanks!

2
  • Just guess. The nw takes 10 parameters and you 4 parameters using %apply directive. So you need to provide 6 parameters. Commented Aug 3, 2013 at 0:47
  • I'm not sure if I follow ... You're implying the %apply directive won't reduce my original parameters to 4 parameters? Commented Aug 3, 2013 at 2:51

1 Answer 1

4

Alright, I think I have figured out the problem.

As it turns out, SWIG really didn't like the apply directives I made for the cstrings.

I should have be should the following directive instead.

%apply (char *STRING, int LENGTH) {(char *xstr, int xL),(char *ystr, int yL)}

Should have been following the cookbook more carefully haha

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.