Skip to content

Commit

Permalink
TST: added test for issue #3425
Browse files Browse the repository at this point in the history
  • Loading branch information
pearu committed Jul 3, 2021
1 parent e6f7ea9 commit 5cec4ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
16 changes: 15 additions & 1 deletion numpy/f2py/capi_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ def getctype(var):
return ctype


def f2cexpr(expr):
"""Rewrite Fortran expression as f2py supported C expression.
Due to the lack of proper expression parser in f2py, this function
uses heuristic approach that assumes that Fortran arithmetic
expressions are often valid C arithmetic expressions when mapping
Fortran function calls to the corresponding C function/CPP macros
calls.
"""
# TODO: support Fortran `len` function with optional kind parameter
expr = re.sub(r'\blen\b', 'f2py_slen', expr)
return expr


def getstrlength(var):
if isstringfunction(var):
if 'result' in var:
Expand All @@ -314,7 +328,7 @@ def getstrlength(var):
if '*' in a:
len = a['*']
elif 'len' in a:
len = a['len']
len = f2cexpr(a['len'])
if re.match(r'\(\s*(\*|:)\s*\)', len) or re.match(r'(\*|:)', len):
if isintent_hide(var):
errmess('getstrlength:intent(hide): expected a string with defined length but got: %s\n' % (
Expand Down
34 changes: 29 additions & 5 deletions numpy/f2py/tests/test_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@ class TestMiscCharacter(util.F2PyTest):
end subroutine {fprefix}_gh18684
subroutine {fprefix}_gh6308(x, i)
integer i
!f2py check(i>=0 && i<12) i
character*5 name, x
common name(12)
name(i + 1) = x
integer i
!f2py check(i>=0 && i<12) i
character*5 name, x
common name(12)
name(i + 1) = x
end subroutine {fprefix}_gh6308
subroutine {fprefix}_gh4519(x)
Expand All @@ -448,6 +448,21 @@ class TestMiscCharacter(util.F2PyTest):
print*, "x(",i,")=", x(i)
end do
end subroutine {fprefix}_gh4519
pure function {fprefix}_gh3425(x) result (y)
character(len=*), intent(in) :: x
character(len=len(x)) :: y
integer :: i
do i = 1, len(x)
j = iachar(x(i:i))
if (j>=iachar("a") .and. j<=iachar("z") ) then
y(i:i) = achar(j-32)
else
y(i:i) = x(i:i)
endif
end do
end function {fprefix}_gh3425
""")

def test_gh18684(self):
Expand Down Expand Up @@ -484,3 +499,12 @@ def test_gh4519(self):
r = f(x)
for k, v in expected.items():
assert_equal(getattr(r, k), v)

def test_gh3425(self):
# Test returning a copy of assumed length string
f = getattr(self.module, self.fprefix + '_gh3425')
# f is equivalent to bytes.upper

assert_equal(f('abC'), b'ABC')
assert_equal(f(''), b'')
assert_equal(f('abC12d'), b'ABC12D')

0 comments on commit 5cec4ad

Please sign in to comment.