[Numpy-discussion] f2py : callbacks without callback function as an argument

Pearu Peterson pearu@cens.ioc...
Wed Mar 12 05:37:11 CDT 2008


On Wed, March 12, 2008 8:38 am, Daniel Creveling wrote:
> Hello-
>
> Is there a way to code a callback to python from
> fortran in a way such that the calling routine does
> not need the callback function as an input argument?
> I'm using the Intel fortran compiler for linux with
> numpy 1.0.4 and f2py gives version 2_4422.  My modules
> crash on loading because the external callback
> function is not set.  I noticed in the release notes
> for f2py 2.46.243 that it was a resolved issue, but I
> don't know how that development compares to version
> 2_4422 that comes with numpy.

The development version of f2py in numpy has a fix for
callback support that was broken for few versions
of numpy. So, use either numpy from svn or wait a bit
for 1.0.5 release.

> The example that I was trying to follow is from some
> documentatdevelopmention off of the web:
>
>       subroutine f1()
>         print *, "in f1, calling f2 twice.."
>         call f2()
>         call f2()
>         return
>       end
>
>       subroutine f2()
> cf2py intent(callback, hide) fpy
>         external fpy
>         print *, "in f2, calling fpy.."
>         call fpy()
>         return
>       end
>
> f2py -c -m pfromf extcallback.f
>
> I'm supposed to be able to define the callback
> function from Python like:
>>>> import pfromf
>>>> def f(): print "This is Python"
>>>> pfromf.fpy = f
>
> but I am unable to even load the module:
>>>> import pfromf
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: ./pfromf.so: undefined symbol: fpy_

Yes, loading the module works with f2py from numpy svn.
However, calling f1 or f2 from Python fail because
the example does not leave a way to specify the fpy function.

Depending on your specific application, there are some ways
to fix it. For example, let fpy function propagete from f1 to
f2 using external argument to f1:

      subroutine f1(fpy)
      external fpy
      call f2(fpy)
      call f2(fpy)
      end

      subroutine f2(fpy)
      external fpy
      call fpy()
      end

If this is something not suitable for your case, then there
exist ways to influence the generated wrapper codes from
signature files using special hacks. I can explain them later
when I get a better idea what you are trying to do.

HTH,
Pearu



More information about the Numpy-discussion mailing list