|
1 | 1 | import unittest
|
2 |
| -from ctypes import create_string_buffer, sizeof, byref, c_char, c_wchar |
| 2 | +from ctypes import (create_string_buffer, create_unicode_buffer, |
| 3 | + sizeof, byref, c_char, c_wchar) |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class StringArrayTestCase(unittest.TestCase):
|
@@ -88,41 +89,35 @@ def test_wchar(self):
|
88 | 89 | repr(byref(c_wchar("x")))
|
89 | 90 | c_wchar("x")
|
90 | 91 |
|
91 |
| - @unittest.skip('test disabled') |
92 | 92 | def test_basic_wstrings(self):
|
93 |
| - cs = c_wstring("abcdef") |
94 |
| - |
95 |
| - # XXX This behaviour is about to change: |
96 |
| - # len returns the size of the internal buffer in bytes. |
97 |
| - # This includes the terminating NUL character. |
98 |
| - self.assertEqual(sizeof(cs), 14) |
99 |
| - |
100 |
| - # The value property is the string up to the first terminating NUL. |
| 93 | + cs = create_unicode_buffer("abcdef") |
101 | 94 | self.assertEqual(cs.value, "abcdef")
|
102 |
| - self.assertEqual(c_wstring("abc\000def").value, "abc") |
103 | 95 |
|
104 |
| - self.assertEqual(c_wstring("abc\000def").value, "abc") |
| 96 | + # value can be changed |
| 97 | + cs.value = "abc" |
| 98 | + self.assertEqual(cs.value, "abc") |
| 99 | + |
| 100 | + # string is truncated at NUL character |
| 101 | + cs.value = "def\0z" |
| 102 | + self.assertEqual(cs.value, "def") |
105 | 103 |
|
106 |
| - # The raw property is the total buffer contents: |
107 |
| - self.assertEqual(cs.raw, "abcdef\000") |
108 |
| - self.assertEqual(c_wstring("abc\000def").raw, "abc\000def\000") |
| 104 | + self.assertEqual(create_unicode_buffer("abc\0def").value, "abc") |
109 | 105 |
|
110 |
| - # We can change the value: |
111 |
| - cs.value = "ab" |
112 |
| - self.assertEqual(cs.value, "ab") |
113 |
| - self.assertEqual(cs.raw, "ab\000\000\000\000\000") |
| 106 | + # created with an empty string |
| 107 | + cs = create_unicode_buffer(3) |
| 108 | + self.assertEqual(cs.value, "") |
114 | 109 |
|
115 |
| - self.assertRaises(TypeError, c_wstring, "123") |
116 |
| - self.assertRaises(ValueError, c_wstring, 0) |
| 110 | + cs.value = "abc" |
| 111 | + self.assertEqual(cs.value, "abc") |
117 | 112 |
|
118 |
| - @unittest.skip('test disabled') |
119 | 113 | def test_toolong(self):
|
120 |
| - cs = c_wstring("abcdef") |
121 |
| - # Much too long string: |
122 |
| - self.assertRaises(ValueError, setattr, cs, "value", "123456789012345") |
| 114 | + cs = create_unicode_buffer("abc") |
| 115 | + with self.assertRaises(ValueError): |
| 116 | + cs.value = "abcdef" |
123 | 117 |
|
124 |
| - # One char too long values: |
125 |
| - self.assertRaises(ValueError, setattr, cs, "value", "1234567") |
| 118 | + cs = create_unicode_buffer(4) |
| 119 | + with self.assertRaises(ValueError): |
| 120 | + cs.value = "abcdef" |
126 | 121 |
|
127 | 122 |
|
128 | 123 | def run_test(rep, msg, func, arg):
|
|
0 commit comments