Skip to content

BUG: Handle --lower for F2PY directives and callbacks #27728

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 7 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/f2py/python-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ In Python:
.. literalinclude:: ./code/results/extcallback_session.dat
:language: python

.. note::

When using modified Fortran code via ``callstatement`` or other directives,
the wrapped Python function must be called as a callback, otherwise only the
bare Fortran routine will be used. For more details, see
https://github.com/numpy/numpy/issues/26681#issuecomment-2466460943

Resolving arguments to call-back functions
------------------------------------------

Expand Down
13 changes: 12 additions & 1 deletion numpy/f2py/crackfortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,14 @@ def readfortrancode(ffile, dowithline=show, istop=1):
if l[-1] not in "\n\r\f":
break
l = l[:-1]
# Do not lower for directives, gh-2547, gh-27697, gh-26681
is_f2py_directive = False
# Unconditionally remove comments
(l, rl) = split_by_unquoted(l, '!')
l += ' '
if rl[:5].lower() == '!f2py': # f2py directive
l, _ = split_by_unquoted(l + 4 * ' ' + rl[5:], '!')
is_f2py_directive = True
if l.strip() == '': # Skip empty line
if sourcecodeform == 'free':
# In free form, a statement continues in the next line
Expand All @@ -449,8 +452,10 @@ def readfortrancode(ffile, dowithline=show, istop=1):
if l[0] in ['*', 'c', '!', 'C', '#']:
if l[1:5].lower() == 'f2py': # f2py directive
l = ' ' + l[5:]
is_f2py_directive = True
else: # Skip comment line
cont = False
is_f2py_directive = False
continue
elif strictf77:
if len(l) > 72:
Expand All @@ -476,6 +481,7 @@ def readfortrancode(ffile, dowithline=show, istop=1):
else:
# clean up line beginning from possible digits.
l = ' ' + l[5:]
# f2py directives are already stripped by this point
if localdolowercase:
finalline = ll.lower()
else:
Expand Down Expand Up @@ -505,7 +511,11 @@ def readfortrancode(ffile, dowithline=show, istop=1):
origfinalline = ''
else:
if localdolowercase:
finalline = ll.lower()
# lines with intent() should be lowered otherwise
# TestString::test_char fails due to mixed case
# f2py directives without intent() should be left untouched
# gh-2547, gh-27697, gh-26681
finalline = ll.lower() if "intent" in ll.lower() or not is_f2py_directive else ll
else:
finalline = ll
origfinalline = ll
Expand Down Expand Up @@ -537,6 +547,7 @@ def readfortrancode(ffile, dowithline=show, istop=1):
else:
dowithline(finalline)
l1 = ll
# Last line should never have an f2py directive anyway
if localdolowercase:
finalline = ll.lower()
else:
Expand Down
18 changes: 18 additions & 0 deletions numpy/f2py/tests/src/callback/gh26681.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module utils
implicit none
contains
subroutine my_abort(message)
implicit none
character(len=*), intent(in) :: message
!f2py callstatement PyErr_SetString(PyExc_ValueError, message);f2py_success = 0;
!f2py callprotoargument char*
write(0,*) "THIS SHOULD NOT APPEAR"
stop 1
end subroutine my_abort

subroutine do_something(message)
!f2py intent(callback, hide) mypy_abort
character(len=*), intent(in) :: message
call mypy_abort(message)
end subroutine do_something
end module utils
12 changes: 12 additions & 0 deletions numpy/f2py/tests/src/crackfortran/gh27697.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module utils
implicit none
contains
subroutine my_abort(message)
implicit none
character(len=*), intent(in) :: message
!f2py callstatement PyErr_SetString(PyExc_ValueError, message);f2py_success = 0;
!f2py callprotoargument char*
write(0,*) "THIS SHOULD NOT APPEAR"
stop 1
end subroutine my_abort
end module utils
15 changes: 15 additions & 0 deletions numpy/f2py/tests/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import threading
import traceback
import time
import platform

import numpy as np
from numpy.testing import IS_PYPY
Expand Down Expand Up @@ -244,3 +245,17 @@ def bar(x):

res = self.module.foo(bar)
assert res == 110


@pytest.mark.slow
@pytest.mark.xfail(condition=(platform.system().lower() == 'darwin'),
run=False,
reason="Callback aborts cause CI failures on macOS")
class TestCBFortranCallstatement(util.F2PyTest):
sources = [util.getpath("tests", "src", "callback", "gh26681.f90")]
options = ['--lower']

def test_callstatement_fortran(self):
with pytest.raises(ValueError, match='helpme') as exc:
self.module.mypy_abort = self.module.utils.my_abort
self.module.utils.do_something('helpme')
9 changes: 9 additions & 0 deletions numpy/f2py/tests/test_crackfortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,12 @@ def test_param_eval_too_many_dims(self):
dimspec = '(0:4, 3:12, 5)'
pytest.raises(ValueError, crackfortran.param_eval, v, g_params, params,
dimspec=dimspec)

@pytest.mark.slow
class TestLowerF2PYDirective(util.F2PyTest):
sources = [util.getpath("tests", "src", "crackfortran", "gh27697.f90")]
options = ['--lower']

def test_no_lower_fail(self):
with pytest.raises(ValueError, match='aborting directly') as exc:
self.module.utils.my_abort('aborting directly')
Loading