Skip to content

Commit

Permalink
Merge pull request #218 from inoelloc/fix-long-subroutine-name
Browse files Browse the repository at this point in the history
FIX: Long subroutine name
  • Loading branch information
jameskermode authored May 17, 2024
2 parents aa19f46 + 76c356a commit a5eec31
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ EXAMPLES = arrayderivedtypes \
docstring \
type_check \
derivedtypes_procedure \
optional_string
optional_string \
long_subroutine_name

PYTHON = python

Expand Down
38 changes: 38 additions & 0 deletions examples/long_subroutine_name/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 --type-check
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} test.py
6 changes: 6 additions & 0 deletions examples/long_subroutine_name/Makefile.meson
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include ../make.meson.inc

NAME := pywrapper

test: build
$(PYTHON) test.py
25 changes: 25 additions & 0 deletions examples/long_subroutine_name/main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module m_long_subroutine_name

implicit none

integer :: m_long_subroutine_name_integer

type m_long_subroutine_name_type

integer :: m_long_subroutine_name_type_integer
integer, dimension(10) :: m_long_subroutine_name_type_integer_array

end type m_long_subroutine_name_type

type m_long_subroutine_name_type_2

type(m_long_subroutine_name_type), dimension(10) :: m_long_subroutine_name_type_2_type_array

end type m_long_subroutine_name_type_2

contains

subroutine m_long_subroutine_name_subroutine()
end subroutine m_long_subroutine_name_subroutine

end module m_long_subroutine_name
23 changes: 23 additions & 0 deletions examples/long_subroutine_name/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from pywrapper import m_long_subroutine_name

class TestLongSubroutineName(unittest.TestCase):

def test_long_subroutine_name(self):

m_long_subroutine_name.m_long_subroutine_name_integer = 42

typ = m_long_subroutine_name.m_long_subroutine_name_type()
typ.m_long_subroutine_name_type_name_integer = 42
typ.m_long_subroutine_name_type_name_integer_array = 42

typ2 = m_long_subroutine_name.m_long_subroutine_name_type_2()
typ2.m_long_subroutine_name_type_2_type_array[0].m_long_subroutine_name_type_integer = 42
typ2.m_long_subroutine_name_type_2_type_array[0].m_long_subroutine_name_type_integer_array = 42

m_long_subroutine_name.m_long_subroutine_name_subroutine()

if __name__ == '__main__':

unittest.main()
1 change: 1 addition & 0 deletions f90wrap/f90wrapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def visit_Procedure(self, node):
arg_names = '(' + ', '.join([arg.name for arg in node.arguments]) + ')' if node.arguments else ''
if node.mod_name is not None:
sub_name = self.prefix + node.mod_name + '__' + node.name
sub_name = shorten_long_name(sub_name)
self.write("subroutine %s%s" % (sub_name, arg_names))
self.indent()
self.write_uses_lines(node)
Expand Down
21 changes: 13 additions & 8 deletions f90wrap/pywrapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def write_constructor(self, node):

if node.mod_name is not None:
dct['func_name'] = node.mod_name + '__' + node.name
dct['subroutine_name'] = shorten_long_name('%(prefix)s%(func_name)s' % dct)

self.write("def __init__(self, %(py_arg_names)s):" % dct)
self.indent()
Expand All @@ -342,7 +343,7 @@ def write_constructor(self, node):
{'arg_py_name': arg.py_name})
self.write('f90wrap.runtime.FortranDerivedType.__init__(self)')

self.write('result = %(mod_name)s.%(prefix)s%(func_name)s(%(f90_arg_names)s)' % dct)
self.write('result = %(mod_name)s.%(subroutine_name)s(%(f90_arg_names)s)' % dct)
self.write('self._handle = result[0] if isinstance(result, tuple) else result')
self.dedent()
self.write()
Expand All @@ -366,8 +367,8 @@ def write_classmethod(self, node):
{'arg_py_name': arg.py_name})
if node.mod_name is not None:
dct['func_name'] = node.mod_name + '__' + node.name

