Skip to content

f2py - f90-style character array definition with character(len=n) fails #18684

Closed

Description

@2sn

this is for f2py (version 1.20.2)

when use f90-style

character(len=n), dimension(m) :: variable

definition it fails (creates single byte string of length m (dim=(), dtype='|Sm')) whereas when I use F77-style length specification

character*n, dimension(m) :: variable

it correctly produces an array dim=(m,), dtype = '|Sn'.

I am hoping that crackfortran could be relatively(?) easily adjusted to add a regexp to also accept the f90-style character length specification? For context (see example below), I use a *.f code file (f77 format) for the code layout, but the same Fortran code should be allowable either way, whether using .f or .f90. (I have not tested whether this already works in an .f90 file.)

Or maybe I am just overlooking something?

Reproducing code example:

To give an real-world example, I have the snippets

integer(kind=int32), parameter :: ndtz  = 30
      character(len=8), dimension(ndtz) ::
     &     idtcsym
      common/charsave/
     &     idtcsym

and then after compilation

In [4]: k._kepler.charsave.idtcsym
Out[4]: array(b'dtr     dtt     dtd     dtq   ', dtype='|S30')

whereas

      character*8, dimension(ndtz) ::
     &     idtcsym
      common/charsave/
     &     idtcsym

as, as it should lines in the output of compilation

analyzevars: charselector={'len': '8'} unhandled.analyzevars: charselector={'len': '8'} unhandled.analyzevars: character array "character*8 idtcsym(30)" is considered as "character idtcsym(30,8)"; "intent(c)" is forced.

and resulting in

In [3]: k._kepler.charsave.idtcsym
Out[3]: 
array([b'dtr     ', b'dtt     ', b'dtd     ', b'dtq     ', b'dti     ',
       b'dtl     ', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'',
       b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b'', b''],
      dtype='|S8')

as it should be.

Error message:

N/A

NumPy/Python version information:

In [1]: import sys, numpy; print(numpy.__version__, sys.version)
1.20.2 3.9.2 (default, Feb 20 2021, 13:24:34) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)]

Activity

  1. 2sn commented on Jun 3, 2021

    @2sn
    ContributorAuthor

    I have some f90 codes I wrap with f2py and in these using character(len=n) does not seem to cause issues.

  2. added a commit that references this issue on Jul 2, 2021
  3. self-assigned this
    on Jul 3, 2021
  4. added a commit that references this issue on Apr 4, 2022
  5. added a commit that references this issue on Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions