[Numpy-discussion] f2py and callbacks with variables

Casey Deen deen at mpia.de
Wed Aug 12 16:46:42 EDT 2015


Hi Pearu-

   Thanks so much!  This works!  Can you point me to a reference for the
format of the .pyf files?  My ~day of searching found a few pages on the
scipy website, but nothing which went into this amount of detail.

I also asked Stackoverflow, and unless you object, I'd like to add your
explanation and mark it as SOLVED for future poor souls wrestling with
this problem.  I'll also update the github repository with before and
after versions of the .pyf file.

Cheers,
Casey

On 08/12/2015 09:34 PM, Pearu Peterson wrote:
> Hi Casey,
> 
> What you observe, is not a f2py bug. When f2py sees a code like
> 
> subroutine foo
>   call bar
> end subroutine foo
> 
> then it will not make an attempt to analyze bar because of implicit
> assumption that all statements that has no references to foo arguments
> are irrelevant for wrapper function generation.
> For your example, f2py needs some help. Try the following signature in
> .pyf file:
> 
>         subroutine barney ! in :flintstone:nocallback.f
>             use test__user__routines, fred=>fred, bambam=>bambam
>             intent(callback, hide) fred
>             external fred
>             intent(callback,hide) bambam
>             external bambam
>         end subroutine barney
> 
> Btw, instead of
> 
>   f2py -c -m flintstone flintstone.pyf callback.f nocallback.f
> 
> use
> 
>   f2py -c flintstone.pyf callback.f nocallback.f
> 
> because module name comes from the .pyf file.
> 
> HTH,
> Pearu
> 
> On Wed, Aug 12, 2015 at 7:12 PM, Casey Deen <deen at mpia.de
> <mailto:deen at mpia.de>> wrote:
> 
>     Hi all-
> 
>        I've run into what I think might be a bug in f2py and callbacks to
>     python.  Or, maybe I'm not using things correctly.  I have created a
>     very minimal example which illustrates my problem at:
> 
>     https://github.com/soylentdeen/fluffy-kumquat
> 
>     The issue seems to affect call backs with variables, but only when they
>     are called indirectly (i.e. from other fortran routines).  For example,
>     if I have a python function
> 
>     def show_number(n):
>         print("%d" % n)
> 
>     and I setup a callback in a fortran routine:
> 
>           subroutine cb
>     cf2py intent(callback, hide) blah
>           external blah
>           call blah(5)
>           end
> 
>     and connect it to the python routine
>     fortranObject.blah = show_number
> 
>     I can successfully call the cb routine from python:
> 
>     >fortranObject.cb
>     5
> 
>     However, if I call the cb routine from within another fortran routine,
>     it seems to lose its marbles
> 
>           subroutine no_cb
>           call cb
>           end
> 
>     capi_return is NULL
>     Call-back cb_blah_in_cb__user__routines failed.
> 
>     For more information, please have a look at the github repository.  I've
>     reproduced the behavior on both linux and mac.  I'm not sure if this is
>     an error in the way I'm using the code, or if it is an actual bug.  Any
>     and all help would be very much appreciated.
> 
>     Cheers,
>     Casey
> 
> 
>     --
>     Dr. Casey Deen
>     Post-doctoral Researcher
>     deen at mpia.de <mailto:deen at mpia.de>                     
>      +49-6221-528-375 <tel:%2B49-6221-528-375>
>     Max Planck Institut für Astronomie (MPIA)
>     Königstuhl 17  D-69117 Heidelberg, Germany
>     _______________________________________________
>     NumPy-Discussion mailing list
>     NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
>     http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 

-- 
Dr. Casey Deen
Post-doctoral Researcher
deen at mpia.de                       +49-6221-528-375
Max Planck Institut für Astronomie (MPIA)
Königstuhl 17  D-69117 Heidelberg, Germany



More information about the NumPy-Discussion mailing list