Repository navigation
BUG, ENH: Fix iso_c_binding type maps and fix bind(c) support
#24555
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a9488ec
MAINT: fix ISO_C type maps in f2py
HaoZeke a34d2bc
TST: Add a test for gh-24553
HaoZeke 539d7a6
DOC: Add a news fragment for iso_c_binding [f2py]
HaoZeke 5a8041f
MAINT: Rework to keep older logic
HaoZeke e2a230b
MAINT: Move into private module
HaoZeke 52d9e7b
MAINT: Add a kind_map for function use
HaoZeke 66f8ba1
ENH: Rework bind(c) detection
HaoZeke 00fa2e7
ENH: Add bind(c) to function subroutine bindings
HaoZeke 27a5be3
MAINT: Do not use bind(c) for subroutine wrappers
HaoZeke 5633f24
MAINT: Use iso_c_binding where necessary
HaoZeke df3a819
ENH: Load iso_c_binding module only when needed
HaoZeke 3ccc483
MAINT: Fix regep for bind matches
HaoZeke 6cc0777
MAINT: More robust test for using iso_c_binding
HaoZeke 320aec7
MAINT: Be more forgiving with bindc matches
HaoZeke fab3843
TST: Add more tests for iso_c_binding
HaoZeke 9b5a168
DOC: Add a release note
HaoZeke f4493e2
MAINT: Rename docstring, modify tests
HaoZeke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ``iso_c_binding`` support for ``f2py`` | ||
| -------------------------------------- | ||
| Previously, users would have to define their own custom ``f2cmap`` file to use | ||
| type mappings defined by the Fortran2003 ``iso_c_binding`` intrinsic module. | ||
| These type maps are now natively supported by ``f2py`` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ``bind(c)`` support for ``f2py`` | ||
| -------------------------------- | ||
| Both functions and subroutines can be annotated with ``bind(c)``. ``f2py`` will | ||
| handle both the correct type mapping, and preserve the unique label for other | ||
| ``C`` interfaces. | ||
|
|
||
| **Note:** ``bind(c, name = 'routine_name_other_than_fortran_routine')`` is not | ||
| honored by the ``f2py`` bindings by design, since ``bind(c)`` with the ``name`` | ||
| is meant to guarantee only the same name in ``C`` and ``Fortran``, not in | ||
| ``Python`` and ``Fortran``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| iso_c_binding_map = { | ||
| 'integer': { | ||
| 'c_int': 'int', | ||
| 'c_short': 'short int', | ||
| 'c_long': 'long int', | ||
| 'c_long_long': 'long long int', | ||
| 'c_signed_char': 'signed char', | ||
| 'c_size_t': 'size_t', | ||
| 'c_int8_t': 'int8_t', | ||
| 'c_int16_t': 'int16_t', | ||
| 'c_int32_t': 'int32_t', | ||
| 'c_int64_t': 'int64_t', | ||
| 'c_int_least8_t': 'int_least8_t', | ||
| 'c_int_least16_t': 'int_least16_t', | ||
| 'c_int_least32_t': 'int_least32_t', | ||
| 'c_int_least64_t': 'int_least64_t', | ||
| 'c_int_fast8_t': 'int_fast8_t', | ||
| 'c_int_fast16_t': 'int_fast16_t', | ||
| 'c_int_fast32_t': 'int_fast32_t', | ||
| 'c_int_fast64_t': 'int_fast64_t', | ||
| 'c_intmax_t': 'intmax_t', | ||
| 'c_intptr_t': 'intptr_t', | ||
| 'c_ptrdiff_t': 'intptr_t', | ||
| }, | ||
| 'real': { | ||
| 'c_float': 'float', | ||
| 'c_double': 'double', | ||
| 'c_long_double': 'long double' | ||
| }, | ||
| 'complex': { | ||
| 'c_float_complex': 'float _Complex', | ||
| 'c_double_complex': 'double _Complex', | ||
| 'c_long_double_complex': 'long double _Complex' | ||
| }, | ||
| 'logical': { | ||
| 'c_bool': '_Bool' | ||
| }, | ||
| 'character': { | ||
| 'c_char': 'char' | ||
| } | ||
| } | ||
|
|
||
| isoc_kindmap = {} | ||
| for fortran_type, c_type_dict in iso_c_binding_map.items(): | ||
| for c_type in c_type_dict.keys(): | ||
| isoc_kindmap[c_type] = fortran_type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module coddity | ||
| use iso_c_binding, only: c_double, c_int | ||
| implicit none | ||
| contains | ||
| subroutine c_add(a, b, c) bind(c, name="c_add") | ||
| real(c_double), intent(in) :: a, b | ||
| real(c_double), intent(out) :: c | ||
| c = a + b | ||
| end subroutine c_add | ||
| ! gh-9693 | ||
| function wat(x, y) result(z) bind(c) | ||
| integer(c_int), intent(in) :: x, y | ||
| integer(c_int) :: z | ||
|
|
||
| z = x + 7 | ||
| end function wat | ||
|
HaoZeke marked this conversation as resolved.
Resolved
|
||
| end module coddity | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from . import util | ||
| import numpy as np | ||
|
|
||
| class TestISOC(util.F2PyTest): | ||
| sources = [ | ||
| util.getpath("tests", "src", "isocintrin", "isoCtests.f90"), | ||
| ] | ||
|
|
||
| # gh-24553 | ||
| def test_c_double(self): | ||
| out = self.module.coddity.c_add(1, 2) | ||
| exp_out = 3 | ||
| assert out == exp_out | ||
|
|
||
| # gh-9693 | ||
| def test_bindc_function(self): | ||
| out = self.module.coddity.wat(1, 20) | ||
| exp_out = 8 | ||
| assert out == exp_out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These long templates are pretty much unreadable. Might be nice to use a generated parser at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah they take forever to write too 😅, would be nice to explore Lark at some time. The problem is most parsers don't actually support regex based parsing (which is needed for fortran because of the whitespace stuff).