diff options
author | Travis E. Oliphant <teoliphant@gmail.com> | 2012-08-02 20:52:39 -0700 |
---|---|---|
committer | Travis E. Oliphant <teoliphant@gmail.com> | 2012-08-02 20:52:39 -0700 |
commit | fd15162fbff5dd68c548284947d39bb2a2481183 (patch) | |
tree | ec083e03053183ccd970580ce10ac55194b43044 /numpy | |
parent | 26fed25e7f040564e7be4f82d7cd8a8fcc8fa287 (diff) | |
parent | d1839283a2b0b393096cf6bda9b386ea5809d4b0 (diff) | |
download | numpy-fd15162fbff5dd68c548284947d39bb2a2481183.tar.gz |
Merge pull request #371 from rlamy/bytes-pickle
Copy bytes object for small arrays when unpickling an array
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 5 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 8 |
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) |