Re: [f2py] Callbacks return 0

Pearu Peterson <[email protected]> Wed, 1 May 2013 08:27:39 +0300
Newsgroups gmane.comp.python.f2py.user
Message-ID <CAPpwKczZ-5SdoR62m=4-2-ai2g+Yh0sqyyYHoPHrwRwOF4cXqA@mail.gmail.com>
Hi,
In Fortran code, you'll need also the following statement:

  REAL*8 FUN

and then the example works.

HTH,
Pearu

On Tue, Apr 30, 2013 at 4:09 PM, Elias Assmann <[email protected]> wr=
ote:
> Hi,
>
> I am able to use f2py in simple cases, but I ran into a problem with
> callbacks.  Using the ``callback.f=92=92 example from the user's guide:
>
> C FILE: CALLBACK.F
>        SUBROUTINE FOO(FUN,R)
>        EXTERNAL FUN
>        INTEGER I
>        REAL*8 R
> Cf2py intent(out) r
>        R =3D 0D0
>        DO I=3D-5,5
>           R =3D R + FUN(I)
>        ENDDO
>        END
> C END OF FILE CALLBACK.F
>
> my Python session looks like this:
>
> Python 2.7.3 (default, Aug  1 2012, 05:14:39)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import callback
>  >>> print callback.foo.__doc__
> foo - Function signature:
>    r =3D foo(fun,[fun_extra_args])
> Required arguments:
>    fun : call-back function
> Optional arguments:
>    fun_extra_args :=3D () input tuple
> Return objects:
>    r : float
> Call-back functions:
>    def fun(i): return r
>    Required arguments:
>      i : input int
>    Return objects:
>      r : float
>
>  >>> def f(i):
> ...     print "f:", i
> ...     return i*i
> ...
>  >>> callback.foo(f)
> f: -5
> f: -4
> f: -3
> f: -2
> f: -1
> f: 0
> f: 1
> f: 2
> f: 3
> f: 4
> f: 5
> 0.0
>
> If I modify ``callback.f=92=92 to print the return value of every call to
> ``f=92=92, that value is always 0.
>
> Any pointers?  My Fortran compiler is gfortran, gcc version 4.6.3
> (Ubuntu/Linaro 4.6.3-1ubuntu5).
>
>         Elias
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users