Skip to content

Commit 1e4d32a

Browse files
docs: Correct ctypes examples and outdated descriptions (GH-161303)
(cherry picked from commit 6fa6d09) Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent 0398aa1 commit 1e4d32a

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)
@@ -809,9 +809,8 @@ compiler does it. It is possible to override this behavior entirely by specifyi
809809
:attr:`~Structure._layout_` class attribute in the subclass definition; see
810810
the attribute documentation for details.
811811

812-
It is possible to specify the maximum alignment for the fields and/or for the
813-
structure itself by setting the class attributes :attr:`~Structure._pack_`
814-
and/or :attr:`~Structure._align_`, respectively.
812+
The :attr:`~Structure._align_` class attribute specifies the minimum alignment
813+
for the structure itself.
815814
See the attribute documentation for details.
816815

817816
:mod:`!ctypes` uses the native byte order for Structures and Unions. To build
@@ -1032,10 +1031,10 @@ to synchronize access to memory:
10321031
>>> lock = threading.Lock()
10331032
>>> # Thread 1
10341033
>>> with lock:
1035-
... pointer_a.contents = 24
1034+
... pointer_a[0] = 24
10361035
>>> # Thread 2
10371036
>>> with lock:
1038-
... pointer_b.contents = 42
1037+
... pointer_b[0] = 42
10391038
10401039
10411040
.. _ctypes-type-conversions:
@@ -1166,7 +1165,7 @@ other, and finally follow the pointer chain a few times::
11661165
... print(p.name, end=" ")
11671166
... p = p.next[0]
11681167
...
1169-
foo bar foo bar foo bar foo bar
1168+
b'foo' b'bar' b'foo' b'bar' b'foo' b'bar' b'foo' b'bar'
11701169
>>>
11711170

11721171

@@ -1302,25 +1301,21 @@ api::
13021301
0x30c00a0
13031302

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

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

13171312
>>> from ctypes import *
13181313
>>>
13191314
>>> class struct_frozen(Structure):
13201315
... _fields_ = [("name", c_char_p),
13211316
... ("code", POINTER(c_ubyte)),
13221317
... ("size", c_int),
1323-
... ("get_code", POINTER(c_ubyte)), # Function pointer
1318+
... ("is_package", c_int),
13241319
... ]
13251320
...
13261321
>>>
@@ -1348,9 +1343,9 @@ hit the ``NULL`` entry::
13481343
zipimport 12345
13491344
>>>
13501345

1351-
The fact that standard Python has a frozen module and a frozen package
1352-
(indicated by the negative ``size`` member) is not well known, it is only used
1353-
for testing. Try it out with ``import __hello__`` for example.
1346+
Frozen packages are indicated by a nonzero ``is_package`` member. Python also
1347+
includes frozen modules and packages used for testing, such as ``__hello__``
1348+
and ``__phello__``, in a separate table, ``_PyImport_FrozenTest``.
13541349

13551350

13561351
.. _ctypes-surprises:
@@ -1927,8 +1922,8 @@ in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class:
19271922
:meth:`~_CData.from_param` class method of the items in the :attr:`argtypes`
19281923
tuple, this method allows adapting the actual argument to an object that
19291924
the foreign function accepts. For example, a :class:`c_char_p` item in
1930-
the :attr:`argtypes` tuple will convert a string passed as argument into
1931-
a bytes object using ctypes conversion rules.
1925+
the :attr:`argtypes` tuple will adapt a bytes object passed as an argument
1926+
to a C pointer to its contents. It does not accept a :class:`str` object.
19321927

19331928
New: It is now possible to put items in argtypes which are not ctypes
19341929
types, but each item must have a :meth:`~_CData.from_param` method which returns a
@@ -2633,7 +2628,7 @@ Fundamental data types
26332628
and :c:expr:`time_t` C types are the same, then :class:`c_long`,
26342629
:class:`c_longlong` and :class:`c_time_t` all refer to a single class,
26352630
:class:`c_long`, whose :attr:`_type_` code is ``'l'``.
2636-
The ``'L'`` code will be unused.
2631+
The ``'q'`` code will be unused.
26372632

