Repository navigation
numpy.f2py produces wrong results for arrays of strings #4519
Description
I have tried several variants with arrays of fortran-character strings and none of them did work (fixed/variable string length and array dimension).
I even tried to use iso_c_binding with 2-dimensional character(kind=C_CHAR) array, but this seemed to produces strided strings (row-/column-major reordering of the data).
I could not really find anything helpful related to this problem. In http://cens.ioc.ee/projects/f2py2e/README.html there's a remark from 2004 that this should work.
So here a simple example:
module CharArrayTest
implicit none
contains
subroutine charArrayIn(myStrList)
character(len=*), intent(in) :: myStrList(:)
integer :: i
write(*,*) 'Called "charArrayIn" with args:'
do i = 1, size(myStrList), 1
write(*,*) i, trim(myStrList(i))
end do
end subroutine charArrayIn
end module CharArrayTest
Obviously calling this function from Fortran works fine.
I can compile it using
f2p -m Fortran -c chararraytest.f90
But with my small test program I obtain strange results:
from Fortran import chararraytest
test_data = ( '',
'text',
[('1',), ('2',), ('3',)],
['',''] )
print chararraytest.chararrayin.__doc__
for test_str in test_data:
print 'calling chararrayin with args:', repr(test_str)
chararraytest.chararrayin(test_str)
I get the following output:
chararrayin - Function signature:
chararrayin(mystrlist)
Required arguments:
mystrlist : input rank-1 array('S') with bounds (f2py_mystrlist_d0)
calling chararrayin with args: ''
Called "charArrayIn" with args:
calling chararrayin with args: 'text'
Called "charArrayIn" with args:
1 text
2 �
3 xV#T
4 �
calling chararrayin with args: (('1',), ('2',), ('3',))
Called "charArrayIn" with args:
1 123
2
3
calling chararrayin with args: ('', '')
unexpected array size: size=2, arr_size=0, rank=1, effrank=1, arr.nd=2, dims=[ 2 ], arr.dims=[ 2 0 ]
Traceback (most recent call last):
File "test.py", line 12, in <module>
chararraytest.chararrayin(test_str)
Fortran.error: failed in converting 1st argument `mystrlist' of Fortran.chararraytest.chararrayin to C/Fortran array
When I use fixed character length strings, e.g. character(len=20), the results look similar.
System:
Debian testing, 64 bit
uname -rvmo
3.13-1-amd64 #1 SMP Debian 3.13.5-1 (2014-03-04) x86_64 GNU/Linux
Python 2.7.6
NumPy 1.7.1
dpkg --list python-numpy
[...]
python-numpy 1.7.1-5 amd64
I also tried an installation of numpy 1.8 on another 64 bit Linux machine.
Activity
melissawm commentedon Nov 7, 2019
Hi, sorry to resurrect an old issue but this problem persists.
Using the
charArrayTestmodule above gives the following results in numpy 1.17.3 :I'm not sure if there's a recommended way to treat arrays of string in f2py.
Sorry I can't help more as I'm not sure what's happening under the hood.
TST: added test for issue numpy#4519
TST: added test for issue numpy#4519
ENH: Support character string arrays
ENH: Support character string arrays
ENH: Support character string arrays