summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2015-12-06 11:44:34 -0700
committerCharles Harris <charlesr.harris@gmail.com>2015-12-06 11:44:34 -0700
commit328f89e48558b8c9523a6c41696fd2b677930fa1 (patch)
treeb69c8b117336271934d815e711928b0d8d3d95d5
parent0ba39a97ce6f38b714b8109bd32573a9eacd4c17 (diff)
parent16c6a361e741685feb67c29c36631829ce3b9559 (diff)
downloadnumpy-328f89e48558b8c9523a6c41696fd2b677930fa1.tar.gz
Merge pull request #6747 from charris/deprecate-fortran-view
DEP: Deprecate changing shape of non-C-contiguous array via descr.
-rw-r--r--doc/release/1.11.0-notes.rst15
-rw-r--r--numpy/core/src/multiarray/getset.c20
-rw-r--r--numpy/core/tests/test_deprecations.py16
3 files changed, 47 insertions, 4 deletions
diff --git a/doc/release/1.11.0-notes.rst b/doc/release/1.11.0-notes.rst
index e60062c81..fac868ca3 100644
--- a/doc/release/1.11.0-notes.rst
+++ b/doc/release/1.11.0-notes.rst
@@ -18,6 +18,8 @@ Dropped Support
Future Changes
==============
+* Relaxed stride checking will become the default.
+
Compatibility notes
===================
@@ -103,3 +105,16 @@ Changes
Deprecations
============
+Views of arrays in Fortran order
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The f_contiguous flag was used to signal that views as a dtypes that
+changed the element size would change the first index. This was always a
+bit problematical for arrays that were both f_contiguous and c_contiguous
+because c_contiguous took precendence. Relaxed stride checking results in
+more such dual contiguous arrays and breaks some existing code as a result.
+Note that this also affects changing the dtype by assigning to the dtype
+attribute of an array. The aim of this deprecation is to restrict views to
+c_contiguous arrays at some future time. A work around that is backward
+compatible is to use `a.T.view(...).T` instead. A parameter will also be
+added to the view method to explicitly ask for Fortran order views, but
+that will not be backward compatible.
diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c
index 549ea333a..c2a88e3b9 100644
--- a/numpy/core/src/multiarray/getset.c
+++ b/numpy/core/src/multiarray/getset.c
@@ -488,11 +488,25 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
if ((newtype->elsize != PyArray_DESCR(self)->elsize) &&
- (PyArray_NDIM(self) == 0 || !PyArray_ISONESEGMENT(self) ||
- PyDataType_HASSUBARRAY(newtype))) {
+ (PyArray_NDIM(self) == 0 ||
+ !PyArray_ISONESEGMENT(self) ||
+ PyDataType_HASSUBARRAY(newtype))) {
goto fail;
}
- if (PyArray_ISCONTIGUOUS(self)) {
+
+ /* Deprecate not C contiguous and a dimension changes */
+ if (newtype->elsize != PyArray_DESCR(self)->elsize &&
+ !PyArray_IS_C_CONTIGUOUS(self)) {
+ /* 11/27/2015 1.11.0 */
+ if (DEPRECATE("Changing the shape of non-C contiguous array by\n"
+ "descriptor assignment is deprecated. To maintain\n"
+ "the Fortran contiguity of a multidimensional Fortran\n"
+ "array, use 'a.T.view(...).T' instead") < 0) {
+ return -1;
+ }
+ }
+
+ if (PyArray_IS_C_CONTIGUOUS(self)) {
i = PyArray_NDIM(self) - 1;
}
else {
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index e2542195f..8f7e55d91 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -375,7 +375,7 @@ class TestBooleanIndexShapeMismatchDeprecation():
arr.__getitem__, (slice(None), index))
-class TestFullDefaultDtype:
+class TestFullDefaultDtype(object):
"""np.full defaults to float when dtype is not set. In the future, it will
use the fill value's dtype.
"""
@@ -386,5 +386,19 @@ class TestFullDefaultDtype:
assert_no_warnings(np.full, 1, 1, float)
+class TestNonCContiguousViewDeprecation(_DeprecationTestCase):
+ """View of non-C-contiguous arrays deprecated in 1.11.0.
+
+ The deprecation will not be raised for arrays that are both C and F
+ contiguous, as C contiguous is dominant. There are more such arrays
+ with relaxed stride checking than without so the deprecation is not
+ as visible with relaxed stride checking in force.
+ """
+
+ def test_fortran_contiguous(self):
+ self.assert_deprecated(np.ones((2,2)).T.view, args=(np.complex,))
+ self.assert_deprecated(np.ones((2,2)).T.view, args=(np.int8,))
+
+
if __name__ == "__main__":
run_module_suite()