
Hi all, I hope this is the right mailing list to post my question to. I'm trying to make some easy C code working with Python by using f2py. For example, take the test.c file: ---------------------------------- typedef struct data { int a; double b; } DATA; /*function with struct*/ DATA incr(DATA x) { DATA y; y.a = x.a + 1; y.b = x.b + 1.; return(y); } /*function: return value*/ int incr0(int x) { return(x+1); } /*subroutine: no return value*/ void incr1(int *x) { *x += 1; } ---------------------------------- If I do: $ f2py -c test.c -m prova all seems fine, but inside ipython I get this error: In [1]: import prova --------------------------------------------------------------------------- <type 'exceptions.ImportError'> Traceback (most recent call last) /xlv1/labsoi_devices/bollalo001/work/test/python/<ipython console> in <module>() <type 'exceptions.ImportError'>: dynamic module does not define init function (initprova) I think I'm missing a correct .pyf file to do: $ f2py -c prova2.pyf test.c I tried writing prova2.pyf by my own, because doing: $ f2py test.c -h prova2.pyf -m prova gives me an empty prova2.pyf, but I wasn't able to do it!!! Can anyone of use kindly show me how to write it? Or giving me a good tutorial to read? I found only very few information on www.scipy.org. Thank you in advance, Lorenzo.

On Thu, May 24, 2007 at 03:40:18PM +0200, lorenzo bolla wrote:
I hope this is the right mailing list to post my question to. I'm trying to make some easy C code working with Python by using f2py.
I can't help too much on that as I gave up using f2Py to wrap C a while ago, but I think you are much better off using xtypes to call C functions. It is dead-easy. I don't know about the overhead, if it is an issue in your case. My 2 euro cents, Gaël

On Thu, May 24, 2007 at 03:50:40PM +0200, Gael Varoquaux wrote:
On Thu, May 24, 2007 at 03:40:18PM +0200, lorenzo bolla wrote:
I hope this is the right mailing list to post my question to. I'm trying to make some easy C code working with Python by using f2py.
I can't help too much on that as I gave up using f2Py to wrap C a while ago, but I think you are much better off using xtypes to call C functions. It is dead-easy.
Oops, forgot the manual ! Have a look at http://www.scipy.org/Cookbook/Ctypes to see how to easily call C function with numpy. Gaël

Lorenzo, you can indeed use f2py to write extensions around some C code: http://cens.ioc.ee/projects/f2py2e/usersguide/index.html http://www.scipy.org/Cookbook/f2py_and_NumPy I think you should also be able to find some actual examples in the scipy sources...

I tried to write my own prova2.pyf and this is it: ---------------------------------------------------------------- ! -*- f90 -*- ! Note: the context of this file is case sensitive. python module prova interface function incr(x) real, dimension(2), intent(c) :: incr real, dimension(2), intent(c,in) :: x end function incr function incr0(x) integer intent(c) :: incr0 integer intent(c,in) :: x end function incr0 subroutine incr1(x) intent(c) :: incr1 integer intent(c,in,out) :: x end subroutine incr1 end interface end python module prova ! This file was auto-generated with f2py (version:2_3473). ! See http://cens.ioc.ee/projects/f2py2e/ ---------------------------------------------------------------- Unfortunately, only the function incr0 works. incr1 gives a segmentation fault and incr does not return an array (or better a struct) as I would... any hints? thank you! L. On 5/24/07, Pierre GM <pgmdevlist@gmail.com> wrote:
Lorenzo, you can indeed use f2py to write extensions around some C code:
http://cens.ioc.ee/projects/f2py2e/usersguide/index.html http://www.scipy.org/Cookbook/f2py_and_NumPy
I think you should also be able to find some actual examples in the scipy sources... _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Gael Varoquaux
-
lorenzo bolla
-
Pierre GM