Skip to content

BUG: f2py does not correctly handle double complex type when using iso_fortran_env kinds #30352

@tgastine

Description

@tgastine

Describe the issue:

When declaring a double complex variable in a Fortran program using either iso_c_binding or iso_fortran_env to handle its kind, i.e. complex(kind=dp), f2py maps it to a single precision complex C
variable typedef struct {float r,i;} complex_float; instead of the expected double precision. This yields a segfault of the program. This is working fine when the variable is declared in the old-fashioned format complex(kind=8) .
Here is a quick reproducer:

module test

   use iso_fortran_env, only: dp=>real64

   implicit none

contains

   subroutine test_function(arr, nt)

      integer :: nt
      ! complex(kind=8), intent(inout) :: arr(nt) ! This is OK but not standard
      complex(kind=dp), intent(inout) :: arr(nt) ! This is segfaulting
                                                 ! C file declares it as a 
                                                 ! typedef struct {float r,i;} complex_float;

      arr(:) = (1.0_dp,-1.0_dp)

   end subroutine test_function

end module test

and the associated python function

from test import *
import numpy as np

x = np.zeros((10), np.complex128)
test.test_function(x)
print(x)

Reproduce the code example:

from test import *
import numpy as np

x = np.zeros((10), np.complex128)
test.test_function(x)
print(x)

Error message:

Python and NumPy Versions:

2.2.2
3.13.5 (main, Aug 19 2025, 14:39:27) [GCC]

Runtime Environment:

No response

Context for the issue:

No response

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