diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-06-09 07:35:48 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-06-09 07:35:48 -0700 |
commit | 704b456e49b13150aa693f4fd7d66dca0d541f0b (patch) | |
tree | 3f3beda2389e6cb6dd64c9da7178f538a525fb30 /numpy | |
parent | 8dcf3979780819db9eb2fe912aa0275f7424d50e (diff) | |
parent | 18e7f40e97cae555f0446da99180525a8d259d81 (diff) | |
download | numpy-704b456e49b13150aa693f4fd7d66dca0d541f0b.tar.gz |
Merge pull request #3415 from charris/pull-2699-rebased
Pull 2699 rebased
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 16 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 24 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 9 |
3 files changed, 45 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 18ff1ec51..e5e21c98d 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1027,6 +1027,16 @@ array_copy(PyArrayObject *self, PyObject *args, PyObject *kwds) return PyArray_NewCopy(self, order); } +/* Separate from array_copy to make __copy__ preserve Fortran contiguity. */ +static PyObject * +array_copy_keeporder(PyArrayObject *self, PyObject *args, PyObject *kwds) +{ + if (!PyArg_ParseTuple(args, "")) { + return NULL; + } + return PyArray_NewCopy(self, NPY_KEEPORDER); +} + #include <stdio.h> static PyObject * array_resize(PyArrayObject *self, PyObject *args, PyObject *kwds) @@ -1291,7 +1301,7 @@ array_deepcopy(PyArrayObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O", &visit)) { return NULL; } - ret = (PyArrayObject *)PyArray_Copy(self); + ret = (PyArrayObject *)PyArray_NewCopy(self, NPY_KEEPORDER); if (PyDataType_REFCHK(PyArray_DESCR(self))) { copy = PyImport_ImportModule("copy"); if (copy == NULL) { @@ -2160,8 +2170,8 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { /* for the copy module */ {"__copy__", - (PyCFunction)array_copy, - METH_VARARGS | METH_KEYWORDS, NULL}, + (PyCFunction)array_copy_keeporder, + METH_VARARGS, NULL}, {"__deepcopy__", (PyCFunction)array_deepcopy, METH_VARARGS, NULL}, diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 87903c28a..232734b84 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -642,6 +642,30 @@ class TestMethods(TestCase): d.sort() assert_equal(d, c, "test sort with default axis") + def test_copy(self): + def assert_fortran(arr): + assert_(arr.flags.fortran) + assert_(arr.flags.f_contiguous) + assert_(not arr.flags.c_contiguous) + + def assert_c(arr): + assert_(not arr.flags.fortran) + assert_(not arr.flags.f_contiguous) + assert_(arr.flags.c_contiguous) + + a = np.empty((2, 2), order='F') + # Test copying a Fortran array + assert_c(a.copy()) + assert_c(a.copy('C')) + assert_fortran(a.copy('F')) + assert_fortran(a.copy('A')) + + # Now test starting with a C array. + a = np.empty((2, 2), order='C') + assert_c(a.copy()) + assert_c(a.copy('C')) + assert_fortran(a.copy('F')) + assert_c(a.copy('A')) def test_sort_order(self): # Test sorting an array with fields diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index bb0bf029b..fcbb75ba2 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1903,7 +1903,14 @@ class TestRegression(TestCase): count = np.count_nonzero(arr) assert_equal(count, 0) - + def test_copymodule_preserves_f_contiguity(self): + a = np.empty((2, 2), order='F') + b = copy.copy(a) + c = copy.deepcopy(a) + assert_(b.flags.fortran) + assert_(b.flags.f_contiguous) + assert_(c.flags.fortran) + assert_(c.flags.f_contiguous) if __name__ == "__main__": |