@@ -41,7 +41,7 @@ You load libraries by accessing them as attributes of these objects. *cdll*
41
41
loads libraries which export functions using the standard ``cdecl `` calling
42
42
convention, while *windll * libraries call functions using the ``stdcall ``
43
43
calling convention. *oledll * also uses the ``stdcall `` calling convention, and
44
- assumes the functions return a Windows :c:type: `HRESULT ` error code. The error
44
+ assumes the functions return a Windows :c:type: `! HRESULT ` error code. The error
45
45
code is used to automatically raise an :class: `OSError ` exception when the
46
46
function call fails.
47
47
@@ -477,7 +477,7 @@ Return types
477
477
478
478
479
479
By default functions are assumed to return the C :c:expr: `int ` type. Other
480
- return types can be specified by setting the :attr: `restype ` attribute of the
480
+ return types can be specified by setting the :attr: `~_FuncPtr. restype ` attribute of the
481
481
function object.
482
482
483
483
The C prototype of :c:func: `time ` is ``time_t time(time_t *) ``. Because :c:type: `time_t `
@@ -495,7 +495,7 @@ To call the function with a ``NULL`` pointer as first argument, use ``None``::
495
495
>>> print(libc.time(None)) # doctest: +SKIP
496
496
1150640792
497
497
498
- Here is a more advanced example, it uses the :func: `strchr ` function, which expects
498
+ Here is a more advanced example, it uses the :func: `! strchr ` function, which expects
499
499
a string pointer and a char, and returns a pointer to a string::
500
500
501
501
>>> strchr = libc.strchr
@@ -528,7 +528,7 @@ single character Python bytes object into a C char:
528
528
>>>
529
529
530
530
You can also use a callable Python object (a function or a class for example) as
531
- the :attr: `restype ` attribute, if the foreign function returns an integer. The
531
+ the :attr: `~_FuncPtr. restype ` attribute, if the foreign function returns an integer. The
532
532
callable will be called with the *integer * the C function returns, and the
533
533
result of this call will be used as the result of your function call. This is
534
534
useful to check for error return values and automatically raise an exception::
@@ -556,7 +556,8 @@ get the string representation of an error code, and *returns* an exception.
556
556
:func: `GetLastError ` to retrieve it.
557
557
558
558
Please note that a much more powerful error checking mechanism is available
559
- through the :attr: `errcheck ` attribute; see the reference manual for details.
559
+ through the :attr: `~_FuncPtr.errcheck ` attribute;
560
+ see the reference manual for details.
560
561
561
562
562
563
.. _ctypes-passing-pointers :
@@ -594,7 +595,7 @@ Structures and unions
594
595
595
596
Structures and unions must derive from the :class: `Structure ` and :class: `Union `
596
597
base classes which are defined in the :mod: `ctypes ` module. Each subclass must
597
- define a :attr: `_fields_ ` attribute. :attr: `_fields_ ` must be a list of
598
+ define a :attr: `~Structure. _fields_ ` attribute. :attr: `! _fields_ ` must be a list of
598
599
*2-tuples *, containing a *field name * and a *field type *.
599
600
600
601
The field type must be a :mod: `ctypes ` type like :class: `c_int `, or any other
@@ -666,9 +667,9 @@ Structure/union alignment and byte order
666
667
667
668
By default, Structure and Union fields are aligned in the same way the C
668
669
compiler does it. It is possible to override this behavior by specifying a
669
- :attr: `_pack_ ` class attribute in the subclass definition. This must be set to a
670
- positive integer and specifies the maximum alignment for the fields. This is
671
- what ``#pragma pack(n) `` also does in MSVC.
670
+ :attr: `~Structure. _pack_ ` class attribute in the subclass definition.
671
+ This must be set to a positive integer and specifies the maximum alignment for the fields.
672
+ This is what ``#pragma pack(n) `` also does in MSVC.
672
673
673
674
:mod: `ctypes ` uses the native byte order for Structures and Unions. To build
674
675
structures with non-native byte order, you can use one of the
@@ -684,7 +685,7 @@ Bit fields in structures and unions
684
685
685
686
It is possible to create structures and unions containing bit fields. Bit fields
686
687
are only possible for integer fields, the bit width is specified as the third
687
- item in the :attr: `_fields_ ` tuples::
688
+ item in the :attr: `~Structure. _fields_ ` tuples::
688
689
689
690
>>> class Int(Structure):
690
691
... _fields_ = [("first_16", c_int, 16),
@@ -876,7 +877,7 @@ pointer types. So, for ``POINTER(c_int)``, ctypes accepts an array of c_int::
876
877
>>>
877
878
878
879
In addition, if a function argument is explicitly declared to be a pointer type
879
- (such as ``POINTER(c_int) ``) in :attr: `_FuncPtr.argtypes `, an object of the pointed
880
+ (such as ``POINTER(c_int) ``) in :attr: `~ _FuncPtr.argtypes `, an object of the pointed
880
881
type (``c_int `` in this case) can be passed to the function. ctypes will apply
881
882
the required :func: `byref ` conversion in this case automatically.
882
883
@@ -952,8 +953,8 @@ work::
952
953
>>>
953
954
954
955
because the new ``class cell `` is not available in the class statement itself.
955
- In :mod: `ctypes `, we can define the ``cell `` class and set the :attr: ` _fields_ `
956
- attribute later, after the class statement::
956
+ In :mod: `ctypes `, we can define the ``cell `` class and set the
957
+ :attr: ` ~Structure._fields_ ` attribute later, after the class statement::
957
958
958
959
>>> from ctypes import *
959
960
>>> class cell(Structure):
@@ -1003,16 +1004,16 @@ argument, and the callback functions expected argument types as the remaining
1003
1004
arguments.
1004
1005
1005
1006
I will present an example here which uses the standard C library's
1006
- :c:func: `qsort ` function, that is used to sort items with the help of a callback
1007
- function. :c:func: `qsort ` will be used to sort an array of integers::
1007
+ :c:func: `! qsort ` function, that is used to sort items with the help of a callback
1008
+ function. :c:func: `! qsort ` will be used to sort an array of integers::
1008
1009
1009
1010
>>> IntArray5 = c_int * 5
1010
1011
>>> ia = IntArray5(5, 1, 7, 33, 99)
1011
1012
>>> qsort = libc.qsort
1012
1013
>>> qsort.restype = None
1013
1014
>>>
1014
1015
1015
- :func: `qsort ` must be called with a pointer to the data to sort, the number of
1016
+ :func: `! qsort ` must be called with a pointer to the data to sort, the number of
1016
1017
items in the data array, the size of one item, and a pointer to the comparison
1017
1018
function, the callback. The callback will then be called with two pointers to
1018
1019
items, and it must return a negative integer if the first item is smaller than
@@ -1104,7 +1105,7 @@ Some shared libraries not only export functions, they also export variables. An
1104
1105
example in the Python library itself is the :c:data: `Py_Version `, Python
1105
1106
runtime version number encoded in a single constant integer.
1106
1107
1107
- :mod: `ctypes ` can access values like this with the :meth: `in_dll ` class methods of
1108
+ :mod: `ctypes ` can access values like this with the :meth: `~_CData. in_dll ` class methods of
1108
1109
the type. *pythonapi * is a predefined symbol giving access to the Python C
1109
1110
api::
1110
1111
@@ -1294,13 +1295,13 @@ Finding shared libraries
1294
1295
When programming in a compiled language, shared libraries are accessed when
1295
1296
compiling/linking a program, and when the program is run.
1296
1297
1297
- The purpose of the :func: `find_library ` function is to locate a library in a way
1298
+ The purpose of the :func: `~ctypes.util. find_library ` function is to locate a library in a way
1298
1299
similar to what the compiler or runtime loader does (on platforms with several
1299
1300
versions of a shared library the most recent should be loaded), while the ctypes
1300
1301
library loaders act like when a program is run, and call the runtime loader
1301
1302
directly.
1302
1303
1303
- The :mod: `ctypes.util ` module provides a function which can help to determine
1304
+ The :mod: `! ctypes.util ` module provides a function which can help to determine
1304
1305
the library to load.
1305
1306
1306
1307
@@ -1315,7 +1316,7 @@ the library to load.
1315
1316
1316
1317
The exact functionality is system dependent.
1317
1318
1318
- On Linux, :func: `find_library ` tries to run external programs
1319
+ On Linux, :func: `~ctypes.util. find_library ` tries to run external programs
1319
1320
(``/sbin/ldconfig ``, ``gcc ``, ``objdump `` and ``ld ``) to find the library file.
1320
1321
It returns the filename of the library file.
1321
1322
@@ -1334,7 +1335,7 @@ Here are some examples::
1334
1335
'libbz2.so.1.0'
1335
1336
>>>
1336
1337
1337
- On macOS, :func: `find_library ` tries several predefined naming schemes and paths
1338
+ On macOS, :func: `~ctypes.util. find_library ` tries several predefined naming schemes and paths
1338
1339
to locate the library, and returns a full pathname if successful::
1339
1340
1340
1341
>>> from ctypes.util import find_library
@@ -1348,13 +1349,13 @@ to locate the library, and returns a full pathname if successful::
1348
1349
'/System/Library/Frameworks/AGL.framework/AGL'
1349
1350
>>>
1350
1351
1351
- On Windows, :func: `find_library ` searches along the system search path, and
1352
+ On Windows, :func: `~ctypes.util. find_library ` searches along the system search path, and
1352
1353
returns the full pathname, but since there is no predefined naming scheme a call
1353
1354
like ``find_library("c") `` will fail and return ``None ``.
1354
1355
1355
1356
If wrapping a shared library with :mod: `ctypes `, it *may * be better to determine
1356
1357
the shared library name at development time, and hardcode that into the wrapper
1357
- module instead of using :func: `find_library ` to locate the library at runtime.
1358
+ module instead of using :func: `~ctypes.util. find_library ` to locate the library at runtime.
1358
1359
1359
1360
1360
1361
.. _ctypes-loading-shared-libraries :
@@ -1439,9 +1440,9 @@ function exported by these libraries, and reacquired afterwards.
1439
1440
All these classes can be instantiated by calling them with at least one
1440
1441
argument, the pathname of the shared library. If you have an existing handle to
1441
1442
an already loaded shared library, it can be passed as the ``handle `` named
1442
- parameter, otherwise the underlying platforms :c:func: `!dlopen ` or :c:func: ` LoadLibrary `
1443
- function is used to load the library into the process, and to get a handle to
1444
- it.
1443
+ parameter, otherwise the underlying platforms :c:func: `!dlopen ` or
1444
+<
F987
/span>:c:func: ` !LoadLibrary ` function is used to load the library into
1445
+ the process, and to get a handle to it.
1445
1446
1446
1447
The *mode * parameter can be used to specify how the library is loaded. For
1447
1448
details, consult the :manpage: `dlopen(3)` manpage. On Windows, *mode * is
@@ -1461,7 +1462,7 @@ to a new value and returns the former value.
1461
1462
1462
1463
The *use_last_error * parameter, when set to true, enables the same mechanism for
1463
1464
the Windows error code which is managed by the :func: `GetLastError ` and
1464
- :func: `SetLastError ` Windows API functions; :func: `ctypes.get_last_error ` and
1465
+ :func: `! SetLastError ` Windows API functions; :func: `ctypes.get_last_error ` and
1465
1466
:func: `ctypes.set_last_error ` are used to request and change the ctypes private
1466
1467
copy of the windows error code.
1467
1468
@@ -1533,7 +1534,7 @@ attribute of the loader instance.
1533
1534
Class which loads shared libraries. *dlltype * should be one of the
1534
1535
:class: `CDLL `, :class: `PyDLL `, :class: `WinDLL `, or :class: `OleDLL ` types.
1535
1536
1536
- :meth: `__getattr__ ` has special behavior: It allows loading a shared library by
1537
+ :meth: `! __getattr__ ` has special behavior: It allows loading a shared library by
1537
1538
accessing it as attribute of a library loader instance. The result is cached,
1538
1539
so repeated attribute accesses return the same library each time.
1539
1540
@@ -1578,7 +1579,7 @@ object is available:
1578
1579
An instance of :class: `PyDLL ` that exposes Python C API functions as
1579
1580
attributes. Note that all these functions are assumed to return C
1580
1581
:c:expr: `int `, which is of course not always the truth, so you have to assign
1581
- the correct :attr: `restype ` attribute to use these functions.
1582
+ the correct :attr: `! restype ` attribute to use these functions.
1582
1583
1583
1584
.. audit-event :: ctypes.dlopen name ctypes.LibraryLoader
1584
1585
@@ -1630,7 +1631,7 @@ They are instances of a private class:
1630
1631
the callable will be called with this integer, allowing further
1631
1632
processing or error checking. Using this is deprecated, for more flexible
1632
1633
post processing or error checking use a ctypes data type as
1633
- :attr: `restype ` and assign a callable to the :attr: `errcheck ` attribute.
1634
+ :attr: `! restype ` and assign a callable to the :attr: `errcheck ` attribute.
1634
1635
1635
1636
.. attribute :: argtypes
1636
1637
@@ -1662,7 +1663,7 @@ They are instances of a private class:
1662
1663
:module:
1663
1664
1664
1665
*result * is what the foreign function returns, as specified by the
1665
- :attr: `restype ` attribute.
1666
+ :attr: `! restype ` attribute.
1666
1667
1667
1668
*func * is the foreign function object itself, this allows reusing the
1668
1669
same callable object to check or post process the results of several
@@ -1772,7 +1773,7 @@ different ways, depending on the type and number of the parameters in the call:
1772
1773
1773
1774
COM methods use a special calling convention: They require a pointer to
1774
1775
the COM interface as first argument, in addition to those parameters that
1775
- are specified in the :attr: `~_FuncPtr. argtypes ` tuple.
1776
+ are specified in the :attr: `! argtypes ` tuple.
1776
1777
1777
1778
The optional *paramflags * parameter creates foreign function wrappers with much
1778
1779
more functionality than the features described above.
@@ -1847,7 +1848,7 @@ value if there is a single one, or a tuple containing the output parameter
1847
1848
values when there are more than one, so the GetWindowRect function now returns a
1848
1849
RECT instance, when called.
1849
1850
1850
- Output parameters can be combined with the :attr: `errcheck ` protocol to do
1851
+ Output parameters can be combined with the :attr: `~_FuncPtr. errcheck ` protocol to do
1851
1852
further output processing and error checking. The win32 ``GetWindowRect `` api
1852
1853
function returns a ``BOOL `` to signal success or failure, so this function could
1853
1854
do the error checking, and raises an exception when the api call failed::
@@ -1860,7 +1861,7 @@ do the error checking, and raises an exception when the api call failed::
1860
1861
>>> GetWindowRect.errcheck = errcheck
1861
1862
>>>
1862
1863
1863
- If the :attr: `errcheck ` function returns the argument tuple it receives
1864
+ If the :attr: `~_FuncPtr. errcheck ` function returns the argument tuple it receives
1864
1865
unchanged, :mod: `ctypes ` continues the normal processing it does on the output
1865
1866
parameters. If you want to return a tuple of window coordinates instead of a
1866
1867
``RECT `` instance, you can retrieve the fields in the function and return them
@@ -2010,7 +2011,7 @@ Utility functions
2010
2011
.. function :: get_last_error()
2011
2012
2012
2013
Windows only: returns the current value of the ctypes-private copy of the system
2013
- :data: `LastError ` variable in the calling thread.
2014
+ :data: `! LastError ` variable in the calling thread.
2014
2015
2015
2016
.. audit-event :: ctypes.get_last_error "" ctypes.get_last_error
2016
2017
@@ -2063,7 +2064,7 @@ Utility functions
2063
2064
.. function :: set_last_error(value)
206
10000
4
2065
2065
2066
Windows only: set the current value of the ctypes-private copy of the system
2066
- :data: `LastError ` variable in the calling thread to *value * and return the
2067
+ :data: `! LastError ` variable in the calling thread to *value * and return the
2067
2068
previous value.
2068
2069
2069
2070
.. audit-event :: ctypes.set_last_error error ctypes.set_last_error
@@ -2225,13 +2226,13 @@ Fundamental data types
2225
2226
Fundamental data types, when returned as foreign function call results, or, for
2226
2227
example, by retrieving structure field members or array items, are transparently
2227
2228
converted to native Python types. In other words, if a foreign function has a
2228
- :attr: `restype ` of :class: `c_char_p `, you will always receive a Python bytes
2229
+ :attr: `~_FuncPtr. restype ` of :class: `c_char_p `, you will always receive a Python bytes
2229
2230
object, *not * a :class: `c_char_p ` instance.
2230
2231
2231
2232
.. XXX above is false, it actually returns a Unicode string
2232
2233
2233
2234
Subclasses of fundamental data types do *not * inherit this behavior. So, if a
2234
- foreign functions :attr: `restype ` is a subclass of :class: `c_void_p `, you will
2235
+ foreign functions :attr: `! restype ` is a subclass of :class: `c_void_p `, you will
2235
2236
receive an instance of this subclass from the function call. Of course, you can
2236
2237
get the value of the pointer by accessing the ``value `` attribute.
2237
2238
@@ -2430,7 +2431,7 @@ These are the fundamental ctypes data types:
2430
2431
2431
2432
.. class :: HRESULT
2432
2433
2433
- Windows only: Represents a :c:type: `HRESULT ` value, which contains success or
2434
+ Windows only: Represents a :c:type: `! HRESULT ` value, which contains success or
2434
2435
error information for a function or method call.
2435
2436
2436
2437
@@ -2439,9 +2440,9 @@ These are the fundamental ctypes data types:
2439
2440
Represents the C :c:expr: `PyObject * ` datatype. Calling this without an
2440
2441
argument creates a ``NULL `` :c:expr: `PyObject * ` pointer.
2441
2442
2442
- The :mod: `ctypes.wintypes ` module provides quite some other Windows specific
2443
- data types, for example :c:type: `HWND `, :c:type: `WPARAM `, or :c:type: `DWORD `. Some
2444
- useful structures like :c:type: `MSG ` or :c:type: `RECT ` are also defined.
2443
+ The :mod: `! ctypes.wintypes ` module provides quite some other Windows specific
2444
+ data types, for example :c:type: `! HWND `, :c:type: `! WPARAM `, or :c:type: `! DWORD `.
2445
+ Some useful structures like :c:type: `! MSG ` or :c:type: `! RECT ` are also defined.
2445
2446
2446
2447
2447
2448
.. _ctypes-str
7E12
uctured-data-types :
0 commit comments