Skip to content

Commit 5df9c4d

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 c99e750 commit 5df9c4d

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
@@ -422,7 +422,7 @@ string objects are immutable)::
422422
>>> print(c_s)
423423
c_wchar_p(139966785747344)
424424
>>> print(c_s.value)
425-
Hello World
425+
Hello, World
426426
>>> c_s.value = "Hi, there"
427427
>>> print(c_s) # the memory location has changed
428428
c_wchar_p(139966783348904)
@@ -807,9 +807,8 @@ compiler does it. It is possible to override this behavior entirely by specifyi
807807
:attr:`~Structure._layout_` class attribute in the subclass definition; see
808808
the attribute documentation for details.
809809

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

815814
:mod:`!ctypes` uses the native byte order for Structures and Unions. To build
@@ -1030,10 +1029,10 @@ to synchronize access to memory:
10301029
>>> lock = threading.Lock()
10311030
>>> # Thread 1
10321031
>>> with lock:
1033-
... pointer_a.contents = 24
1032+
... pointer_a[0] = 24
10341033
>>> # Thread 2
10351034
>>> with lock:
1036-
... pointer_b.contents = 42
1035+
... pointer_b[0] = 42
10371036
10381037
10391038
.. _ctypes-type-conversions:
@@ -1164,7 +1163,7 @@ other, and finally follow the pointer chain a few times::
11641163
... print(p.name, end=" ")
11651164
... p = p.next[0]
11661165
...
1167-
foo bar foo bar foo bar foo bar
1166+
b'foo' b'bar' b'foo' b'bar' b'foo' b'bar' b'foo' b'bar'
11681167
>>>
11691168

11701169

@@ -1300,25 +1299,21 @@ api::
13001299
0x30c00a0
13011300

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

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

13151310
>>> from ctypes import *
13161311
>>>
13171312
>>> class struct_frozen(Structure):
13181313
... _fields_ = [("name", c_char_p),
13191314
... ("code", POINTER(c_ubyte)),
13201315
... ("size", c_int),
1321-
... ("get_code", POINTER(c_ubyte)), # Function pointer
1316+
... ("is_package", c_int),
13221317
... ]
13231318
...
13241319
>>>
@@ -1346,9 +1341,9 @@ hit the ``NULL`` entry::
13461341
zipimport 12345
13471342
>>>
13481343

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

13531348

13541349
.. _ctypes-surprises:
@@ -1889,8 +1884,8 @@ in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class:
18891884
:meth:`~_CData.from_param` class method of the items in the :attr:`argtypes`
18901885
tuple, this method allows adapting the actual argument to an object that
18911886
the foreign function accepts. For example, a :class:`c_char_p` item in
1892-
the :attr:`argtypes` tuple will convert a string passed as argument into
1893-
a bytes object using ctypes conversion rules.
1887+
the :attr:`argtypes` tuple will adapt a bytes object passed as an argument
1888+
to a C pointer to its contents. It does not accept a :class:`str` object.
18941889

18951890
New: It is now possible to put items in argtypes which are not ctypes
18961891
types, but each item must have a :meth:`~_CData.from_param` method which returns a
@@ -2623,7 +2618,7 @@ Fundamental data types
26232618
and :c:expr:`time_t` C types are the same, then :class:`c_long`,
26242619
:class:`c_longlong` and :class:`c_time_t` all refer to a single class,
26252620
:class:`c_long`, whose :attr:`_type_` code is ``'l'``.
2626-
The ``'L'`` code will be unused.
2621+
The ``'q'`` code will be unused.
26272622

26282623
.. seealso::
26292624

@@ -2635,10 +2630,8 @@ Fundamental data types
26352630
Fundamental data types, when returned as foreign function call results, or, for
26362631
example, by retrieving structure field members or array items, are transparently
26372632
converted to native Python types. In other words, if a foreign function has a
2638-
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
2639-
object, *not* a :class:`c_char_p` instance.
2640-
2641-
.. XXX above is false, it actually returns a Unicode string
2633+
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will receive a Python bytes
2634+
object or ``None`` for a ``NULL`` pointer, *not* a :class:`c_char_p` instance.
26422635

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

33533346

33543347
.. attribute:: _fields_
@@ -3378,8 +3371,8 @@ fields, or any other data types containing pointer type fields.
33783371
Later assignments will raise an :exc:`AttributeError`.
33793372

33803373
Additionally, the :attr:`!_fields_` class variable must be defined before
3381-
the structure or union type is first used: an instance or subclass is
3382-
created, :func:`sizeof` is called on it, and so on.
3374+
the structure or union type is first used, for example when an instance
3375+
or subclass is created.
33833376
Later assignments to :attr:`!_fields_` will raise an :exc:`AttributeError`.
33843377
If :attr:`!_fields_` has not been set before such use,
33853378
the structure or union will have no own fields, as if :attr:`!_fields_`
@@ -3476,15 +3469,17 @@ fields, or any other data types containing pointer type fields.
34763469

34773470
Here is an example type (Windows)::
34783471

3472+
class TYPEDESC(Structure):
3473+
pass
3474+
34793475
class _U(Union):
34803476
_fields_ = [("lptdesc", POINTER(TYPEDESC)),
34813477
("lpadesc", POINTER(ARRAYDESC)),
34823478
("hreftype", HREFTYPE)]
34833479

3484-
class TYPEDESC(Structure):
3485-
_anonymous_ = ("u",)
3486-
_fields_ = [("u", _U),
3487-
("vt", VARTYPE)]
3480+
TYPEDESC._anonymous_ = ("u",)
3481+
TYPEDESC._fields_ = [("u", _U),
3482+
("vt", VARTYPE)]
34883483

34893484

34903485
The ``TYPEDESC`` structure describes a COM data type, the ``vt`` field
@@ -3494,10 +3489,11 @@ fields, or any other data types containing pointer type fields.
34943489
are equivalent, but the former is faster since it does not need to create
34953490
a temporary union instance::
34963491

3492+
some_type = TYPEDESC()
34973493
td = TYPEDESC()
34983494
td.vt = VT_PTR
3499-
td.lptdesc = POINTER(some_type)
3500-
td.u.lptdesc = POINTER(some_type)
3495+
td.lptdesc = pointer(some_type)
3496+
td.u.lptdesc = pointer(some_type)
35013497

35023498
It is possible to define sub-subclasses of structures, they inherit the
35033499
fields of the base class. If the subclass definition has a separate
@@ -3592,7 +3588,7 @@ fields, or any other data types containing pointer type fields.
35923588
:attr:`~CField.byte_offset`.
35933589

35943590
To get the field's value, read the storage unit as an integer,
3595-
:ref:`shift left <shifting>` by :attr:`!bit_offset` and
3591+
:ref:`shift right <shifting>` by :attr:`!bit_offset` and
35963592
take the :attr:`!bit_size` least significant bits.
35973593

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

0 commit comments

Comments
 (0)