Skip to content

Commit 417c130

Browse files
authored
gh-120313: amend documentation regarding ctypes._CFuncPtr (GH-120989)
improve docs
1 parent cae853e commit 417c130

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
@@ -397,15 +397,15 @@ as calling functions with a fixed number of parameters. On some platforms, and i
397397
particular ARM64 for Apple Platforms, the calling convention for variadic functions
398398
is different than that for regular functions.
399399

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

403403
.. code-block:: python3
404404
405405
libc.printf.argtypes = [ctypes.c_char_p]
406406
407407
Because specifying the attribute does not inhibit portability it is advised to always
408-
specify :attr:`~_FuncPtr.argtypes` for all variadic functions.
408+
specify :attr:`~_CFuncPtr.argtypes` for all variadic functions.
409409

410410

411411
.. _ctypes-calling-functions-with-own-custom-data-types:
@@ -440,9 +440,9 @@ Specifying the required argument types (function prototypes)
440440
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
441441

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

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

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

491491

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

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

500500
>>> libc.time.restype = c_time_t
501501

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

504504
>>> libc.time.argtypes = (POINTER(c_time_t),)
505505

@@ -522,7 +522,7 @@ a string pointer and a char, and returns a pointer to a string::
522522
>>>
523523

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

528528
.. doctest::
@@ -541,7 +541,7 @@ single character Python bytes object into a C char:
541541
>>>
542542

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

571571
Please note that a much more powerful error checking mechanism is available
572-
through the :attr:`~_FuncPtr.errcheck` attribute;
572+
through the :attr:`~_CFuncPtr.errcheck` attribute;
573573
see the reference manual for details.
574574

575575

@@ -877,7 +877,7 @@ Type conversions
877877
^^^^^^^^^^^^^^^^
878878

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

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

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

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

1633-
.. class:: _FuncPtr
1634+
.. doctest::
1635+
1636+
>>> import ctypes
1637+
>>> lib = ctypes.CDLL(None)
1638+
>>> issubclass(lib._FuncPtr, ctypes._CFuncPtr)
1639+
True
1640+
>>> lib._FuncPtr is ctypes._CFuncPtr
1641+
False
1642+
1643+
.. class:: _CFuncPtr
16341644

16351645
Base class for C callable foreign functions.
16361646

@@ -1796,7 +1806,7 @@ different ways, depending on the type and number of the parameters in the call:
17961806
The optional *paramflags* parameter creates foreign function wrappers with much
17971807
more functionality than the features described above.
17981808

1799-
*paramflags* must be a tuple of the same length as :attr:`~_FuncPtr.argtypes`.
1809+
*paramflags* must be a tuple of the same length as :attr:`~_CFuncPtr.argtypes`.
18001810

18011811
Each item in this tuple contains further information about a parameter, it must
18021812
be a tuple containing one, two, or three items.
@@ -1867,7 +1877,7 @@ value if there is a single one, or a tuple containing the output parameter
18671877
values when there are more than one, so the GetWindowRect function now returns a
18681878
RECT instance, when called.
18691879

1870-
Output parameters can be combined with the :attr:`~_FuncPtr.errcheck` protocol to do
1880+
Output parameters can be combined with the :attr:`~_CFuncPtr.errcheck` protocol to do
18711881
further output processing and error checking. The win32 ``GetWindowRect`` api
18721882
function returns a ``BOOL`` to signal success or failure, so this function could
18731883
do the error checking, and raises an exception when the api call failed::
@@ -1880,7 +1890,7 @@ do the error checking, and raises an exception when the api call failed::
18801890
>>> GetWindowRect.errcheck = errcheck
18811891
>>>
18821892

1883-
If the :attr:`~_FuncPtr.errcheck` function returns the argument tuple it receives
1893+
If the :attr:`~_CFuncPtr.errcheck` function returns the argument tuple it receives
18841894
unchanged, :mod:`ctypes` continues the normal processing it does on the output
18851895
parameters. If you want to return a tuple of window coordinates instead of a
18861896
``RECT`` instance, you can retrieve the fields in the function and return them
@@ -2180,7 +2190,7 @@ Data types
21802190

21812191
This method adapts *obj* to a ctypes type. It is called with the actual
21822192
object used in a foreign function call when the type is present in the
2183-
foreign function's :attr:`~_FuncPtr.argtypes` tuple;
2193+
foreign function's :attr:`~_CFuncPtr.argtypes` tuple;
21842194
it must return an object that can be used as a function call parameter.
21852195

21862196
All ctypes data types have a default implementation of this classmethod
@@ -2246,7 +2256,7 @@ Fundamental data types
22462256
Fundamental data types, when returned as foreign function call results, or, for
22472257
example, by retrieving structure field members or array items, are transparently
22482258
converted to native Python types. In other words, if a foreign function has a
2249-
:attr:`~_FuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
2259+
:attr:`~_CFuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
22502260
object, *not* a :class:`c_char_p` instance.
22512261

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

0 commit comments

Comments
 (0)