Author: Ronan Lamy <[email protected]>
Branch:
Changeset: r91411:3855d4baa148
Date: 2017-05-25 20:20 +0100
http://bitbucket.org/pypy/pypy/changeset/3855d4baa148/
Log: Fix space.bufferstr_w() returning a wrong value for numpy arrays and
add an indirect test for this
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -389,6 +389,9 @@
for i in range(len(string)):
self.ptr[start + i] = string[i]
+ def as_str(self):
+ return CBuffer(self).as_str()
+
def as_readbuf(self):
return CBuffer(self)
diff --git a/pypy/module/cpyext/test/array.c b/pypy/module/cpyext/test/array.c
--- a/pypy/module/cpyext/test/array.c
+++ b/pypy/module/cpyext/test/array.c
@@ -2011,9 +2011,19 @@
static int
array_getbuffer(PyObject* obj, Py_buffer* view, int flags)
{
+ int ret;
arrayobject* self = (arrayobject*)obj;
- return PyBuffer_FillInfo(view, obj, self->ob_item,
+ ret = PyBuffer_FillInfo(view, obj, self->ob_item,
Py_SIZE(self)*self->ob_descr->itemsize, 0, flags);
+ if (ret < 0) {
+ return ret;
+ }
+ if ((flags & PyBUF_ND) != PyBUF_ND) {
+ // numpy effectively does this
+ view->ndim = 0;
+ view->shape = NULL;
+ }
+ return ret;
}
static long releasebuffer_cnt = 0;
diff --git a/pypy/module/cpyext/test/test_arraymodule.py
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -34,27 +34,27 @@
def test_index(self):
module = self.import_module(name='array')
- arr = module.array('i', [1,2,3,4])
+ arr = module.array('i', [1, 2, 3, 4])
assert arr[3] == 4
raises(IndexError, arr.__getitem__, 10)
del arr[2]
- assert arr.tolist() == [1,2,4]
+ assert arr.tolist() == [1, 2, 4]
arr[2] = 99
- assert arr.tolist() == [1,2,99]
+ assert arr.tolist() == [1, 2, 99]
def test_slice_get(self):
module = self.import_module(name='array')
- arr = module.array('i', [1,2,3,4])
- assert arr[:].tolist() == [1,2,3,4]
- assert arr[1:].tolist() == [2,3,4]
- assert arr[:2].tolist() == [1,2]
- assert arr[1:3].tolist() == [2,3]
+ arr = module.array('i', [1, 2, 3, 4])
+ assert arr[:].tolist() == [1, 2, 3, 4]
+ assert arr[1:].tolist() == [2, 3, 4]
+ assert arr[:2].tolist() == [1, 2]
+ assert arr[1:3].tolist() == [2, 3]
def test_slice_object(self):
module = self.import_module(name='array')
- arr = module.array('i', [1,2,3,4])
- assert arr[slice(1,3)].tolist() == [2,3]
- arr[slice(1,3)] = module.array('i', [21, 22, 23])
+ arr = module.array('i', [1, 2, 3, 4])
+ assert arr[slice(1, 3)].tolist() == [2,3]
+ arr[slice(1, 3)] = module.array('i', [21, 22, 23])
assert arr.tolist() == [1, 21, 22, 23, 4]
del arr[slice(1, 3)]
assert arr.tolist() == [1, 23, 4]
@@ -63,20 +63,16 @@
def test_buffer(self):
import sys
module = self.import_module(name='array')
- arr = module.array('i', [1,2,3,4])
+ arr = module.array('i', [1, 2, 3, 4])
buf = buffer(arr)
exc = raises(TypeError, "buf[1] = '1'")
assert str(exc.value) == "buffer is read-only"
if sys.byteorder == 'big':
- assert str(buf) == ('\0\0\0\x01'
- '\0\0\0\x02'
- '\0\0\0\x03'
- '\0\0\0\x04')
+ expected = '\0\0\0\x01' '\0\0\0\x02' '\0\0\0\x03' '\0\0\0\x04'
else:
- assert str(buf) == ('\x01\0\0\0'
- '\x02\0\0\0'
- '\x03\0\0\0'
- '\x04\0\0\0')
+ expected = '\x01\0\0\0' '\x02\0\0\0' '\x03\0\0\0' '\x04\0\0\0'
+ assert str(buf) == expected
+ assert str(buffer('') + arr) == expected
def test_releasebuffer(self):
module = self.import_module(name='array')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit