[SciPy-user] f2py, fortran 95, and present()

Gabriel Gellner ggellner at uoguelph.ca
Wed Apr 11 16:13:32 EDT 2007


Not sure if f2py allows this,
but I have some code that uses optional arguments in fortran 95. I
want these to be optional in the python interface as well, but it
seems that the intrinsic 'present()' does not work correctly (that is
if you have an optional F95 argument, it is made equal to 0, and is
always present from the python interface) with the wrapped interface,
that is, the optional variable is set.

I imagine this is an issue with the C interface, that doesn't support
optional arguments in the way that fortran does. But I am not great in
either C, or the C extension interface. So I was hoping someone could
tell me definitively

Here is a simple example (which does nothing useful) to make what I am saying clear:

fadd.f95
subroutine fadd(x, a, b)
    implicit none

    real(8), intent(out) :: x
    real(8), intent(in) :: a
    real(8), intent(in), optional :: b

    if (present(b)) then
        x = a + b
    else
        x = a + 1.0d0
    endif
end subroutine fadd

We compile this with $ f2py -c -m fadd fadd.f95
but when I import and use the module in python I get
>>> fadd.fadd(2)
2.0

Instead of 3.0. Is this just something that can't be done with f2py?

Gabriel



More information about the SciPy-User mailing list