Skip to content

Commit 7e8fe2f

Browse files
committed
docs: Correct ctypes examples and outdated descriptions
1 parent 405f127 commit 7e8fe2f

1 file changed

Lines changed: 34 additions & 38 deletions

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ string objects are immutable)::
424424
>>> print(c_s)
425425
c_wchar_p(139966785747344)
426426
>>> print(c_s.value)
427-
Hello World
427+
Hello, World
428428
>>> c_s.value = "Hi, there"
429429
>>> print(c_s) # the memory location has changed
430430
c_wchar_p(139966783348904)
@@ -851,9 +851,8 @@ compiler does it. It is possible to override this behavior entirely by specifyi
851851
:attr:`~Structure._layout_` class attribute in the subclass definition; see
852852
the attribute documentation for details.
853853

854-
It is possible to specify the maximum alignment for the fields and/or for the
855-
structure itself by setting the class attributes :attr:`~Structure._pack_`
856-
and/or :attr:`~Structure._align_`, respectively.
854+
The :attr:`~Structure._align_` class attribute specifies the minimum alignment
855+
for the structure itself.
857856
See the attribute documentation for details.
858857

859858
:mod:`!ctypes` uses the native byte order for Structures and Unions. To build
@@ -1074,10 +1073,10 @@ to synchronize access to memory:
10741073
>>> lock = threading.Lock()
10751074
>>> # Thread 1
10761075
>>> with lock:
1077-
... pointer_a.contents = 24
1076+
... pointer_a[0] = 24
10781077
>>> # Thread 2
10791078
>>> with lock:
1080-
... pointer_b.contents = 42
1079+
... pointer_b[0] = 42
10811080
10821081
10831082
.. _ctypes-type-conversions:
@@ -1219,7 +1218,7 @@ other, and finally follow the pointer chain a few times::
12191218
... print(p.name, end=" ")
12201219
... p = p.next[0]
12211220
...
1222-
foo bar foo bar foo bar foo bar
1221+
b'foo' b'bar' b'foo' b'bar' b'foo' b'bar' b'foo' b'bar'
12231222
>>>
12241223

12251224

@@ -1380,25 +1379,21 @@ api::
13801379
0x30c00a0
13811380

13821381
An extended example which also demonstrates the use of pointers accesses the
1383-
:c:data:`PyImport_FrozenModules` pointer exported by Python.
1384-
1385-
Quoting the docs for that value:
1382+
``_PyImport_FrozenBootstrap`` pointer exported by Python. It points to an array
1383+
of :c:struct:`_frozen` records describing frozen modules used during startup,
1384+
terminated by one whose members are all ``NULL`` or zero. This is similar to
1385+
the :c:data:`PyImport_FrozenModules` table, which third-party code can use to
1386+
provide additional frozen modules.
13861387

1387-
This pointer is initialized to point to an array of :c:struct:`_frozen`
1388-
records, terminated by one whose members are all ``NULL`` or zero. When a frozen
1389-
module is imported, it is searched in this table. Third-party code could play
1390-
tricks with this to provide a dynamically created collection of frozen modules.
1391-
1392-
So manipulating this pointer could even prove useful. To restrict the example
1393-
size, we show only how this table can be read with :mod:`!ctypes`::
1388+
We can read this table with :mod:`!ctypes`::
13941389

13951390
>>> from ctypes import *
13961391
>>>
13971392
>>> class struct_frozen(Structure):
13981393
... _fields_ = [("name", c_char_p),
13991394
... ("code", POINTER(c_ubyte)),
14001395
... ("size", c_int),
1401-
... ("get_code", POINTER(c_ubyte)), # Function pointer
1396+
... ("is_package", c_int),
14021397
... ]
14031398
...
14041399
>>>
@@ -1426,9 +1421,9 @@ hit the ``NULL`` entry::
14261421
zipimport 12345
14271422
>>>
14281423

1429-
The fact that standard Python has a frozen module and a frozen package
1430-
(indicated by the negative ``size`` member) is not well known, it is only used
1431-
for testing. Try it out with ``import __hello__`` for example.
1424+
Frozen packages are indicated by a nonzero ``is_package`` member. Python also
1425+
includes frozen modules and packages used for testing, such as ``__hello__``
1426+
and ``__phello__``, in a separate table, ``_PyImport_FrozenTest``.
14321427

14331428

14341429
.. _ctypes-surprises:
@@ -2009,8 +2004,8 @@ in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class:
20092004
:meth:`~_CData.from_param` class method of the items in the :attr:`argtypes`
20102005
tuple, this method allows adapting the actual argument to an object that
20112006
the foreign function accepts. For example, a :class:`c_char_p` item in
2012-
the :attr:`argtypes` tuple will convert a string passed as argument into
2013-
a bytes object using ctypes conversion rules.
2007+
the :attr:`argtypes` tuple will adapt a bytes object passed as an argument
2008+
to a C pointer to its contents. It does not accept a :class:`str` object.
20142009

