Skip to content

Commit 71a7c96

Browse files
Docs: Fix Sphinx annotations in Doc/library/ctypes.rst (#107672)
Co-authored-by: Adam Turner <[email protected]>
1 parent ecb05e0 commit 71a7c96

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

Doc/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
('c:type', 'uintptr_t'),
111111
('c:type', 'va_list'),
112112
('c:type', 'wchar_t'),
113+
('c:type', '__int64'),
114+
('c:type', 'unsigned __int64'),
113115
# Standard C structures
114116
('c:struct', 'in6_addr'),
115117
('c:struct', 'in_addr'),

Doc/library/ctypes.rst

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ Windows appends the usual ``.dll`` file suffix automatically.
7272

7373
On Linux, it is required to specify the filename *including* the extension to
7474
load a library, so attribute access can not be used to load libraries. Either the
75-
:meth:`LoadLibrary` method of the dll loaders should be used, or you should load
76-
the library by creating an instance of CDLL by calling the constructor::
75+
:meth:`~LibraryLoader.LoadLibrary` method of the dll loaders should be used,
76+
or you should load the library by creating an instance of CDLL by calling
77+
the constructor::
7778

7879
>>> cdll.LoadLibrary("libc.so.6") # doctest: +LINUX
7980
<CDLL 'libc.so.6', handle ... at ...>
@@ -333,7 +334,7 @@ property::
333334
10 b'Hi\x00lo\x00\x00\x00\x00\x00'
334335
>>>
335336

336-
The :func:`create_string_buffer` function replaces the old :func:`c_buffer`
337+
The :func:`create_string_buffer` function replaces the old :func:`!c_buffer`
337338
function (which is still available as an alias). To create a mutable memory
338339
block containing unicode characters of the C type :c:type:`wchar_t`, use the
339340
:func:`create_unicode_buffer` function.
@@ -383,15 +384,15 @@ as calling functions with a fixed number of parameters. On some platforms, and i
383384
particular ARM64 for Apple Platforms, the calling convention for variadic functions
384385
is different than that for regular functions.
385386

386-
On those platforms it is required to specify the *argtypes* attribute for the
387-
regular, non-variadic, function arguments:
387+
On those platforms it is required to specify the :attr:`~_FuncPtr.argtypes`
388+
attribute for the regular, non-variadic, function arguments:
388389

389390
.. code-block:: python3
390391
391392
libc.printf.argtypes = [ctypes.c_char_p]
392393
393394
Because specifying the attribute does not inhibit portability it is advised to always
394-
specify ``argtypes`` for all variadic functions.
395+
specify :attr:`~_FuncPtr.argtypes` for all variadic functions.
395396

396397

397398
.. _ctypes-calling-functions-with-own-custom-data-types:
@@ -401,7 +402,7 @@ Calling functions with your own custom data types
401402

402403
You can also customize :mod:`ctypes` argument conversion to allow instances of
403404
your own classes be used as function arguments. :mod:`ctypes` looks for an
404-
:attr:`_as_parameter_` attribute and uses this as the function argument. Of
405+
:attr:`!_as_parameter_` attribute and uses this as the function argument. Of
405406
course, it must be one of integer, string, or bytes::
406407

407408
>>> class Bottles:
@@ -414,7 +415,7 @@ course, it must be one of integer, string, or bytes::
414415
19
415416
>>>
416417

417-
If you don't want to store the instance's data in the :attr:`_as_parameter_`
418+
If you don't want to store the instance's data in the :attr:`!_as_parameter_`
418419
instance variable, you could define a :class:`property` which makes the
419420
attribute available on request.
420421

@@ -425,9 +426,9 @@ Specifying the required argument types (function prototypes)
425426
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
426427

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

430-
:attr:`argtypes` must be a sequence of C data types (the ``printf`` function is
431+
:attr:`~_FuncPtr.argtypes` must be a sequence of C data types (the :func:`!printf` function is
431432
probably not a good example here, because it takes a variable number and
432433
different types of parameters depending on the format string, on the other hand
433434
this is quite handy to experiment with this feature)::
@@ -451,14 +452,14 @@ prototype for a C function), and tries to convert the arguments to valid types::
451452
>>>
452453

453454
If you have defined your own classes which you pass to function calls, you have
454-
to implement a :meth:`from_param` class method for them to be able to use them
455-
in the :attr:`argtypes` sequence. The :meth:`from_param` class method receives
455+
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
456457
the Python object passed to the function call, it should do a typecheck or
457458
whatever is needed to make sure this object is acceptable, and then return the
458-
object itself, its :attr:`_as_parameter_` attribute, or whatever you want to
459+
object itself, its :attr:`!_as_parameter_` attribute, or whatever you want to
459460
pass as the C function argument in this case. Again, the result should be an
460461
integer, string, bytes, a :mod:`ctypes` instance, or an object with an
461-
:attr:`_as_parameter_` attribute.
462+
:attr:`!_as_parameter_` attribute.
462463

463464

464465
.. _ctypes-return-types:
@@ -478,13 +479,13 @@ By default functions are assumed to return the C :c:expr:`int` type. Other
478479
return types can be specified by setting the :attr:`restype` attribute of the
479480
function object.
480481

481-
The C prototype of ``time()`` is ``time_t time(time_t *)``. Because :c:type:`time_t`
482-
might be of a different type than the default return type ``int``, you should
483-
specify the ``restype``::
482+
The C prototype of :c:func:`time` is ``time_t time(time_t *)``. Because :c:type:`time_t`
483+
might be of a different type than the default return type :c:expr:`int`, you should
484+
specify the :attr:`!restype` attribute::
484485

485486
>>> libc.time.restype = c_time_t
486487

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

489490
>>> libc.time.argtypes = (POINTER(c_time_t),)
490491

@@ -493,7 +494,7 @@ To call the function with a ``NULL`` pointer as first argument, use ``None``::
493494
>>> print(libc.time(None)) # doctest: +SKIP
494495
1150640792
495496

496-
Here is a more advanced example, it uses the ``strchr`` function, which expects
497+
Here is a more advanced example, it uses the :func:`strchr` function, which expects
497498
a string pointer and a char, and returns a pointer to a string::
498499

499500
>>> strchr = libc.strchr
@@ -506,8 +507,8 @@ a string pointer and a char, and returns a pointer to a string::
506507
None
507508
>>>
508509

509-
If you want to avoid the ``ord("x")`` calls above, you can set the
510-
:attr:`argtypes` attribute, and the second argument will be converted from a
510+
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
511512
single character Python bytes object into a C char:
512513

513514
.. doctest::
@@ -853,7 +854,7 @@ Type conversions
853854
^^^^^^^^^^^^^^^^
854855

855856
Usually, ctypes does strict type checking. This means, if you have
856-
``POINTER(c_int)`` in the :attr:`argtypes` list of a function or as the type of
857+
``POINTER(c_int)`` in the :attr:`~_FuncPtr.argtypes` list of a function or as the type of
857858
a member field in a structure definition, only instances of exactly the same
858859
type are accepted. There are some exceptions to this rule, where ctypes accepts
859860
other objects. For example, you can pass compatible array instances instead of
@@ -874,7 +875,7 @@ pointer types. So, for ``POINTER(c_int)``, ctypes accepts an array of c_int::
874875
>>>
875876

876877
In addition, if a function argument is explicitly declared to be a pointer type
877-
(such as ``POINTER(c_int)``) in :attr:`argtypes`, an object of the pointed
878+
(such as ``POINTER(c_int)``) in :attr:`_FuncPtr.argtypes`, an object of the pointed
878879
type (``c_int`` in this case) can be passed to the function. ctypes will apply
879880
the required :func:`byref` conversion in this case automatically.
880881

@@ -1437,7 +1438,7 @@ function exported by these libraries, and reacquired afterwards.
14371438
All these classes can be instantiated by calling them with at least one
14381439
argument, the pathname of the shared library. If you have an existing handle to
14391440
an already loaded shared library, it can be passed as the ``handle`` named
1440-
parameter, otherwise the underlying platforms ``dlopen`` or ``LoadLibrary``
1441+
parameter, otherwise the underlying platforms :c:func:`!dlopen` or :c:func:`LoadLibrary`
14411442
function is used to load the library into the process, and to get a handle to
14421443
it.
14431444

@@ -1522,8 +1523,8 @@ underscore to not clash with exported function names:
15221523

15231524
Shared libraries can also be loaded by using one of the prefabricated objects,
15241525
which are instances of the :class:`LibraryLoader` class, either by calling the
1525-
:meth:`LoadLibrary` method, or by retrieving the library as attribute of the
1526-
loader instance.
1526+
:meth:`~LibraryLoader.LoadLibrary` method, or by retrieving the library as
1527+
attribute of the loader instance.
15271528

15281529

15291530
.. class:: LibraryLoader(dlltype)
@@ -1639,14 +1640,14 @@ They are instances of a private class:
16391640
unspecified arguments as well.
16401641

16411642
When a foreign function is called, each actual argument is passed to the
1642-
:meth:`from_param` class method of the items in the :attr:`argtypes`
1643+
:meth:`~_CData.from_param` class method of the items in the :attr:`argtypes`
16431644
tuple, this method allows adapting the actual argument to an object that
16441645
the foreign function accepts. For example, a :class:`c_char_p` item in
16451646
the :attr:`argtypes` tuple will convert a string passed as argument into
16461647
a bytes object using ctypes conversion rules.
16471648

16481649
New: It is now possible to put items in argtypes which are not ctypes
1649-
types, but each item must have a :meth:`from_param` method which returns a
1650+
types, but each item must have a :meth:`~_CData.from_param` method which returns a
16501651
value usable as argument (integer, string, ctypes instance). This allows
16511652
defining adapters that can adapt custom objects as function parameters.
16521653

@@ -1770,12 +1771,12 @@ different ways, depending on the type and number of the parameters in the call:
17701771

17711772
COM methods use a special calling convention: They require a pointer to
17721773
the COM interface as first argument, in addition to those parameters that
1773-
are specified in the :attr:`argtypes` tuple.
1774+
are specified in the :attr:`~_FuncPtr.argtypes` tuple.
17741775

17751776
The optional *paramflags* parameter creates foreign function wrappers with much
17761777
more functionality than the features described above.
17771778

1778-
*paramflags* must be a tuple of the same length as :attr:`argtypes`.
1779+
*paramflags* must be a tuple of the same length as :attr:`~_FuncPtr.argtypes`.
17791780

17801781
Each item in this tuple contains further information about a parameter, it must
17811782
be a tuple containing one, two, or three items.
@@ -2157,8 +2158,8 @@ Data types
21572158

21582159
This method adapts *obj* to a ctypes type. It is called with the actual
21592160
object used in a foreign function call when the type is present in the
2160-
foreign function's :attr:`argtypes` tuple; it must return an object that
2161-
can be used as a function call parameter.
2161+
foreign function's :attr:`~_FuncPtr.argtypes` tuple;
2162+
it must return an object that can be used as a function call parameter.
21622163

21632164
All ctypes data types have a default implementation of this classmethod
21642165
that normally returns *obj* if that is an instance of the type. Some

0 commit comments

Comments
 (0)