diff options
author | seberg <sebastian@sipsolutions.net> | 2016-09-19 19:16:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-19 19:16:50 +0200 |
commit | cb4f81737041dc1d26053f5bec4e5c12fd749375 (patch) | |
tree | f0e644ed735757b67e9d96f97331075a9a0be49c /numpy/core | |
parent | 1147490663d36b05fad8dcce1e104601c2724560 (diff) | |
parent | 8c1ca4cb5662a80110fd2020634b4deea7717661 (diff) | |
download | numpy-cb4f81737041dc1d26053f5bec4e5c12fd749375.tar.gz |
Merge pull request #8050 from mattip/pypy-fixes
ENH: a.resize(.., refcheck=True) is almost unusable on PyPy
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/include/numpy/ndarrayobject.h | 3 | ||||
-rw-r--r-- | numpy/core/include/numpy/noprefix.h | 2 | ||||
-rw-r--r-- | numpy/core/include/numpy/oldnumeric.h | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/shape.c | 12 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 29 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 12 |
6 files changed, 48 insertions, 12 deletions
diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h index b51be6e58..f26d64efb 100644 --- a/numpy/core/include/numpy/ndarrayobject.h +++ b/numpy/core/include/numpy/ndarrayobject.h @@ -116,9 +116,10 @@ extern "C" CONFUSE_EMACS #define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), val, \ PyArray_NBYTES(obj)) - +#ifndef PYPY_VERSION #define PyArray_REFCOUNT(obj) (((PyObject *)(obj))->ob_refcnt) #define NPY_REFCOUNT PyArray_REFCOUNT +#endif #define NPY_MAX_ELSIZE (2 * NPY_SIZEOF_LONGDOUBLE) #define PyArray_ContiguousFromAny(op, type, min_depth, max_depth) \ diff --git a/numpy/core/include/numpy/noprefix.h b/numpy/core/include/numpy/noprefix.h index 830617087..45130d16e 100644 --- a/numpy/core/include/numpy/noprefix.h +++ b/numpy/core/include/numpy/noprefix.h @@ -203,7 +203,9 @@ #define MAX_UINTP NPY_MAX_UINTP #define INTP_FMT NPY_INTP_FMT +#ifndef PYPY_VERSION #define REFCOUNT PyArray_REFCOUNT #define MAX_ELSIZE NPY_MAX_ELSIZE +#endif #endif diff --git a/numpy/core/include/numpy/oldnumeric.h b/numpy/core/include/numpy/oldnumeric.h index 748f06da3..38530faf0 100644 --- a/numpy/core/include/numpy/oldnumeric.h +++ b/numpy/core/include/numpy/oldnumeric.h @@ -1,9 +1,11 @@ #include "arrayobject.h" +#ifndef PYPY_VERSION #ifndef REFCOUNT # define REFCOUNT NPY_REFCOUNT # define MAX_ELSIZE 16 #endif +#endif #define PyArray_UNSIGNED_TYPES #define PyArray_SBYTE NPY_BYTE diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index 30fd45f33..ae7020b1b 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -87,7 +87,15 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, } if (refcheck) { +#ifdef PYPY_VERSION + PyErr_SetString(PyExc_ValueError, + "cannot resize an array with refcheck=True on PyPy.\n" + "Use the resize function or refcheck=False"); + + return NULL; +#else refcnt = PyArray_REFCOUNT(self); +#endif } else { refcnt = 1; @@ -96,8 +104,8 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, || (PyArray_BASE(self) != NULL) || (((PyArrayObject_fields *)self)->weakreflist != NULL)) { PyErr_SetString(PyExc_ValueError, - "cannot resize an array that "\ - "references or is referenced\n"\ + "cannot resize an array that " + "references or is referenced\n" "by another array in this way. Use the resize function"); return NULL; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 2b585f4dc..0f3581225 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -10,6 +10,7 @@ import io import itertools import ctypes import os +import gc if sys.version_info[0] >= 3: import builtins else: @@ -4080,7 +4081,10 @@ class TestFlat(TestCase): class TestResize(TestCase): def test_basic(self): x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) - x.resize((5, 5)) + if IS_PYPY: + x.resize((5, 5), refcheck=False) + else: + x.resize((5, 5)) assert_array_equal(x.flat[:9], np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).flat) assert_array_equal(x[9:].flat, 0) @@ -4093,7 +4097,10 @@ class TestResize(TestCase): def test_int_shape(self): x = np.eye(3) - x.resize(3) + if IS_PYPY: + x.resize(3, refcheck=False) + else: + x.resize(3) assert_array_equal(x, np.eye(3)[0,:]) def test_none_shape(self): @@ -4111,19 +4118,28 @@ class TestResize(TestCase): def test_freeform_shape(self): x = np.eye(3) - x.resize(3, 2, 1) + if IS_PYPY: + x.resize(3, 2, 1, refcheck=False) + else: + x.resize(3, 2, 1) assert_(x.shape == (3, 2, 1)) def test_zeros_appended(self): x = np.eye(3) - x.resize(2, 3, 3) + if IS_PYPY: + x.resize(2, 3, 3, refcheck=False) + else: + x.resize(2, 3, 3) assert_array_equal(x[0], np.eye(3)) assert_array_equal(x[1], np.zeros((3, 3))) def test_obj_obj(self): # check memory is initialized on resize, gh-4857 a = np.ones(10, dtype=[('k', object, 2)]) - a.resize(15,) + if IS_PYPY: + a.resize(15, refcheck=False) + else: + a.resize(15,) assert_equal(a.shape, (15,)) assert_array_equal(a['k'][-5:], 0) assert_array_equal(a['k'][:-5], 1) @@ -5988,7 +6004,7 @@ class TestNewBufferProtocol(object): c = np.asarray(b) if HAS_REFCOUNT: count_2 = sys.getrefcount(np.core._internal) - assert_equal(count_1, count_2) + assert_equal(count_1, count_2) del c # avoid pyflakes unused variable warning. def test_padded_struct_array(self): @@ -6141,6 +6157,7 @@ class TestMemEventHook(TestCase): # needs to be larger then limit of small memory cacher in ctors.c a = np.zeros(1000) del a + gc.collect() test_pydatamem_seteventhook_end() class TestMapIter(TestCase): diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 51d0447e3..40b456f35 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -13,7 +13,7 @@ from itertools import chain import numpy as np from numpy.testing import ( - run_module_suite, TestCase, assert_, assert_equal, + run_module_suite, TestCase, assert_, assert_equal, IS_PYPY, assert_almost_equal, assert_array_equal, assert_array_almost_equal, assert_raises, assert_warns, dec, suppress_warnings ) @@ -1293,9 +1293,15 @@ class TestRegression(TestCase): for k in range(3): # Try to ensure that x->data contains non-zero floats x = np.array([123456789e199], dtype=np.float64) - x.resize((m, 0)) + if IS_PYPY: + x.resize((m, 0), refcheck=False) + else: + x.resize((m, 0)) y = np.array([123456789e199], dtype=np.float64) - y.resize((0, n)) + if IS_PYPY: + y.resize((0, n),refcheck=False) + else: + y.resize((0, n)) # `dot` should just return zero (m,n) matrix z = np.dot(x, y) |