@@ -72,8 +72,9 @@ Windows appends the usual ``.dll`` file suffix automatically.
72
72
73
73
On Linux, it is required to specify the filename *including * the extension to
74
74
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::
77
78
78
79
>>> cdll.LoadLibrary("libc.so.6") # doctest: +LINUX
79
80
<CDLL 'libc.so.6', handle ... at ...>
@@ -333,7 +334,7 @@ property::
333
334
10 b'Hi\x00lo\x00\x00\x00\x00\x00'
334
335
>>>
335
336
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 `
337
338
function (which is still available as an alias). To create a mutable memory
338
339
block containing unicode characters of the C type :c:type: `wchar_t `, use the
339
340
:func: `create_unicode_buffer ` function.
@@ -383,15 +384,15 @@ as calling functions with a fixed number of parameters. On some platforms, and i
383
384
particular ARM64 for Apple Platforms, the calling convention for variadic functions
384
385
is different than that for regular functions.
385
386
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:
388
389
389
390
.. code-block :: python3
390
391
391
392
libc.printf.argtypes = [ctypes.c_char_p]
392
393
393
394
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.
395
396
396
397
397
398
.. _ctypes-calling-functions-with-own-custom-data-types :
@@ -401,7 +402,7 @@ Calling functions with your own custom data types
401
402
402
403
You can also customize :mod: `ctypes ` argument conversion to allow instances of
403
404
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
405
406
course, it must be one of integer, string, or bytes::
406
407
407
408
>>> class Bottles:
@@ -414,7 +415,7 @@ course, it must be one of integer, string, or bytes::
414
415
19
415
416
>>>
416
417
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_ `
418
419
instance variable, you could define a :class: `property ` which makes the
419
420
attribute available on request.
420
421
@@ -425,9 +426,9 @@ Specifying the required argument types (function prototypes)
425
426
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
426
427
427
428
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.
429
430
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
431
432
probably not a good example here, because it takes a variable number and
432
433
different types of parameters depending on the format string, on the other hand
433
434
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::
451
452
>>>
452
453
453
454
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
456
457
the Python object passed to the function call, it should do a typecheck or
457
458
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
459
460
pass as the C function argument in this case. Again, the result should be an
460
461
integer, string, bytes, a :mod: `ctypes ` instance, or an object with an
461
- :attr: `_as_parameter_ ` attribute.
462
+ :attr: `! _as_parameter_ ` attribute.
462
463
463
464
464
465
.. _ctypes-return-types :
@@ -478,13 +479,13 @@ By default functions are assumed to return the C :c:expr:`int` type. Other
478
479
return types can be specified by setting the :attr: `restype ` attribute of the
479
480
function object.
480
481
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 ::
484
485
485
486
>>> libc.time.restype = c_time_t
486
487
487
- The argument types can be specified using `` argtypes ` `::
488
+ The argument types can be specified using :attr: ` ~_FuncPtr. argtypes `::
488
489
489
490
>>> libc.time.argtypes = (POINTER(c_time_t),)
490
491
@@ -493,7 +494,7 @@ To call the function with a ``NULL`` pointer as first argument, use ``None``::
493
494
>>> print(libc.time(None)) # doctest: +SKIP
494
495
1150640792
495
496
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
497
498
a string pointer and a char, and returns a pointer to a string::
498
499
499
500
>>> strchr = libc.strchr
@@ -506,8 +507,8 @@ a string pointer and a char, and returns a pointer to a string::
506
507
None
507
508
>>>
508
509
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
511
512
single character Python bytes object into a C char:
512
513
513
514
.. doctest ::
@@ -853,7 +854,7 @@ Type conversions
853
854
^^^^^^^^^^^^^^^^
854
855
855
856
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
857
858
a member field in a structure definition, only instances of exactly the same
858
859
type are accepted. There are some exceptions to this rule, where ctypes accepts
859
860
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::
874
875
>>>
875
876
876
877
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
878
879
type (``c_int `` in this case) can be passed to the function. ctypes will apply
879
880
the required :func: `byref ` conversion in this case automatically.
880
881
@@ -1437,7 +1438,7 @@ function exported by these libraries, and reacquired afterwards.
1437
1438
All these classes can be instantiated by calling them with at least one
1438
1439
argument, the pathname of the shared library. If you have an existing handle to
1439
1440
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 `
1441
1442
function is used to load the library into the process, and to get a handle to
1442
1443
it.
1443
1444
@@ -1522,8 +1523,8 @@ underscore to not clash with exported function names:
1522
1523
1523
1524
Shared libraries can also be loaded by using one of the prefabricated objects,
1524
1525
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.
1527
1528
1528
1529
1529
1530
.. class :: LibraryLoader(dlltype)
@@ -1639,14 +1640,14 @@ They are instances of a private class:
1639
1640
unspecified arguments as well.
1640
1641
1641
1642
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 `
1643
1644
tuple, this method allows adapting the actual argument to an object that
1644
1645
the foreign function accepts. For example, a :class: `c_char_p ` item in
1645
1646
the :attr: `argtypes ` tuple will convert a string passed as argument into
1646
1647
a bytes object using ctypes conversion rules.
1647
1648
1648
1649
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
1650
1651
value usable as argument (integer, string, ctypes instance). This allows
1651
1652
defining adapters that can adapt custom objects as function parameters.
1652
1653
@@ -1770,12 +1771,12 @@ different ways, depending on the type and number of the parameters in the call:
1770
1771
1771
1772
COM methods use a special calling convention: They require a pointer to
1772
1773
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.
1774
1775
1775
1776
The optional *paramflags * parameter creates foreign function wrappers with much
1776
1777
more functionality than the features described above.
1777
1778
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 `.
1779
1780
1780
1781
Each item in this tuple contains further information about a parameter, it must
1781
1782
be a tuple containing one, two, or three items.
@@ -2157,8 +2158,8 @@ Data types
2157
2158
2158
2159
This method adapts *obj * to a ctypes type. It is called with the actual
2159
2160
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.
2162
2163
2163
2164
All ctypes data types have a default implementation of this classmethod
2164
2165
that normally returns *obj * if that is an instance of the type. Some
0 commit comments