| 1 |
import pytest |
| 2 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 3 |
from pypy.conftest import option |
| 4 |
|
| 5 |
class AppTestArrayModule(AppTestCpythonExtensionBase): |
| 6 |
enable_leak_checking = True |
| 7 |
|
| 8 |
def test_basic(self): |
| 9 |
module = self.import_module(name='array') |
| 10 |
import sys |
| 11 |
arr = module.array('i', [1,2,3]) |
| 12 |
assert arr.typecode == 'i' |
| 13 |
assert arr.itemsize == 4 |
| 14 |
assert arr[2] == 3 |
| 15 |
assert len(arr.buffer_info()) == 2 |
| 16 |
exc = raises(TypeError, module.array.append) |
| 17 |
errstr = str(exc.value) |
| 18 |
if sys.implementation.name == 'pypy': |
| 19 |
assert errstr.startswith("descriptor 'append' of 'array' object"), errstr |
| 20 |
else: |
| 21 |
assert errstr.startswith("unbound method array.append"), errstr |
| 22 |
arr.append(4) |
| 23 |
assert arr.tolist() == [1, 2, 3, 4] |
| 24 |
assert len(arr) == 4 |
| 25 |
|
| 26 |
def test_iter(self): |
| 27 |
module = self.import_module(name='array') |
| 28 |
arr = module.array('i', [1,2,3]) |
| 29 |
sum = 0 |
| 30 |
for i in arr: |
| 31 |
sum += i |
| 32 |
assert sum == 6 |
| 33 |
|
| 34 |
def test_index(self): |
| 35 |
module = self.import_module(name='array') |
| 36 |
arr = module.array('i', [1, 2, 3, 4]) |
| 37 |
assert arr[3] == 4 |
| 38 |
raises(IndexError, arr.__getitem__, 10) |
| 39 |
del arr[2] |
| 40 |
assert arr.tolist() == [1, 2, 4] |
| 41 |
arr[2] = 99 |
| 42 |
assert arr.tolist() == [1, 2, 99] |
| 43 |
|
| 44 |
def test_slice_get(self): |
| 45 |
module = self.import_module(name='array') |
| 46 |
arr = module.array('i', [1, 2, 3, 4]) |
| 47 |
assert arr[:].tolist() == [1, 2, 3, 4] |
| 48 |
assert arr[1:].tolist() == [2, 3, 4] |
| 49 |
assert arr[:2].tolist() == [1, 2] |
| 50 |
assert arr[1:3].tolist() == [2, 3] |
| 51 |
|
| 52 |
def test_slice_object(self): |
| 53 |
module = self.import_module(name='array') |
| 54 |
arr = module.array('i', [1, 2, 3, 4]) |
| 55 |
assert arr[slice(1, 3)].tolist() == [2,3] |
| 56 |
arr[slice(1, 3)] = module.array('i', [21, 22, 23]) |
| 57 |
assert arr.tolist() == [1, 21, 22, 23, 4] |
| 58 |
del arr[slice(1, 3)] |
| 59 |
assert arr.tolist() == [1, 23, 4] |
| 60 |
raises(TypeError, 'arr[slice(1, 3)] = "abc"') |
| 61 |
|
| 62 |
def test_buffer(self): |
| 63 |
import sys |
| 64 |
module = self.import_module(name='array') |
| 65 |
arr = module.array('i', [1, 2, 3, 4]) |
| 66 |
buf = memoryview(arr) |
| 67 |
buf[1] = 2 |
| 68 |
if sys.byteorder == 'big': |
| 69 |
expected = b'\0\0\0\x01\0\0\0\x02\0\0\0\x03\0\0\0\x04' |
| 70 |
else: |
| 71 |
expected = b'\x01\0\0\0\x02\0\0\0\x03\0\0\0\x04\0\0\0' |
| 72 |
assert bytes(buf) == expected |
| 73 |
|
| 74 |
buf2 = module.passthrough(buf) |
| 75 |
assert str(buf2) == str(buf) |
| 76 |
|
| 77 |
def test_releasebuffer(self): |
| 78 |
module = self.import_module(name='array') |
| 79 |
arr = module.array('i', [1,2,3,4]) |
| 80 |
initial = module.get_releasebuffer_cnt() |
| 81 |
module.create_and_release_buffer(arr) |
| 82 |
assert module.get_releasebuffer_cnt() == initial + 1 |
| 83 |
|
| 84 |
def test_Py_buffer(self): |
| 85 |
module = self.import_module(name='array') |
| 86 |
arr = module.array('i', [1,2,3,4]) |
| 87 |
initial = module.get_releasebuffer_cnt() |
| 88 |
m = memoryview(arr) |
| 89 |
assert module.get_releasebuffer_cnt() == initial |
| 90 |
del m |
| 91 |
self.debug_collect() |
| 92 |
assert module.get_releasebuffer_cnt() == initial + 1 |
| 93 |
|
| 94 |
def test_0d_view(self): |
| 95 |
module = self.import_module(name='array') |
| 96 |
arr = module.array('B', b'\0\0\0\x01') |
| 97 |
buf = memoryview(arr).cast('i', shape=()) |
| 98 |
assert bytes(buf) == b'\0\0\0\x01' |
| 99 |
assert buf.shape == () |
| 100 |
assert buf.strides == () |
| 101 |
|
| 102 |
def test_binop_mul_impl(self): |
| 103 |
|
| 104 |
module = self.import_module(name='array') |
| 105 |
arr = module.array('i', [2]) |
| 106 |
res = [1, 2, 3] * arr |
| 107 |
assert res == [1, 2, 3, 1, 2, 3] |
| 108 |
|
| 109 |
@pytest.mark.xfail |
| 110 |
def test_subclass_dealloc(self): |
| 111 |
module = self.import_module(name='array') |
| 112 |
class Sub(module.array): |
| 113 |
pass |
| 114 |
|
| 115 |
arr = Sub('i', [2]) |
| 116 |
module.readbuffer_as_string(arr) |
| 117 |
class A(object): |
| 118 |
pass |
| 119 |
assert not module.same_dealloc(arr, module.array('i', [2])) |
| 120 |
assert module.same_dealloc(arr, A()) |
| 121 |
|
| 122 |
def test_string_buf(self): |
| 123 |
module = self.import_module(name='array') |
| 124 |
import sys |
| 125 |
arr = module.array('u', '123') |
| 126 |
view = memoryview(arr) |
| 127 |
print("view.shape", view.shape) |
| 128 |
print("view.strides", view.strides) |
| 129 |
isize = view.itemsize |
| 130 |
if sys.platform == 'win32': |
| 131 |
assert isize == 2 |
| 132 |
else: |
| 133 |
assert isize == 4 |
| 134 |
assert module.write_buffer_len(arr) == isize * 3 |
| 135 |
assert len(module.readbuffer_as_string(arr)) == isize * 3 |
| 136 |
assert len(module.readbuffer_as_string(view)) == isize * 3 |
| 137 |
|
| 138 |
def test_subclass(self): |
| 139 |
import struct |
| 140 |
module = self.import_module(name='array') |
| 141 |
class Sub(module.array): |
| 142 |
pass |
| 143 |
|
| 144 |
arr = Sub('i', [2]) |
| 145 |
res = [1, 2, 3] * arr |
| 146 |
assert res == [1, 2, 3, 1, 2, 3] |
| 147 |
|
| 148 |
val = module.readbuffer_as_string(arr) |
| 149 |
assert val == struct.pack('i', 2) |
| 150 |
|
| 151 |
def test_unicode_readbuffer(self): |
| 152 |
|
| 153 |
import struct |
| 154 |
module = self.import_module(name='array') |
| 155 |
val = module.readbuffer_as_string(b'abcd') |
| 156 |
assert val == b'abcd' |
| 157 |
|
| 158 |
def test_readinto(self): |
| 159 |
module = self.import_module(name='array') |
| 160 |
a = module.array('B') |
| 161 |
a.fromstring(b'0123456789') |
| 162 |
filename = self.udir + "/_test_file" |
| 163 |
f = open(filename, 'w+b') |
| 164 |
f.write(b'foobar') |
| 165 |
f.seek(0) |
| 166 |
n = f.readinto(a) |
| 167 |
f.close() |
| 168 |
assert n == 6 |
| 169 |
assert len(a) == 10 |
| 170 |
assert a.tostring() == b'foobar6789' |
| 171 |
|
| 172 |
def test_iowrite(self): |
| 173 |
module = self.import_module(name='array') |
| 174 |
from io import BytesIO |
| 175 |
a = module.array('B') |
| 176 |
a.fromstring(b'0123456789') |
| 177 |
fd = BytesIO() |
| 178 |
|
| 179 |
fd.write(a) |
| 180 |
|
| 181 |
def test_getitem_via_PySequence_GetItem(self): |
| 182 |
module = self.import_module(name='array') |
| 183 |
a = module.array('i', range(10)) |
| 184 |
|
| 185 |
assert 5 == a[-5] |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
raises(IndexError, module.getitem, a, -5) |
| 191 |
|
| 192 |
def test_subclass_with_attribute(self): |
| 193 |
module = self.import_module(name='array') |
| 194 |
class Sub(module.array): |
| 195 |
def addattrib(self): |
| 196 |
print('called addattrib') |
| 197 |
self.attrib = True |
| 198 |
import gc |
| 199 |
module.subclass_with_attribute(Sub, "addattrib", "attrib", gc.collect) |
| 200 |
assert Sub.__module__ == __name__ |