Skip to content

Commit

Permalink
Refactor f2c_type to enable mixing of kind_map and default conversion…
Browse files Browse the repository at this point in the history
… with test
  • Loading branch information
daniel committed Sep 4, 2024
1 parent a26be76 commit 5584972
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ list(APPEND tests
strings
subroutine_contains_issue101
type_bn
kind_map_default
)

foreach(test ${tests})
Expand Down
3 changes: 2 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ EXAMPLES = arrayderivedtypes \
type_check \
derivedtypes_procedure \
optional_string \
long_subroutine_name
long_subroutine_name \
kind_map_default

PYTHON = python

Expand Down
38 changes: 38 additions & 0 deletions examples/kind_map_default/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#=======================================================================
# define the compiler names
#=======================================================================

CC = gcc
F90 = gfortran
PYTHON = python
CFLAGS = -fPIC
F90FLAGS = -fPIC
PY_MOD = pywrapper
F90_SRC = main.f90
OBJ = $(F90_SRC:.f90=.o)
F90WRAP_SRC = $(addprefix f90wrap_,${F90_SRC})
WRAPFLAGS = -v --kind-map kind.map
F2PYFLAGS = --build-dir build
F90WRAP = f90wrap
F2PY = f2py-f90wrap
.PHONY: all clean

all: test

clean:
rm -rf *.mod *.smod *.o f90wrap*.f90 ${PY_MOD}.py _${PY_MOD}*.so __pycache__/ .f2py_f2cmap build ${PY_MOD}/

main.o: ${F90_SRC}
${F90} ${F90FLAGS} -c $< -o $@

%.o: %.f90
${F90} ${F90FLAGS} -c $< -o $@

${F90WRAP_SRC}: ${OBJ}
${F90WRAP} -m ${PY_MOD} ${WRAPFLAGS} ${F90_SRC}

f2py: ${F90WRAP_SRC}
CFLAGS="${CFLAGS}" ${F2PY} -c -m _${PY_MOD} ${F2PYFLAGS} f90wrap_*.f90 *.o

test: f2py
${PYTHON} tests.py
7 changes: 7 additions & 0 deletions examples/kind_map_default/Makefile.meson
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include ../make.meson.inc

NAME := pywrapper
WRAPFLAGS += --kind_map kind.map

test: build
$(PYTHON) tests.py
3 changes: 3 additions & 0 deletions examples/kind_map_default/kind.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{\
'real':{'8':'double'},\
}
30 changes: 30 additions & 0 deletions examples/kind_map_default/main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
program main

end program

module m_test

implicit none
public

contains

function test_real(in_real) result(out_int)
real :: in_real
integer :: out_int
out_int = 1
end function test_real

function test_real4(in_real) result(out_int)
real(kind=4) :: in_real
integer :: out_int
out_int = 2
end function test_real4

function test_real8(in_real) result(out_int)
real(kind=8) :: in_real
integer :: out_int
out_int = 3
end function test_real8

end module m_test
17 changes: 17 additions & 0 deletions examples/kind_map_default/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from pywrapper import m_test

class TestKindMap(unittest.TestCase):

def test_real(self):
_ = m_test.test_real(1.)

def test_real4(self):
_ = m_test.test_real4(2.)

def test_real8(self):
_ = m_test.test_real8(3.)

if __name__ == '__main__':
unittest.main()
31 changes: 15 additions & 16 deletions f90wrap/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,23 +861,22 @@ def f2c_type(typename, kind_map):
type, kind = split_type_kind(typename)
kind = kind.replace('(', '').replace(')', '')


if type in kind_map:
if kind in kind_map[type]:
c_type = kind_map[type][kind]
else:
raise RuntimeError('Unknown combination of type "%s" and kind "%s"' % (type, kind) +
' - add to kind map and try again')
else:
if type in default_f2c_type:
try:
c_type = kind_map[type][kind]
except KeyError:
try:
c_type = default_f2c_type[type]
elif type.startswith('type'):
return 'type'
elif type.startswith('class'):
return 'type'
else:
raise RuntimeError('Unknown type "%s" - ' % type +
'add to kind map and try again')
except KeyError:
if type.startswith('type') or type.startswith('class'):
c_type = 'type'
else:
if type in kind_map:
raise RuntimeError('Unknown combination of type "%s" and kind "%s"' % (type, kind) +
' - add to kind map and try again')
else:
raise RuntimeError('Unknown type "%s" - ' % type +
'add to kind map and try again')

return c_type


Expand Down

0 comments on commit 5584972

Please sign in to comment.