Discussion:
[f2py] Callbacks return 0
Elias Assmann
2013-04-30 13:09:07 UTC
Permalink
Hi,

I am able to use f2py in simple cases, but I ran into a problem with
callbacks. Using the ``callback.f’’ 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 = 0D0
DO I=-5,5
R = 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 = foo(fun,[fun_extra_args])
Required arguments:
fun : call-back function
Optional arguments:
fun_extra_args := () input tuple
Return objects:
r : float
Call-back functions:
def fun(i): return r
Required arguments:
i : input int
Return objects:
r : float
... 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’’ to print the return value of every call to
``f’’, that value is always 0.

Any pointers? My Fortran compiler is gfortran, gcc version 4.6.3
(Ubuntu/Linaro 4.6.3-1ubuntu5).

Elias
Pearu Peterson
2013-05-01 05:27:39 UTC
Permalink
Hi,
In Fortran code, you'll need also the following statement:

REAL*8 FUN

and then the example works.

HTH,
Pearu
Post by Elias Assmann
Hi,
I am able to use f2py in simple cases, but I ran into a problem with
C FILE: CALLBACK.F
SUBROUTINE FOO(FUN,R)
EXTERNAL FUN
INTEGER I
REAL*8 R
Cf2py intent(out) r
R = 0D0
DO I=-5,5
R = R + FUN(I)
ENDDO
END
C END OF FILE CALLBACK.F
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__
r = foo(fun,[fun_extra_args])
fun : call-back function
fun_extra_args := () input tuple
r : float
def fun(i): return r
i : input int
r : float
... 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’’ to print the return value of every call to
``f’’, 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
http://cens.ioc.ee/mailman/listinfo/f2py-users
Elias Assmann
2013-05-03 08:05:04 UTC
Permalink
Post by Pearu Peterson
REAL*8 FUN
and then the example works.
Thank you, that does work. Now, I can see that my question looks pretty
stupid. But the example was taken literally from
<http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#call-back-arguments>,
so I did not doubt the code. Is this an outdated version of the user's
guide? Or is the problem maybe compiler dependent?

Thanks anyhow,

Elias
Pearu Peterson
2013-05-03 08:09:19 UTC
Permalink
Yes, I think it is compiler dependent. Default compiler used to be g77
but now it is gfortran. Different compilers can have different
defaults to interpreting incomplete codes, this could explain what you
have observed.

Pearu
Post by Elias Assmann
Post by Pearu Peterson
REAL*8 FUN
and then the example works.
Thank you, that does work. Now, I can see that my question looks pretty
stupid. But the example was taken literally from
<http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#call-back-arguments>,
so I did not doubt the code. Is this an outdated version of the user's
guide? Or is the problem maybe compiler dependent?
Thanks anyhow,
Elias
Continue reading on narkive:
Loading...