26382633
.. seealso::
26392634

@@ -2645,10 +2640,8 @@ Fundamental data types
26452640
Fundamental data types, when returned as foreign function call results, or, for
26462641
example, by retrieving structure field members or array items, are transparently
26472642
converted to native Python types. In other words, if a foreign function has a
2648-
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
2649-
object, *not* a :class:`c_char_p` instance.
2650-
2651-
.. XXX above is false, it actually returns a Unicode string
2643+
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will receive a Python bytes
2644+
object or ``None`` for a ``NULL`` pointer, *not* a :class:`c_char_p` instance.
26522645

26532646
Subclasses of fundamental data types do *not* inherit this behavior. So, if a
26542647
foreign functions :attr:`!restype` is a subclass of :class:`c_void_p`, you will
@@ -3358,7 +3351,7 @@ fields, or any other data types containing pointer type fields.
33583351
Concrete structure and union types must be created by subclassing one of these
33593352
types, and at least define a :attr:`_fields_` class variable. :mod:`!ctypes` will
33603353
create :term:`descriptor`\s which allow reading and writing the fields by direct
3361-
attribute accesses. These are the
3354+
attribute accesses.
33623355

33633356

33643357
.. attribute:: _fields_
@@ -3388,8 +3381,8 @@ fields, or any other data types containing pointer type fields.
33883381
Later assignments will raise an :exc:`AttributeError`.
33893382

33903383
Additionally, the :attr:`!_fields_` class variable must be defined before
3391-
the structure or union type is first used: an instance or subclass is
3392-
created, :func:`sizeof` is called on it, and so on.
3384+
the structure or union type is first used, for example when an instance
3385+
or subclass is created.
33933386
Later assignments to :attr:`!_fields_` will raise an :exc:`AttributeError`.
33943387
If :attr:`!_fields_` has not been set before such use,
33953388
the structure or union will have no own fields, as if :attr:`!_fields_`
@@ -3486,15 +3479,17 @@ fields, or any other data types containing pointer type fields.
34863479

34873480
Here is an example type (Windows)::
34883481

3482+
class TYPEDESC(Structure):
3483+
pass
3484+
34893485
class _U(Union):
34903486
_fields_ = [("lptdesc", POINTER(TYPEDESC)),
34913487
("lpadesc", POINTER(ARRAYDESC)),
34923488
("hreftype", HREFTYPE)]
34933489

3494-
class TYPEDESC(Structure):
3495-
_anonymous_ = ("u",)
3496-
_fields_ = [("u", _U),
3497-
("vt", VARTYPE)]
3490+
TYPEDESC._anonymous_ = ("u",)
3491+
TYPEDESC._fields_ = [("u", _U),
3492+
("vt", VARTYPE)]
34983493

34993494

35003495
The ``TYPEDESC`` structure describes a COM data type, the ``vt`` field
@@ -3504,10 +3499,11 @@ fields, or any other data types containing pointer type fields.
35043499
are equivalent, but the former is faster since it does not need to create
35053500
a temporary union instance::
35063501

3502+
some_type = TYPEDESC()
35073503
td = TYPEDESC()
35083504
td.vt = VT_PTR
3509-
td.lptdesc = POINTER(some_type)
3510-
td.u.lptdesc = POINTER(some_type)
3505+
td.lptdesc = pointer(some_type)
3506+
td.u.lptdesc = pointer(some_type)
35113507

35123508
It is possible to define sub-subclasses of structures, they inherit the
35133509
fields of the base class. If the subclass definition has a separate
@@ -3602,7 +3598,7 @@ fields, or any other data types containing pointer type fields.
36023598
:attr:`~CField.byte_offset`.
36033599

36043600
To get the field's value, read the storage unit as an integer,
3605-
:ref:`shift left <shifting>` by :attr:`!bit_offset` and
3601+
:ref:`shift right <shifting>` by :attr:`!bit_offset` and
36063602
take the :attr:`!bit_size` least significant bits.
36073603

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

0 commit comments

Comments
 (0)