Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d48869

Browse files
committed
Issue #19023: Merge ctypes doc and tests from 3.5
2 parents 81a5092 + 34360c8 commit 9d48869

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

Doc/library/ctypes.rst

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ Pointer instances are created by calling the :func:`pointer` function on a
716716
>>> pi = pointer(i)
717717
>>>
718718

719-
Pointer instances have a :attr:`contents` attribute which returns the object to
720-
which the pointer points, the ``i`` object above::
719+
Pointer instances have a :attr:`~_Pointer.contents` attribute which
720+
returns the object to which the pointer points, the ``i`` object above::
721721

722722
>>> pi.contents
723723
c_long(42)
@@ -2401,6 +2401,56 @@ other data types containing pointer type fields.
24012401
Arrays and pointers
24022402
^^^^^^^^^^^^^^^^^^^
24032403

2404-
Not yet written - please see the sections :ref:`ctypes-pointers` and section
2405-
:ref:`ctypes-arrays` in the tutorial.
2404+
.. class:: Array(\*args)
2405+
2406+
Abstract base class for arrays.
2407+
2408+
The recommended way to create concrete array types is by multiplying any
2409+
:mod:`ctypes` data type with a positive integer. Alternatively, you can subclass
2410+
this type and define :attr:`_length_` and :attr:`_type_` class variables.
2411+
Array elements can be read and written using standard
2412+
subscript and slice accesses; for slice reads, the resulting object is
2413+
*not* itself an :class:`Array`.
2414+
2415+
2416+
.. attribute:: _length_
2417+
2418+
A positive integer specifying the number of elements in the array.
2419+
Out-of-range subscripts result in an :exc:`IndexError`. Will be
2420+
returned by :func:`len`.
2421+
2422+
2423+
.. attribute:: _type_
2424+
2425+
Specifies the type of each element in the array.
2426+
2427+
2428+
Array subclass constructors accept positional arguments, used to
2429+
initialize the elements in order.
2430+
2431+
2432+
.. class:: _Pointer
2433+
2434+
Private, abstract base class for pointers.
2435+
2436+
Concrete pointer types are created by calling :func:`POINTER` with the
2437+
type that will be pointed to; this is done automatically by
2438+
:func:`pointer`.
2439+
2440+
If a pointer points to an array, its elements can be read and
2441+
written using standard subscript and slice accesses. Pointer objects
2442+
have no size, so :func:`len` will raise :exc:`TypeError`. Negative
2443+
subscripts will read from the memory *before* the pointer (as in C), and
2444+
out-of-range subscripts will probably crash with an access violation (if
2445+
you're lucky).
2446+
2447+
2448+
.. attribute:: _type_
2449+
2450+
Specifies the type pointed to.
2451+
2452+
.. attribute:: contents
2453+
2454+
Returns the object to which to pointer points. Assigning to this
2455+
attribute changes the pointer to point to the assigned object.
24062456

Lib/ctypes/test/test_arrays.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ def test_simple(self):
2424
self.assertEqual(len(ia), alen)
2525

2626
# slot values ok?
27-
values = [ia[i] for i in range(len(init))]
27+
values = [ia[i] for i in range(alen)]
2828
self.assertEqual(values, init)
2929

30+
# out-of-bounds accesses should be caught
31+
with self.assertRaises(IndexError): ia[alen]
32+
with self.assertRaises(IndexError): ia[-alen-1]
33+
3034
# change the items
3135
from operator import setitem
3236
new_values = list(range(42, 42+alen))
3337
[setitem(ia, n, new_values[n]) for n in range(alen)]
34-
values = [ia[i] for i in range(len(init))]
38+
values = [ia[i] for i in range(alen)]
3539
self.assertEqual(values, new_values)
3640

3741
# are the items initialized to 0?
3842
ia = int_array()
39-
values = [ia[i] for i in range(len(init))]
40-
self.assertEqual(values, [0] * len(init))
43+
values = [ia[i] for i in range(alen)]
44+
self.assertEqual(values, [0] * alen)
4145

4246
# Too many initializers should be caught
4347
self.assertRaises(IndexError, int_array, *range(alen*2))

Lib/ctypes/test/test_pointers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ def test_change_pointers(self):
5656
# C code:
5757
# int x = 12321;
5858
# res = &x
59-
res.contents = c_int(12321)
59+
x = c_int(12321)
60+
res.contents = x
6061
self.assertEqual(i.value, 54345)
6162

63+
x.value = -99
64+
self.assertEqual(res.contents.value, -99)
65+
6266
def test_callbacks_with_pointers(self):
6367
# a function type receiving a pointer
6468
PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int))
@@ -131,9 +135,10 @@ class Table(Structure):
131135

132136
def test_basic(self):
133137
p = pointer(c_int(42))
134-
# Although a pointer can be indexed, it ha no length
138+
# Although a pointer can be indexed, it has no length
135139
self.assertRaises(TypeError, len, p)
136140
self.assertEqual(p[0], 42)
141+
self.assertEqual(p[0:1], [42])
137142
self.assertEqual(p.contents.value, 42)
138143

139144
def test_charpp(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ Dmitry Vasiliev
15041504
Sebastian Ortiz Vasquez
15051505
Alexandre Vassalotti
15061506
Nadeem Vawda
1507+
Sye van der Veen
15071508
Frank Vercruesse
15081509
Mike Verdone
15091510
Jaap Vermeulen

0 commit comments

Comments
 (0)