Kaydet (Commit) aae8e3e4 authored tarafından Georg Brandl's avatar Georg Brandl

Merged revisions 77108-77109,77111 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r77108 | georg.brandl | 2009-12-29 11:34:34 +0100 (Di, 29 Dez 2009) | 1 line

  #7569: clarification about c_char_p.
........
  r77109 | georg.brandl | 2009-12-29 12:06:31 +0100 (Di, 29 Dez 2009) | 1 line

  Improve markup of ctypes docs.
........
  r77111 | georg.brandl | 2009-12-29 12:25:38 +0100 (Di, 29 Dez 2009) | 1 line

  Fix wrong markup.
........
üst 33613676
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
.. versionadded:: 2.5 .. versionadded:: 2.5
``ctypes`` is a foreign function library for Python. It provides C compatible :mod:`ctypes` is a foreign function library for Python. It provides C compatible
data types, and allows calling functions in DLLs or shared libraries. It can be data types, and allows calling functions in DLLs or shared libraries. It can be
used to wrap these libraries in pure Python. used to wrap these libraries in pure Python.
...@@ -18,9 +18,9 @@ used to wrap these libraries in pure Python. ...@@ -18,9 +18,9 @@ used to wrap these libraries in pure Python.
ctypes tutorial ctypes tutorial
--------------- ---------------
Note: The code samples in this tutorial use ``doctest`` to make sure that they Note: The code samples in this tutorial use :mod:`doctest` to make sure that
actually work. Since some code samples behave differently under Linux, Windows, they actually work. Since some code samples behave differently under Linux,
or Mac OS X, they contain doctest directives in comments. Windows, or Mac OS X, they contain doctest directives in comments.
Note: Some code samples reference the ctypes :class:`c_int` type. This type is Note: Some code samples reference the ctypes :class:`c_int` type. This type is
an alias for the :class:`c_long` type on 32-bit systems. So, you should not be an alias for the :class:`c_long` type on 32-bit systems. So, you should not be
...@@ -33,16 +33,16 @@ they are actually the same type. ...@@ -33,16 +33,16 @@ they are actually the same type.
Loading dynamic link libraries Loading dynamic link libraries
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
``ctypes`` exports the *cdll*, and on Windows *windll* and *oledll* :mod:`ctypes` exports the *cdll*, and on Windows *windll* and *oledll*
objects, for loading dynamic link libraries. objects, for loading dynamic link libraries.
You load libraries by accessing them as attributes of these objects. *cdll* You load libraries by accessing them as attributes of these objects. *cdll*
loads libraries which export functions using the standard ``cdecl`` calling loads libraries which export functions using the standard ``cdecl`` calling
convention, while *windll* libraries call functions using the ``stdcall`` convention, while *windll* libraries call functions using the ``stdcall``
calling convention. *oledll* also uses the ``stdcall`` calling convention, and calling convention. *oledll* also uses the ``stdcall`` calling convention, and
assumes the functions return a Windows :class:`HRESULT` error code. The error assumes the functions return a Windows :ctype:`HRESULT` error code. The error
code is used to automatically raise a :class:`WindowsError` exception when code is used to automatically raise a :class:`WindowsError` exception when the
the function call fails. function call fails.
Here are some examples for Windows. Note that ``msvcrt`` is the MS standard C Here are some examples for Windows. Note that ``msvcrt`` is the MS standard C
library containing most standard C functions, and uses the cdecl calling library containing most standard C functions, and uses the cdecl calling
...@@ -112,8 +112,8 @@ explicitly, and then call it with strings or unicode strings ...@@ -112,8 +112,8 @@ explicitly, and then call it with strings or unicode strings
respectively. respectively.
Sometimes, dlls export functions with names which aren't valid Python Sometimes, dlls export functions with names which aren't valid Python
identifiers, like ``"??2@YAPAXI@Z"``. In this case you have to use ``getattr`` identifiers, like ``"??2@YAPAXI@Z"``. In this case you have to use
to retrieve the function:: :func:`getattr` to retrieve the function::
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z") # doctest: +WINDOWS >>> getattr(cdll.msvcrt, "??2@YAPAXI@Z") # doctest: +WINDOWS
<_FuncPtr object at 0x...> <_FuncPtr object at 0x...>
...@@ -152,8 +152,8 @@ as the NULL pointer):: ...@@ -152,8 +152,8 @@ as the NULL pointer)::
0x1d000000 0x1d000000
>>> >>>
``ctypes`` tries to protect you from calling functions with the wrong number of :mod:`ctypes` tries to protect you from calling functions with the wrong number
arguments or the wrong calling convention. Unfortunately this only works on of arguments or the wrong calling convention. Unfortunately this only works on
Windows. It does this by examining the stack after the function returns, so Windows. It does this by examining the stack after the function returns, so
although an error is raised the function *has* been called:: although an error is raised the function *has* been called::
...@@ -185,7 +185,7 @@ The same exception is raised when you call an ``stdcall`` function with the ...@@ -185,7 +185,7 @@ The same exception is raised when you call an ``stdcall`` function with the
To find out the correct calling convention you have to look into the C header To find out the correct calling convention you have to look into the C header
file or the documentation for the function you want to call. file or the documentation for the function you want to call.
On Windows, ``ctypes`` uses win32 structured exception handling to prevent On Windows, :mod:`ctypes` uses win32 structured exception handling to prevent
crashes from general protection faults when functions are called with invalid crashes from general protection faults when functions are called with invalid
argument values:: argument values::
...@@ -195,18 +195,19 @@ argument values:: ...@@ -195,18 +195,19 @@ argument values::
WindowsError: exception: access violation reading 0x00000020 WindowsError: exception: access violation reading 0x00000020
>>> >>>
There are, however, enough ways to crash Python with ``ctypes``, so you should There are, however, enough ways to crash Python with :mod:`ctypes`, so you
be careful anyway. should be careful anyway.
``None``, integers, longs, byte strings and unicode strings are the only native ``None``, integers, longs, byte strings and unicode strings are the only native
Python objects that can directly be used as parameters in these function calls. Python objects that can directly be used as parameters in these function calls.
``None`` is passed as a C ``NULL`` pointer, byte strings and unicode strings are ``None`` is passed as a C ``NULL`` pointer, byte strings and unicode strings are
passed as pointer to the memory block that contains their data (``char *`` or passed as pointer to the memory block that contains their data (:ctype:`char *`
``wchar_t *``). Python integers and Python longs are passed as the platforms or :ctype:`wchar_t *`). Python integers and Python longs are passed as the
default C ``int`` type, their value is masked to fit into the C type. platforms default C :ctype:`int` type, their value is masked to fit into the C
type.
Before we move on calling functions with other parameter types, we have to learn Before we move on calling functions with other parameter types, we have to learn
more about ``ctypes`` data types. more about :mod:`ctypes` data types.
.. _ctypes-fundamental-data-types: .. _ctypes-fundamental-data-types:
...@@ -214,49 +215,48 @@ more about ``ctypes`` data types. ...@@ -214,49 +215,48 @@ more about ``ctypes`` data types.
Fundamental data types Fundamental data types
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
``ctypes`` defines a number of primitive C compatible data types : :mod:`ctypes` defines a number of primitive C compatible data types :
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| ctypes type | C type | Python type | | ctypes type | C type | Python type |
+======================+================================+============================+ +======================+========================================+============================+
| :class:`c_char` | ``char`` | 1-character string | | :class:`c_char` | :ctype:`char` | 1-character string |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_wchar` | ``wchar_t`` | 1-character unicode string | | :class:`c_wchar` | :ctype:`wchar_t` | 1-character unicode string |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_byte` | ``char`` | int/long | | :class:`c_byte` | :ctype:`char` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_ubyte` | ``unsigned char`` | int/long | | :class:`c_ubyte` | :ctype:`unsigned char` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_short` | ``short`` | int/long | | :class:`c_short` | :ctype:`short` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_ushort` | ``unsigned short`` | int/long | | :class:`c_ushort` | :ctype:`unsigned short` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_int` | ``int`` | int/long | | :class:`c_int` | :ctype:`int` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_uint` | ``unsigned int`` | int/long | | :class:`c_uint` | :ctype:`unsigned int` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_long` | ``long`` | int/long | | :class:`c_long` | :ctype:`long` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_ulong` | ``unsigned long`` | int/long | | :class:`c_ulong` | :ctype:`unsigned long` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_longlong` | ``__int64`` or ``long long`` | int/long | | :class:`c_longlong` | :ctype:`__int64` or :ctype:`long long` | int/long |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_ulonglong` | ``unsigned __int64`` or | int/long | | :class:`c_ulonglong` | :ctype:`unsigned __int64` or | int/long |
| | ``unsigned long long`` | | | | :ctype:`unsigned long long` | |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_float` | ``float`` | float | | :class:`c_float` | :ctype:`float` | float |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_double` | ``double`` | float | | :class:`c_double` | :ctype:`double` | float |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_longdouble`| ``long double`` | float | | :class:`c_longdouble`| :ctype:`long double` | float |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_char_p` | ``char *`` (NUL terminated) | string or ``None`` | | :class:`c_char_p` | :ctype:`char *` (NUL terminated) | string or ``None`` |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_wchar_p` | ``wchar_t *`` (NUL terminated) | unicode or ``None`` | | :class:`c_wchar_p` | :ctype:`wchar_t *` (NUL terminated) | unicode or ``None`` |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
| :class:`c_void_p` | ``void *`` | int/long or ``None`` | | :class:`c_void_p` | :ctype:`void *` | int/long or ``None`` |
+----------------------+--------------------------------+----------------------------+ +----------------------+----------------------------------------+----------------------------+
All these types can be created by calling them with an optional initializer of All these types can be created by calling them with an optional initializer of
the correct type and value:: the correct type and value::
...@@ -299,7 +299,7 @@ strings are immutable):: ...@@ -299,7 +299,7 @@ strings are immutable)::
You should be careful, however, not to pass them to functions expecting pointers You should be careful, however, not to pass them to functions expecting pointers
to mutable memory. If you need mutable memory blocks, ctypes has a to mutable memory. If you need mutable memory blocks, ctypes has a
``create_string_buffer`` function which creates these in various ways. The :func:`create_string_buffer` function which creates these in various ways. The
current memory block contents can be accessed (or changed) with the ``raw`` current memory block contents can be accessed (or changed) with the ``raw``
property; if you want to access it as NUL terminated string, use the ``value`` property; if you want to access it as NUL terminated string, use the ``value``
property:: property::
...@@ -321,10 +321,11 @@ property:: ...@@ -321,10 +321,11 @@ property::
10 'Hi\x00lo\x00\x00\x00\x00\x00' 10 'Hi\x00lo\x00\x00\x00\x00\x00'
>>> >>>
The ``create_string_buffer`` function replaces the ``c_buffer`` function (which The :func:`create_string_buffer` function replaces the :func:`c_buffer` function
is still available as an alias), as well as the ``c_string`` function from (which is still available as an alias), as well as the :func:`c_string` function
earlier ctypes releases. To create a mutable memory block containing unicode from earlier ctypes releases. To create a mutable memory block containing
characters of the C type ``wchar_t`` use the ``create_unicode_buffer`` function. unicode characters of the C type :ctype:`wchar_t` use the
:func:`create_unicode_buffer` function.
.. _ctypes-calling-functions-continued: .. _ctypes-calling-functions-continued:
...@@ -333,8 +334,8 @@ Calling functions, continued ...@@ -333,8 +334,8 @@ Calling functions, continued
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note that printf prints to the real standard output channel, *not* to Note that printf prints to the real standard output channel, *not* to
``sys.stdout``, so these examples will only work at the console prompt, not from :data:`sys.stdout`, so these examples will only work at the console prompt, not
within *IDLE* or *PythonWin*:: from within *IDLE* or *PythonWin*::
>>> printf = libc.printf >>> printf = libc.printf
>>> printf("Hello, %s\n", "World!") >>> printf("Hello, %s\n", "World!")
...@@ -353,7 +354,7 @@ within *IDLE* or *PythonWin*:: ...@@ -353,7 +354,7 @@ within *IDLE* or *PythonWin*::
>>> >>>
As has been mentioned before, all Python types except integers, strings, and As has been mentioned before, all Python types except integers, strings, and
unicode strings have to be wrapped in their corresponding ``ctypes`` type, so unicode strings have to be wrapped in their corresponding :mod:`ctypes` type, so
that they can be converted to the required C data type:: that they can be converted to the required C data type::
>>> printf("An int %d, a double %f\n", 1234, c_double(3.14)) >>> printf("An int %d, a double %f\n", 1234, c_double(3.14))
...@@ -367,9 +368,9 @@ that they can be converted to the required C data type:: ...@@ -367,9 +368,9 @@ that they can be converted to the required C data type::
Calling functions with your own custom data types Calling functions with your own custom data types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can also customize ``ctypes`` argument conversion to allow instances of your You can also customize :mod:`ctypes` argument conversion to allow instances of
own classes be used as function arguments. ``ctypes`` looks for an your own classes be used as function arguments. :mod:`ctypes` looks for an
:attr:`_as_parameter_` attribute and uses this as the function argument. Of :attr:`_as_parameter_` attribute and uses this as the function argument. Of
course, it must be one of integer, string, or unicode:: course, it must be one of integer, string, or unicode::
>>> class Bottles(object): >>> class Bottles(object):
...@@ -383,7 +384,7 @@ course, it must be one of integer, string, or unicode:: ...@@ -383,7 +384,7 @@ course, it must be one of integer, string, or unicode::
>>> >>>
If you don't want to store the instance's data in the :attr:`_as_parameter_` If you don't want to store the instance's data in the :attr:`_as_parameter_`
instance variable, you could define a ``property`` which makes the data instance variable, you could define a :func:`property` which makes the data
available. available.
...@@ -425,7 +426,7 @@ the Python object passed to the function call, it should do a typecheck or ...@@ -425,7 +426,7 @@ the Python object passed to the function call, it should do a typecheck or
whatever is needed to make sure this object is acceptable, and then return the whatever is needed to make sure this object is acceptable, and then return the
object itself, its :attr:`_as_parameter_` attribute, or whatever you want to object itself, its :attr:`_as_parameter_` attribute, or whatever you want to
pass as the C function argument in this case. Again, the result should be an pass as the C function argument in this case. Again, the result should be an
integer, string, unicode, a ``ctypes`` instance, or an object with an integer, string, unicode, a :mod:`ctypes` instance, or an object with an
:attr:`_as_parameter_` attribute. :attr:`_as_parameter_` attribute.
...@@ -434,9 +435,9 @@ integer, string, unicode, a ``ctypes`` instance, or an object with an ...@@ -434,9 +435,9 @@ integer, string, unicode, a ``ctypes`` instance, or an object with an
Return types Return types
^^^^^^^^^^^^ ^^^^^^^^^^^^
By default functions are assumed to return the C ``int`` type. Other return By default functions are assumed to return the C :ctype:`int` type. Other
types can be specified by setting the :attr:`restype` attribute of the function return types can be specified by setting the :attr:`restype` attribute of the
object. function object.
Here is a more advanced example, it uses the ``strchr`` function, which expects Here is a more advanced example, it uses the ``strchr`` function, which expects
a string pointer and a char, and returns a pointer to a string:: a string pointer and a char, and returns a pointer to a string::
...@@ -471,7 +472,7 @@ single character Python string into a C char:: ...@@ -471,7 +472,7 @@ single character Python string into a C char::
You can also use a callable Python object (a function or a class for example) as 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:`restype` attribute, if the foreign function returns an integer. The
callable will be called with the ``integer`` the C function returns, and 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 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:: useful to check for error return values and automatically raise an exception::
...@@ -510,11 +511,11 @@ Sometimes a C api function expects a *pointer* to a data type as parameter, ...@@ -510,11 +511,11 @@ Sometimes a C api function expects a *pointer* to a data type as parameter,
probably to write into the corresponding location, or if the data is too large probably to write into the corresponding location, or if the data is too large
to be passed by value. This is also known as *passing parameters by reference*. to be passed by value. This is also known as *passing parameters by reference*.
``ctypes`` exports the :func:`byref` function which is used to pass parameters :mod:`ctypes` exports the :func:`byref` function which is used to pass
by reference. The same effect can be achieved with the ``pointer`` function, parameters by reference. The same effect can be achieved with the
although ``pointer`` does a lot more work since it constructs a real pointer :func:`pointer` function, although :func:`pointer` does a lot more work since it
object, so it is faster to use :func:`byref` if you don't need the pointer constructs a real pointer object, so it is faster to use :func:`byref` if you
object in Python itself:: don't need the pointer object in Python itself::
>>> i = c_int() >>> i = c_int()
>>> f = c_float() >>> f = c_float()
...@@ -535,16 +536,15 @@ Structures and unions ...@@ -535,16 +536,15 @@ Structures and unions
^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
Structures and unions must derive from the :class:`Structure` and :class:`Union` Structures and unions must derive from the :class:`Structure` and :class:`Union`
base classes which are defined in the ``ctypes`` module. Each subclass must 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:`_fields_` attribute. :attr:`_fields_` must be a list of
*2-tuples*, containing a *field name* and a *field type*. *2-tuples*, containing a *field name* and a *field type*.
The field type must be a ``ctypes`` type like :class:`c_int`, or any other The field type must be a :mod:`ctypes` type like :class:`c_int`, or any other
derived ``ctypes`` type: structure, union, array, pointer. derived :mod:`ctypes` type: structure, union, array, pointer.
Here is a simple example of a POINT structure, which contains two integers named Here is a simple example of a POINT structure, which contains two integers named
``x`` and ``y``, and also shows how to initialize a structure in the *x* and *y*, and also shows how to initialize a structure in the constructor::
constructor::
>>> from ctypes import * >>> from ctypes import *
>>> class POINT(Structure): >>> class POINT(Structure):
...@@ -566,8 +566,8 @@ constructor:: ...@@ -566,8 +566,8 @@ constructor::
You can, however, build much more complicated structures. Structures can itself You can, however, build much more complicated structures. Structures can itself
contain other structures by using a structure as a field type. contain other structures by using a structure as a field type.
Here is a RECT structure which contains two POINTs named ``upperleft`` and Here is a RECT structure which contains two POINTs named *upperleft* and
``lowerright`` :: *lowerright*::
>>> class RECT(Structure): >>> class RECT(Structure):
... _fields_ = [("upperleft", POINT), ... _fields_ = [("upperleft", POINT),
...@@ -606,10 +606,11 @@ compiler does it. It is possible to override this behavior be specifying a ...@@ -606,10 +606,11 @@ compiler does it. It is possible to override this behavior be specifying a
positive integer and specifies the maximum alignment for the fields. This is positive integer and specifies the maximum alignment for the fields. This is
what ``#pragma pack(n)`` also does in MSVC. what ``#pragma pack(n)`` also does in MSVC.
``ctypes`` uses the native byte order for Structures and Unions. To build :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 structures with non-native byte order, you can use one of the
BigEndianStructure, LittleEndianStructure, BigEndianUnion, and LittleEndianUnion :class:`BigEndianStructure`, :class:`LittleEndianStructure`,
base classes. These classes cannot contain pointer fields. :class:`BigEndianUnion`, and :class:`LittleEndianUnion` base classes. These
classes cannot contain pointer fields.
.. _ctypes-bit-fields-in-structures-unions: .. _ctypes-bit-fields-in-structures-unions:
...@@ -687,22 +688,22 @@ Initializers of the correct type can also be specified:: ...@@ -687,22 +688,22 @@ Initializers of the correct type can also be specified::
Pointers Pointers
^^^^^^^^ ^^^^^^^^
Pointer instances are created by calling the ``pointer`` function on a Pointer instances are created by calling the :func:`pointer` function on a
``ctypes`` type:: :mod:`ctypes` type::
>>> from ctypes import * >>> from ctypes import *
>>> i = c_int(42) >>> i = c_int(42)
>>> pi = pointer(i) >>> pi = pointer(i)
>>> >>>
Pointer instances have a ``contents`` attribute which returns the object to Pointer instances have a :attr:`contents` attribute which returns the object to
which the pointer points, the ``i`` object above:: which the pointer points, the ``i`` object above::
>>> pi.contents >>> pi.contents
c_long(42) c_long(42)
>>> >>>
Note that ``ctypes`` does not have OOR (original object return), it constructs a Note that :mod:`ctypes` does not have OOR (original object return), it constructs a
new, equivalent object each time you retrieve an attribute:: new, equivalent object each time you retrieve an attribute::
>>> pi.contents is i >>> pi.contents is i
...@@ -720,7 +721,8 @@ would cause the pointer to point to the memory location where this is stored:: ...@@ -720,7 +721,8 @@ would cause the pointer to point to the memory location where this is stored::
c_long(99) c_long(99)
>>> >>>
.. XXX Document dereferencing pointers, and that it is preferred over the .contents attribute. .. XXX Document dereferencing pointers, and that it is preferred over the
.contents attribute.
Pointer instances can also be indexed with integers:: Pointer instances can also be indexed with integers::
...@@ -743,10 +745,10 @@ Generally you only use this feature if you receive a pointer from a C function, ...@@ -743,10 +745,10 @@ Generally you only use this feature if you receive a pointer from a C function,
and you *know* that the pointer actually points to an array instead of a single and you *know* that the pointer actually points to an array instead of a single
item. item.
Behind the scenes, the ``pointer`` function does more than simply create pointer Behind the scenes, the :func:`pointer` function does more than simply create
instances, it has to create pointer *types* first. This is done with the pointer instances, it has to create pointer *types* first. This is done with
``POINTER`` function, which accepts any ``ctypes`` type, and returns a new the :func:`POINTER` function, which accepts any :mod:`ctypes` type, and returns
type:: a new type::
>>> PI = POINTER(c_int) >>> PI = POINTER(c_int)
>>> PI >>> PI
...@@ -767,7 +769,7 @@ Calling the pointer type without an argument creates a ``NULL`` pointer. ...@@ -767,7 +769,7 @@ Calling the pointer type without an argument creates a ``NULL`` pointer.
False False
>>> >>>
``ctypes`` checks for ``NULL`` when dereferencing pointers (but dereferencing :mod:`ctypes` checks for ``NULL`` when dereferencing pointers (but dereferencing
invalid non-\ ``NULL`` pointers would crash Python):: invalid non-\ ``NULL`` pointers would crash Python)::
>>> null_ptr[0] >>> null_ptr[0]
...@@ -816,8 +818,8 @@ To set a POINTER type field to ``NULL``, you can assign ``None``:: ...@@ -816,8 +818,8 @@ To set a POINTER type field to ``NULL``, you can assign ``None``::
.. XXX list other conversions... .. XXX list other conversions...
Sometimes you have instances of incompatible types. In C, you can cast one Sometimes you have instances of incompatible types. In C, you can cast one type
type into another type. ``ctypes`` provides a ``cast`` function which can be into another type. :mod:`ctypes` provides a :func:`cast` function which can be
used in the same way. The ``Bar`` structure defined above accepts used in the same way. The ``Bar`` structure defined above accepts
``POINTER(c_int)`` pointers or :class:`c_int` arrays for its ``values`` field, ``POINTER(c_int)`` pointers or :class:`c_int` arrays for its ``values`` field,
but not instances of other types:: but not instances of other types::
...@@ -828,20 +830,20 @@ but not instances of other types:: ...@@ -828,20 +830,20 @@ but not instances of other types::
TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance
>>> >>>
For these cases, the ``cast`` function is handy. For these cases, the :func:`cast` function is handy.
The ``cast`` function can be used to cast a ctypes instance into a pointer to a The :func:`cast` function can be used to cast a ctypes instance into a pointer
different ctypes data type. ``cast`` takes two parameters, a ctypes object that to a different ctypes data type. :func:`cast` takes two parameters, a ctypes
is or can be converted to a pointer of some kind, and a ctypes pointer type. It object that is or can be converted to a pointer of some kind, and a ctypes
returns an instance of the second argument, which references the same memory pointer type. It returns an instance of the second argument, which references
block as the first argument:: the same memory block as the first argument::
>>> a = (c_byte * 4)() >>> a = (c_byte * 4)()
>>> cast(a, POINTER(c_int)) >>> cast(a, POINTER(c_int))
<ctypes.LP_c_long object at ...> <ctypes.LP_c_long object at ...>
>>> >>>
So, ``cast`` can be used to assign to the ``values`` field of ``Bar`` the So, :func:`cast` can be used to assign to the ``values`` field of ``Bar`` the
structure:: structure::
>>> bar = Bar() >>> bar = Bar()
...@@ -881,7 +883,7 @@ work:: ...@@ -881,7 +883,7 @@ work::
>>> >>>
because the new ``class cell`` is not available in the class statement itself. because the new ``class cell`` is not available in the class statement itself.
In ``ctypes``, we can define the ``cell`` class and set the :attr:`_fields_` In :mod:`ctypes`, we can define the ``cell`` class and set the :attr:`_fields_`
attribute later, after the class statement:: attribute later, after the class statement::
>>> from ctypes import * >>> from ctypes import *
...@@ -915,7 +917,7 @@ other, and finally follow the pointer chain a few times:: ...@@ -915,7 +917,7 @@ other, and finally follow the pointer chain a few times::
Callback functions Callback functions
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
``ctypes`` allows to create C callable function pointers from Python callables. :mod:`ctypes` allows to create C callable function pointers from Python callables.
These are sometimes called *callback functions*. These are sometimes called *callback functions*.
First, you must create a class for the callback function, the class knows the First, you must create a class for the callback function, the class knows the
...@@ -1064,7 +1066,7 @@ As we can easily check, our array is sorted now:: ...@@ -1064,7 +1066,7 @@ As we can easily check, our array is sorted now::
**Important note for callback functions:** **Important note for callback functions:**
Make sure you keep references to CFUNCTYPE objects as long as they are used from Make sure you keep references to CFUNCTYPE objects as long as they are used from
C code. ``ctypes`` doesn't, and if you don't, they may be garbage collected, C code. :mod:`ctypes` doesn't, and if you don't, they may be garbage collected,
crashing your program when a callback is made. crashing your program when a callback is made.
...@@ -1078,7 +1080,7 @@ example in the Python library itself is the ``Py_OptimizeFlag``, an integer set ...@@ -1078,7 +1080,7 @@ example in the Python library itself is the ``Py_OptimizeFlag``, an integer set
to 0, 1, or 2, depending on the :option:`-O` or :option:`-OO` flag given on to 0, 1, or 2, depending on the :option:`-O` or :option:`-OO` flag given on
startup. startup.
``ctypes`` can access values like this with the :meth:`in_dll` class methods of :mod:`ctypes` can access values like this with the :meth:`in_dll` class methods of
the type. *pythonapi* is a predefined symbol giving access to the Python C the type. *pythonapi* is a predefined symbol giving access to the Python C
api:: api::
...@@ -1101,7 +1103,7 @@ could play tricks with this to provide a dynamically created collection of ...@@ -1101,7 +1103,7 @@ could play tricks with this to provide a dynamically created collection of
frozen modules.* frozen modules.*
So manipulating this pointer could even prove useful. To restrict the example So manipulating this pointer could even prove useful. To restrict the example
size, we show only how this table can be read with ``ctypes``:: size, we show only how this table can be read with :mod:`ctypes`::
>>> from ctypes import * >>> from ctypes import *
>>> >>>
...@@ -1146,7 +1148,7 @@ testing. Try it out with ``import __hello__`` for example. ...@@ -1146,7 +1148,7 @@ testing. Try it out with ``import __hello__`` for example.
Surprises Surprises
^^^^^^^^^ ^^^^^^^^^
There are some edges in ``ctypes`` where you may be expect something else than There are some edges in :mod:`ctypes` where you may be expect something else than
what actually happens. what actually happens.
Consider the following example:: Consider the following example::
...@@ -1209,13 +1211,13 @@ constructs a new Python object each time! ...@@ -1209,13 +1211,13 @@ constructs a new Python object each time!
Variable-sized data types Variable-sized data types
^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^
``ctypes`` provides some support for variable-sized arrays and structures. :mod:`ctypes` provides some support for variable-sized arrays and structures.
The ``resize`` function can be used to resize the memory buffer of an existing The :func:`resize` function can be used to resize the memory buffer of an
ctypes object. The function takes the object as first argument, and the existing ctypes object. The function takes the object as first argument, and
requested size in bytes as the second argument. The memory block cannot be made the requested size in bytes as the second argument. The memory block cannot be
smaller than the natural memory block specified by the objects type, a made smaller than the natural memory block specified by the objects type, a
``ValueError`` is raised if this is tried:: :exc:`ValueError` is raised if this is tried::
>>> short_array = (c_short * 4)() >>> short_array = (c_short * 4)()
>>> print sizeof(short_array) >>> print sizeof(short_array)
...@@ -1243,7 +1245,7 @@ get errors accessing other elements:: ...@@ -1243,7 +1245,7 @@ get errors accessing other elements::
IndexError: invalid index IndexError: invalid index
>>> >>>
Another way to use variable-sized data types with ``ctypes`` is to use the Another way to use variable-sized data types with :mod:`ctypes` is to use the
dynamic nature of Python, and (re-)define the data type after the required size dynamic nature of Python, and (re-)define the data type after the required size
is already known, on a case by case basis. is already known, on a case by case basis.
...@@ -1262,12 +1264,12 @@ Finding shared libraries ...@@ -1262,12 +1264,12 @@ Finding shared libraries
When programming in a compiled language, shared libraries are accessed when When programming in a compiled language, shared libraries are accessed when
compiling/linking a program, and when the program is run. compiling/linking a program, and when the program is run.
The purpose of the ``find_library`` function is to locate a library in a way The purpose of the :func:`find_library` function is to locate a library in a way
similar to what the compiler does (on platforms with several versions of a similar to what the compiler does (on platforms with several versions of a
shared library the most recent should be loaded), while the ctypes library 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. loaders act like when a program is run, and call the runtime loader directly.
The ``ctypes.util`` module provides a function which can help to determine the The :mod:`ctypes.util` module provides a function which can help to determine the
library to load. library to load.
...@@ -1282,9 +1284,9 @@ library to load. ...@@ -1282,9 +1284,9 @@ library to load.
The exact functionality is system dependent. The exact functionality is system dependent.
On Linux, ``find_library`` tries to run external programs (/sbin/ldconfig, gcc, On Linux, :func:`find_library` tries to run external programs
and objdump) to find the library file. It returns the filename of the library (``/sbin/ldconfig``, ``gcc``, and ``objdump``) to find the library file. It
file. Here are some examples:: returns the filename of the library file. Here are some examples::
>>> from ctypes.util import find_library >>> from ctypes.util import find_library
>>> find_library("m") >>> find_library("m")
...@@ -1295,8 +1297,8 @@ file. Here are some examples:: ...@@ -1295,8 +1297,8 @@ file. Here are some examples::
'libbz2.so.1.0' 'libbz2.so.1.0'
>>> >>>
On OS X, ``find_library`` tries several predefined naming schemes and paths to On OS X, :func:`find_library` tries several predefined naming schemes and paths
locate the library, and returns a full pathname if successful:: to locate the library, and returns a full pathname if successful::
>>> from ctypes.util import find_library >>> from ctypes.util import find_library
>>> find_library("c") >>> find_library("c")
...@@ -1309,13 +1311,13 @@ locate the library, and returns a full pathname if successful:: ...@@ -1309,13 +1311,13 @@ locate the library, and returns a full pathname if successful::
'/System/Library/Frameworks/AGL.framework/AGL' '/System/Library/Frameworks/AGL.framework/AGL'
>>> >>>
On Windows, ``find_library`` searches along the system search path, and returns On Windows, :func:`find_library` searches along the system search path, and
the full pathname, but since there is no predefined naming scheme a call like returns the full pathname, but since there is no predefined naming scheme a call
``find_library("c")`` will fail and return ``None``. like ``find_library("c")`` will fail and return ``None``.
If wrapping a shared library with ``ctypes``, it *may* be better to determine If wrapping a shared library with :mod:`ctypes`, it *may* be better to determine
the shared library name at development type, and hardcode that into the wrapper the shared library name at development type, and hardcode that into the wrapper
module instead of using ``find_library`` to locate the library at runtime. module instead of using :func:`find_library` to locate the library at runtime.
.. _ctypes-loading-shared-libraries: .. _ctypes-loading-shared-libraries:
...@@ -1331,7 +1333,7 @@ way is to instantiate one of the following classes: ...@@ -1331,7 +1333,7 @@ way is to instantiate one of the following classes:
Instances of this class represent loaded shared libraries. Functions in these Instances of this class represent loaded shared libraries. Functions in these
libraries use the standard C calling convention, and are assumed to return libraries use the standard C calling convention, and are assumed to return
``int``. :ctype:`int`.
.. class:: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False) .. class:: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)
...@@ -1348,7 +1350,7 @@ way is to instantiate one of the following classes: ...@@ -1348,7 +1350,7 @@ way is to instantiate one of the following classes:
Windows only: Instances of this class represent loaded shared libraries, Windows only: Instances of this class represent loaded shared libraries,
functions in these libraries use the ``stdcall`` calling convention, and are functions in these libraries use the ``stdcall`` calling convention, and are
assumed to return ``int`` by default. assumed to return :ctype:`int` by default.
On Windows CE only the standard calling convention is used, for convenience the On Windows CE only the standard calling convention is used, for convenience the
:class:`WinDLL` and :class:`OleDLL` use the standard calling convention on this :class:`WinDLL` and :class:`OleDLL` use the standard calling convention on this
...@@ -1370,12 +1372,13 @@ function exported by these libraries, and reacquired afterwards. ...@@ -1370,12 +1372,13 @@ function exported by these libraries, and reacquired afterwards.
All these classes can be instantiated by calling them with at least one 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 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 an already loaded shared library, it can be passed as the ``handle`` named
parameter, otherwise the underlying platforms ``dlopen`` or :meth:`LoadLibrary` parameter, otherwise the underlying platforms ``dlopen`` or ``LoadLibrary``
function is used to load the library into the process, and to get a handle to function is used to load the library into the process, and to get a handle to
it. it.
The *mode* parameter can be used to specify how the library is loaded. For The *mode* parameter can be used to specify how the library is loaded. For
details, consult the ``dlopen(3)`` manpage, on Windows, *mode* is ignored. details, consult the :manpage:`dlopen(3)` manpage, on Windows, *mode* is
ignored.
The *use_errno* parameter, when set to True, enables a ctypes mechanism that The *use_errno* parameter, when set to True, enables a ctypes mechanism that
allows to access the system :data:`errno` error number in a safe way. allows to access the system :data:`errno` error number in a safe way.
...@@ -1395,8 +1398,7 @@ the Windows error code which is managed by the :func:`GetLastError` and ...@@ -1395,8 +1398,7 @@ the Windows error code which is managed by the :func:`GetLastError` and
copy of the windows error code. copy of the windows error code.
.. versionadded:: 2.6 .. versionadded:: 2.6
The ``use_last_error`` and ``use_errno`` optional parameters The *use_last_error* and *use_errno* optional parameters were added.
were added.
.. data:: RTLD_GLOBAL .. data:: RTLD_GLOBAL
:noindex: :noindex:
...@@ -1445,7 +1447,7 @@ loader instance. ...@@ -1445,7 +1447,7 @@ loader instance.
.. class:: LibraryLoader(dlltype) .. class:: LibraryLoader(dlltype)
Class which loads shared libraries. ``dlltype`` should be one of the Class which loads shared libraries. *dlltype* should be one of the
:class:`CDLL`, :class:`PyDLL`, :class:`WinDLL`, or :class:`OleDLL` types. :class:`CDLL`, :class:`PyDLL`, :class:`WinDLL`, or :class:`OleDLL` types.
:meth:`__getattr__` has special behavior: It allows to load a shared library by :meth:`__getattr__` has special behavior: It allows to load a shared library by
...@@ -1491,10 +1493,10 @@ object is available: ...@@ -1491,10 +1493,10 @@ object is available:
.. data:: pythonapi .. data:: pythonapi
:noindex: :noindex:
An instance of :class:`PyDLL` that exposes Python C api functions as attributes. An instance of :class:`PyDLL` that exposes Python C API functions as
Note that all these functions are assumed to return C ``int``, which is of attributes. Note that all these functions are assumed to return C
course not always the truth, so you have to assign the correct :attr:`restype` :ctype:`int`, which is of course not always the truth, so you have to assign
attribute to use these functions. the correct :attr:`restype` attribute to use these functions.
.. _ctypes-foreign-functions: .. _ctypes-foreign-functions:
...@@ -1523,11 +1525,11 @@ They are instances of a private class: ...@@ -1523,11 +1525,11 @@ They are instances of a private class:
.. attribute:: restype .. attribute:: restype
Assign a ctypes type to specify the result type of the foreign function. Assign a ctypes type to specify the result type of the foreign function.
Use ``None`` for ``void`` a function not returning anything. Use ``None`` for :ctype:`void`, a function not returning anything.
It is possible to assign a callable Python object that is not a ctypes It is possible to assign a callable Python object that is not a ctypes
type, in this case the function is assumed to return a C ``int``, and the type, in this case the function is assumed to return a C :ctype:`int`, and
callable will be called with this integer, allowing to do further the callable will be called with this integer, allowing to do further
processing or error checking. Using this is deprecated, for more flexible processing or error checking. Using this is deprecated, for more flexible
post processing or error checking use a ctypes data type as 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.
...@@ -1562,16 +1564,16 @@ They are instances of a private class: ...@@ -1562,16 +1564,16 @@ They are instances of a private class:
.. function:: callable(result, func, arguments) .. function:: callable(result, func, arguments)
:noindex: :noindex:
``result`` is what the foreign function returns, as specified *result* is what the foreign function returns, as specified by the
by the :attr:`restype` attribute. :attr:`restype` attribute.
``func`` is the foreign function object itself, this allows *func* is the foreign function object itself, this allows to reuse the
to reuse the same callable object to check or post process same callable object to check or post process the results of several
the results of several functions. functions.
``arguments`` is a tuple containing the parameters originally *arguments* is a tuple containing the parameters originally passed to
passed to the function call, this allows to specialize the the function call, this allows to specialize the behavior on the
behavior on the arguments used. arguments used.
The object that this function returns will be returned from the The object that this function returns will be returned from the
foreign function call, but it can also check the result value foreign function call, but it can also check the result value
...@@ -1638,14 +1640,14 @@ different ways, depending on the type and number of the parameters in the call: ...@@ -1638,14 +1640,14 @@ different ways, depending on the type and number of the parameters in the call:
:noindex: :noindex:
:module: :module:
Create a C callable function (a callback function) from a Python ``callable``. Create a C callable function (a callback function) from a Python *callable*.
.. function:: prototype(func_spec[, paramflags]) .. function:: prototype(func_spec[, paramflags])
:noindex: :noindex:
:module: :module:
Returns a foreign function exported by a shared library. ``func_spec`` must be a Returns a foreign function exported by a shared library. *func_spec* must be a
2-tuple ``(name_or_ordinal, library)``. The first item is the name of the 2-tuple ``(name_or_ordinal, library)``. The first item is the name of the
exported function as string, or the ordinal of the exported function as small exported function as string, or the ordinal of the exported function as small
integer. The second item is the shared library instance. integer. The second item is the shared library instance.
...@@ -1655,7 +1657,7 @@ different ways, depending on the type and number of the parameters in the call: ...@@ -1655,7 +1657,7 @@ different ways, depending on the type and number of the parameters in the call:
:noindex: :noindex:
:module: :module:
Returns a foreign function that will call a COM method. ``vtbl_index`` is the Returns a foreign function that will call a COM method. *vtbl_index* is the
index into the virtual function table, a small non-negative integer. *name* is index into the virtual function table, a small non-negative integer. *name* is
name of the COM method. *iid* is an optional pointer to the interface identifier name of the COM method. *iid* is an optional pointer to the interface identifier
which is used in extended error reporting. which is used in extended error reporting.
...@@ -1700,7 +1702,7 @@ the windows header file is this:: ...@@ -1700,7 +1702,7 @@ the windows header file is this::
LPCSTR lpCaption, LPCSTR lpCaption,
UINT uType); UINT uType);
Here is the wrapping with ``ctypes``:: Here is the wrapping with :mod:`ctypes`::
>>> from ctypes import c_int, WINFUNCTYPE, windll >>> from ctypes import c_int, WINFUNCTYPE, windll
>>> from ctypes.wintypes import HWND, LPCSTR, UINT >>> from ctypes.wintypes import HWND, LPCSTR, UINT
...@@ -1725,7 +1727,7 @@ function retrieves the dimensions of a specified window by copying them into ...@@ -1725,7 +1727,7 @@ function retrieves the dimensions of a specified window by copying them into
HWND hWnd, HWND hWnd,
LPRECT lpRect); LPRECT lpRect);
Here is the wrapping with ``ctypes``:: Here is the wrapping with :mod:`ctypes`::
>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError >>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
>>> from ctypes.wintypes import BOOL, HWND, RECT >>> from ctypes.wintypes import BOOL, HWND, RECT
...@@ -1753,7 +1755,7 @@ do the error checking, and raises an exception when the api call failed:: ...@@ -1753,7 +1755,7 @@ do the error checking, and raises an exception when the api call failed::
>>> >>>
If the :attr:`errcheck` function returns the argument tuple it receives If the :attr:`errcheck` function returns the argument tuple it receives
unchanged, ``ctypes`` continues the normal processing it does on the output 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 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 ``RECT`` instance, you can retrieve the fields in the function and return them
instead, the normal processing will no longer take place:: instead, the normal processing will no longer take place::
...@@ -1776,21 +1778,21 @@ Utility functions ...@@ -1776,21 +1778,21 @@ Utility functions
.. function:: addressof(obj) .. function:: addressof(obj)
Returns the address of the memory buffer as integer. ``obj`` must be an Returns the address of the memory buffer as integer. *obj* must be an
instance of a ctypes type. instance of a ctypes type.
.. function:: alignment(obj_or_type) .. function:: alignment(obj_or_type)
Returns the alignment requirements of a ctypes type. ``obj_or_type`` must be a Returns the alignment requirements of a ctypes type. *obj_or_type* must be a
ctypes type or instance. ctypes type or instance.
.. function:: byref(obj[, offset]) .. function:: byref(obj[, offset])
Returns a light-weight pointer to ``obj``, which must be an Returns a light-weight pointer to *obj*, which must be an instance of a
instance of a ctypes type. ``offset`` defaults to zero, and must be ctypes type. *offset* defaults to zero, and must be an integer that will be
an integer that will be added to the internal pointer value. added to the internal pointer value.
``byref(obj, offset)`` corresponds to this C code:: ``byref(obj, offset)`` corresponds to this C code::
...@@ -1801,14 +1803,15 @@ Utility functions ...@@ -1801,14 +1803,15 @@ Utility functions
construction is a lot faster. construction is a lot faster.
.. versionadded:: 2.6 .. versionadded:: 2.6
The ``offset`` optional argument was added. The *offset* optional argument was added.
.. function:: cast(obj, type) .. function:: cast(obj, type)
This function is similar to the cast operator in C. It returns a new instance of This function is similar to the cast operator in C. It returns a new
``type`` which points to the same memory block as ``obj``. ``type`` must be a instance of *type* which points to the same memory block as *obj*. *type*
pointer type, and ``obj`` must be an object that can be interpreted as a must be a pointer type, and *obj* must be an object that can be interpreted
pointer. as a pointer.
.. function:: create_string_buffer(init_or_size[, size]) .. function:: create_string_buffer(init_or_size[, size])
...@@ -1816,7 +1819,7 @@ Utility functions ...@@ -1816,7 +1819,7 @@ Utility functions
This function creates a mutable character buffer. The returned object is a This function creates a mutable character buffer. The returned object is a
ctypes array of :class:`c_char`. ctypes array of :class:`c_char`.
``init_or_size`` must be an integer which specifies the size of the array, or a *init_or_size* must be an integer which specifies the size of the array, or a
string which will be used to initialize the array items. string which will be used to initialize the array items.
If a string is specified as first argument, the buffer is made one item larger If a string is specified as first argument, the buffer is made one item larger
...@@ -1833,7 +1836,7 @@ Utility functions ...@@ -1833,7 +1836,7 @@ Utility functions
This function creates a mutable unicode character buffer. The returned object is This function creates a mutable unicode character buffer. The returned object is
a ctypes array of :class:`c_wchar`. a ctypes array of :class:`c_wchar`.
``init_or_size`` must be an integer which specifies the size of the array, or a *init_or_size* must be an integer which specifies the size of the array, or a
unicode string which will be used to initialize the array items. unicode string which will be used to initialize the array items.
If a unicode string is specified as first argument, the buffer is made one item If a unicode string is specified as first argument, the buffer is made one item
...@@ -1848,16 +1851,17 @@ Utility functions ...@@ -1848,16 +1851,17 @@ Utility functions
.. function:: DllCanUnloadNow() .. function:: DllCanUnloadNow()
Windows only: This function is a hook which allows to implement in-process COM Windows only: This function is a hook which allows to implement in-process
servers with ctypes. It is called from the DllCanUnloadNow function that the COM servers with ctypes. It is called from the DllCanUnloadNow function that
_ctypes extension dll exports. the _ctypes extension dll exports.
.. function:: DllGetClassObject() .. function:: DllGetClassObject()
Windows only: This function is a hook which allows to implement in-process COM Windows only: This function is a hook which allows to implement in-process
servers with ctypes. It is called from the DllGetClassObject function that the COM servers with ctypes. It is called from the DllGetClassObject function
``_ctypes`` extension dll exports. that the ``_ctypes`` extension dll exports.
.. function:: find_library(name) .. function:: find_library(name)
:module: ctypes.util :module: ctypes.util
...@@ -1870,28 +1874,29 @@ Utility functions ...@@ -1870,28 +1874,29 @@ Utility functions
The exact functionality is system dependent. The exact functionality is system dependent.
.. versionchanged:: 2.6 .. versionchanged:: 2.6
Windows only: ``find_library("m")`` or Windows only: ``find_library("m")`` or ``find_library("c")`` return the
``find_library("c")`` return the result of a call to result of a call to ``find_msvcrt()``.
``find_msvcrt()``.
.. function:: find_msvcrt() .. function:: find_msvcrt()
:module: ctypes.util :module: ctypes.util
Windows only: return the filename of the VC runtype library used Windows only: return the filename of the VC runtype library used by Python,
by Python, and by the extension modules. If the name of the and by the extension modules. If the name of the library cannot be
library cannot be determined, ``None`` is returned. determined, ``None`` is returned.
If you need to free memory, for example, allocated by an extension If you need to free memory, for example, allocated by an extension module
module with a call to the ``free(void *)``, it is important that you with a call to the ``free(void *)``, it is important that you use the
use the function in the same library that allocated the memory. function in the same library that allocated the memory.
.. versionadded:: 2.6 .. versionadded:: 2.6
.. function:: FormatError([code]) .. function:: FormatError([code])
Windows only: Returns a textual description of the error code. If no error code Windows only: Returns a textual description of the error code *code*. If no
is specified, the last error code is used by calling the Windows api function error code is specified, the last error code is used by calling the Windows
GetLastError. api function GetLastError.
.. function:: GetLastError() .. function:: GetLastError()
...@@ -1917,8 +1922,8 @@ Utility functions ...@@ -1917,8 +1922,8 @@ Utility functions
.. function:: memmove(dst, src, count) .. function:: memmove(dst, src, count)
Same as the standard C memmove library function: copies *count* bytes from Same as the standard C memmove library function: copies *count* bytes from
``src`` to *dst*. *dst* and ``src`` must be integers or ctypes instances that *src* to *dst*. *dst* and *src* must be integers or ctypes instances that can
can be converted to pointers. be converted to pointers.
.. function:: memset(dst, c, count) .. function:: memset(dst, c, count)
...@@ -1932,13 +1937,13 @@ Utility functions ...@@ -1932,13 +1937,13 @@ Utility functions
This factory function creates and returns a new ctypes pointer type. Pointer This factory function creates and returns a new ctypes pointer type. Pointer
types are cached an reused internally, so calling this function repeatedly is types are cached an reused internally, so calling this function repeatedly is
cheap. type must be a ctypes type. cheap. *type* must be a ctypes type.
.. function:: pointer(obj) .. function:: pointer(obj)
This function creates a new pointer instance, pointing to ``obj``. The returned This function creates a new pointer instance, pointing to *obj*. The returned
object is of the type POINTER(type(obj)). object is of the type ``POINTER(type(obj))``.
Note: If you just want to pass a pointer to an object to a foreign function Note: If you just want to pass a pointer to an object to a foreign function
call, you should use ``byref(obj)`` which is much faster. call, you should use ``byref(obj)`` which is much faster.
...@@ -1946,23 +1951,23 @@ Utility functions ...@@ -1946,23 +1951,23 @@ Utility functions
.. function:: resize(obj, size) .. function:: resize(obj, size)
This function resizes the internal memory buffer of obj, which must be an This function resizes the internal memory buffer of *obj*, which must be an
instance of a ctypes type. It is not possible to make the buffer smaller than instance of a ctypes type. It is not possible to make the buffer smaller
the native size of the objects type, as given by sizeof(type(obj)), but it is than the native size of the objects type, as given by ``sizeof(type(obj))``,
possible to enlarge the buffer. but it is possible to enlarge the buffer.
.. function:: set_conversion_mode(encoding, errors) .. function:: set_conversion_mode(encoding, errors)
This function sets the rules that ctypes objects use when converting between This function sets the rules that ctypes objects use when converting between
8-bit strings and unicode strings. encoding must be a string specifying an 8-bit strings and unicode strings. *encoding* must be a string specifying an
encoding, like ``'utf-8'`` or ``'mbcs'``, errors must be a string specifying the encoding, like ``'utf-8'`` or ``'mbcs'``, *errors* must be a string
error handling on encoding/decoding errors. Examples of possible values are specifying the error handling on encoding/decoding errors. Examples of
``"strict"``, ``"replace"``, or ``"ignore"``. possible values are ``"strict"``, ``"replace"``, or ``"ignore"``.
``set_conversion_mode`` returns a 2-tuple containing the previous conversion :func:`set_conversion_mode` returns a 2-tuple containing the previous
rules. On windows, the initial conversion rules are ``('mbcs', 'ignore')``, on conversion rules. On windows, the initial conversion rules are ``('mbcs',
other systems ``('ascii', 'strict')``. 'ignore')``, on other systems ``('ascii', 'strict')``.
.. function:: set_errno(value) .. function:: set_errno(value)
...@@ -1972,6 +1977,7 @@ Utility functions ...@@ -1972,6 +1977,7 @@ Utility functions
.. versionadded:: 2.6 .. versionadded:: 2.6
.. function:: set_last_error(value) .. function:: set_last_error(value)
Windows only: set the current value of the ctypes-private copy of the system Windows only: set the current value of the ctypes-private copy of the system
...@@ -1980,6 +1986,7 @@ Utility functions ...@@ -1980,6 +1986,7 @@ Utility functions
.. versionadded:: 2.6 .. versionadded:: 2.6
.. function:: sizeof(obj_or_type) .. function:: sizeof(obj_or_type)
Returns the size in bytes of a ctypes type or instance memory buffer. Does the Returns the size in bytes of a ctypes type or instance memory buffer. Does the
...@@ -1995,17 +2002,17 @@ Utility functions ...@@ -1995,17 +2002,17 @@ Utility functions
.. function:: WinError(code=None, descr=None) .. function:: WinError(code=None, descr=None)
Windows only: this function is probably the worst-named thing in ctypes. It Windows only: this function is probably the worst-named thing in ctypes. It
creates an instance of WindowsError. If *code* is not specified, creates an instance of WindowsError. If *code* is not specified,
``GetLastError`` is called to determine the error code. If ``descr`` is not ``GetLastError`` is called to determine the error code. If ``descr`` is not
specified, :func:`FormatError` is called to get a textual description of the specified, :func:`FormatError` is called to get a textual description of the
error. error.
.. function:: wstring_at(address) .. function:: wstring_at(address[, size])
This function returns the wide character string starting at memory address This function returns the wide character string starting at memory address
``address`` as unicode string. If ``size`` is specified, it is used as the *address* as unicode string. If *size* is specified, it is used as the
number of characters of the string, otherwise the string is assumed to be number of characters of the string, otherwise the string is assumed to be
zero-terminated. zero-terminated.
...@@ -2018,12 +2025,12 @@ Data types ...@@ -2018,12 +2025,12 @@ Data types
.. class:: _CData .. class:: _CData
This non-public class is the common base class of all ctypes data types. Among This non-public class is the common base class of all ctypes data types.
other things, all ctypes type instances contain a memory block that hold C Among other things, all ctypes type instances contain a memory block that
compatible data; the address of the memory block is returned by the hold C compatible data; the address of the memory block is returned by the
``addressof()`` helper function. Another instance variable is exposed as :func:`addressof` helper function. Another instance variable is exposed as
:attr:`_objects`; this contains other Python objects that need to be kept alive :attr:`_objects`; this contains other Python objects that need to be kept
in case the memory block contains pointers. alive in case the memory block contains pointers.
Common methods of ctypes data types, these are all class methods (to be Common methods of ctypes data types, these are all class methods (to be
exact, they are methods of the :term:`metaclass`): exact, they are methods of the :term:`metaclass`):
...@@ -2031,22 +2038,22 @@ Data types ...@@ -2031,22 +2038,22 @@ Data types
.. method:: _CData.from_buffer(source[, offset]) .. method:: _CData.from_buffer(source[, offset])
This method returns a ctypes instance that shares the buffer of This method returns a ctypes instance that shares the buffer of the
the ``source`` object. The ``source`` object must support the *source* object. The *source* object must support the writeable buffer
writeable buffer interface. The optional ``offset`` parameter interface. The optional *offset* parameter specifies an offset into the
specifies an offset into the source buffer in bytes; the default source buffer in bytes; the default is zero. If the source buffer is not
is zero. If the source buffer is not large enough a ValueError large enough a :exc:`ValueError` is raised.
is raised.
.. versionadded:: 2.6 .. versionadded:: 2.6
.. method:: _CData.from_buffer_copy(source[, offset]) .. method:: _CData.from_buffer_copy(source[, offset])
This method creates a ctypes instance, copying the buffer from This method creates a ctypes instance, copying the buffer from the
the source object buffer which must be readable. The optional *source* object buffer which must be readable. The optional *offset*
``offset`` parameter specifies an offset into the source buffer parameter specifies an offset into the source buffer in bytes; the default
in bytes; the default is zero. If the source buffer is not is zero. If the source buffer is not large enough a :exc:`ValueError` is
large enough a ValueError is raised. raised.
.. versionadded:: 2.6 .. versionadded:: 2.6
...@@ -2054,7 +2061,7 @@ Data types ...@@ -2054,7 +2061,7 @@ Data types
.. method:: from_address(address) .. method:: from_address(address)
This method returns a ctypes type instance using the memory specified by This method returns a ctypes type instance using the memory specified by
address which must be an integer. *address* which must be an integer.
.. method:: from_param(obj) .. method:: from_param(obj)
...@@ -2065,7 +2072,7 @@ Data types ...@@ -2065,7 +2072,7 @@ Data types
can be used as a function call parameter. can be used as a function call parameter.
All ctypes data types have a default implementation of this classmethod All ctypes data types have a default implementation of this classmethod
that normally returns ``obj`` if that is an instance of the type. Some that normally returns *obj* if that is an instance of the type. Some
types accept other objects as well. types accept other objects as well.
...@@ -2078,7 +2085,6 @@ Data types ...@@ -2078,7 +2085,6 @@ Data types
Common instance variables of ctypes data types: Common instance variables of ctypes data types:
.. attribute:: _b_base_ .. attribute:: _b_base_
Sometimes ctypes data instances do not own the memory block they contain, Sometimes ctypes data instances do not own the memory block they contain,
...@@ -2109,18 +2115,17 @@ Fundamental data types ...@@ -2109,18 +2115,17 @@ Fundamental data types
.. class:: _SimpleCData .. class:: _SimpleCData
This non-public class is the base class of all fundamental ctypes data types. It This non-public class is the base class of all fundamental ctypes data
is mentioned here because it contains the common attributes of the fundamental types. It is mentioned here because it contains the common attributes of the
ctypes data types. ``_SimpleCData`` is a subclass of ``_CData``, so it inherits fundamental ctypes data types. :class:`_SimpleCData` is a subclass of
their methods and attributes. :class:`_CData`, so it inherits their methods and attributes.
.. versionchanged:: 2.6 .. versionchanged:: 2.6
ctypes data types that are not and do not contain pointers can ctypes data types that are not and do not contain pointers can now be
now be pickled. pickled.
Instances have a single attribute: Instances have a single attribute:
.. attribute:: value .. attribute:: value
This attribute contains the actual value of the instance. For integer and This attribute contains the actual value of the instance. For integer and
...@@ -2129,10 +2134,11 @@ Fundamental data types ...@@ -2129,10 +2134,11 @@ Fundamental data types
unicode string. unicode string.
When the ``value`` attribute is retrieved from a ctypes instance, usually When the ``value`` attribute is retrieved from a ctypes instance, usually
a new object is returned each time. ``ctypes`` does *not* implement a new object is returned each time. :mod:`ctypes` does *not* implement
original object return, always a new object is constructed. The same is original object return, always a new object is constructed. The same is
true for all other ctypes object instances. true for all other ctypes object instances.
Fundamental data types, when returned as foreign function call results, or, for Fundamental data types, when returned as foreign function call results, or, for
example, by retrieving structure field members or array items, are transparently 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 converted to native Python types. In other words, if a foreign function has a
...@@ -2146,200 +2152,201 @@ get the value of the pointer by accessing the ``value`` attribute. ...@@ -2146,200 +2152,201 @@ get the value of the pointer by accessing the ``value`` attribute.
These are the fundamental ctypes data types: These are the fundamental ctypes data types:
.. class:: c_byte .. class:: c_byte
Represents the C signed char datatype, and interprets the value as small Represents the C :ctype:`signed char` datatype, and interprets the value as
integer. The constructor accepts an optional integer initializer; no overflow small integer. The constructor accepts an optional integer initializer; no
checking is done. overflow checking is done.
.. class:: c_char .. class:: c_char
Represents the C char datatype, and interprets the value as a single character. Represents the C :ctype:`char` datatype, and interprets the value as a single
The constructor accepts an optional string initializer, the length of the string character. The constructor accepts an optional string initializer, the
must be exactly one character. length of the string must be exactly one character.
.. class:: c_char_p .. class:: c_char_p
Represents the C char \* datatype, which must be a pointer to a zero-terminated Represents the C :ctype:`char *` datatype when it points to a zero-terminated
string. The constructor accepts an integer address, or a string. string. For a general character pointer that may also point to binary data,
``POINTER(c_char)`` must be used. The constructor accepts an integer
address, or a string.
.. class:: c_double .. class:: c_double
Represents the C double datatype. The constructor accepts an optional float Represents the C :ctype:`double` datatype. The constructor accepts an
initializer. optional float initializer.
.. class:: c_longdouble .. class:: c_longdouble
Represents the C long double datatype. The constructor accepts an Represents the C :ctype:`long double` datatype. The constructor accepts an
optional float initializer. On platforms where ``sizeof(long optional float initializer. On platforms where ``sizeof(long double) ==
double) == sizeof(double)`` it is an alias to :class:`c_double`. sizeof(double)`` it is an alias to :class:`c_double`.
.. versionadded:: 2.6 .. versionadded:: 2.6
.. class:: c_float .. class:: c_float
Represents the C float datatype. The constructor accepts an optional float Represents the C :ctype:`float` datatype. The constructor accepts an
initializer. optional float initializer.
.. class:: c_int .. class:: c_int
Represents the C signed int datatype. The constructor accepts an optional Represents the C :ctype:`signed int` datatype. The constructor accepts an
integer initializer; no overflow checking is done. On platforms where optional integer initializer; no overflow checking is done. On platforms
``sizeof(int) == sizeof(long)`` it is an alias to :class:`c_long`. where ``sizeof(int) == sizeof(long)`` it is an alias to :class:`c_long`.
.. class:: c_int8 .. class:: c_int8
Represents the C 8-bit ``signed int`` datatype. Usually an alias for Represents the C 8-bit :ctype:`signed int` datatype. Usually an alias for
:class:`c_byte`. :class:`c_byte`.
.. class:: c_int16 .. class:: c_int16
Represents the C 16-bit signed int datatype. Usually an alias for Represents the C 16-bit :ctype:`signed int` datatype. Usually an alias for
:class:`c_short`. :class:`c_short`.
.. class:: c_int32 .. class:: c_int32
Represents the C 32-bit signed int datatype. Usually an alias for Represents the C 32-bit :ctype:`signed int` datatype. Usually an alias for
:class:`c_int`. :class:`c_int`.
.. class:: c_int64 .. class:: c_int64
Represents the C 64-bit ``signed int`` datatype. Usually an alias for Represents the C 64-bit :ctype:`signed int` datatype. Usually an alias for
:class:`c_longlong`. :class:`c_longlong`.
.. class:: c_long .. class:: c_long
Represents the C ``signed long`` datatype. The constructor accepts an optional Represents the C :ctype:`signed long` datatype. The constructor accepts an
integer initializer; no overflow checking is done. optional integer initializer; no overflow checking is done.
.. class:: c_longlong .. class:: c_longlong
Represents the C ``signed long long`` datatype. The constructor accepts an Represents the C :ctype:`signed long long` datatype. The constructor accepts
optional integer initializer; no overflow checking is done. an optional integer initializer; no overflow checking is done.
.. class:: c_short .. class:: c_short
Represents the C ``signed short`` datatype. The constructor accepts an optional Represents the C :ctype:`signed short` datatype. The constructor accepts an
integer initializer; no overflow checking is done. optional integer initializer; no overflow checking is done.
.. class:: c_size_t .. class:: c_size_t
Represents the C ``size_t`` datatype. Represents the C :ctype:`size_t` datatype.
.. class:: c_ubyte .. class:: c_ubyte
Represents the C ``unsigned char`` datatype, it interprets the value as small Represents the C :ctype:`unsigned char` datatype, it interprets the value as
integer. The constructor accepts an optional integer initializer; no overflow small integer. The constructor accepts an optional integer initializer; no
checking is done. overflow checking is done.
.. class:: c_uint .. class:: c_uint
Represents the C ``unsigned int`` datatype. The constructor accepts an optional Represents the C :ctype:`unsigned int` datatype. The constructor accepts an
integer initializer; no overflow checking is done. On platforms where optional integer initializer; no overflow checking is done. On platforms
``sizeof(int) == sizeof(long)`` it is an alias for :class:`c_ulong`. where ``sizeof(int) == sizeof(long)`` it is an alias for :class:`c_ulong`.
.. class:: c_uint8 .. class:: c_uint8
Represents the C 8-bit unsigned int datatype. Usually an alias for Represents the C 8-bit :ctype:`unsigned int` datatype. Usually an alias for
:class:`c_ubyte`. :class:`c_ubyte`.
.. class:: c_uint16 .. class:: c_uint16
Represents the C 16-bit unsigned int datatype. Usually an alias for Represents the C 16-bit :ctype:`unsigned int` datatype. Usually an alias for
:class:`c_ushort`. :class:`c_ushort`.
.. class:: c_uint32 .. class:: c_uint32
Represents the C 32-bit unsigned int datatype. Usually an alias for Represents the C 32-bit :ctype:`unsigned int` datatype. Usually an alias for
:class:`c_uint`. :class:`c_uint`.
.. class:: c_uint64 .. class:: c_uint64
Represents the C 64-bit unsigned int datatype. Usually an alias for Represents the C 64-bit :ctype:`unsigned int` datatype. Usually an alias for
:class:`c_ulonglong`. :class:`c_ulonglong`.
.. class:: c_ulong .. class:: c_ulong
Represents the C ``unsigned long`` datatype. The constructor accepts an optional Represents the C :ctype:`unsigned long` datatype. The constructor accepts an
integer initializer; no overflow checking is done. optional integer initializer; no overflow checking is done.
.. class:: c_ulonglong .. class:: c_ulonglong
Represents the C ``unsigned long long`` datatype. The constructor accepts an Represents the C :ctype:`unsigned long long` datatype. The constructor
optional integer initializer; no overflow checking is done. accepts an optional integer initializer; no overflow checking is done.
.. class:: c_ushort .. class:: c_ushort
Represents the C ``unsigned short`` datatype. The constructor accepts an Represents the C :ctype:`unsigned short` datatype. The constructor accepts
optional integer initializer; no overflow checking is done. an optional integer initializer; no overflow checking is done.
.. class:: c_void_p .. class:: c_void_p
Represents the C ``void *`` type. The value is represented as integer. The Represents the C :ctype:`void *` type. The value is represented as integer.
constructor accepts an optional integer initializer. The constructor accepts an optional integer initializer.
.. class:: c_wchar .. class:: c_wchar
Represents the C ``wchar_t`` datatype, and interprets the value as a single Represents the C :ctype:`wchar_t` datatype, and interprets the value as a
character unicode string. The constructor accepts an optional string single character unicode string. The constructor accepts an optional string
initializer, the length of the string must be exactly one character. initializer, the length of the string must be exactly one character.
.. class:: c_wchar_p .. class:: c_wchar_p
Represents the C ``wchar_t *`` datatype, which must be a pointer to a Represents the C :ctype:`wchar_t *` datatype, which must be a pointer to a
zero-terminated wide character string. The constructor accepts an integer zero-terminated wide character string. The constructor accepts an integer
address, or a string. address, or a string.
.. class:: c_bool .. class:: c_bool
Represent the C ``bool`` datatype (more accurately, _Bool from C99). Its value Represent the C :ctype:`bool` datatype (more accurately, :ctype:`_Bool` from
can be True or False, and the constructor accepts any object that has a truth C99). Its value can be True or False, and the constructor accepts any object
value. that has a truth value.
.. versionadded:: 2.6 .. versionadded:: 2.6
.. class:: HRESULT .. class:: HRESULT
Windows only: Represents a :class:`HRESULT` value, which contains success or Windows only: Represents a :ctype:`HRESULT` value, which contains success or
error information for a function or method call. error information for a function or method call.
.. class:: py_object .. class:: py_object
Represents the C ``PyObject *`` datatype. Calling this without an argument Represents the C :ctype:`PyObject *` datatype. Calling this without an
creates a ``NULL`` ``PyObject *`` pointer. argument creates a ``NULL`` :ctype:`PyObject *` pointer.
The ``ctypes.wintypes`` module provides quite some other Windows specific data The :mod:`ctypes.wintypes` module provides quite some other Windows specific
types, for example ``HWND``, ``WPARAM``, or ``DWORD``. Some useful structures data types, for example :ctype:`HWND`, :ctype:`WPARAM`, or :ctype:`DWORD`. Some
like ``MSG`` or ``RECT`` are also defined. useful structures like :ctype:`MSG` or :ctype:`RECT` are also defined.
.. _ctypes-structured-data-types: .. _ctypes-structured-data-types:
...@@ -2371,7 +2378,7 @@ other data types containing pointer type fields. ...@@ -2371,7 +2378,7 @@ other data types containing pointer type fields.
Abstract base class for structures in *native* byte order. Abstract base class for structures in *native* byte order.
Concrete structure and union types must be created by subclassing one of these Concrete structure and union types must be created by subclassing one of these
types, and at least define a :attr:`_fields_` class variable. ``ctypes`` will types, and at least define a :attr:`_fields_` class variable. :mod:`ctypes` will
create :term:`descriptor`\s which allow reading and writing the fields by direct create :term:`descriptor`\s which allow reading and writing the fields by direct
attribute accesses. These are the attribute accesses. These are the
...@@ -2424,11 +2431,11 @@ other data types containing pointer type fields. ...@@ -2424,11 +2431,11 @@ other data types containing pointer type fields.
.. attribute:: _anonymous_ .. attribute:: _anonymous_
An optional sequence that lists the names of unnamed (anonymous) fields. An optional sequence that lists the names of unnamed (anonymous) fields.
``_anonymous_`` must be already defined when :attr:`_fields_` is assigned, :attr:`_anonymous_` must be already defined when :attr:`_fields_` is
otherwise it will have no effect. assigned, otherwise it will have no effect.
The fields listed in this variable must be structure or union type fields. The fields listed in this variable must be structure or union type fields.
``ctypes`` will create descriptors in the structure type that allows to :mod:`ctypes` will create descriptors in the structure type that allows to
access the nested fields directly, without the need to create the access the nested fields directly, without the need to create the
structure or union field. structure or union field.
...@@ -2457,17 +2464,17 @@ other data types containing pointer type fields. ...@@ -2457,17 +2464,17 @@ other data types containing pointer type fields.
td.lptdesc = POINTER(some_type) td.lptdesc = POINTER(some_type)
td.u.lptdesc = POINTER(some_type) td.u.lptdesc = POINTER(some_type)
It is possible to defined sub-subclasses of structures, they inherit the fields It is possible to defined sub-subclasses of structures, they inherit the
of the base class. If the subclass definition has a separate :attr:`_fields_` fields of the base class. If the subclass definition has a separate
variable, the fields specified in this are appended to the fields of the base :attr:`_fields_` variable, the fields specified in this are appended to the
class. fields of the base class.
Structure and union constructors accept both positional and keyword arguments. Structure and union constructors accept both positional and keyword
Positional arguments are used to initialize member fields in the same order as arguments. Positional arguments are used to initialize member fields in the
they are appear in :attr:`_fields_`. Keyword arguments in the constructor are same order as they are appear in :attr:`_fields_`. Keyword arguments in the
interpreted as attribute assignments, so they will initialize :attr:`_fields_` constructor are interpreted as attribute assignments, so they will initialize
with the same name, or create new attributes for names not present in :attr:`_fields_` with the same name, or create new attributes for names not
:attr:`_fields_`. present in :attr:`_fields_`.
.. _ctypes-arrays-pointers: .. _ctypes-arrays-pointers:
...@@ -2475,6 +2482,6 @@ with the same name, or create new attributes for names not present in ...@@ -2475,6 +2482,6 @@ with the same name, or create new attributes for names not present in
Arrays and pointers Arrays and pointers
^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
Not yet written - please see the sections :ref:`ctypes-pointers` and Not yet written - please see the sections :ref:`ctypes-pointers` and section
section :ref:`ctypes-arrays` in the tutorial. :ref:`ctypes-arrays` in the tutorial.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment