summaryrefslogtreecommitdiff
path: root/numpy/lib/twodim_base.py
diff options
context:
space:
mode:
authorNathaniel J. Smith <njs@pobox.com>2012-05-15 22:12:41 +0100
committerNathaniel J. Smith <njs@pobox.com>2012-05-16 14:11:28 +0100
commitbea52bf307782b2a211b7fcfa6696fad45dae275 (patch)
tree435834d34ff9dd7a12f5acc62b45cbf77d623453 /numpy/lib/twodim_base.py
parentd403fed2423caec4149937fe48781ac68b21fddb (diff)
downloadnumpy-bea52bf307782b2a211b7fcfa6696fad45dae275.tar.gz
Transition scheme for allowing PyArray_Diagonal to return a view
PyArray_Diagonal is changed to return a copy of the diagonal (as in numpy 1.6 and earlier), but with a new (hidden) WARN_ON_WRITE flag set. Writes to this array (or views thereof) will continue to work as normal, but the first write will trigger a DeprecationWarning. We also issue this warning if someone extracts a non-numpy writeable view of the array (e.g., by accessing the Python-level .data attribute). There are likely still places where the data buffer is exposed that I've missed -- review welcome! New known-fail test: eye() for maskna arrays was only implemented by exploiting ndarray.diagonal's view-ness, so it is now unimplemented again, and the corresponding test is marked known-fail.
Diffstat (limited to 'numpy/lib/twodim_base.py')
-rw-r--r--numpy/lib/twodim_base.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index 58d8250a1..2b518aeae 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -210,7 +210,13 @@ def eye(N, M=None, k=0, dtype=float, maskna=False):
if M is None:
M = N
m = zeros((N, M), dtype=dtype, maskna=maskna)
- diagonal(m, k)[...] = 1
+ if k >= M:
+ return m
+ if k >= 0:
+ i = k
+ else:
+ i = (-k) * M
+ m[:M-k].flat[i::M+1] = 1
return m
def diag(v, k=0):