feat: support optional complex scalar arguments#372
Open
krystophny wants to merge 1 commit into
Open
Conversation
UnwrappablesRemover dropped every optional complex scalar argument, so present(z) could never be exercised from Python; required complex scalars were wrapped normally. Remove that restriction so the f2py path handles optional complex scalars directly. For --direct-c, two further gaps are fixed so complex scalar values work: - the scalar argument parser had no complex case; read a Python complex via PyComplex_AsCComplex. - default complex mapped to double _Complex / NPY_CDOUBLE, a different size than Fortran's single-precision default. Map it to float _Complex / NPY_COMPLEX64, mirroring default real -> float. double complex and complex(8) still map to double; complex(dp) via the kind map is unchanged. Add a transform regression test (the optional complex argument survives), update the direct-C complex mapping tests to the corrected defaults, and add examples/issue371_optional_complex covering both backends.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #371.
UnwrappablesRemover.visit_Argumentdropped every optional complex scalar argument, sopresent(z)could never be exercised from Python. Required complex scalars were wrapped normally. This removes that restriction, after which the f2py path handles optional complex scalars directly.For
--direct-c, two further gaps are fixed so complex scalar values work:PyComplex_AsCComplex.complexmapped todouble _Complex/NPY_CDOUBLE, a different size than Fortran's single-precision default, which dropped the imaginary part. It now maps tofloat _Complex/NPY_COMPLEX64, mirroring defaultreal -> float.double complexandcomplex(8)still map to double;complex(dp)via the kind map is unchanged.Verification
test/test_transform.py::TestTransform::test_optional_complex_scalar_keptasserts the optional complex argument survives the transform. On master it fails:AssertionError: 'z' not found in ['flag'].test/test_directc.pycomplex mapping tests updated to the corrected single-precision defaults (anddouble complex/complex(4)added); 29 tests pass./tmp/f90wrap371-venv/bin/python -m pytest --import-mode=importlib -q /home/ert/code/f90wrap-dev/f90wrap/test/test_transform.py::TestTransform::test_optional_complex_scalar_kept /home/ert/code/f90wrap-dev/f90wrap/test/test_directc.py:30 passed in 0.27s.examples/issue371_optional_complexround-trips a complex value throughpresent(z)on both the f2py and--direct-cpaths.Compiler matrix for
examples/issue371_optional_complex, usingmake -f Makefile.meson testwithDIRECTC=noandDIRECTC=yes:gfortranRan 2 tests ... OKRan 2 tests ... OKflangRan 2 tests ... OKRan 2 tests ... OKifxRan 2 tests ... OKRan 2 tests ... OKnvfortranRan 2 tests ... OKRan 2 tests ... OKThe
ifxruns need Intel's runtime library directory on the link path:LIBRARY_PATH=/opt/intel/oneapi/compiler/2025.0/lib.Note
The absent-argument case for optional scalars under
--direct-c(passingNonesopresent()is.false.) is handled by a separate change for #369.