[pypy-commit] pypy default: have frombuffer actually use buffer's storage
bdkearns
noreply at buildbot.pypy.org
Sat May 24 00:39:41 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch:
Changeset: r71689:e115979b6395
Date: 2014-05-23 18:27 -0400
http://bitbucket.org/pypy/pypy/changeset/e115979b6395/
Log: have frombuffer actually use buffer's storage
diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -215,7 +215,8 @@
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
- buf = _getbuffer(space, space.getattr(w_buffer, space.wrap('__buffer__')))
+ w_buffer = space.getattr(w_buffer, space.wrap('__buffer__'))
+ buf = _getbuffer(space, w_buffer)
ts = buf.getlength()
if offset < 0 or offset > ts:
@@ -240,6 +241,13 @@
raise oefmt(space.w_ValueError,
"buffer is smaller than requested size")
- a = W_NDimArray.from_shape(space, [n], dtype=dtype)
- loop.fromstring_loop(space, a, dtype, itemsize, buf.as_str())
- return space.wrap(a)
+ try:
+ storage = buf.get_raw_address()
+ except ValueError:
+ a = W_NDimArray.from_shape(space, [n], dtype=dtype)
+ loop.fromstring_loop(space, a, dtype, itemsize, buf.as_str())
+ return a
+ else:
+ writable = not buf.readonly
+ return W_NDimArray.from_shape_and_storage(space, [n], storage, dtype=dtype,
+ w_base=w_buffer, writable=writable)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3132,6 +3132,8 @@
class AppTestSupport(BaseNumpyAppTest):
+ spaceconfig = {'usemodules': ['micronumpy', 'array']}
+
def setup_class(cls):
import struct
BaseNumpyAppTest.setup_class.im_func(cls)
@@ -3159,6 +3161,19 @@
for i in range(4):
assert a[i] == i + 1
+ import array
+ data = array.array('c', 'testing')
+ a = np.frombuffer(data, 'c')
+ assert a.base is data
+ a[2] = 'Z'
+ assert data.tostring() == 'teZting'
+
+ data = buffer(data)
+ a = np.frombuffer(data, 'c')
+ assert a.base is data
+ exc = raises(ValueError, "a[2] = 'Z'")
+ assert str(exc.value) == "assignment destination is read-only"
+
def test_fromstring(self):
import sys
from numpypy import fromstring, dtype
More information about the pypy-commit
mailing list