Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Fix f2py to enable use of string optional inout argument #25201

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

Merged
merged 4 commits into from
Nov 20, 2023
Merged
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
3 changes: 3 additions & 0 deletions numpy/f2py/cfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@
fprintf(stderr, "try_pyarr_from_string(str='%s', len=%d, obj=%p)\\n",
(char*)str,len, obj);
#endif
if (!obj) return -2; /* Object missing */
if (obj == Py_None) return -1; /* None */
if (!PyArray_Check(obj)) goto capi_fail; /* not an ndarray */
if (PyArray_Check(obj)) {
PyArrayObject *arr = (PyArrayObject *)obj;
assert(ISCONTIGUOUS(arr));
Expand Down
7 changes: 7 additions & 0 deletions numpy/f2py/tests/src/string/gh24662.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
subroutine string_inout_optional(output)
implicit none
character*(32), optional, intent(inout) :: output
if (present(output)) then
output="output string"
endif
end subroutine
12 changes: 12 additions & 0 deletions numpy/f2py/tests/test_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,15 @@ class TestStringAssumedLength(util.F2PyTest):

def test_gh24008(self):
self.module.greet("joe", "bob")

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

def test_gh24662(self):
self.module.string_inout_optional()
a = np.array('hi', dtype='S32')
self.module.string_inout_optional(a)
assert "output string" in a.tobytes().decode()
with pytest.raises(Exception):
aa = "Hi"
self.module.string_inout_optional(aa)