summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorIan Thomas <ianthomas23@gmail.com>2020-10-27 14:34:47 +0000
committerIan Thomas <ianthomas23@gmail.com>2020-10-27 14:34:47 +0000
commit2a7d75712cdbbe9969b385df748e0c024ea7a1b7 (patch)
tree54166fae2123c3058c362dc77554d34d891c6914 /numpy
parent119a0330c9cbeb13fbf8801b4f5d956659ff92cc (diff)
downloadnumpy-2a7d75712cdbbe9969b385df748e0c024ea7a1b7.tar.gz
BUG: f2py incorrectly translates dimension declarations.
In fortran functions passed to f2py, calculations that occur in the dimensions of array declarations are interpreted as floats. Valid fortran requires integers. Problem only occurs with fortran functions not subroutines as only for the former does f2py generate fortran wrapper code that fails to compile. Relevant code is in numpy.f2py.crackfortran.getlincoef(), which calculates and returns the coefficients to getarrlen() for writing to the fortran wrapper code. Fixes gh-8062.
Diffstat (limited to 'numpy')
-rwxr-xr-xnumpy/f2py/crackfortran.py9
-rw-r--r--numpy/f2py/tests/test_crackfortran.py22
2 files changed, 31 insertions, 0 deletions
diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py
index 3d2f97a56..2614d3dca 100755
--- a/numpy/f2py/crackfortran.py
+++ b/numpy/f2py/crackfortran.py
@@ -2156,6 +2156,15 @@ def getlincoef(e, xset): # e = a*x+b ; x in xset
m1 = re_1.match(ee)
c2 = myeval(ee, {}, {})
if (a * 0.5 + b == c and a * 1.5 + b == c2):
+ # gh-8062: return integers instead of floats if possible.
+ try:
+ a = int(a)
+ except:
+ pass
+ try:
+ b = int(b)
+ except:
+ pass
return a, b, x
except Exception:
pass
diff --git a/numpy/f2py/tests/test_crackfortran.py b/numpy/f2py/tests/test_crackfortran.py
index 735804024..96adffc72 100644
--- a/numpy/f2py/tests/test_crackfortran.py
+++ b/numpy/f2py/tests/test_crackfortran.py
@@ -86,3 +86,25 @@ class TestPublicPrivate():
assert 'public' not in mod['vars']['a']['attrspec']
assert 'private' not in mod['vars']['seta']['attrspec']
assert 'public' in mod['vars']['seta']['attrspec']
+
+
+class TestArrayDimCalculation(util.F2PyTest):
+ # Issue gh-8062. Calculations that occur in the dimensions of fortran
+ # array declarations should be interpreted by f2py as integers not floats.
+ # Prior to fix, test fails as generated fortran wrapper does not compile.
+ code = """
+ function test(n, a)
+ integer, intent(in) :: n
+ real(8), intent(out) :: a(0:2*n/2)
+ integer :: test
+ a(:) = n
+ test = 1
+ endfunction
+ """
+
+ def test_issue_8062(self):
+ for n in (5, 11):
+ _, a = self.module.test(n)
+ assert(a.shape == (n+1,))
+ assert_array_equal(a, n)
+