summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/methods.c5
-rw-r--r--numpy/core/tests/test_regression.py8
2 files changed, 11 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index c4147eff2..5ad4f2fd6 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -1587,8 +1587,9 @@ array_setstate(PyArrayObject *self, PyObject *args)
/* Check that the string is not interned */
if (!_IsAligned(self) || swap || PyString_CHECK_INTERNED(rawdata)) {
#else
- /* Bytes are never interned */
- if (!_IsAligned(self) || swap) {
+ /* Bytes should always be considered immutable, but we just grab the
+ * pointer if they are large, to save memory. */
+ if (!_IsAligned(self) || swap || (len <= 1000)) {
#endif
npy_intp num = PyArray_NBYTES(self);
fa->data = PyDataMem_NEW(num);
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index ac36aa479..492a4f64b 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1601,6 +1601,14 @@ class TestRegression(TestCase):
s = re.sub("a(.)", "\x01\\1", "a_")
assert_equal(s[0], "\x01")
+ def test_pickle_bytes_overwrite(self):
+ if sys.version_info[0] >= 3:
+ data = np.array([1], dtype='b')
+ data = pickle.loads(pickle.dumps(data))
+ data[0] = 0xdd
+ bytestring = "\x01 ".encode('ascii')
+ assert_equal(bytestring[0:1], '\x01'.encode('ascii'))
+
def test_structured_type_to_object(self):
a_rec = np.array([(0,1), (3,2)], dtype='i4,i8')
a_obj = np.empty((2,), dtype=object)