call_line = '%(call)s%(mod_name)s.%(prefix)s%(func_name)s(%(f90_arg_names)s)' % dct
dct['subroutine_name'] = shorten_long_name('%(prefix)s%(func_name)s' % dct)
call_line = '%(call)s%(mod_name)s.%(subroutine_name)s(%(f90_arg_names)s)' % dct

self.write('@classmethod')
self.write("def %(method_name)s(cls, %(py_arg_names)s):" % dct)
Expand All @@ -394,12 +395,14 @@ def write_destructor(self, node):
f90_arg_names=', '.join(['%s=%s' % (arg.name, arg.py_value) for arg in node.arguments]))
if node.mod_name is not None:
dct['func_name'] = node.mod_name + '__' + node.name
dct['subroutine_name'] = shorten_long_name('%(prefix)s%(func_name)s' % dct)

self.write("def __del__(%(py_arg_names)s):" % dct)
self.indent()
self.write(format_doc_string(node))
self.write('if self._alloc:')
self.indent()
self.write('%(mod_name)s.%(prefix)s%(func_name)s(%(f90_arg_names)s)' % dct)
self.write('%(mod_name)s.%(subroutine_name)s(%(f90_arg_names)s)' % dct)
self.dedent()
self.dedent()
self.write()
Expand All @@ -424,6 +427,7 @@ def visit_Procedure(self, node):
call='')
if node.mod_name is not None:
dct['func_name'] = node.mod_name + '__' + node.name
dct['subroutine_name'] = shorten_long_name('%(prefix)s%(func_name)s' % dct)

if isinstance(node, ft.Function):
dct['result'] = ', '.join([ret_val.name for ret_val in node.ret_val])
Expand All @@ -445,7 +449,7 @@ def visit_Procedure(self, node):
(
'None if %(arg_py_name)s is None else %(arg_py_name)s._handle') %
{'arg_py_name': arg.py_name})
call_line = '%(call)s%(mod_name)s.%(prefix)s%(func_name)s(%(f90_arg_names)s)' % dct
call_line = '%(call)s%(mod_name)s.%(subroutine_name)s(%(f90_arg_names)s)' % dct
self.write(call_line)

if isinstance(node, ft.Function):
Expand Down Expand Up @@ -604,12 +608,13 @@ def write_scalar_wrappers(self, node, el, properties):
if dct['el_name_set'] in procs:
dct['el_name_set'] += '_'

dct['subroutine_name'] = shorten_long_name('%(prefix)s%(type_name)s__get__%(el_name)s' % dct)
dct['subroutine_name_get'] = shorten_long_name('%(prefix)s%(type_name)s__get__%(el_name)s' % dct)
dct['subroutine_name_set'] = shorten_long_name('%(prefix)s%(type_name)s__set__%(el_name)s' % dct)

self.write('def %(el_name_get)s(%(self)s):' % dct)
self.indent()
self.write(format_doc_string(el))
self.write('return %(mod_name)s.%(subroutine_name)s(%(handle)s)' % dct)
self.write('return %(mod_name)s.%(subroutine_name_get)s(%(handle)s)' % dct)
self.dedent()
self.write()
if 'parameter' in el.attributes and isinstance(node, ft.Module) and self.make_package:
Expand All @@ -620,7 +625,7 @@ def write_scalar_wrappers(self, node, el, properties):
if not isinstance(node, ft.Module) or not self.make_package:
self.write('@%(el_name_get)s.setter' % dct)
self.write('''def %(el_name_set)s(%(selfcomma)s%(el_name)s):
%(mod_name)s.%(prefix)s%(type_name)s__set__%(el_name)s(%(set_args)s)
%(mod_name)s.%(subroutine_name_set)s(%(set_args)s)
''' % dct)
self.write()

Expand Down

0 comments on commit a5eec31

Please sign in to comment.