Skip to content

Commit acefc26

Browse files
authored
Merge pull request numpy#26537 from HaoZeke/fixF77comments_26148
BUG: Fix F77 ! comment handling
2 parents e41b9c1 + 0888577 commit acefc26

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed

numpy/f2py/_backends/_distutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
class DistutilsBackend(Backend):
1414
def __init__(sef, *args, **kwargs):
1515
warnings.warn(
16-
"distutils has been deprecated since NumPy 1.26.x"
16+
"\ndistutils has been deprecated since NumPy 1.26.x\n"
1717
"Use the Meson backend instead, or generate wrappers"
18-
"without -c and use a custom build script",
18+
" without -c and use a custom build script",
1919
VisibleDeprecationWarning,
2020
stacklevel=2,
2121
)

numpy/f2py/crackfortran.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,11 @@ def readfortrancode(ffile, dowithline=show, istop=1):
425425
if l[-1] not in "\n\r\f":
426426
break
427427
l = l[:-1]
428-
if not strictf77:
429-
(l, rl) = split_by_unquoted(l, '!')
430-
l += ' '
431-
if rl[:5].lower() == '!f2py': # f2py directive
432-
l, _ = split_by_unquoted(l + 4 * ' ' + rl[5:], '!')
428+
# Unconditionally remove comments
429+
(l, rl) = split_by_unquoted(l, '!')
430+
l += ' '
431+
if rl[:5].lower() == '!f2py': # f2py directive
432+
l, _ = split_by_unquoted(l + 4 * ' ' + rl[5:], '!')
433433
if l.strip() == '': # Skip empty line
434434
if sourcecodeform == 'free':
435435
# In free form, a statement continues in the next line
@@ -466,25 +466,13 @@ def readfortrancode(ffile, dowithline=show, istop=1):
466466
finalline = ''
467467
origfinalline = ''
468468
else:
469-
if not strictf77:
470-
# F90 continuation
471-
r = cont1.match(l)
472-
if r:
473-
l = r.group('line') # Continuation follows ..
474-
if cont:
475-
ll = ll + cont2.match(l).group('line')
476-
finalline = ''
477-
origfinalline = ''
478-
else:
479-
# clean up line beginning from possible digits.
480-
l = ' ' + l[5:]
481-
if localdolowercase:
482-
finalline = ll.lower()
483-
else:
484-
finalline = ll
485-
origfinalline = ll
486-
ll = l
487-
cont = (r is not None)
469+
r = cont1.match(l)
470+
if r:
471+
l = r.group('line') # Continuation follows ..
472+
if cont:
473+
ll = ll + cont2.match(l).group('line')
474+
finalline = ''
475+
origfinalline = ''
488476
else:
489477
# clean up line beginning from possible digits.
490478
l = ' ' + l[5:]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SUBROUTINE TESTSUB(
2+
& INPUT1, INPUT2, !Input
3+
& OUTPUT1, OUTPUT2) !Output
4+
5+
IMPLICIT NONE
6+
INTEGER, INTENT(IN) :: INPUT1, INPUT2
7+
INTEGER, INTENT(OUT) :: OUTPUT1, OUTPUT2
8+
9+
OUTPUT1 = INPUT1 + INPUT2
10+
OUTPUT2 = INPUT1 * INPUT2
11+
12+
RETURN
13+
END SUBROUTINE TESTSUB
14+
15+
SUBROUTINE TESTSUB2(OUTPUT)
16+
IMPLICIT NONE
17+
INTEGER, PARAMETER :: N = 10 ! Array dimension
18+
REAL, INTENT(OUT) :: OUTPUT(N)
19+
INTEGER :: I
20+
21+
DO I = 1, N
22+
OUTPUT(I) = I * 2.0
23+
END DO
24+
25+
RETURN
26+
END
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SUBROUTINE TESTSUB(INPUT1, & ! Hello
2+
! commenty
3+
INPUT2, OUTPUT1, OUTPUT2) ! more comments
4+
INTEGER, INTENT(IN) :: INPUT1, INPUT2
5+
INTEGER, INTENT(OUT) :: OUTPUT1, OUTPUT2
6+
OUTPUT1 = INPUT1 + &
7+
INPUT2
8+
OUTPUT2 = INPUT1 * INPUT2
9+
END SUBROUTINE TESTSUB

numpy/f2py/tests/test_regression.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import numpy as np
5+
import numpy.testing as npt
56

67
from . import util
78

@@ -76,3 +77,34 @@ def test_gh25344(self):
7677
exp = 7.0
7778
res = self.module.add(3.0, 4.0)
7879
assert exp == res
80+
81+
class TestF77Comments(util.F2PyTest):
82+
# Check that comments are stripped from F77 continuation lines
83+
sources = [util.getpath("tests", "src", "regression", "f77comments.f")]
84+
85+
@pytest.mark.slow
86+
def test_gh26148(self):
87+
x1 = np.array(3, dtype=np.int32)
88+
x2 = np.array(5, dtype=np.int32)
89+
res=self.module.testsub(x1, x2)
90+
assert(res[0] == 8)
91+
assert(res[1] == 15)
92+
93+
@pytest.mark.slow
94+
def test_gh26466(self):
95+
# Check that comments after PARAMETER directions are stripped
96+
expected = np.arange(1, 11, dtype=np.float32)*2
97+
res=self.module.testsub2()
98+
npt.assert_allclose(expected, res)
99+
100+
class TestF90Contiuation(util.F2PyTest):
101+
# Check that comments are stripped from F90 continuation lines
102+
sources = [util.getpath("tests", "src", "regression", "f90continuation.f90")]
103+
104+
@pytest.mark.slow
105+
def test_gh26148b(self):
106+
x1 = np.array(3, dtype=np.int32)
107+
x2 = np.array(5, dtype=np.int32)
108+
res=self.module.testsub(x1, x2)
109+
assert(res[0] == 8)
110+
assert(res[1] == 15)

0 commit comments

Comments
 (0)