Debian Codegraph @

Lib/test/test_ctypes/test_unicode.py

raw
1 import ctypes
2 import unittest
3 from test.support import import_helper
4 _ctypes_test = import_helper.import_module("_ctypes_test")
5
6
7 class UnicodeTestCase(unittest.TestCase):
8 def test_wcslen(self):
9 dll = ctypes.CDLL(_ctypes_test.__file__)
10 wcslen = dll.my_wcslen
11 wcslen.argtypes = [ctypes.c_wchar_p]
12
13 self.assertEqual(wcslen("abc"), 3)
14 self.assertEqual(wcslen("ab\u2070"), 3)
15 self.assertRaises(ctypes.ArgumentError, wcslen, b"ab\xe4")
16
17 def test_buffers(self):
18 buf = ctypes.create_unicode_buffer("abc")
19 self.assertEqual(len(buf), 3+1)
20
21 buf = ctypes.create_unicode_buffer("ab\xe4\xf6\xfc")
22 self.assertEqual(buf[:], "ab\xe4\xf6\xfc\0")
23 self.assertEqual(buf[::], "ab\xe4\xf6\xfc\0")
24 self.assertEqual(buf[::-1], '\x00\xfc\xf6\xe4ba')
25 self.assertEqual(buf[::2], 'a\xe4\xfc')
26 self.assertEqual(buf[6:5:-1], "")
27
28 def test_embedded_null(self):
29 class TestStruct(ctypes.Structure):
30 _fields_ = [("unicode", ctypes.c_wchar_p)]
31 t = TestStruct()
32 # This would raise a ValueError:
33 t.unicode = "foo\0bar\0\0"
34
35
36 func = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
37
38 class StringTestCase(UnicodeTestCase):
39 def setUp(self):
40 func.argtypes = [ctypes.c_char_p]
41 func.restype = ctypes.c_char_p
42
43 def tearDown(self):
44 func.argtypes = None
45 func.restype = ctypes.c_int
46
47 def test_func(self):
48 self.assertEqual(func(b"abc\xe4"), b"abc\xe4")
49
50 def test_buffers(self):
51 buf = ctypes.create_string_buffer(b"abc")
52 self.assertEqual(len(buf), 3+1)
53
54 buf = ctypes.create_string_buffer(b"ab\xe4\xf6\xfc")
55 self.assertEqual(buf[:], b"ab\xe4\xf6\xfc\0")
56 self.assertEqual(buf[::], b"ab\xe4\xf6\xfc\0")
57 self.assertEqual(buf[::-1], b'\x00\xfc\xf6\xe4ba')
58 self.assertEqual(buf[::2], b'a\xe4\xfc')
59 self.assertEqual(buf[6:5:-1], b"")
60
61
62 if __name__ == '__main__':
63 unittest.main()