Protected allocatable variables in f2py

Is it possible to use protected allocatable module variables with f2py? Consider this example

module mymod
  implicit none
  integer, allocatable, protected :: arr(:)
end module

You can try to compile with f2py -c test.f90 -m test, and get the error

/var/folders/rh/h610cb491m93m8kyz5br7gwc0000gq/T/tmpf_lm4z5p/src.macosx-10.9-x86_64-3.8/test-f2pywrappers2.f90:21:23:

   21 |             deallocate(d)
      |                       1
Error: Variable 'arr' is PROTECTED and cannot appear in a variable definition context (DEALLOCATE object) at (1)
/var/folders/rh/h610cb491m93m8kyz5br7gwc0000gq/T/tmpf_lm4z5p/src.macosx-10.9-x86_64-3.8/test-f2pywrappers2.f90:25:16:

   25 |        allocate(d(s(1)))
      |                1
Error: Variable 'arr' is PROTECTED and cannot appear in a variable definition context (ALLOCATE object) at (1)

The problem is that the wrapper file test-f2pywrappers2.f90 tries to allocate and deallocate arr, but its protected, so this isn’t allowed.

I’m just wondering if there is a workaround?

Thanks

C857 A nonpointer object that has the PROTECTED attribute and is accessed by use association shall not appear in a variable definition context (19.6.7) or as a data-target or initial-data-target.

You will have to write module procedures that allocate and deallocate the protected variable.

1 Like