[Numpy-discussion] f2py callback bug?

Pearu Peterson pearu.peterson@gmail....
Wed Nov 25 13:42:12 CST 2009


Hi James,

To answer the second question, use:

   j = 1+numpy.array([2], numpy.int32)

The answer to the first question is that
the type of 1+numpy.array([2]) is
numpy.int64 but Fortran function expects
an array of type numpy.int32 and hence
the wrapper makes a copy of the input
array (which is also returned by the wrapper)
before passing it to Fortran.

Regards,
Pearu

James McEnerney wrote:
> Pearu,
> Thanks. a follow question.
> Using fortran
> 
>       subroutine calc(j)
> Cf2py intent(callback) pycalc
>       external pycalc
> Cf2py integer dimension(1), intent(in,out):: j
> 
>       integer j(1)
>       print *, 'in fortran before pycalc ', 'j=', j(1)
>       call pycalc(j)
>       print *, 'in fortran after pycalc ', ' j=', j(1)
>       end
> 
> in python
> 
> import foo,numpy
> def pycalc(j):
>    
>     print ' in pycalc ', 'j=', j
>     j[:] = 20*j
>     return
> 
> print foo.calc.__doc__
> j = 1+numpy.array([2])
> print foo.calc(j, pycalc)
> print j
> 
> the output is
> 
> calc - Function signature:
>   j = calc(j,pycalc,[pycalc_extra_args])
> Required arguments:
>   j : input rank-1 array('i') with bounds (1)
>   pycalc : call-back function
> Optional arguments:
>   pycalc_extra_args := () input tuple
> Return objects:
>   j : rank-1 array('i') with bounds (1)
> Call-back functions:
>   def pycalc(j): return j
>   Required arguments:
>     j : input rank-1 array('i') with bounds (1)
>   Return objects:
>     j : rank-1 array('i') with bounds (1)
> 
>  in fortran before pycalc j=           3
>  in pycalc  j= [3]
>  in fortran after pycalc  j=          60
> [60]
> [3]
> 
> Why is the return from foo.calc different from j?
> How do I make them the same?
> "return j" in pycalc doesn't change things.
> 
> Thanks again!
> 
> At 12:06 AM 11/25/2009, you wrote:
> 
> 
>> Pearu Peterson wrote:
>>
>> > Hmm, regarding `intent(in, out) j`, this should work. I'll check what
>> > is going on..
>>
>> The `intent(in, out) j` works when pycalc is defined as subroutine:
>>
>>    call pycalc(i, j)
>>
>> instead of
>>
>>    pyreturn = pycalc(i, j)
>>
>> Pearu
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://*mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 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


More information about the NumPy-Discussion mailing list