Debian Codegraph @

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

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