Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type check for better fortran interface support #182

Merged
merged 3 commits into from
Jan 18, 2023
Merged

Type check for better fortran interface support #182

merged 3 commits into from
Jan 18, 2023

Conversation

danbeibei
Copy link

@danbeibei danbeibei commented Jan 12, 2023

This pull request addresses two current limitations:

This pr adds type and shape checks in the python wrapper function. It raises TypeError if the type/shape provided to the python function does not match the type/shape of the wrapped Fortran subroutine.
It allows better support for Fortran interfaces. For instance, it enables one to wrap:

  • A Fortran interface with kind=4 and kind=8 implementations of a subroutine.
  • A Fortran interface with 2d arrays and 3d arrays implementations of a subroutine.
  • A Fortran interface with implementations for two different derived types.

Known limitation: as Fortran logical is mapped to C int there is no differentiation of python bool and int32.

It should solve issue #107

A command line flag is added to activate those checks, so that old behavior is still possible and is the default.
The flag could be removed so that the checks are always added, but this could break existing code. It does break some tests in the exemple directory, though they seem easily fixable.

Copy link
Owner

@jameskermode jameskermode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution. Looks good from a quick scan apart from the one minor comment noted.

I wasn't able to figure out how this allows polymorphism, but I'll read it again more carefully later before merging.

@@ -155,7 +155,8 @@ def _format_line_no(lineno):
class PythonWrapperGenerator(ft.FortranVisitor, cg.CodeGenerator):
def __init__(self, prefix, mod_name, types, f90_mod_name=None,
make_package=False, kind_map=None, init_file=None,
py_mod_names=None, class_names=None, max_length=None):
py_mod_names=None, class_names=None, max_length=None,
type_check=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value of type_check should probably be False, not None?

@jameskermode
Copy link
Owner

OK, I understand the point about polymorphism now. Happy to merge once you fix the minor issue with the type_check default value.

@danbeibei
Copy link
Author

I fixed the type_check default value. I also reworked a bit the case where the fortran type is not known to f90wrap.

@jameskermode jameskermode merged commit 7237260 into jameskermode:master Jan 18, 2023
@jameskermode
Copy link
Owner

thank you! I'll wait for the upcoming build improvements before doing a new release.

@danbeibei danbeibei mentioned this pull request Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants