8000 Merge pull request #8050 from mattip/pypy-fixes · numpy/numpy@cb4f817 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb4f817

Browse files
authored
Merge pull request #8050 from mattip/pypy-fixes
ENH: a.resize(.., refcheck=True) is almost unusable on PyPy
2 parents 1147490 + 8c1ca4c commit cb4f817

File tree

7 files changed

+51
-12
lines changed

7 files changed

+51
-12
lines changed

numpy/add_newdocs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4123,6 +4123,9 @@ def luf(lamdaexpr, *args, **kwargs):
41234123
ValueError
41244124
If `a` does not own its own data or references or views to it exist,
41254125
and the data memory must be changed.
4126+
PyPy only: will always raise if the data memory must be changed, since
4127+
there is no reliable way to determine if references or views to it
4128+
exist.
41264129
41274130
SystemError
41284131
If the `order` keyword argument is specified. This behaviour is a

numpy/core/include/numpy/ndarrayobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ extern "C" CONFUSE_EMACS
116116

117117
#define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), val, \
118118
PyArray_NBYTES(obj))
119-
119+
#ifndef PYPY_VERSION
120120
#define PyArray_REFCOUNT(obj) (((PyObject *)(obj))->ob_refcnt)
121121
#define NPY_REFCOUNT PyArray_REFCOUNT
122+
#endif
122123
#define NPY_MAX_ELSIZE (2 * NPY_SIZEOF_LONGDOUBLE)
123124

124125
#define PyArray_ContiguousFromAny(op, type, min_depth, max_depth) \

numpy/core/include/numpy/noprefix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@
203203
#define MAX_UINTP NPY_MAX_UINTP
204204
#define INTP_FMT NPY_INTP_FMT
205205

206+
#ifndef PYPY_VERSION
206207
#define REFCOUNT PyArray_REFCOUNT
207208
#define MAX_ELSIZE NPY_MAX_ELSIZE
209+
#endif
208210

209211
#endif

numpy/core/include/numpy/oldnumeric.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "arrayobject.h"
22

3+
#ifndef PYPY_VERSION
34
#ifndef REFCOUNT
45
# define REFCOUNT NPY_REFCOUNT
56
# define MAX_ELSIZE 16
67
#endif
8+
#endif
79

810
#define PyArray_UNSIGNED_TYPES
911
#define PyArray_SBYTE NPY_BYTE

numpy/core/src/multiarray/shape.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck,
8787
}
8888

8989
if (refcheck) {
90+
#ifdef PYPY_VERSION
91+
PyErr_SetString(PyExc_ValueError,
92+
"cannot resize an array with refcheck=True on PyPy.\n"
93+
"Use the resize function or refcheck=False");
94+
95+
return NULL;
96+
#else
9097
refcnt = PyArray_REFCOUNT(self);
98+
#endif
9199
}
92100
else {
93101
refcnt = 1;
@@ -96,8 +104,8 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck,
96104
|| (PyArray_BASE(self) != NULL)
97105
|| (((PyArrayObject_fields *)self)->weakreflist != NULL)) {
98106
PyErr_SetString(PyExc_ValueError,
99-
"cannot resize an array that "\
100-
"references or is referenced\n"\
107+
"cannot resize an array that "
108+
"references or is referenced\n"
101109
"by another array in this way. Use the resize function");
102110
return NULL;
103111
}

numpy/core/tests/test_multiarray.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import itertools
1111
import ctypes
1212
import os
13+
import gc
1314
if sys.version_info[0] >= 3:
1415
import builtins
1516
else:
@@ -4080,7 +4081,10 @@ def test___array__(self):
40804081
class TestResize(TestCase):
40814082
def test_basic(self):
40824083
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
4083-
x.resize((5, 5))
4084+
if IS_PYPY:
4085+
x.resize((5, 5), refcheck=False)
4086+
else:
4087+
x.resize((5, 5))
40844088
assert_array_equal(x.flat[:9],
40854089
np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).flat)
40864090
assert_array_equal(x[9:].flat, 0)
@@ -4093,7 +4097,10 @@ def test_check_reference(self):
40934097

40944098
def test_int_shape(self):
40954099
x = np.eye(3)
4096-
x.resize(3)
4100+
if IS_PYPY:
4101+
x.resize(3, refcheck=False)
4102+
else:
4103+
x.resize(3)
40974104
assert_array_equal(x, np.eye(3)[0,:])
40984105

40994106
def test_none_shape(self):
@@ -4111,19 +4118,28 @@ def test_invalid_arguements(self):
41114118

41124119
def test_freeform_shape(self):
41134120
x = np.eye(3)
4114-
x.resize(3, 2, 1)
4121+
if IS_PYPY:
4122+
x.resize(3, 2, 1, refcheck=False)
4123+
else:
4124+
x.resize(3, 2, 1)
41154125
assert_(x.shape == (3, 2, 1))
41164126

41174127
def test_zeros_appended(self):
41184128
x = np.eye(3)
4119-
x.resize(2, 3, 3)
4129+
if IS_PYPY:
4130+
x.resize(2, 3, 3, refcheck=False)
4131+
else:
4132+
x.resize(2, 3, 3)
41204133
assert_array_equal(x[0], np.eye(3))
41214134
assert_array_equal(x[1], np.zeros((3, 3)))
41224135

41234136
def test_obj_obj(self):
41244137
# check memory is initialized on resize, gh-4857
41254138
a = np.ones(10, dtype=[('k', object, 2)])
4126-
a.resize(15,)
4139+
if IS_PYPY:
4140+
a.resize(15, refcheck=False)
4141+
else:
4142+
a.resize(15,)
41274143
assert_equal(a.shape, (15,))
41284144
assert_array_equal(a['k'][-5:], 0)
41294145
assert_array_equal(a['k'][:-5], 1)
@@ -5988,7 +6004,7 @@ def test_reference_leak(self):
59886004
c = np.asarray(b)
59896005
if HAS_REFCOUNT:
59906006
count_2 = sys.getrefcount(np.core._internal)
5991-
assert_equal(count_1, count_2)
6007+
assert_equal(count_1, count_2)
59926008
del c # avoid pyflakes unused variable warning.
59936009

59946010
def test_padded_struct_array(self):
@@ -6141,6 +6157,7 @@ def test_mem_seteventhook(self):
61416157
# needs to be larger then limit of small memory cacher in ctors.c
61426158
a = np.zeros(1000)
61436159
del a
6160+
gc.collect()
61446161
< FCF2 span class=pl-en>test_pydatamem_seteventhook_end()
61456162

61466163
class TestMapIter(TestCase):

numpy/core/tests/test_regression.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import numpy as np
1515
from numpy.testing import (
16-
run_module_suite, TestCase, assert_, assert_equal,
16+
run_module_suite, TestCase, assert_, assert_equal, IS_PYPY,
1717
assert_almost_equal, assert_array_equal, assert_array_almost_equal,
1818
assert_raises, assert_warns, dec, suppress_warnings
1919
)
@@ -1293,9 +1293,15 @@ def test_blasdot_uninitialized_memory(self):
12931293
for k in range(3):
12941294
# Try to ensure that x->data contains non-zero floats
12951295
x = np.array([123456789e199], dtype=np.float64)
1296-
x.resize((m, 0))
1296+
if IS_PYPY:
1297+
x.resize((m, 0), refcheck=False)
1298+
else:
1299+
x.resize((m, 0))
12971300
y = np.array([123456789e199], dtype=np.float64)
1298-
y.resize((0, n))
1301+
if IS_PYPY:
1302+
y.resize((0, n),refcheck=False)
1303+
else:
1304+
y.resize((0, n))
12991305

13001306
# `dot` should just return zero (m,n) matrix
13011307
z = np.dot(x, y)

0 commit comments

Comments
 (0)
0