Debian Codegraph @

Lib/test/test_ctypes/test_memfunctions.py

raw
1 import sys
2 import unittest
3 from test import support
4 from ctypes import (POINTER, sizeof, cast,
5 create_string_buffer, string_at,
6 create_unicode_buffer, wstring_at,
7 memmove, memset,
8 c_char_p, c_byte, c_ubyte, c_wchar)
9
10
11 class MemFunctionsTest(unittest.TestCase):
12 def test_overflow(self):
13 # string_at and wstring_at must use the Python calling
14 # convention (which acquires the GIL and checks the Python
15 # error flag). Provoke an error and catch it; see also issue
16 # gh-47804.
17 self.assertRaises((OverflowError, MemoryError, SystemError),
18 lambda: wstring_at(u"foo", sys.maxsize - 1))
19 self.assertRaises((OverflowError, MemoryError, SystemError),
20 lambda: string_at("foo", sys.maxsize - 1))
21
22 def test_memmove(self):
23 # large buffers apparently increase the chance that the memory
24 # is allocated in high address space.
25 a = create_string_buffer(1000000)
26 p = b"Hello, World"
27 result = memmove(a, p, len(p))
28 self.assertEqual(a.value, b"Hello, World")
29
30 self.assertEqual(string_at(result), b"Hello, World")
31 self.assertEqual(string_at(result, 5), b"Hello")
32 self.assertEqual(string_at(result, 16), b"Hello, World\0\0\0\0")
33 self.assertEqual(string_at(result, 0), b"")
34
35 def test_memset(self):
36 a = create_string_buffer(1000000)
37 result = memset(a, ord('x'), 16)
38 self.assertEqual(a.value, b"xxxxxxxxxxxxxxxx")
39
40 self.assertEqual(string_at(result), b"xxxxxxxxxxxxxxxx")
41 self.assertEqual(string_at(a), b"xxxxxxxxxxxxxxxx")
42 self.assertEqual(string_at(a, 20), b"xxxxxxxxxxxxxxxx\0\0\0\0")
43
44 def test_cast(self):
45 a = (c_ubyte * 32)(*map(ord, "abcdef"))
46 self.assertEqual(cast(a, c_char_p).value, b"abcdef")
47 self.assertEqual(cast(a, POINTER(c_byte))[:7],
48 [97, 98, 99, 100, 101, 102, 0])
49 self.assertEqual(cast(a, POINTER(c_byte))[:7:],
50 [97, 98, 99, 100, 101, 102, 0])
51 self.assertEqual(cast(a, POINTER(c_byte))[6:-1:-1],
52 [0, 102, 101, 100, 99, 98, 97])
53 self.assertEqual(cast(a, POINTER(c_byte))[:7:2],
54 [97, 99, 101, 0])
55 self.assertEqual(cast(a, POINTER(c_byte))[:7:7],
56 [97])
57
58 @support.refcount_test
59 def test_string_at(self):
60 s = string_at(b"foo bar")
61 # XXX The following may be wrong, depending on how Python
62 # manages string instances
63 self.assertEqual(2, sys.getrefcount(s))
64 self.assertTrue(s, "foo bar")
65
66 self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
67 self.assertEqual(string_at(b"foo bar", 3), b"foo")
68
69 def test_wstring_at(self):
70 p = create_unicode_buffer("Hello, World")
71 a = create_unicode_buffer(1000000)
72 result = memmove(a, p, len(p) * sizeof(c_wchar))
73 self.assertEqual(a.value, "Hello, World")
74
75 self.assertEqual(wstring_at(a), "Hello, World")
76 self.assertEqual(wstring_at(a, 5), "Hello")
77 self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
78 self.assertEqual(wstring_at(a, 0), "")
79
80
81 if __name__ == "__main__":
82 unittest.main()