Tôi muốn sử dụng f2pyvới Fortran hiện đại. Cụ thể, tôi đang cố gắng lấy ví dụ cơ bản sau để làm việc. Đây là ví dụ hữu ích nhỏ nhất tôi có thể tạo ra.
! alloc_test.f90
subroutine f(x, z)
  implicit none
! Argument Declarations !
  real*8, intent(in) ::  x(:)
  real*8, intent(out) :: z(:)
! Variable Declarations !
  real*8, allocatable :: y(:)
  integer :: n
! Variable Initializations !
  n = size(x)
  allocate(y(n))
! Statements !
  y(:) = 1.0
  z = x + y
  deallocate(y)
  return
end subroutine f
Lưu ý rằng nđược suy ra từ hình dạng của tham số đầu vào x. Lưu ý rằng yđược phân bổ và giải quyết trong cơ thể của chương trình con.
Khi tôi biên dịch cái này với f2py
f2py -c alloc_test.f90 -m allocVà sau đó chạy trong Python
from alloc import f
from numpy import ones
x = ones(5)
print f(x)
Tôi nhận được lỗi sau đây
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1,)Vì vậy, tôi đi và tạo và chỉnh sửa các pyftập tin bằng tay
f2py -h alloc_test.pyf -m alloc alloc_test.f90Nguyên
python module alloc ! in 
    interface  ! in :alloc
        subroutine f(x,z) ! in :alloc:alloc_test.f90
            real*8 dimension(:),intent(in) :: x
            real*8 dimension(:),intent(out) :: z
        end subroutine f
    end interface 
end python module allocSửa đổi
python module alloc ! in 
    interface  ! in :alloc
        subroutine f(x,z,n) ! in :alloc:alloc_test.f90
            integer, intent(in) :: n
            real*8 dimension(n),intent(in) :: x
            real*8 dimension(n),intent(out) :: z
        end subroutine f
    end interface 
end python module allocBây giờ nó chạy nhưng các giá trị của đầu ra zluôn luôn 0. Một số in gỡ lỗi cho thấy ncó giá trị 0trong chương trình con f. Tôi cho rằng tôi đang thiếu một số f2pyma thuật tiêu đề để quản lý tình huống này đúng cách.  
Nói chung, cách tốt nhất để liên kết chương trình con trên với Python là gì? Tôi thực sự không muốn phải sửa đổi chương trình con.