20152010
New: It is now possible to put items in argtypes which are not ctypes
20162011
types, but each item must have a :meth:`~_CData.from_param` method which returns a
@@ -2715,7 +2710,7 @@ Fundamental data types
27152710
and :c:expr:`time_t` C types are the same, then :class:`c_long`,
27162711
:class:`c_longlong` and :class:`c_time_t` all refer to a single class,
27172712
:class:`c_long`, whose :attr:`_type_` code is ``'l'``.
2718-
The ``'L'`` code will be unused.
2713+
The ``'q'`` code will be unused.
27192714

27202715
.. seealso::
27212716

@@ -2727,10 +2722,8 @@ Fundamental data types
27272722
Fundamental data types, when returned as foreign function call results, or, for
27282723
example, by retrieving structure field members or array items, are transparently
27292724
converted to native Python types. In other words, if a foreign function has a
2730-
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
2731-
object, *not* a :class:`c_char_p` instance.
2732-
2733-
.. XXX above is false, it actually returns a Unicode string
2725+
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will receive a Python bytes
2726+
object or ``None`` for a ``NULL`` pointer, *not* a :class:`c_char_p` instance.
27342727

27352728
Subclasses of fundamental data types do *not* inherit this behavior. So, if a
27362729
foreign functions :attr:`!restype` is a subclass of :class:`c_void_p`, you will
@@ -3440,7 +3433,7 @@ fields, or any other data types containing pointer type fields.
34403433
Concrete structure and union types must be created by subclassing one of these
34413434
types, and at least define a :attr:`_fields_` class variable. :mod:`!ctypes` will
34423435
create :term:`descriptor`\s which allow reading and writing the fields by direct
3443-
attribute accesses. These are the
3436+
attribute accesses.
34443437

34453438

34463439
.. attribute:: _fields_
@@ -3470,8 +3463,8 @@ fields, or any other data types containing pointer type fields.
34703463
Later assignments will raise an :exc:`AttributeError`.
34713464

34723465
Additionally, the :attr:`!_fields_` class variable must be defined before
3473-
the structure or union type is first used: an instance or subclass is
3474-
created, :func:`sizeof` is called on it, and so on.
3466+
the structure or union type is first used, for example when an instance
3467+
or subclass is created.
34753468
Later assignments to :attr:`!_fields_` will raise an :exc:`AttributeError`.
34763469
If :attr:`!_fields_` has not been set before such use,
34773470
the structure or union will have no own fields, as if :attr:`!_fields_`
@@ -3568,15 +3561,17 @@ fields, or any other data types containing pointer type fields.
35683561

35693562
Here is an example type (Windows)::
35703563

3564+
class TYPEDESC(Structure):
3565+
pass
3566+
35713567
class _U(Union):
35723568
_fields_ = [("lptdesc", POINTER(TYPEDESC)),
35733569
("lpadesc", POINTER(ARRAYDESC)),
35743570
("hreftype", HREFTYPE)]
35753571

3576-
class TYPEDESC(Structure):
3577-
_anonymous_ = ("u",)
3578-
_fields_ = [("u", _U),
3579-
("vt", VARTYPE)]
3572+
TYPEDESC._anonymous_ = ("u",)
3573+
TYPEDESC._fields_ = [("u", _U),
3574+
("vt", VARTYPE)]
35803575

35813576

35823577
The ``TYPEDESC`` structure describes a COM data type, the ``vt`` field
@@ -3586,10 +3581,11 @@ fields, or any other data types containing pointer type fields.
35863581
are equivalent, but the former is faster since it does not need to create
35873582
a temporary union instance::
35883583

3584+
some_type = TYPEDESC()
35893585
td = TYPEDESC()
35903586
td.vt = VT_PTR
3591-
td.lptdesc = POINTER(some_type)
3592-
td.u.lptdesc = POINTER(some_type)
3587+
td.lptdesc = pointer(some_type)
3588+
td.u.lptdesc = pointer(some_type)
35933589

35943590
It is possible to define sub-subclasses of structures, they inherit the
35953591
fields of the base class. If the subclass definition has a separate
@@ -3684,7 +3680,7 @@ fields, or any other data types containing pointer type fields.
36843680
:attr:`~CField.byte_offset`.
36853681

36863682
To get the field's value, read the storage unit as an integer,
3687-
:ref:`shift left <shifting>` by :attr:`!bit_offset` and
3683+
:ref:`shift right <shifting>` by :attr:`!bit_offset` and
36883684
take the :attr:`!bit_size` least significant bits.
36893685

36903686
For non-bitfields, :attr:`!bit_offset` is zero

0 commit comments

Comments
 (0)