Skip to content

Commit 7e77d36

Browse files
authored
Merge pull request #24791 from danbeibei/bug_f2py_string
BUG: Fix f2py to enable use of string optional inout argument
2 parents b850c3b + d65d7df commit 7e77d36

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

numpy/f2py/cfuncs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@
634634
fprintf(stderr, "try_pyarr_from_string(str='%s', len=%d, obj=%p)\\n",
635635
(char*)str,len, obj);
636636
#endif
637+
if (!obj) return -2; /* Object missing */
638+
if (obj == Py_None) return -1; /* None */
639+
if (!PyArray_Check(obj)) goto capi_fail; /* not an ndarray */
637640
if (PyArray_Check(obj)) {
638641
PyArrayObject *arr = (PyArrayObject *)obj;
639642
assert(ISCONTIGUOUS(arr));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
subroutine string_inout_optional(output)
2+
implicit none
3+
character*(32), optional, intent(inout) :: output
4+
if (present(output)) then
5+
output="output string"
6+
endif
7+
end subroutine

numpy/f2py/tests/test_character.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,15 @@ class TestStringAssumedLength(util.F2PyTest):
595595

596596
def test_gh24008(self):
597597
self.module.greet("joe", "bob")
598+
599+
class TestStringOptionalInOut(util.F2PyTest):
600+
sources = [util.getpath("tests", "src", "string", "gh24662.f90")]
601+
602+
def test_gh24662(self):
603+
self.module.string_inout_optional()
604+
a = np.array('hi', dtype='S32')
605+
self.module.string_inout_optional(a)
606+
assert "output string" in a.tobytes().decode()
607+
with pytest.raises(Exception):
608+
aa = "Hi"
609+
self.module.string_inout_optional(aa)

0 commit comments

Comments
 (0)