You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
f2py fails for functions returning character(len=len(input_string)) #3425
When wrapping these with f2py, I get an error looking like this:
/tmp/tmpmrZ5Ra/src.linux-x86_64-2.7/RMCfunctionsmodule.c: In function ‘f2py_rout_RMCfunctions_mod_functions_toupper’:
/tmp/tmpmrZ5Ra/src.linux-x86_64-2.7/RMCfunctionsmodule.c:926:23: error: ‘str_Dims’ undeclared (first use in this function)
slen(toupper) =len(str);
^
I guess the len(str) needs to be replaced by slen(str).
This problem occurs with both f2py-2.7 and f2py-3.3
It does not depend on the name of the variable str - I tried a couple of other names.
purefunctiontest(str)
character(len=*), intent(in) :: str
character(len=len(str)) :: test
integer:: i
test(1:1) ='A'do i =2, len(str)
test(i:i) = str(i:i)
enddoendfunction test
I get:
/tmp/tmpdg8zrhbc/src.linux-x86_64-3.9/stringsmodule.c: In function'f2py_rout_strings_strings_test':
/tmp/tmpdg8zrhbc/src.linux-x86_64-3.9/stringsmodule.c:279:20: error: 'str_Dims' undeclared (first use in this function)
279 | slen(test) = len(str);| ^~~
I tried using a subroutine, with the following signature file:
python module strings ! in
interface ! in :strings
module strings ! in :strings:strings.f90
subroutinetest(str, a) ! in :strings:strings.f90:strings
character*(*) intent(in) :: str
character*len(str) intent(out) :: a
endsubroutine test
end module strings
end interface
end python module strings
Running $ f2py -c strings.f90 strings.pyf compiles fine, but
>>>importstrings>>>print(strings.strings.test.__doc__) # should return a as output, but returnstest(str,a)
Wrapperfor``test``.
Parameters----------str : inputstring(len=-1)
a : inputstring(len=1)
(note the incorrect intent for a)
Changing strings.pyf to use character(len=slen(str)), intent(out) :: a runs fine and works.
So the problem seems to be when using a function but not a subroutine.
When inspecting the code, IIUC the problem comes from the fact that we need to use slen for the C function wrapper, but len for the fortran function wrapper (both generated by f2py).
My question to @pearu is if we should require the use of the slen expression in the pyf file in this case, or try to fix this from f2py itself, considering the two separate cases of the fortran and C function wrappers.
My first reaction (without looking at code) is that f2py should be fixed by replacing len(..) call with slen(..) when len is used in the context of character len specification. Note that there are three kinds of length functions involved here: slen and len defined by f2py and to be used withing .pyf files, and len that is Fortran intrinsic. f2py len should be used in the context of arrays while f2py slen in the context of character types.
Activity
chatcannon commentedon Jun 12, 2013
Sorry, I couldn't work out how to tag this with component: numpy.f2py
chatcannon commentedon Jun 12, 2013
P.S. I'm running numpy version 1.7.1 on Gentoo GNU/Linux
charris commentedon Feb 23, 2014
@chatcannon I suspect this problem persists, but could you try with numpy 1.8 or current development?
Goutte commentedon Apr 7, 2021
I can confirm the issue is still here in numpy
1.20.Barring a fix (it's been 8 years), any tip on how to bypass this issue would be welcome!
melissawm commentedon May 13, 2021
Here's what I've managed to get so far:
I get:
Running
$ f2py -c strings.f90 strings.pyfcompiles fine, but(note the incorrect intent for
a)strings.pyfto usecharacter(len=slen(str)), intent(out) :: aruns fine and works.So the problem seems to be when using a function but not a subroutine.
When inspecting the code, IIUC the problem comes from the fact that we need to use
slenfor the C function wrapper, butlenfor the fortran function wrapper (both generated by f2py).My question to @pearu is if we should require the use of the
slenexpression in the pyf file in this case, or try to fix this from f2py itself, considering the two separate cases of the fortran and C function wrappers.pearu commentedon May 31, 2021
My first reaction (without looking at code) is that f2py should be fixed by replacing
len(..)call withslen(..)whenlenis used in the context of characterlenspecification. Note that there are three kinds of length functions involved here:slenandlendefined by f2py and to be used withing .pyf files, andlenthat is Fortran intrinsic. f2pylenshould be used in the context of arrays while f2pyslenin the context of character types.TST: added test for issue numpy#3425
TST: added test for issue numpy#3425
ENH: Support character string arrays
ENH: Support character string arrays
ENH: Support character string arrays