diff -r ed590800fde7 -r 75843d82f6cf Doc/library/ctypes.rst --- a/Doc/library/ctypes.rst Sun Sep 15 10:37:57 2013 +0200 +++ b/Doc/library/ctypes.rst Sun Sep 15 07:35:31 2013 -0400 @@ -2393,6 +2324,50 @@ Arrays and pointers ^^^^^^^^^^^^^^^^^^^ -Not yet written - please see the sections :ref:`ctypes-pointers` and section -:ref:`ctypes-arrays` in the tutorial. - +.. class:: Array(\*args) + + Abstract base class for arrays. + + The recommended way to create concrete array types is by multiplying any + :mod:`ctypes` data type with a positive integer. Alternatively, you can subclass + this type and define :attr:`_length_` and :attr:`_type_` class variables. + :mod:`ctypes` allows reading and writing the elements using standard + subscript and slice accesses; for slice reads, the resulting object is + *not* itself an :class:`Array`. + + + .. attribute:: _length_ + + A positive integer specifying the number of elements in the array. + Out-of-range subscripts result in an :exc:`IndexError`. Will be + returned by :func:`len`. + + + .. attribute:: _type_ + + Specifies the type of each element in the array. + + + Array subclass constructors accept positional arguments, used to + initialize the elements in order. + + +.. class:: _Pointer + + Private, abstract base class for pointers. + + Concrete pointer types are created by calling :func:`POINTER` with the + type that will be pointed to; this is done automatically by + :func:`pointer`. + + :mod:`ctypes` allows reading and writing the elements using standard + subscript and slice accesses. :class:`_Pointer` objects have no size, + so :func:`len` will raise :exc:`TypeError`, negative subscripts will + read from the memory *before* the pointer (as in C), and out-of-range + subscripts will probably crash with an access violation (if you're lucky). + + + .. attribute:: _type_ + + Specifies the type pointed to. + diff -r ed590800fde7 -r 75843d82f6cf Lib/ctypes/test/test_arrays.py --- a/Lib/ctypes/test/test_arrays.py Sun Sep 15 10:37:57 2013 +0200 +++ b/Lib/ctypes/test/test_arrays.py Sun Sep 15 07:35:31 2013 -0400 @@ -22,20 +22,24 @@ 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 -r ed590800fde7 -r 75843d82f6cf Lib/ctypes/test/test_pointers.py --- a/Lib/ctypes/test/test_pointers.py Sun Sep 15 10:37:57 2013 +0200 +++ b/Lib/ctypes/test/test_pointers.py Sun Sep 15 07:35:31 2013 -0400 @@ -128,9 +128,10 @@ 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):