[Numpy-discussion] documentation suggestion

Pauli Virtanen pav at iki.fi
Mon Apr 13 13:17:26 EDT 2009


Sun, 12 Apr 2009 17:32:31 -0400, Neal Becker wrote:
[clip]
> Done, but can someone check that what I wrote is accurate?  I wrote that
> changes to the ndarray will change the underlying buffer object.  But,
> the buffer protocol allows for read-only buffers.  Not sure what ndarray
> would do if you tried to write in that case.

For read-only buffers, the new array will not be writeable:

>>> x="abcde"
>>> z=np.ndarray((5,), buffer=x, dtype='1S')
>>> z[0] = 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: array is not writeable

However, you can override this, and misusing it potentially leads to 
nasty things:

>>> x="abcde"
>>> y="abcde"
>>> z=np.ndarray((5,), buffer=x, dtype='1S')
>>> z.flags.writeable = True
>>> z[0] = 'x'
>>> x
'xbcde'
>>> y
'xbcde'
>>> hash(x)
-1332677140
>>> hash("abcde")
-1332677140

Hashing breaks, and the assumed immutability of strings causes nonlocal 
effects!

    ***

However, for some reason, I don't see your contribution in the change 
list:

	http://docs.scipy.org/numpy/changes/

Can you check if the change you made went through? What page did you edit?

-- 
Pauli Virtanen




More information about the NumPy-Discussion mailing list