ฉันต้องการใช้f2pyกับ Fortran ที่ทันสมัย โดยเฉพาะอย่างยิ่งฉันพยายามทำให้ตัวอย่างพื้นฐานต่อไปนี้ทำงาน นี่คือตัวอย่างที่มีประโยชน์น้อยที่สุดที่ฉันสามารถสร้างได้
! 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
ทราบว่ามีการสรุปจากรูปร่างของพารามิเตอร์การป้อนข้อมูลn xโปรดทราบว่าyมีการจัดสรรและยกเลิกการจัดสรรภายในเนื้อหาของรูทีนย่อย
เมื่อฉันรวบรวมสิ่งนี้ด้วย f2py
f2py -c alloc_test.f90 -m alloc
และเรียกใช้ใน Python
from alloc import f
from numpy import ones
x = ones(5)
print f(x)
ฉันได้รับข้อผิดพลาดดังต่อไปนี้
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1,)
ดังนั้นฉันไปและสร้างและแก้ไขpyfไฟล์ด้วยตนเอง
f2py -h alloc_test.pyf -m alloc alloc_test.f90
เป็นต้นฉบับ
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 alloc
ดัดแปลง
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 alloc
ตอนนี้ก็ทำงาน แต่ค่าของการส่งออกอยู่เสมอz 0บางพิมพ์แก้ปัญหาเผยให้เห็นว่าnมีค่าภายในย่อย0 fฉันคิดว่าฉันขาดf2pyเวทมนต์ส่วนหัวเพื่อจัดการสถานการณ์นี้อย่างเหมาะสม  
โดยทั่วไปแล้ววิธีที่ดีที่สุดในการเชื่อมโยงรูทีนย่อยด้านบนเข้ากับ Python คืออะไร? ฉันต้องการอย่างยิ่งที่จะไม่ต้องแก้ไขรูทีนย่อยเอง