[Numpy-discussion] A basic question about swig and Numeric

Lisandro Dalcin dalcinl at gmail.com
Wed Sep 24 17:48:44 EDT 2008


I believe you should look at the SWIG docs and then write a typecheck
typemap. Checking foir the type of and array and returning NULL is not
fair play for SWIG, nor for Python. Before returning NULL, and
exception should be set. For this, SWIG provides some 'SWIG_xxx_fail'
macros. Typemaps and fragments are powerfull features, but it takes
some time to learn the right way to write them.

On Wed, Sep 24, 2008 at 8:59 AM, Michel Dupront
<michel.dupront at hotmail.fr> wrote:
>
>
>
>
> Hello,
>
> I am trying to use Numeric and swig but it seems that
> there are few points that I don't understand.
> The only excuse I have is that I am new to these tools.
> I have a simple example that I cannot make work the
> way I would like.
> I have a c++ function that take as argument a std::vector.
> >From python I want to call the c++ function with an array
> object. For that purpose I want to write a typemap.
>
> Here is what was supposed to be a very simple example.
>
> example.h:
> #include
> #include
>
> void globale_print(std::vector vint)
> {
>  std::cout<<"globale print vecteur: ";
>  for (size_t i=0; i
> {
>  PyArrayObject* aa=(PyArrayObject *) $input;
>  $1.resize(aa->dimensions[0]);
>  for (unsigned int i=0; idimensions[0]; i++)
>  {
>    $1[i]= *(int *) (aa->data + i* aa->strides[0] );
>  }
> };
>
> %include "example.h"
>
>
> runme.py:
> import example
> import Numeric
>
> array=Numeric.array(range(5))
> print array
> print type(array)
>
> example.globale_print(array)
>
>
>
> Like it is, it works. But there are a lot of things that I don't
> understand.
> For example if I want to check aa, like:
> """
>  if (!PyArray_Check(aa)) {
>    return NULL;
>  }
> """
>
> the compilation of the wrapper is ok but when I run the python
> script runme.py, I have a segmentation fault because of the first line.
>
> Here I really need help.
> Please keep in mind that it is my first try to make a typemap ....
>
> Thanks a lot
> Michel
> _________________________________________________________________
> Installez gratuitement les 20 émôticones Windows Live Messenger les plus fous ! Cliquez ici !
> http://www.emoticones-messenger.fr/
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594



More information about the NumPy-Discussion mailing list