Skip to content

Commit

Permalink
BUG: fix for f2py string scalars (numpy#23194)
Browse files Browse the repository at this point in the history
in previous version, any string scalar was converted to a string array of dimension len, i.e., a definition

character(len=N) :: X
effectively became

character(len=NNN), dimension(NNN) :: X
from the point of few of the numpy (python) interface:

X.shape == (NNN,)
X.dtype == '|SNNN'

Closes numpygh-23192
  • Loading branch information
2sn authored and charris committed Feb 14, 2023
1 parent 6829a6f commit efcb0fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions numpy/f2py/capi_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ def getstrlength(var):
def getarrdims(a, var, verbose=0):
ret = {}
if isstring(var) and not isarray(var):
ret['dims'] = getstrlength(var)
ret['size'] = ret['dims']
ret['rank'] = '1'
ret['size'] = getstrlength(var)
ret['rank'] = '0'
ret['dims'] = ''
elif isscalar(var):
ret['size'] = '1'
ret['rank'] = '0'
Expand Down
7 changes: 7 additions & 0 deletions numpy/f2py/tests/src/string/scalar_string.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MODULE string_test

character(len=8) :: string

character(len=12), dimension(5,7) :: strarr

END MODULE string_test
20 changes: 20 additions & 0 deletions numpy/f2py/tests/test_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,23 @@ def test_character_bc(self, state):
assert_equal(len(a), 2)

assert_raises(Exception, lambda: f(b'c'))


class TestStringScalarArr(util.F2PyTest):
sources = [util.getpath("tests", "src", "string", "scalar_string.f90")]

@pytest.mark.slow
def test_char(self):
out = self.module.string_test.string
expected = ()
assert out.shape == expected
expected = '|S8'
assert out.dtype == expected

@pytest.mark.slow
def test_char_arr(self):
out = self.module.string_test.strarr
expected = (5,7)
assert out.shape == expected
expected = '|S12'
assert out.dtype == expected

0 comments on commit efcb0fb

Please sign in to comment.