cpython: a78cbb2125a8 (original) (raw)
Mercurial > cpython
changeset 100088:a78cbb2125a8
Issue #19023: Merge ctypes doc and tests from 3.5 [#19023]
Martin Panter vadmium+py@gmail.com | |
---|---|
date | Fri, 29 Jan 2016 10:25:40 +0000 |
parents | 0fb96a43d381(current diff)732fdd03e708(diff) |
children | df3290046e62 |
files | Misc/ACKS |
diffstat | 4 files changed, 71 insertions(+), 11 deletions(-)[+] [-] Doc/library/ctypes.rst 60 Lib/ctypes/test/test_arrays.py 12 Lib/ctypes/test/test_pointers.py 9 Misc/ACKS 1 |
line wrap: on
line diff
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -716,8 +716,8 @@ Pointer instances are created by calling
>>> pi = pointer(i)
>>>
-Pointer instances have a :attr:contents
attribute which returns the object to
-which the pointer points, the i
object above::
+Pointer instances have a :attr:~_Pointer.contents
attribute which
+returns the object to which the pointer points, the i
object above::
>>> pi.contents
c_long(42)
@@ -2401,6 +2401,56 @@ other data types containing pointer type
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. - Array elements can be read and written 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.[](#l1.35)
Out-of-range subscripts result in an :exc:`IndexError`. Will be[](#l1.36)
returned by :func:`len`.[](#l1.37)
- Array subclass constructors accept positional arguments, used to
- initialize the elements in order. +
- 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
. + - If a pointer points to an array, its elements can be read and
- written using standard subscript and slice accesses. 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:: contents +
Returns the object to which to pointer points. Assigning to this[](#l1.71)
attribute changes the pointer to point to the assigned object.[](#l1.72)
--- 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))][](#l2.7)
values = [ia[i] for i in range(alen)][](#l2.8) self.assertEqual(values, init)[](#l2.9)
# out-of-bounds accesses should be caught[](#l2.11)
with self.assertRaises(IndexError): ia[alen][](#l2.12)
with self.assertRaises(IndexError): ia[-alen-1][](#l2.13)
+ # 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))][](#l2.19)
values = [ia[i] for i in range(alen)][](#l2.20) self.assertEqual(values, new_values)[](#l2.21)
# are the items initialized to 0? ia = int_array()
values = [ia[i] for i in range(len(init))][](#l2.25)
self.assertEqual(values, [0] * len(init))[](#l2.26)
values = [ia[i] for i in range(alen)][](#l2.27)
self.assertEqual(values, [0] * alen)[](#l2.28)
# Too many initializers should be caught self.assertRaises(IndexError, int_array, range(alen2))
--- 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)[](#l3.7)
x = c_int(12321)[](#l3.8)
res.contents = x[](#l3.9) self.assertEqual(i.value, 54345)[](#l3.10)
x.value = -99[](#l3.12)
self.assertEqual(res.contents.value, -99)[](#l3.13)
+ 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[](#l3.22)
# Although a pointer can be indexed, it has no length[](#l3.23) self.assertRaises(TypeError, len, p)[](#l3.24) self.assertEqual(p[0], 42)[](#l3.25)
self.assertEqual(p[0:1], [42])[](#l3.26) self.assertEqual(p.contents.value, 42)[](#l3.27)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -1504,6 +1504,7 @@ Dmitry Vasiliev Sebastian Ortiz Vasquez Alexandre Vassalotti Nadeem Vawda +Sye van der Veen Frank Vercruesse Mike Verdone Jaap Vermeulen