Skip to content

Commit ccf0408

Browse files
[3.13] gh-120313: amend documentation regarding ctypes._CFuncPtr (GH-120989) (GH-125979)
gh-120313: amend documentation regarding `ctypes._CFuncPtr` (GH-120989) (cherry picked from commit 417c130) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 86ec8aa commit ccf0408

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

Doc/library/ctypes.rst

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,15 @@ as calling functions with a fixed number of parameters. On some platforms, and i
383383
particular ARM64 for Apple Platforms, the calling convention for variadic functions
384384
is different than that for regular functions.
385385

386-
On those platforms it is required to specify the :attr:`~_FuncPtr.argtypes`
386+
On those platforms it is required to specify the :attr:`~_CFuncPtr.argtypes`
387387
attribute for the regular, non-variadic, function arguments:
388388

389389
.. code-block:: python3
390390
391391
libc.printf.argtypes = [ctypes.c_char_p]
392392
393393
Because specifying the attribute does not inhibit portability it is advised to always
394-
specify :attr:`~_FuncPtr.argtypes` for all variadic functions.
394+
specify :attr:`~_CFuncPtr.argtypes` for all variadic functions.
395395

396396

397397
.. _ctypes-calling-functions-with-own-custom-data-types:
@@ -426,9 +426,9 @@ Specifying the required argument types (function prototypes)
426426
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
427427

428428
It is possible to specify the required argument types of functions exported from
429-
DLLs by setting the :attr:`~_FuncPtr.argtypes` attribute.
429+
DLLs by setting the :attr:`~_CFuncPtr.argtypes` attribute.
430430

431-
:attr:`~_FuncPtr.argtypes` must be a sequence of C data types (the :func:`!printf` function is
431+
:attr:`~_CFuncPtr.argtypes` must be a sequence of C data types (the :func:`!printf` function is
432432
probably not a good example here, because it takes a variable number and
433433
different types of parameters depending on the format string, on the other hand
434434
this is quite handy to experiment with this feature)::
@@ -453,7 +453,7 @@ prototype for a C function), and tries to convert the arguments to valid types::
453453

454454
If you have defined your own classes which you pass to function calls, you have
455455
to implement a :meth:`~_CData.from_param` class method for them to be able to use them
456-
in the :attr:`~_FuncPtr.argtypes` sequence. The :meth:`~_CData.from_param` class method receives
456+
in the :attr:`~_CFuncPtr.argtypes` sequence. The :meth:`~_CData.from_param` class method receives
457457
the Python object passed to the function call, it should do a typecheck or
458458
whatever is needed to make sure this object is acceptable, and then return the
459459
object itself, its :attr:`!_as_parameter_` attribute, or whatever you want to
@@ -476,7 +476,7 @@ Return types
476476

477477

478478
By default functions are assumed to return the C :c:expr:`int` type. Other
479-
return types can be specified by setting the :attr:`~_FuncPtr.restype` attribute of the
479+
return types can be specified by setting the :attr:`~_CFuncPtr.restype` attribute of the
480480
function object.
481481

482482
The C prototype of :c:func:`time` is ``time_t time(time_t *)``. Because :c:type:`time_t`
@@ -485,7 +485,7 @@ specify the :attr:`!restype` attribute::
485485

486486
>>> libc.time.restype = c_time_t
487487

488-
The argument types can be specified using :attr:`~_FuncPtr.argtypes`::
488+
The argument types can be specified using :attr:`~_CFuncPtr.argtypes`::
489489

490490
>>> libc.time.argtypes = (POINTER(c_time_t),)
491491

@@ -508,7 +508,7 @@ a string pointer and a char, and returns a pointer to a string::
508508
>>>
509509

510510
If you want to avoid the :func:`ord("x") <ord>` calls above, you can set the
511-
:attr:`~_FuncPtr.argtypes` attribute, and the second argument will be converted from a
511+
:attr:`~_CFuncPtr.argtypes` attribute, and the second argument will be converted from a
512512
single character Python bytes object into a C char:
513513

514514
.. doctest::
@@ -527,7 +527,7 @@ single character Python bytes object into a C char:
527527
>>>
528528

529529
You can also use a callable Python object (a function or a class for example) as
530-
the :attr:`~_FuncPtr.restype` attribute, if the foreign function returns an integer. The
530+
the :attr:`~_CFuncPtr.restype` attribute, if the foreign function returns an integer. The
531531
callable will be called with the *integer* the C function returns, and the
532532
result of this call will be used as the result of your function call. This is
533533
useful to check for error return values and automatically raise an exception::
@@ -555,7 +555,7 @@ get the string representation of an error code, and *returns* an exception.
555555
:func:`GetLastError` to retrieve it.
556556

557557
Please note that a much more powerful error checking mechanism is available
558-
through the :attr:`~_FuncPtr.errcheck` attribute;
558+
through the :attr:`~_CFuncPtr.errcheck` attribute;
559559
see the reference manual for details.
560560

