Calling Fortran from Python

Mangabasi mangabasi at gmail.com
Thu Apr 5 10:33:12 EDT 2007


On Apr 4, 10:10 pm, Lenard Lindstrom <l... at telus.net> wrote:
> Mangabasi wrote:
> > On Apr 4, 5:48 pm, Robert Kern <robert.k... at gmail.com> wrote:
> >> Mangabasi wrote:
> >>> Would Python 2.5 work with Visual Studio 6.6?
> >> No.
>
> >> --
> >> Robert Kern
>
> >> "I have come to believe that the whole world is an enigma, a harmless enigma
> >>  that is made terrible by our own mad attempt to interpret it as though it had
> >>  an underlying truth."
> >>   -- Umberto Eco
>
> > I will try the GCC then.  It is a shame that I could not get calldll
> > to work.  It was very simple to use.  I think I am making a mistake
> > with the argument types but not sure.
>
> > Thanks for your help, it is greatly appreciated.
>
> Did you try ctypes?
>
>  >>> from ctypes import *
>  >>> sample=cdll.sample.sample_
>  >>> sample.restype=None
>  >>> sample.argtypes=[POINTER(c_int), POINTER(c_int), POINTER(c_double),
> POINTER(c_double)]
>  >>> e1 = c_int(0)
>  >>> e2 = c_int(0)
>  >>> ain = (c_double*3)(2.0, 3.0, 4.0)
>  >>> aout = (c_double*4)()
>  >>> sample(e1, e2, ain, aout)
>  >>> aout[:]
> [6.0, 9.0, 8.0, 12.0]
>  >>> e1.value
> 0
>  >>> e2.value
> 0
>
> I compile the SAMPLE example with mingw g77 3.4.5:
>
> g77 -shared -o sample.dll sample.for
>
> I had to take out the "INTENT(OUT)"s because g77 didn't like them. And
> "SAMPLE" became "sample_" in the dll. Also note that argument passing to
> Fortran subroutines is strictly pass-by-reference. Thus the ain pointer.
>
> Lenard Lindstrom- Hide quoted text -
>
> - Show quoted text -

A little bit of googling solved the problem. instead of

>>> sample = cdll.sample_dll.SAMPLE

I used


>>> sample = windll.sample_dll.SAMPLE

and now it seems to be working without error messages.

Thanks a lot.





More information about the Python-list mailing list