Discussion:
[Numpy-discussion] f2py and callbacks with variables
Casey Deen
2015-08-12 16:12:28 UTC
Permalink
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
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
***@mpia.de +49-6221-528-375
Max Planck Institut für Astronomie (MPIA)
Königstuhl 17 D-69117 Heidelberg, Germany
Pearu Peterson
2015-08-12 19:34:08 UTC
Permalink
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
Post by Casey Deen
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
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
print("%d" % n)
subroutine cb
cf2py intent(callback, hide) blah
external blah
call blah(5)
end
and connect it to the python routine
fortranObject.blah = show_number
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
Max Planck Institut fÃŒr Astronomie (MPIA)
Königstuhl 17 D-69117 Heidelberg, Germany
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Casey Deen
2015-08-12 20:46:42 UTC
Permalink
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
Post by Pearu Peterson
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
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
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
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
print("%d" % n)
subroutine cb
cf2py intent(callback, hide) blah
external blah
call blah(5)
end
and connect it to the python routine
fortranObject.blah = show_number
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
+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
http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
--
Dr. Casey Deen
Post-doctoral Researcher
***@mpia.de +49-6221-528-375
Max Planck Institut für Astronomie (MPIA)
Königstuhl 17 D-69117 Heidelberg, Germany
Pearu Peterson
2015-08-13 19:50:55 UTC
Permalink
Hi Casey,
Post by Casey Deen
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.
Try this:

https://sysbio.ioc.ee/projects/f2py2e/usersguide/index.html#signature-file
Post by Casey Deen
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.
Go ahead with stackoverflow.

Best regards,
Pearu

Cheers,
Post by Casey Deen
Casey
Post by Pearu Peterson
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
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
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
https://github.com/soylentdeen/fluffy-kumquat
The issue seems to affect call backs with variables, but only when
they
Post by Pearu Peterson
are called indirectly (i.e. from other fortran routines). For
example,
Post by Pearu Peterson
if I have a python function
print("%d" % n)
subroutine cb
cf2py intent(callback, hide) blah
external blah
call blah(5)
end
and connect it to the python routine
fortranObject.blah = show_number
fortranObject.cb
5
However, if I call the cb routine from within another fortran
routine,
Post by Pearu Peterson
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
Post by Pearu Peterson
reproduced the behavior on both linux and mac. I'm not sure if this
is
Post by Pearu Peterson
an error in the way I'm using the code, or if it is an actual bug.
Any
Post by Pearu Peterson
and all help would be very much appreciated.
Cheers,
Casey
--
Dr. Casey Deen
Post-doctoral Researcher
+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
http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
--
Dr. Casey Deen
Post-doctoral Researcher
Max Planck Institut fÃŒr Astronomie (MPIA)
Königstuhl 17 D-69117 Heidelberg, Germany
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...