Skip to content

numpy.f2py produces wrong results for arrays of strings #4519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
melven opened this issue Mar 19, 2014 · 1 comment · Fixed by #19388
Closed

numpy.f2py produces wrong results for arrays of strings #4519

melven opened this issue Mar 19, 2014 · 1 comment · Fixed by #19388
Assignees

Comments

@melven
Copy link

melven commented Mar 19, 2014

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.

@melissawm
Copy link
Member

Hi, sorry to resurrect an old issue but this problem persists.

Using the charArrayTest module above gives the following results in numpy 1.17.3 :

>>> from Fortran import chararraytest
>>> import numpy
>>> chararraytest.chararrayin(numpy.array(['1', '2', '3'], numpy.dtype(numpy.string_)))
 Called "charArrayIn" with args:
           1 123
           2 n"
           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.

pearu added a commit to pearu/numpy that referenced this issue Jul 3, 2021
@pearu pearu self-assigned this Jul 3, 2021
@pearu pearu linked a pull request Jul 3, 2021 that will close this issue
pearu added a commit to pearu/numpy that referenced this issue Sep 1, 2021
pearu added a commit to pearu/numpy that referenced this issue Sep 6, 2021
TST: added test for issue numpy#18684

ENH: f2py opens files with correct encoding, fixes numpy#635

TST: added test for issue numpy#6308

TST: added test for issue numpy#4519

TST: added test for issue numpy#3425

ENH: Implement user-defined hooks support for post-processing f2py data structure. Implement character BC hook.

ENH: Add support for detecting utf-16 and utf-32 encodings.
melissawm pushed a commit to pearu/numpy that referenced this issue Apr 4, 2022
TST: added test for issue numpy#18684

ENH: f2py opens files with correct encoding, fixes numpy#635

TST: added test for issue numpy#6308

TST: added test for issue numpy#4519

TST: added test for issue numpy#3425

ENH: Implement user-defined hooks support for post-processing f2py data structure. Implement character BC hook.

ENH: Add support for detecting utf-16 and utf-32 encodings.
HaoZeke pushed a commit to pearu/numpy that referenced this issue Jun 5, 2022
TST: added test for issue numpy#18684

ENH: f2py opens files with correct encoding, fixes numpy#635

TST: added test for issue numpy#6308

TST: added test for issue numpy#4519

TST: added test for issue numpy#3425

ENH: Implement user-defined hooks support for post-processing f2py data structure. Implement character BC hook.

ENH: Add support for detecting utf-16 and utf-32 encodings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants