Debian Codegraph @

lib-python/3/ctypes/test/test_memfunctions.py

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