Skip to content

Pywrap basic functionality

Harris Snyder requested to merge hsnyder/lfortran:pywrap into master

Pywrap is very close to usable. It generates .h, .pyx, and .pxd files.

Short term things to fix:

  • Function return values are not implemented.
  • Currently, intent(out) arguments still need to be passed to the cython wrapper, even though they get overwritten and returned. I'm unsure whether we want to leave them this way or whether we want to allocate them in the wrapper.

Longer term, things we need to add support for:

  • derived type dummy arguments
  • assumed shape array dummy arguments
  • array dummy arguments with lower bounds != 1

(there are probably a few other limitations as well)

Here is an example of it working in it's current state:

The fortran file:

module pywraptest
    implicit none
    integer, parameter :: c_int = 4
    integer, parameter :: c_double = 8
contains
    subroutine do_something(m, n, R, o1, o2, o3) bind(c, name='ftn_sub1')
        integer(c_int), intent(in), value :: m
        integer(c_int), intent(in) :: n
        real(c_double), intent(in) :: R(m,n)
        integer(c_int), intent(out) :: o1, o3(m,1)
        integer(c_int), intent(inout) :: o2
        o1 = sum(R)
        o2 = o1
        o3(:,1) = R(:,1)
    end subroutine
end module

The generated .h file:

// This file was automatically generated by the LFortran compiler.
// Editing by hand is discouraged.

#include <stdint.h>

void ftn_sub1 (int32_t m, const int32_t * n, const double * R, int32_t * o1, int32_t * o2, int32_t * o3);

The generated .pxd file:

# This file was automatically generated by the LFortran compiler.
# Editing by hand is discouraged.

from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
cdef extern from "pywraptestmodule.h":
    void ftn_sub1 (int32_t m, const int32_t * n, const double * R, int32_t * o1, int32_t * o2, int32_t * o3)

The generated .pyx file:

# This file was automatically generated by the LFortran compiler.
# Editing by hand is discouraged.

from numpy cimport import_array, ndarray, int8_t, int16_t, int32_t, int64_t
from numpy import empty, int8, int16, int32, int64
cimport pywraptestmodule_pxd 

def ftn_sub1 (ndarray[double, ndim=2, mode="fortran"] R, int32_t o1, int32_t o2, ndarray[int32_t, ndim=2, mode="fortran"] o3):
    cdef int32_t m = R.shape[0]
    assert(o3.shape[0] == R.shape[0])
    cdef int32_t n = R.shape[1]
    pywraptestmodule_pxd.ftn_sub1 (m, &n, &R[0,0], &o1, &o2, &o3[0,0])
    return o1, o2, o3

Merge request reports