Skip to content

BUG: f2py generates incorrect wrapper files when passed -m and a .pyf #25182

Closed
@HaoZeke

Description

@HaoZeke

Note that when -c is passed, the relevant bug is that it will not compile a working module and is reported at #22819.

Consider:

C          fib.f
      SUBROUTINE FIB(A,N)
C
C     CALCULATE FIRST N FIBONACCI NUMBERS
C
      INTEGER N
      REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
      DO I=1,N
         IF (I.EQ.1) THEN
            A(I) = 0.0D0
         ELSEIF (I.EQ.2) THEN
            A(I) = 1.0D0
         ELSE 
            A(I) = A(I-1) + A(I-2)
         ENDIF
      ENDDO
      END

Which can be used with f2py very easily:

❯ f2py -h simple.pyf -m simple fib.f
❯ ls
fib.f  simple.pyf

So far so good, and now the user can modify the .pyf.

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module simple ! in 
    interface  ! in :simple
        subroutine fib(a,n) ! in :simple:fib.f
            real*8 dimension(n),intent(out),depend(n) :: a
            integer intent(in) :: n
        end subroutine fib
    end interface 
end python module simple

! This file was auto-generated with f2py (version:1.26.0).
! See:
! https://web.archive.org/web/20140822061353/http://cens.ioc.ee/projects/f2py2e

Consider what happens next though:

❯ f2py -m blah fib.f simple.pyf
Reading fortran codes...
	Reading file 'fib.f' (format:fix,strict)
	Reading file 'simple.pyf' (format:free)
Post-processing...
	Block: blah
			Block: FIB
			Block: simple
					Block: fib
Applying post-processing hooks...
  character_backward_compatibility_hook
Post-processing (stage 2)...
Building modules...
    Building module "simple"...
    Generating possibly empty wrappers"
    Maybe empty "simple-f2pywrappers.f"
        Constructing wrapper function "fib"...
          a = fib(n)
    Wrote C/API module "simple" to file "./simplemodule.c"
    Building module "blah"...
    Generating possibly empty wrappers"
    Maybe empty "blah-f2pywrappers.f"
        Constructing wrapper function "FIB"...
          FIB(A,[N])
    Wrote C/API module "blah" to file "./blahmodule.c"

... which is incredibly confusing, and downright wrong, if the user modified the .pyf.

What should happen is that -m blah should be ignored in favor of the .pyf file which already has the module name and other information.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions