SWIG, c++ to Python: array of pointers (double pointer) not working

Discussion in 'Python' started by Matteo, Mar 14, 2009.

  1. Matteo

    Matteo Guest

    Re-posting in more simple and precise terms from a previous thread
    http://groups.google.it/group/comp....read/thread/6dd7bd9a09b8a011/5119cf15ebfa38b8

    Problem:
    SWIG doesn't properly wrap c++ arrays of pointers, therefore when you
    try to call a c++ function which requires them, a TypeError exception
    is raised.

    Similar story here: http://osdir.com/ml/programming.swig/2003-02/msg00064.html

    Already tried:
    - some ctypes functions
    - tuple or string instead of list

    Possibile solutions:
    something like http://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapFAQ.html#20
    that didn't work either, but I think I was not able to adapt the code
    to my case, since the example is poorly explained.

    Code to reproduce error:
    I made a dptest.cpp function that calculates the sum of an array of
    pointers to ints.

    #include "dptest.h"
    //the header file is just
    //int sum(int**, int);

    int sum(int** dp, int len){
    int sum = 0;
    for (int i = 0; i < len; i++){
    sum += *(dp);
    }
    return sum;
    }

    swig -c++ -python, then setup.py build_ext --inplace gets it nicely
    compiled and wrapped for python use. It also is imported without
    problems, but then...

    matteo@matteo:~/lab/sandbox$ python
    Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dptest as dp
    >>> l = [1, 2, 3, 4]
    >>> size = len(l)
    >>> dp.sum(l,size)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: in method 'sum', argument 1 of type 'int **'

    NOTE: A pure c++ program works as expected:

    #include <iostream>

    int sum(int**, int);

    int main(){
    int **array_of_ptr = new int*[4];
    for (int i = 0; i < 4; i++){
    array_of_ptr = new int;
    *array_of_ptr = i+1; //fill it with 1,2,3,4: 1+2+3+4 = 10
    }
    std::cout << sum(array_of_ptr, 4) << std::endl;
    }

    int sum(int** dp, int len){
    int sum = 0;
    for (int i = 0; i < len; i++){
    sum += *(dp);
    }
    return sum;
    }

    compiling and running prints the correct result:
    matteo@matteo:~/lab/sandbox$ ./purecpp
    10
     
    Matteo, Mar 14, 2009
    #1
    1. Advertising

  2. Re: SWIG, c++ to Python: array of pointers (double pointer) notworking

    On Mar 14, 5:22 am, Matteo <> wrote:
    > Re-posting in more simple and precise terms from a previous threadhttp://groups.google.it/group/comp.lang.python/browse_thread/thread/6...
    >
    > Problem:
    > SWIG doesn't properly wrap c++ arrays of pointers, therefore when you
    > try to call a c++ function which requires them, a TypeError exception
    > is raised.
    >
    > Similar story here:http://osdir.com/ml/programming.swig/2003-02/msg00064.html
    >
    > Already tried:
    > - some ctypes functions
    > - tuple or string instead of list
    >
    > Possibile solutions:
    > something likehttp://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapF...
    > that didn't work either, but I think I was not able to adapt the code
    > to my case, since the example is poorly explained.
    >
    > Code to reproduce error:
    > I made a dptest.cpp function that calculates the sum of an array of
    > pointers to ints.
    >
    > #include "dptest.h"
    > //the header file is just
    > //int sum(int**, int);
    >
    > int sum(int** dp, int len){
    > int sum = 0;
    > for (int i = 0; i < len; i++){
    > sum += *(dp);
    > }
    > return sum;
    >
    > }
    >
    > swig -c++ -python, then setup.py build_ext --inplace gets it nicely
    > compiled and wrapped for python use. It also is imported without
    > problems, but then...
    >
    > matteo@matteo:~/lab/sandbox$ python
    > Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
    > [GCC 4.3.2] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> import dptest as dp
    > >>> l = [1, 2, 3, 4]
    > >>> size = len(l)
    > >>> dp.sum(l,size)

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: in method 'sum', argument 1 of type 'int **'
    >
    > NOTE: A pure c++ program works as expected:
    >
    > #include <iostream>
    >
    > int sum(int**, int);
    >
    > int main(){
    > int **array_of_ptr = new int*[4];
    > for (int i = 0; i < 4; i++){
    > array_of_ptr = new int;
    > *array_of_ptr = i+1; //fill it with 1,2,3,4: 1+2+3+4 = 10
    > }
    > std::cout << sum(array_of_ptr, 4) << std::endl;
    >
    > }
    >
    > int sum(int** dp, int len){
    > int sum = 0;
    > for (int i = 0; i < len; i++){
    > sum += *(dp);
    > }
    > return sum;
    >
    > }
    >
    > compiling and running prints the correct result:
    > matteo@matteo:~/lab/sandbox$ ./purecpp
    > 10



    Depending on what you are wanting to accomplish with your SWIG library
    you may be able to more easily glue your Python code to the C++ piece
    using SIMPL (http://www.icanprogram.com/simpl). SIMPL is an ultra
    lightweight toolkit useful for gluing modules written in different
    languages (C, C++, Python, Tcl/Tk, JAVA) using Send/Receive/Reply
    messaging first pioneered by QNX.

    If you think SIMPL might help, don't hesitate to contact me offlist
    at this email address.

    bob
    SIMPL project coordinator
     
    bobicanprogram, Mar 17, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,506
    Victor Bazarov
    Feb 17, 2005
  2. Ernie
    Replies:
    1
    Views:
    1,736
    Lyle Johnson
    Feb 11, 2004
  3. Replies:
    3
    Views:
    975
    sturlamolden
    Mar 31, 2008
  4. Replies:
    5
    Views:
    434
    James Kanze
    Jun 27, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    682
Loading...

Share This Page