From 5584972e8ec6db690d8c268ece8fa71321755a98 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 29 Aug 2024 14:46:37 +0200 Subject: [PATCH] Refactor f2c_type to enable mixing of kind_map and default conversion with test --- examples/CMakeLists.txt | 1 + examples/Makefile | 3 +- examples/kind_map_default/Makefile | 38 ++++++++++++++++++++++++ examples/kind_map_default/Makefile.meson | 7 +++++ examples/kind_map_default/kind.map | 3 ++ examples/kind_map_default/main.f90 | 30 +++++++++++++++++++ examples/kind_map_default/tests.py | 17 +++++++++++ f90wrap/fortran.py | 31 ++++++++++--------- 8 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 examples/kind_map_default/Makefile create mode 100644 examples/kind_map_default/Makefile.meson create mode 100644 examples/kind_map_default/kind.map create mode 100644 examples/kind_map_default/main.f90 create mode 100644 examples/kind_map_default/tests.py diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f7657c8..c27c315 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -25,6 +25,7 @@ list(APPEND tests strings subroutine_contains_issue101 type_bn + kind_map_default ) foreach(test ${tests}) diff --git a/examples/Makefile b/examples/Makefile index 844745f..a3cac48 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -24,7 +24,8 @@ EXAMPLES = arrayderivedtypes \ type_check \ derivedtypes_procedure \ optional_string \ - long_subroutine_name + long_subroutine_name \ + kind_map_default PYTHON = python diff --git a/examples/kind_map_default/Makefile b/examples/kind_map_default/Makefile new file mode 100644 index 0000000..89afd4f --- /dev/null +++ b/examples/kind_map_default/Makefile @@ -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 diff --git a/examples/kind_map_default/Makefile.meson b/examples/kind_map_default/Makefile.meson new file mode 100644 index 0000000..f9d7dbb --- /dev/null +++ b/examples/kind_map_default/Makefile.meson @@ -0,0 +1,7 @@ +include ../make.meson.inc + +NAME := pywrapper +WRAPFLAGS += --kind_map kind.map + +test: build + $(PYTHON) tests.py diff --git a/examples/kind_map_default/kind.map b/examples/kind_map_default/kind.map new file mode 100644 index 0000000..a737ae0 --- /dev/null +++ b/examples/kind_map_default/kind.map @@ -0,0 +1,3 @@ +{\ +'real':{'8':'double'},\ +} diff --git a/examples/kind_map_default/main.f90 b/examples/kind_map_default/main.f90 new file mode 100644 index 0000000..d00557d --- /dev/null +++ b/examples/kind_map_default/main.f90 @@ -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 diff --git a/examples/kind_map_default/tests.py b/examples/kind_map_default/tests.py new file mode 100644 index 0000000..bc504a8 --- /dev/null +++ b/examples/kind_map_default/tests.py @@ -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() diff --git a/f90wrap/fortran.py b/f90wrap/fortran.py index 2449e22..ca0c984 100644 --- a/f90wrap/fortran.py +++ b/f90wrap/fortran.py @@ -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