Skip to content

BUG: f2py incorrectly translates dimension declarations - part 2. #17687

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

Closed
Closed
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
13 changes: 6 additions & 7 deletions numpy/f2py/crackfortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,10 @@ def buildimplicitrules(block):


def myeval(e, g=None, l=None):
""" Like `eval` but returns only integers and floats """
"""
Like `eval` but returns only integers and floats and uses integer division.
"""
e = re.sub(r'/+', r'//', e)
r = eval(e, g, l)
if type(r) in [int, float]:
return r
Expand Down Expand Up @@ -2178,14 +2181,10 @@ def getlincoef(e, xset): # e = a*x+b ; x in xset
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:
if a.is_integer():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if a is an integer; the same holds for b.

Suggested change
if a.is_integer():
if isinstance(a, float) and a.is_integer():
In [8]: int().is_integer()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-d4bbeabb52a2> in <module>
----> 1 int().is_integer()

AttributeError: 'int' object has no attribute 'is_integer'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what's supposed to happen here if a.is_integer() evaluates to False?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that int.is_integer actually does exist in newer versions of python.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new feature planned for 3.10?
On both 3.8 and 3.9 I'm just getting an AttributeError.

a = int(a)
except:
pass
try:
if b.is_integer():
b = int(b)
except:
pass
return a, b, x
except Exception:
pass
Expand Down
10 changes: 7 additions & 3 deletions numpy/f2py/tests/test_crackfortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ class TestArrayDimCalculation(util.F2PyTest):
# 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)
function test(n, a, b)
integer, intent(in) :: n
real(8), intent(out) :: a(0:2*n/2)
real(8), intent(out) :: b(0:n/2*2)
integer :: test
a(:) = n
b(:) = 3
test = 1
endfunction
"""

def test_issue_8062(self):
for n in (5, 11):
_, a = self.module.test(n)
for n in (5, 12): # Test both odd and even n.
_, a, b = self.module.test(n)
assert(a.shape == (n+1,))
assert_array_equal(a, n)
assert(b.shape == (2*(n//2)+1,))
assert_array_equal(b, 3)