diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 14 |
2 files changed, 20 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index e53177519..a4c92b2a1 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1481,7 +1481,13 @@ array_setstate(PyArrayObject *self, PyObject *args) if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) { int swap=!PyArray_ISNOTSWAPPED(self); self->data = datastr; +#ifndef NPY_PY3K + /* Check that the string is not interned */ + if (!_IsAligned(self) || swap || PyString_CHECK_INTERNED(rawdata)) { +#else + /* Bytes are never interned */ if (!_IsAligned(self) || swap) { +#endif intp num = PyArray_NBYTES(self); self->data = PyDataMem_NEW(num); if (self->data == NULL) { diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index bcb34d04e..867df6c9d 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1580,5 +1580,19 @@ class TestRegression(TestCase): y = np.array(x.flat) assert_equal(x, [[1,3],[2,4]]) + def test_pickle_string_overwrite(self): + import re + + data = np.array([1], dtype='b') + blob = pickle.dumps(data, protocol=1) + data = pickle.loads(blob) + + # Check that loads does not clobber interned strings + s = re.sub("a(.)", "\x01\\1", "a_") + assert_equal(s[0], "\x01") + data[0] = 0xbb + s = re.sub("a(.)", "\x01\\1", "a_") + assert_equal(s[0], "\x01") + if __name__ == "__main__": run_module_suite() |