ยปCore Development>Code coverage>Lib/ctypes/test/test_frombuffer.py

Python code coverage for Lib/ctypes/test/test_frombuffer.py

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