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