While using the call-back feature of f2py I stumbled across what appears
to be a bug and I'm asking the community to look into this.

Background: I'm in the middle of converting some legacy fortran to python.
There is one routine that is particulary thorny that calls more easily
convertible service routines and my intention is to convert the latter
and use the callback feature of f2py to execute them within the fortran
followed by a systematic conversion of what remains.  This seems to be
doable from what I've read on callback. I have not seen an example
of using callback where the python actually changes parameters that are
returned to the fortran; this is a requirement for me. While setting up
an example to illustrate this I came across a syntactically correct
situation(this means it compiles & executes) but gives the wrong answer.
Here's the code:
In fortran, source foo.f
      subroutine calc(i, j)
Cf2py intent(callback) pycalc
      external pycalc
Cf2py integer intent(in,out,copy):: i
Cf2py integer dimension(1), intent(in,out):: j
      integer pyreturn

      integer i, j(1)
      print *, 'in fortran before pycalc ','i=',i, ' j=', j(1)
      pyreturn = pycalc(i, j)
      print *, 'in fortran after pycalc ','i=', i, ' j=', j(1)

      end
     
Standard build: f2py -c -m foo foo.f

In python, execute
import foo,numpy

def pycalc(i, j):
    print ' in pycalc ', 'i=',i, 'j=', j
    i=10*i
    j = 20*j
    return i, j

print foo.calc.__doc__
i=2
j = 1+numpy.array([i])
print foo.calc(i,j, pycalc)

Here's the output:
calc - Function signature:
  i,j = calc(i,j,pycalc,[pycalc_extra_args])
Required arguments:
  i : input int
  j : input rank-1 array('i') with bounds (1)
  pycalc : call-back function
Optional arguments:
  pycalc_extra_args := () input tuple
Return objects:
  i : int
  j : rank-1 array('i') with bounds (1)
Call-back functions:
  def pycalc(i,j): return pyreturn,i,j
  Required arguments:
    i : input int
    j : input rank-1 array('i') with bounds (1)
  Return objects:
    pyreturn : int
    i : int
    j : rank-1 array('i') with bounds (1)


 in fortran before pycalc i= 2 j= 3
 in pycalc  i= 2 j= [3]
 in fortran after pycalc i= 60 j= 3
(60, array([3]))

The bug:
on return to the fortran why is i=60 & j=3?
shouldn't it be i=10 & j=60

While that's what I expect, I might not be defining the
interface properly; but this compiles & executes. If this
is incorrect, what is?  In the fortran, pyreturn appears
to be an address; how do I get the retuned values?

I'm running 
Redhat Linux
python version 2.5
f2py version 2_3979
numpy version 1.0.3.1
Thanks

Jim McEnerney
Lawrence Livermore National Laboratory
7000 East Ave.
Livermore, Ca. 94550-9234

USA

925-422-1963

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to