| 1 |
import array |
| 2 |
import gc |
| 3 |
import unittest |
| 4 |
from ctypes import (Structure, Union, Array, sizeof, |
| 5 |
_Pointer, _SimpleCData, _CFuncPtr, |
| 6 |
c_char, c_int) |
| 7 |
|
| 8 |
|
| 9 |
class X(Structure): |
| 10 |
_fields_ = [("c_int", c_int)] |
| 11 |
init_called = False |
| 12 |
def __init__(self): |
| 13 |
self._init_called = True |
| 14 |
|
| 15 |
|
| 16 |
class Test(unittest.TestCase): |
| 17 |
def test_from_buffer(self): |
| 18 |
a = array.array("i", range(16)) |
| 19 |
x = (c_int * 16).from_buffer(a) |
| 20 |
|
| 21 |
y = X.from_buffer(a) |
| 22 |
self.assertEqual(y.c_int, a[0]) |
| 23 |
self.assertFalse(y.init_called) |
| 24 |
|
| 25 |
self.assertEqual(x[:], a.tolist()) |
| 26 |
|
| 27 |
a[0], a[-1] = 200, -200 |
| 28 |
self.assertEqual(x[:], a.tolist()) |
| 29 |
|
| 30 |
self.assertRaises(BufferError, a.append, 100) |
| 31 |
self.assertRaises(BufferError, a.pop) |
| 32 |
|
| 33 |
del x; del y; gc.collect(); gc.collect(); gc.collect() |
| 34 |
a.append(100) |
| 35 |
a.pop() |
| 36 |
x = (c_int * 16).from_buffer(a) |
| 37 |
|
| 38 |
self.assertIn(a, [obj.obj if isinstance(obj, memoryview) else obj |
| 39 |
for obj in x._objects.values()]) |
| 40 |
|
| 41 |
expected = x[:] |
| 42 |
del a; gc.collect(); gc.collect(); gc.collect() |
| 43 |
self.assertEqual(x[:], expected) |
| 44 |
|
| 45 |
with self.assertRaisesRegex(TypeError, "not writable"): |
| 46 |
(c_char * 16).from_buffer(b"a" * 16) |
| 47 |
with self.assertRaisesRegex(TypeError, "not writable"): |
| 48 |
(c_char * 16).from_buffer(memoryview(b"a" * 16)) |
| 49 |
with self.assertRaisesRegex(TypeError, "not C contiguous"): |
| 50 |
(c_char * 16).from_buffer(memoryview(bytearray(b"a" * 16))[::-1]) |
| 51 |
msg = "bytes-like object is required" |
| 52 |
with self.assertRaisesRegex(TypeError, msg): |
| 53 |
(c_char * 16).from_buffer("a" * 16) |
| 54 |
|
| 55 |
def test_fortran_contiguous(self): |
| 56 |
try: |
| 57 |
import _testbuffer |
| 58 |
except ImportError as err: |
| 59 |
self.skipTest(str(err)) |
| 60 |
flags = _testbuffer.ND_WRITABLE | _testbuffer.ND_FORTRAN |
| 61 |
array = _testbuffer.ndarray( |
| 62 |
[97] * 16, format="B", shape=[4, 4], flags=flags) |
| 63 |
with self.assertRaisesRegex(TypeError, "not C contiguous"): |
| 64 |
(c_char * 16).from_buffer(array) |
| 65 |
array = memoryview(array) |
| 66 |
self.assertTrue(array.f_contiguous) |
| 67 |
self.assertFalse(array.c_contiguous) |
| 68 |
with self.assertRaisesRegex(TypeError, "not C contiguous"): |
| 69 |
(c_char * 16).from_buffer(array) |
| 70 |
|
| 71 |
def test_from_buffer_with_offset(self): |
| 72 |
a = array.array("i", range(16)) |
| 73 |
x = (c_int * 15).from_buffer(a, sizeof(c_int)) |
| 74 |
|
| 75 |
self.assertEqual(x[:], a.tolist()[1:]) |
| 76 |
with self.assertRaises(ValueError): |
| 77 |
c_int.from_buffer(a, -1) |
| 78 |
with self.assertRaises(ValueError): |
| 79 |
(c_int * 16).from_buffer(a, sizeof(c_int)) |
| 80 |
with self.assertRaises(ValueError): |
| 81 |
(c_int * 1).from_buffer(a, 16 * sizeof(c_int)) |
| 82 |
|
| 83 |
def test_from_buffer_memoryview(self): |
| 84 |
a = [c_char.from_buffer(memoryview(bytearray(b'a')))] |
| 85 |
a.append(a) |
| 86 |
del a |
| 87 |
gc.collect() # Should not crash |
| 88 |
|
| 89 |
def test_from_buffer_copy(self): |
| 90 |
a = array.array("i", range(16)) |
| 91 |
x = (c_int * 16).from_buffer_copy(a) |
| 92 |
|
| 93 |
y = X.from_buffer_copy(a) |
| 94 |
self.assertEqual(y.c_int, a[0]) |
| 95 |
self.assertFalse(y.init_called) |
| 96 |
|
| 97 |
self.assertEqual(x[:], list(range(16))) |
| 98 |
|
| 99 |
a[0], a[-1] = 200, -200 |
| 100 |
self.assertEqual(x[:], list(range(16))) |
| 101 |
|
| 102 |
a.append(100) |
| 103 |
self.assertEqual(x[:], list(range(16))) |
| 104 |
|
| 105 |
self.assertEqual(x._objects, None) |
| 106 |
|
| 107 |
del a; gc.collect(); gc.collect(); gc.collect() |
| 108 |
self.assertEqual(x[:], list(range(16))) |
| 109 |
|
| 110 |
x = (c_char * 16).from_buffer_copy(b"a" * 16) |
| 111 |
self.assertEqual(x[:], b"a" * 16) |
| 112 |
with self.assertRaises(TypeError): |
| 113 |
(c_char * 16).from_buffer_copy("a" * 16) |
| 114 |
|
| 115 |
def test_from_buffer_copy_with_offset(self): |
| 116 |
a = array.array("i", range(16)) |
| 117 |
x = (c_int * 15).from_buffer_copy(a, sizeof(c_int)) |
| 118 |
|
| 119 |
self.assertEqual(x[:], a.tolist()[1:]) |
| 120 |
with self.assertRaises(ValueError): |
| 121 |
c_int.from_buffer_copy(a, -1) |
| 122 |
with self.assertRaises(ValueError): |
| 123 |
(c_int * 16).from_buffer_copy(a, sizeof(c_int)) |
| 124 |
with self.assertRaises(ValueError): |
| 125 |
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int)) |
| 126 |
|
| 127 |
def test_abstract(self): |
| 128 |
self.assertRaises(TypeError, Array.from_buffer, bytearray(10)) |
| 129 |
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10)) |
| 130 |
self.assertRaises(TypeError, Union.from_buffer, bytearray(10)) |
| 131 |
self.assertRaises(TypeError, _CFuncPtr.from_buffer, bytearray(10)) |
| 132 |
self.assertRaises(TypeError, _Pointer.from_buffer, bytearray(10)) |
| 133 |
self.assertRaises(TypeError, _SimpleCData.from_buffer, bytearray(10)) |
| 134 |
|
| 135 |
self.assertRaises(TypeError, Array.from_buffer_copy, b"123") |
| 136 |
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123") |
| 137 |
self.assertRaises(TypeError, Union.from_buffer_copy, b"123") |
| 138 |
self.assertRaises(TypeError, _CFuncPtr.from_buffer_copy, b"123") |
| 139 |
self.assertRaises(TypeError, _Pointer.from_buffer_copy, b"123") |
| 140 |
self.assertRaises(TypeError, _SimpleCData.from_buffer_copy, b"123") |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == '__main__': |
| 144 |
unittest.main() |