[Numpy-discussion] f2py and string arrays

Mark.Miller mpmusu at cc.usu.edu
Mon Aug 13 11:04:32 EDT 2007

Quick question...

I have a Fortran function for f2py declared as follows (just an example).

module test1
     subroutine manip(length, array, a, b)
          integer   :: length, a,b
          character(length) :: array(0:a-1,0:b-1)
!f2py intent(in) length,a,b
!f2py intent(inout) array
          array(0,0) = "1111"
     end subroutine manip
end module test1

It compiles using f2py without issue.  The f2py-generated docstring 
correctly lists the required arguments:

manip - Function signature:
Required arguments:
   length : input int
   array : in/output rank-2 array('S') with bounds (a,b)

However, I'm getting some type conversion errors when using the function 
in python:

 >>> from mymodule import test1
 >>> import numpy
 >>> a=numpy.empty((3,3,'S4',order='F')
 >>> a[:,:]='2222'
 >>> test1.manip(4,a,3,3)
Traceback (most recent call last):
   File "(stdin)", line 1, in (module)
ValueError: failed to initialize intent(inout) array --expected elsize = 
1 but got 4 -- input 'S' not compatible to 'c'

Does f2py not work with numpy string arrays?  I have some excellent 
alternate implementations for this type of thing, but would prefer an 
approach similar to that outlined above.



More information about the NumPy-Discussion mailing list