diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2021-02-08 10:01:08 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-08 10:01:08 -0700 |
| commit | 8eff1f6a2fed8d9df52750271f4e07dc4ea5e1bf (patch) | |
| tree | 961065e49a2684fea440f46113b10b63ce1e38f4 /numpy | |
| parent | abe26c2e3e3bce8227af75fbaf7ab7e36a9cd1cb (diff) | |
| parent | 4874af99fcb9c9af869536a2bd390e9e94d07b8a (diff) | |
| download | numpy-8eff1f6a2fed8d9df52750271f4e07dc4ea5e1bf.tar.gz | |
Merge pull request #18361 from pearu/16347
ENH: Share memory of read-only intent(in) arrays.
Diffstat (limited to 'numpy')
| -rwxr-xr-x | numpy/f2py/rules.py | 2 | ||||
| -rw-r--r-- | numpy/f2py/src/fortranobject.c | 4 | ||||
| -rw-r--r-- | numpy/f2py/tests/test_array_from_pyobj.py | 13 |
3 files changed, 16 insertions, 3 deletions
diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index 4e1cf0c7d..b9cbc5487 100755 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -809,7 +809,7 @@ if (#varname#_cb.capi==Py_None) { """, {debugcapi: ["""\ fprintf(stderr,\"debug-capi:Assuming %d arguments; at most #maxnofargs#(-#nofoptargs#) is expected.\\n\",#varname#_cb.nofargs); - CFUNCSMESSPY(\"for #varname#=\",#cbname#_capi);""", + CFUNCSMESSPY(\"for #varname#=\",#varname#_cb.capi);""", {l_not(isintent_callback): """ fprintf(stderr,\"#vardebugshowvalue# (call-back in C).\\n\",#cbname#);"""}]}, """\ CFUNCSMESS(\"Saving callback variables for `#varname#`.\\n\"); diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c index 3275f90ad..b9ef18701 100644 --- a/numpy/f2py/src/fortranobject.c +++ b/numpy/f2py/src/fortranobject.c @@ -799,7 +799,7 @@ PyArrayObject* array_from_pyobj(const int type_num, && ARRAY_ISCOMPATIBLE(arr,type_num) && F2PY_CHECK_ALIGNMENT(arr, intent) ) { - if ((intent & F2PY_INTENT_C)?PyArray_ISCARRAY(arr):PyArray_ISFARRAY(arr)) { + if ((intent & F2PY_INTENT_C)?PyArray_ISCARRAY_RO(arr):PyArray_ISFARRAY_RO(arr)) { if ((intent & F2PY_INTENT_OUT)) { Py_INCREF(arr); } @@ -807,9 +807,9 @@ PyArrayObject* array_from_pyobj(const int type_num, return arr; } } - if (intent & F2PY_INTENT_INOUT) { strcpy(mess, "failed to initialize intent(inout) array"); + /* Must use PyArray_IS*ARRAY because intent(inout) requires writable input */ if ((intent & F2PY_INTENT_C) && !PyArray_ISCARRAY(arr)) strcat(mess, " -- input not contiguous"); if (!(intent & F2PY_INTENT_C) && !PyArray_ISFARRAY(arr)) diff --git a/numpy/f2py/tests/test_array_from_pyobj.py b/numpy/f2py/tests/test_array_from_pyobj.py index 0524da342..77149e4e7 100644 --- a/numpy/f2py/tests/test_array_from_pyobj.py +++ b/numpy/f2py/tests/test_array_from_pyobj.py @@ -233,6 +233,7 @@ class Array: order=self.intent.is_intent('c') and 'C' or 'F') assert_(self.pyarr.dtype == typ, repr((self.pyarr.dtype, typ))) + self.pyarr.setflags(write=self.arr.flags['WRITEABLE']) assert_(self.pyarr.flags['OWNDATA'], (obj, intent)) self.pyarr_attr = wrap.array_attrs(self.pyarr) @@ -326,6 +327,18 @@ class TestSharedMemory: else: assert_(not a.has_shared_memory(), repr(t.dtype)) + @pytest.mark.parametrize('write', ['w', 'ro']) + @pytest.mark.parametrize('order', ['C', 'F']) + @pytest.mark.parametrize('inp', ['2seq', '23seq']) + def test_in_nocopy(self, write, order, inp): + """Test if intent(in) array can be passed without copies + """ + seq = getattr(self, 'num' + inp) + obj = np.array(seq, dtype=self.type.dtype, order=order) + obj.setflags(write=(write == 'w')) + a = self.array(obj.shape, ((order=='C' and intent.in_.c) or intent.in_), obj) + assert a.has_shared_memory() + def test_inout_2seq(self): obj = np.array(self.num2seq, dtype=self.type.dtype) a = self.array([len(self.num2seq)], intent.inout, obj) |
