summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-11-17 20:35:14 -0700
committerGitHub <noreply@github.com>2017-11-17 20:35:14 -0700
commit88ef6c50e2d202a2c318d5b08803d72e12a2c6de (patch)
tree090eacd95085c4a35b3487d42afdd3c61d7a34d8
parent3fa0096a0689ea5ede1bee63cea16f1c0ce40953 (diff)
parent8825fac808151a015076abf1766e87dccb715cb0 (diff)
downloadnumpy-88ef6c50e2d202a2c318d5b08803d72e12a2c6de.tar.gz
Merge pull request #10046 from charris/fix-f2py-string-callback
Fix f2py string callback
-rw-r--r--numpy/f2py/cb_rules.py8
-rw-r--r--numpy/f2py/tests/test_callback.py34
2 files changed, 39 insertions, 3 deletions
diff --git a/numpy/f2py/cb_rules.py b/numpy/f2py/cb_rules.py
index 2f68c4d50..e68fdc17d 100644
--- a/numpy/f2py/cb_rules.py
+++ b/numpy/f2py/cb_rules.py
@@ -367,11 +367,15 @@ cb_arg_rules = [
'pyobjfrom': [{debugcapi: '\tfprintf(stderr,"debug-capi:cb:#varname#\\n");'},
{isintent_c: """\
\tif (#name#_nofargs>capi_i) {
-\t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,0,NPY_ARRAY_CARRAY,NULL); /*XXX: Hmm, what will destroy this array??? */
+\t\tint itemsize_ = #atype# == NPY_STRING ? 1 : 0;
+\t\t/*XXX: Hmm, what will destroy this array??? */
+\t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,itemsize_,NPY_ARRAY_CARRAY,NULL);
""",
l_not(isintent_c): """\
\tif (#name#_nofargs>capi_i) {
-\t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,0,NPY_ARRAY_FARRAY,NULL); /*XXX: Hmm, what will destroy this array??? */
+\t\tint itemsize_ = #atype# == NPY_STRING ? 1 : 0;
+\t\t/*XXX: Hmm, what will destroy this array??? */
+\t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,itemsize_,NPY_ARRAY_FARRAY,NULL);
""",
},
"""
diff --git a/numpy/f2py/tests/test_callback.py b/numpy/f2py/tests/test_callback.py
index 6824a2042..a224e42fd 100644
--- a/numpy/f2py/tests/test_callback.py
+++ b/numpy/f2py/tests/test_callback.py
@@ -1,9 +1,10 @@
from __future__ import division, absolute_import, print_function
import math
+import sys
import textwrap
-from numpy import array
+import numpy as np
from numpy.testing import run_module_suite, assert_, assert_equal, dec
import util
@@ -47,6 +48,16 @@ cf2py intent(out) a
a = callback(r)
end
+ subroutine string_callback_array(callback, cu, lencu, a)
+ external callback
+ integer callback
+ integer lencu
+ character*8 cu(lencu)
+ integer a
+cf2py intent(out) a
+
+ a = callback(cu, lencu)
+ end
"""
@dec.slow
@@ -119,6 +130,8 @@ cf2py intent(out) a
r = t(a.mth)
assert_(r == 9, repr(r))
+ @dec.knownfailureif(sys.platform=='win32',
+ msg='Fails with MinGW64 Gfortran (Issue #9673)')
def test_string_callback(self):
def callback(code):
@@ -131,6 +144,25 @@ cf2py intent(out) a
r = f(callback)
assert_(r == 0, repr(r))
+ @dec.knownfailureif(sys.platform=='win32',
+ msg='Fails with MinGW64 Gfortran (Issue #9673)')
+ def test_string_callback_array(self):
+ # See gh-10027
+ cu = np.zeros((1, 8), 'S1')
+
+ def callback(cu, lencu):
+ if cu.shape != (lencu, 8):
+ return 1
+ if cu.dtype != 'S1':
+ return 2
+ if not np.all(cu == b''):
+ return 3
+ return 0
+
+ f = getattr(self.module, 'string_callback_array')
+ res = f(callback, cu, len(cu))
+ assert_(res == 0, repr(res))
+
if __name__ == "__main__":
run_module_suite()