loads libraries which export functions using the standard ``cdecl`` calling
convention, while *windll* libraries call functions using the ``stdcall``
calling convention. *oledll* also uses the ``stdcall`` calling convention, and
-assumes the functions return a Windows :c:type:`HRESULT` error code. The error
+assumes the functions return a Windows :c:type:`!HRESULT` error code. The error
code is used to automatically raise an :class:`OSError` exception when the
function call fails.
By default functions are assumed to return the C :c:expr:`int` type. Other
-return types can be specified by setting the :attr:`restype` attribute of the
+return types can be specified by setting the :attr:`~_FuncPtr.restype` attribute of the
function object.
The C prototype of :c:func:`time` is ``time_t time(time_t *)``. Because :c:type:`time_t`
>>> print(libc.time(None)) # doctest: +SKIP
1150640792
-Here is a more advanced example, it uses the :func:`strchr` function, which expects
+Here is a more advanced example, it uses the :func:`!strchr` function, which expects
a string pointer and a char, and returns a pointer to a string::
>>> strchr = libc.strchr
>>>
You can also use a callable Python object (a function or a class for example) as
-the :attr:`restype` attribute, if the foreign function returns an integer. The
+the :attr:`~_FuncPtr.restype` attribute, if the foreign function returns an integer. The
callable will be called with the *integer* the C function returns, and the
result of this call will be used as the result of your function call. This is
useful to check for error return values and automatically raise an exception::
:func:`GetLastError` to retrieve it.
Please note that a much more powerful error checking mechanism is available
-through the :attr:`errcheck` attribute; see the reference manual for details.
+through the :attr:`~_FuncPtr.errcheck` attribute;
+see the reference manual for details.
.. _ctypes-passing-pointers:
Structures and unions must derive from the :class:`Structure` and :class:`Union`
base classes which are defined in the :mod:`ctypes` module. Each subclass must
-define a :attr:`_fields_` attribute. :attr:`_fields_` must be a list of
+define a :attr:`~Structure._fields_` attribute. :attr:`!_fields_` must be a list of
*2-tuples*, containing a *field name* and a *field type*.
The field type must be a :mod:`ctypes` type like :class:`c_int`, or any other
By default, Structure and Union fields are aligned in the same way the C
compiler does it. It is possible to override this behavior by specifying a
-:attr:`_pack_` class attribute in the subclass definition. This must be set to a
-positive integer and specifies the maximum alignment for the fields. This is
-what ``#pragma pack(n)`` also does in MSVC.
+:attr:`~Structure._pack_` class attribute in the subclass definition.
+This must be set to a positive integer and specifies the maximum alignment for the fields.
+This is what ``#pragma pack(n)`` also does in MSVC.
:mod:`ctypes` uses the native byte order for Structures and Unions. To build
structures with non-native byte order, you can use one of the
It is possible to create structures and unions containing bit fields. Bit fields
are only possible for integer fields, the bit width is specified as the third
-item in the :attr:`_fields_` tuples::
+item in the :attr:`~Structure._fields_` tuples::
>>> class Int(Structure):
... _fields_ = [("first_16", c_int, 16),
>>>
In addition, if a function argument is explicitly declared to be a pointer type
-(such as ``POINTER(c_int)``) in :attr:`_FuncPtr.argtypes`, an object of the pointed
+(such as ``POINTER(c_int)``) in :attr:`~_FuncPtr.argtypes`, an object of the pointed
type (``c_int`` in this case) can be passed to the function. ctypes will apply
the required :func:`byref` conversion in this case automatically.
>>>
because the new ``class cell`` is not available in the class statement itself.
-In :mod:`ctypes`, we can define the ``cell`` class and set the :attr:`_fields_`
-attribute later, after the class statement::
+In :mod:`ctypes`, we can define the ``cell`` class and set the
+:attr:`~Structure._fields_` attribute later, after the class statement::
>>> from ctypes import *
>>> class cell(Structure):
arguments.
I will present an example here which uses the standard C library's
-:c:func:`qsort` function, that is used to sort items with the help of a callback
-function. :c:func:`qsort` will be used to sort an array of integers::
+:c:func:`!qsort` function, that is used to sort items with the help of a callback
+function. :c:func:`!qsort` will be used to sort an array of integers::
>>> IntArray5 = c_int * 5
>>> ia = IntArray5(5, 1, 7, 33, 99)
>>> qsort.restype = None
>>>
-:func:`qsort` must be called with a pointer to the data to sort, the number of
+:func:`!qsort` must be called with a pointer to the data to sort, the number of
items in the data array, the size of one item, and a pointer to the comparison
function, the callback. The callback will then be called with two pointers to
items, and it must return a negative integer if the first item is smaller than
example in the Python library itself is the :c:data:`Py_Version`, Python
runtime version number encoded in a single constant integer.
-:mod:`ctypes` can access values like this with the :meth:`in_dll` class methods of
+:mod:`ctypes` can access values like this with the :meth:`~_CData.in_dll` class methods of
the type. *pythonapi* is a predefined symbol giving access to the Python C
api::
When programming in a compiled language, shared libraries are accessed when
compiling/linking a program, and when the program is run.
-The purpose of the :func:`find_library` function is to locate a library in a way
+The purpose of the :func:`~ctypes.util.find_library` function is to locate a library in a way
similar to what the compiler or runtime loader does (on platforms with several
versions of a shared library the most recent should be loaded), while the ctypes
library loaders act like when a program is run, and call the runtime loader
directly.
-The :mod:`ctypes.util` module provides a function which can help to determine
+The :mod:`!ctypes.util` module provides a function which can help to determine
the library to load.
The exact functionality is system dependent.
-On Linux, :func:`find_library` tries to run external programs
+On Linux, :func:`~ctypes.util.find_library` tries to run external programs
(``/sbin/ldconfig``, ``gcc``, ``objdump`` and ``ld``) to find the library file.
It returns the filename of the library file.
'libbz2.so.1.0'
>>>
-On macOS, :func:`find_library` tries several predefined naming schemes and paths
+On macOS, :func:`~ctypes.util.find_library` tries several predefined naming schemes and paths
to locate the library, and returns a full pathname if successful::
>>> from ctypes.util import find_library
'/System/Library/Frameworks/AGL.framework/AGL'
>>>
-On Windows, :func:`find_library` searches along the system search path, and
+On Windows, :func:`~ctypes.util.find_library` searches along the system search path, and
returns the full pathname, but since there is no predefined naming scheme a call
like ``find_library("c")`` will fail and return ``None``.
If wrapping a shared library with :mod:`ctypes`, it *may* be better to determine
the shared library name at development time, and hardcode that into the wrapper
-module instead of using :func:`find_library` to locate the library at runtime.
+module instead of using :func:`~ctypes.util.find_library` to locate the library at runtime.
.. _ctypes-loading-shared-libraries:
All these classes can be instantiated by calling them with at least one
argument, the pathname of the shared library. If you have an existing handle to
an already loaded shared library, it can be passed as the ``handle`` named
-parameter, otherwise the underlying platforms :c:func:`!dlopen` or :c:func:`LoadLibrary`
-function is used to load the library into the process, and to get a handle to
-it.
+parameter, otherwise the underlying platforms :c:func:`!dlopen` or
+:c:func:`!LoadLibrary` function is used to load the library into
+the process, and to get a handle to it.
The *mode* parameter can be used to specify how the library is loaded. For
details, consult the :manpage:`dlopen(3)` manpage. On Windows, *mode* is
The *use_last_error* parameter, when set to true, enables the same mechanism for
the Windows error code which is managed by the :func:`GetLastError` and
-:func:`SetLastError` Windows API functions; :func:`ctypes.get_last_error` and
+:func:`!SetLastError` Windows API functions; :func:`ctypes.get_last_error` and
:func:`ctypes.set_last_error` are used to request and change the ctypes private
copy of the windows error code.
Class which loads shared libraries. *dlltype* should be one of the
:class:`CDLL`, :class:`PyDLL`, :class:`WinDLL`, or :class:`OleDLL` types.
- :meth:`__getattr__` has special behavior: It allows loading a shared library by
+ :meth:`!__getattr__` has special behavior: It allows loading a shared library by
accessing it as attribute of a library loader instance. The result is cached,
so repeated attribute accesses return the same library each time.
An instance of :class:`PyDLL` that exposes Python C API functions as
attributes. Note that all these functions are assumed to return C
:c:expr:`int`, which is of course not always the truth, so you have to assign
- the correct :attr:`restype` attribute to use these functions.
+ the correct :attr:`!restype` attribute to use these functions.
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
the callable will be called with this integer, allowing further
processing or error checking. Using this is deprecated, for more flexible
post processing or error checking use a ctypes data type as
- :attr:`restype` and assign a callable to the :attr:`errcheck` attribute.
+ :attr:`!restype` and assign a callable to the :attr:`errcheck` attribute.
.. attribute:: argtypes
:module:
*result* is what the foreign function returns, as specified by the
- :attr:`restype` attribute.
+ :attr:`!restype` attribute.
*func* is the foreign function object itself, this allows reusing the
same callable object to check or post process the results of several
COM methods use a special calling convention: They require a pointer to
the COM interface as first argument, in addition to those parameters that
- are specified in the :attr:`~_FuncPtr.argtypes` tuple.
+ are specified in the :attr:`!argtypes` tuple.
The optional *paramflags* parameter creates foreign function wrappers with much
more functionality than the features described above.
values when there are more than one, so the GetWindowRect function now returns a
RECT instance, when called.
-Output parameters can be combined with the :attr:`errcheck` protocol to do
+Output parameters can be combined with the :attr:`~_FuncPtr.errcheck` protocol to do
further output processing and error checking. The win32 ``GetWindowRect`` api
function returns a ``BOOL`` to signal success or failure, so this function could
do the error checking, and raises an exception when the api call failed::
>>> GetWindowRect.errcheck = errcheck
>>>
-If the :attr:`errcheck` function returns the argument tuple it receives
+If the :attr:`~_FuncPtr.errcheck` function returns the argument tuple it receives
unchanged, :mod:`ctypes` continues the normal processing it does on the output
parameters. If you want to return a tuple of window coordinates instead of a
``RECT`` instance, you can retrieve the fields in the function and return them
.. function:: get_last_error()
Windows only: returns the current value of the ctypes-private copy of the system
- :data:`LastError` variable in the calling thread.
+ :data:`!LastError` variable in the calling thread.
.. audit-event:: ctypes.get_last_error "" ctypes.get_last_error
.. function:: set_last_error(value)
Windows only: set the current value of the ctypes-private copy of the system
- :data:`LastError` variable in the calling thread to *value* and return the
+ :data:`!LastError` variable in the calling thread to *value* and return the
previous value.
.. audit-event:: ctypes.set_last_error error ctypes.set_last_error
Fundamental data types, when returned as foreign function call results, or, for
example, by retrieving structure field members or array items, are transparently
converted to native Python types. In other words, if a foreign function has a
-:attr:`restype` of :class:`c_char_p`, you will always receive a Python bytes
+:attr:`~_FuncPtr.restype` of :class:`c_char_p`, you will always receive a Python bytes
object, *not* a :class:`c_char_p` instance.
.. XXX above is false, it actually returns a Unicode string
Subclasses of fundamental data types do *not* inherit this behavior. So, if a
-foreign functions :attr:`restype` is a subclass of :class:`c_void_p`, you will
+foreign functions :attr:`!restype` is a subclass of :class:`c_void_p`, you will
receive an instance of this subclass from the function call. Of course, you can
get the value of the pointer by accessing the ``value`` attribute.
.. class:: HRESULT
- Windows only: Represents a :c:type:`HRESULT` value, which contains success or
+ Windows only: Represents a :c:type:`!HRESULT` value, which contains success or
error information for a function or method call.
Represents the C :c:expr:`PyObject *` datatype. Calling this without an
argument creates a ``NULL`` :c:expr:`PyObject *` pointer.
-The :mod:`ctypes.wintypes` module provides quite some other Windows specific
-data types, for example :c:type:`HWND`, :c:type:`WPARAM`, or :c:type:`DWORD`. Some
-useful structures like :c:type:`MSG` or :c:type:`RECT` are also defined.
+The :mod:`!ctypes.wintypes` module provides quite some other Windows specific
+data types, for example :c:type:`!HWND`, :c:type:`!WPARAM`, or :c:type:`!DWORD`.
+Some useful structures like :c:type:`!MSG` or :c:type:`!RECT` are also defined.
.. _ctypes-structured-data-types: