diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-01-29 10:12:19 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-01-29 10:12:19 (GMT) |
commit | 34360c8e09f51075fec0c673b2674d20bed33bf5 (patch) | |
tree | 9751e4dd731fbe62cbea636b83b1a9551b4712d9 /Lib/ctypes | |
parent | 22532ac2eb4440ff40ceefe510069a4927ab63ee (diff) | |
download | cpython-34360c8e09f51075fec0c673b2674d20bed33bf5.zip cpython-34360c8e09f51075fec0c673b2674d20bed33bf5.tar.gz cpython-34360c8e09f51075fec0c673b2674d20bed33bf5.tar.bz2 |
Issue #19023: Document ctypes array and pointer classes
Also add some more tests. Based on patch by Sye van der Veen.
Diffstat (limited to 'Lib/ctypes')
-rw-r--r-- | Lib/ctypes/test/test_arrays.py | 12 | ||||
-rw-r--r-- | Lib/ctypes/test/test_pointers.py | 9 |
2 files changed, 15 insertions, 6 deletions
diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py index 8ca77e0..4ed566b 100644 --- a/Lib/ctypes/test/test_arrays.py +++ b/Lib/ctypes/test/test_arrays.py @@ -24,20 +24,24 @@ class ArrayTestCase(unittest.TestCase): self.assertEqual(len(ia), alen) # slot values ok? - values = [ia[i] for i in range(len(init))] + values = [ia[i] for i in range(alen)] self.assertEqual(values, init) + # out-of-bounds accesses should be caught + with self.assertRaises(IndexError): ia[alen] + with self.assertRaises(IndexError): ia[-alen-1] + # change the items from operator import setitem new_values = list(range(42, 42+alen)) [setitem(ia, n, new_values[n]) for n in range(alen)] - values = [ia[i] for i in range(len(init))] + values = [ia[i] for i in range(alen)] self.assertEqual(values, new_values) # are the items initialized to 0? ia = int_array() - values = [ia[i] for i in range(len(init))] - self.assertEqual(values, [0] * len(init)) + values = [ia[i] for i in range(alen)] + self.assertEqual(values, [0] * alen) # Too many initializers should be caught self.assertRaises(IndexError, int_array, *range(alen*2)) diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py index 225b9d1..751f85f 100644 --- a/Lib/ctypes/test/test_pointers.py +++ b/Lib/ctypes/test/test_pointers.py @@ -56,9 +56,13 @@ class PointersTestCase(unittest.TestCase): # C code: # int x = 12321; # res = &x - res.contents = c_int(12321) + x = c_int(12321) + res.contents = x self.assertEqual(i.value, 54345) + x.value = -99 + self.assertEqual(res.contents.value, -99) + def test_callbacks_with_pointers(self): # a function type receiving a pointer PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int)) @@ -131,9 +135,10 @@ class PointersTestCase(unittest.TestCase): def test_basic(self): p = pointer(c_int(42)) - # Although a pointer can be indexed, it ha no length + # Although a pointer can be indexed, it has no length self.assertRaises(TypeError, len, p) self.assertEqual(p[0], 42) + self.assertEqual(p[0:1], [42]) self.assertEqual(p.contents.value, 42) def test_charpp(self): |