561561

@@ -859,7 +859,7 @@ Type conversions
859859
^^^^^^^^^^^^^^^^
860860

861861
Usually, ctypes does strict type checking. This means, if you have
862-
``POINTER(c_int)`` in the :attr:`~_FuncPtr.argtypes` list of a function or as the type of
862+
``POINTER(c_int)`` in the :attr:`~_CFuncPtr.argtypes` list of a function or as the type of
863863
a member field in a structure definition, only instances of exactly the same
864864
type are accepted. There are some exceptions to this rule, where ctypes accepts
865865
other objects. For example, you can pass compatible array instances instead of
@@ -880,7 +880,7 @@ pointer types. So, for ``POINTER(c_int)``, ctypes accepts an array of c_int::
880880
>>>
881881

882882
In addition, if a function argument is explicitly declared to be a pointer type
883-
(such as ``POINTER(c_int)``) in :attr:`~_FuncPtr.argtypes`, an object of the pointed
883+
(such as ``POINTER(c_int)``) in :attr:`~_CFuncPtr.argtypes`, an object of the pointed
884884
type (``c_int`` in this case) can be passed to the function. ctypes will apply
885885
the required :func:`byref` conversion in this case automatically.
886886

@@ -1609,10 +1609,20 @@ As explained in the previous section, foreign functions can be accessed as
16091609
attributes of loaded shared libraries. The function objects created in this way
16101610
by default accept any number of arguments, accept any ctypes data instances as
16111611
arguments, and return the default result type specified by the library loader.
1612-
They are instances of a private class:
16131612

1613+
They are instances of a private local class :class:`!_FuncPtr` (not exposed
1614+
in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class:
16141615

1615-
.. class:: _FuncPtr
1616+
.. doctest::
1617+
1618+
>>> import ctypes
1619+
>>> lib = ctypes.CDLL(None)
1620+
>>> issubclass(lib._FuncPtr, ctypes._CFuncPtr)
1621+
True
1622+
>>> lib._FuncPtr is ctypes._CFuncPtr
1623+
False
1624+
1625+
.. class:: _CFuncPtr
16161626

16171627
Base class for C callable foreign functions.
16181628

@@ -1778,7 +1788,7 @@ different ways, depending on the type and number of the parameters in the call:
17781788
The optional *paramflags* parameter creates foreign function wrappers with much
17791789
more functionality than the features described above.
17801790

1781-
*paramflags* must be a tuple of the same length as :attr:`~_FuncPtr.argtypes`.
1791+
*paramflags* must be a tuple of the same length as :attr:`~_CFuncPtr.argtypes`.
17821792

17831793
Each item in this tuple contains further information about a parameter, it must
17841794
be a tuple containing one, two, or three items.
@@ -1849,7 +1859,7 @@ value if there is a single one, or a tuple containing the output parameter
18491859
values when there are more than one, so the GetWindowRect function now returns a
18501860
RECT instance, when called.
18511861

1852-
Output parameters can be combined with the :attr:`~_FuncPtr.errcheck` protocol to do
1862+
Output parameters can be combined with the :attr:`~_CFuncPtr.errcheck` protocol to do
18531863
further output processing and error checking. The win32 ``GetWindowRect`` api
18541864
function returns a ``BOOL`` to signal success or failure, so this function could
18551865
do the error checking, and raises an exception when the api call failed::
@@ -1862,7 +1872,7 @@ do the error checking, and raises an exception when the api call failed::
18621872
>>> GetWindowRect.errcheck = errcheck
18631873
>>>
18641874

1865-
If the :attr:`~_FuncPtr.errcheck` function returns the argument tuple it receives
1875+
If the :attr:`~_CFuncPtr.errcheck` function returns the argument tuple it receives
18661876
unchanged, :mod:`ctypes` continues the normal processing it does on the output
18671877
parameters. If you want to return a tuple of window coordinates instead of a
18681878
``RECT`` instance, you can retrieve the fields in the function and return them
@@ -2162,7 +2172,7 @@ Data types
21622172

21632173
This method adapts *obj* to a ctypes type. It is called with the actual
21642174
object used in a foreign function call when the type is present in the
2165-
foreign function's :attr:`~_FuncPtr.argtypes` tuple;
2175+
foreign function's :attr:`~_CFuncPtr.argtypes` tuple;
21662176
it must return an object that can be used as a function call parameter.
21672177

21682178
All ctypes data types have a default implementation of this classmethod
@@ -2228,7 +2238,7 @@ Fundamental data types
22282238
Fundamental data types, when returned as foreign function call results, or, for
22292239
example, by retrieving structure field members or array items, are transparently
22302240
converted to native Python types. In other words, if a foreign function has a
2231-
:attr:`~_FuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
2241+
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
22322242
object, *not* a :class:`c_char_p` instance.
22332243

22342244
.. XXX above is false, it actually returns a Unicode string

0 commit comments

Comments
 (0)