-
Notifications
You must be signed in to change notification settings - Fork 91
fix: pass NULL for absent optional arguments in --direct-c #370
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
Open
krystophny
wants to merge
3
commits into
jameskermode:master
Choose a base branch
from
krystophny:fix-issue-369-optional-directc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,33 @@ | ||
| #======================================================================= | ||
| # define the compiler names | ||
| #======================================================================= | ||
|
|
||
| include ../make.inc | ||
| PY_MOD = pywrapper | ||
| F90_SRC = main.f90 | ||
| OBJ = $(F90_SRC:.f90=.o) | ||
| F90WRAP_SRC = $(addprefix f90wrap_,${F90_SRC}) | ||
| WRAPFLAGS = -v | ||
| F2PY = f2py-f90wrap | ||
| SRC_AR = libsrc.a | ||
| .PHONY: all clean | ||
|
|
||
| all: test | ||
|
|
||
| clean: | ||
| rm -rf *.mod *.smod *.o ${SRC_AR} f90wrap*.f90 ${PY_MOD}.py _${PY_MOD}*.so __pycache__/ .f2py_f2cmap build ${PY_MOD}/ | ||
|
|
||
| %.o: %.f90 | ||
| ${F90} ${F90FLAGS} -c $< -o $@ | ||
|
|
||
| ${F90WRAP_SRC}: ${OBJ} | ||
| ${F90WRAP} -m ${PY_MOD} ${WRAPFLAGS} ${F90_SRC} | ||
|
|
||
| ${SRC_AR}: ${OBJ} | ||
| ar rcs $@ ${OBJ} | ||
|
|
||
| f2py: ${F90WRAP_SRC} ${SRC_AR} | ||
| CFLAGS="${CFLAGS}" ${F2PY} -c -m _${PY_MOD} ${F2PYFLAGS} f90wrap_*.f90 -L. -lsrc | ||
|
|
||
| test: f2py | ||
| ${PYTHON} tests.py |
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,6 @@ | ||
| include ../make.meson.inc | ||
|
|
||
| NAME := pywrapper | ||
|
|
||
| test: build | ||
| $(PYTHON) tests.py |
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,47 @@ | ||
| ! Regression example for issue #369. | ||
| ! | ||
| ! With --direct-c, passing None for an OPTIONAL argument must make present(arg) | ||
| ! return .false. inside Fortran. The bug passed a non-NULL pointer (scalar) or an | ||
| ! allocated blank buffer (character), so present() wrongly returned .true. | ||
| ! | ||
| ! Each subroutine reports its decision through an intent(out) result so the Python | ||
| ! test can assert which branch ran. Both the regular f90wrap+f2py path and the | ||
| ! --direct-c path are exercised by the shared tests.py. | ||
| module m_optional | ||
| implicit none | ||
| contains | ||
|
|
||
| ! Optional scalar: the exact case from issue #369. | ||
| subroutine scalar_default(x, n) | ||
| real, intent(out) :: x | ||
| integer, intent(in), optional :: n | ||
| if (.not. present(n)) then | ||
| x = 42.0 | ||
| else | ||
| x = real(n) | ||
| end if | ||
| end subroutine scalar_default | ||
|
|
||
| ! Optional character input. | ||
| subroutine char_default(label, name) | ||
| character(len=16), intent(out) :: label | ||
| character(len=*), intent(in), optional :: name | ||
| if (present(name)) then | ||
| label = name | ||
| else | ||
| label = "ABSENT" | ||
| end if | ||
| end subroutine char_default | ||
|
|
||
| ! Optional array input (already correct before the fix: a regression lock). | ||
| subroutine array_default(s, v) | ||
| integer, intent(out) :: s | ||
| real, intent(in), optional :: v(:) | ||
| if (present(v)) then | ||
| s = size(v) | ||
| else | ||
| s = -1 | ||
| end if | ||
| end subroutine array_default | ||
|
|
||
| end module m_optional |
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,39 @@ | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from pywrapper import m_optional | ||
|
|
||
|
|
||
| def as_text(value): | ||
| """Character returns come back as bytes (direct-c) or str (f2py).""" | ||
| if isinstance(value, bytes): | ||
| value = value.decode() | ||
| return value.strip() | ||
|
|
||
|
|
||
| class TestOptionalPresent(unittest.TestCase): | ||
| def test_scalar_present(self): | ||
| self.assertEqual(m_optional.scalar_default(n=5), 5.0) | ||
|
|
||
| def test_scalar_absent_uses_default(self): | ||
| # Bug #369: direct-c saw present(n) == .true. with value 0. | ||
| self.assertEqual(m_optional.scalar_default(), 42.0) | ||
|
|
||
| def test_char_present(self): | ||
| self.assertEqual(as_text(m_optional.char_default(name="hello")), "hello") | ||
|
|
||
| def test_char_absent_uses_default(self): | ||
| self.assertEqual(as_text(m_optional.char_default()), "ABSENT") | ||
|
|
||
| def test_array_present(self): | ||
| self.assertEqual( | ||
| m_optional.array_default(v=np.array([1.0, 2.0, 3.0], dtype=np.float32)), 3 | ||
| ) | ||
|
|
||
| def test_array_absent(self): | ||
| self.assertEqual(m_optional.array_default(), -1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
Oops, something went wrong.
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.
@krystophny @jameskermode yes it works now thanks. Curious about the still present {arg.name}_val = 0. Is this not now redundant?
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.
Good catch, you're right. The pointer is overwritten with
NULLbefore the Fortran call, so_val = 0was never read. Removed in c4b40af. The optional character-input branch was already clean (sets the pointer toNULLwith no zeroing).