Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fadaa2

Browse files
author
Thomas Heller
committed
Merged revisions 64131,64134-64141,64143-64146 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r64131 | thomas.heller | 2008-06-11 19:58:19 +0200 (Wed, 11 Jun 2008) | 1 line Markup fixes, spelling corrections, and better wordings. Hopefully. ........ r64135 | thomas.heller | 2008-06-11 20:10:43 +0200 (Wed, 11 Jun 2008) | 1 line More doc fixes. ........ r64139 | thomas.heller | 2008-06-11 20:40:51 +0200 (Wed, 11 Jun 2008) | 1 line Smaller doc fixes. ........ r64143 | thomas.heller | 2008-06-11 21:10:22 +0200 (Wed, 11 Jun 2008) | 1 line Add versionadded marker to ctypes.c_longdouble. ........ r64146 | thomas.heller | 2008-06-11 21:58:22 +0200 (Wed, 11 Jun 2008) | 2 lines Markup fixes, thanks Georg for the help. Document ctypes.util.find_library() and ctypes.util.find_msvcrt(). ........
1 parent c856c7a commit 2fadaa2

1 file changed

Lines changed: 101 additions & 90 deletions

File tree

Doc/library/ctypes.rst

Lines changed: 101 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ loads libraries which export functions using the standard ``cdecl`` calling
4040
convention, while *windll* libraries call functions using the ``stdcall``
4141
calling convention. *oledll* also uses the ``stdcall`` calling convention, and
4242
assumes the functions return a Windows :class:`HRESULT` error code. The error
43-
code is used to automatically raise :class:`WindowsError` Python exceptions when
43+
code is used to automatically raise a :class:`WindowsError` exception when
4444
the function call fails.
4545

4646
Here are some examples for Windows. Note that ``msvcrt`` is the MS standard C
@@ -55,10 +55,10 @@ convention::
5555
>>> libc = cdll.msvcrt # doctest: +WINDOWS
5656
>>>
5757

58-
Windows appends the usual '.dll' file suffix automatically.
58+
Windows appends the usual ``.dll`` file suffix automatically.
5959

6060
On Linux, it is required to specify the filename *including* the extension to
61-
load a library, so attribute access does not work. Either the
61+
load a library, so attribute access can not be used to load libraries. Either the
6262
:meth:`LoadLibrary` method of the dll loaders should be used, or you should load
6363
the library by creating an instance of CDLL by calling the constructor::
6464

@@ -107,7 +107,7 @@ UNICODE is defined or not::
107107

108108
*windll* does not try to select one of them by magic, you must access the
109109
version you need by specifying ``GetModuleHandleA`` or ``GetModuleHandleW``
110-
explicitly, and then call it with normal strings or unicode strings
110+
explicitly, and then call it with strings or unicode strings
111111
respectively.
112112

113113
Sometimes, dlls export functions with names which aren't valid Python
@@ -422,9 +422,9 @@ to implement a :meth:`from_param` class method for them to be able to use them
422422
in the :attr:`argtypes` sequence. The :meth:`from_param` class method receives
423423
the Python object passed to the function call, it should do a typecheck or
424424
whatever is needed to make sure this object is acceptable, and then return the
425-
object itself, it's :attr:`_as_parameter_` attribute, or whatever you want to
425+
object itself, its :attr:`_as_parameter_` attribute, or whatever you want to
426426
pass as the C function argument in this case. Again, the result should be an
427-
integer, string, unicode, a ``ctypes`` instance, or something having the
427+
integer, string, unicode, a ``ctypes`` instance, or an object with an
428428
:attr:`_as_parameter_` attribute.
429429

430430

@@ -719,6 +719,8 @@ would cause the pointer to point to the memory location where this is stored::
719719
c_long(99)
720720
>>>
721721

722+
.. XXX Document dereferencing pointers, and that it is preferred over the .contents attribute.
723+
722724
Pointer instances can also be indexed with integers::
723725

724726
>>> pi[0]
@@ -765,7 +767,7 @@ Calling the pointer type without an argument creates a ``NULL`` pointer.
765767
>>>
766768

767769
``ctypes`` checks for ``NULL`` when dereferencing pointers (but dereferencing
768-
non-\ ``NULL`` pointers would crash Python)::
770+
invalid non-\ ``NULL`` pointers would crash Python)::
769771

770772
>>> null_ptr[0]
771773
Traceback (most recent call last):
@@ -811,9 +813,9 @@ To set a POINTER type field to ``NULL``, you can assign ``None``::
811813
>>> bar.values = None
812814
>>>
813815

814-
XXX list other conversions...
816+
.. XXX list other conversions...
815817
816-
Sometimes you have instances of incompatible types. In ``C``, you can cast one
818+
Sometimes you have instances of incompatible types. In C, you can cast one
817819
type into another type. ``ctypes`` provides a ``cast`` function which can be
818820
used in the same way. The ``Bar`` structure defined above accepts
819821
``POINTER(c_int)`` pointers or :class:`c_int` arrays for its ``values`` field,
@@ -1070,7 +1072,7 @@ crashing your program when a callback is made.
10701072
Accessing values exported from dlls
10711073
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10721074

1073-
Sometimes, a dll not only exports functions, it also exports variables. An
1075+
Some shared libraries not only export functions, they also export variables. An
10741076
example in the Python library itself is the ``Py_OptimizeFlag``, an integer set
10751077
to 0, 1, or 2, depending on the :option:`-O` or :option:`-OO` flag given on
10761078
startup.
@@ -1246,17 +1248,6 @@ dynamic nature of Python, and (re-)define the data type after the required size
12461248
is already known, on a case by case basis.
12471249

12481250

1249-
.. _ctypes-bugs-todo-non-implemented-things:
1250-
1251-
Bugs, ToDo and non-implemented things
1252-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1253-
1254-
Enumeration types are not implemented. You can do it easily yourself, using
1255-
:class:`c_int` as the base class.
1256-
1257-
``long double`` is not implemented.
1258-
1259-
12601251
.. _ctypes-ctypes-reference:
12611252

12621253
ctypes reference
@@ -1615,9 +1606,8 @@ type and the argument types of the function.
16151606
`use_last_error` does the same for the Windows error code.
16161607

16171608
.. versionchanged:: 2.6
1618-
1619-
The optional `use_errno` and `use_last_error` parameters were added
1620-
in Python 2.6.
1609+
The optional `use_errno` and `use_last_error` parameters were
1610+
added.
16211611

16221612

16231613
.. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
@@ -1633,75 +1623,71 @@ type and the argument types of the function.
16331623
The returned function prototype creates functions that use the Python calling
16341624
convention. The function will *not* release the GIL during the call.
16351625

1636-
Function prototypes created by the factory functions can be instantiated in
1637-
different ways, depending on the type and number of the parameters in the call.
1638-
1639-
1640-
.. function:: prototype(address)
1641-
:noindex:
1642-
1643-
Returns a foreign function at the specified address.
1644-
1645-
1646-
.. function:: prototype(callable)
1647-
:noindex:
1648-
1649-
Create a C callable function (a callback function) from a Python ``callable``.
1626+
Function prototypes created by these factory functions can be instantiated in
1627+
different ways, depending on the type and number of the parameters in the call:
16501628

16511629

1652-
.. function:: prototype(func_spec[, paramflags])
1653-
:noindex:
1630+
.. function:: prototype(address)
1631+
:noindex:
1632+
:module:
16541633

1655-
Returns a foreign function exported by a shared library. ``func_spec`` must be a
1656-
2-tuple ``(name_or_ordinal, library)``. The first item is the name of the
1657-
exported function as string, or the ordinal of the exported function as small
1658-
integer. The second item is the shared library instance.
1634+
Returns a foreign function at the specified address which must be an integer.
16591635

16601636

1661-
.. function:: prototype(vtbl_index, name[, paramflags[, iid]])
1662-
:noindex:
1637+
.. function:: prototype(callable)
1638+
:noindex:
1639+
:module:
16631640

1664-
Returns a foreign function that will call a COM method. ``vtbl_index`` is the
1665-
index into the virtual function table, a small non-negative integer. *name* is
1666-
name of the COM method. *iid* is an optional pointer to the interface identifier
1667-
which is used in extended error reporting.
1641+
Create a C callable function (a callback function) from a Python ``callable``.
16681642

1669-
COM methods use a special calling convention: They require a pointer to the COM
1670-
interface as first argument, in addition to those parameters that are specified
1671-
in the :attr:`argtypes` tuple.
16721643

1673-
The optional *paramflags* parameter creates foreign function wrappers with much
1674-
more functionality than the features described above.
1644+
.. function:: prototype(func_spec[, paramflags])
1645+
:noindex:
1646+
:module:
16751647

1676-
*paramflags* must be a tuple of the same length as :attr:`argtypes`.
1648+
Returns a foreign function exported by a shared library. ``func_spec`` must be a
1649+
2-tuple ``(name_or_ordinal, library)``. The first item is the name of the
1650+
exported function as string, or the ordinal of the exported function as small
1651+
integer. The second item is the shared library instance.
16771652

1678-
Each item in this tuple contains further information about a parameter, it must
1679-
be a tuple containing 1, 2, or 3 items.
16801653

1681-
The first item is an integer containing flags for the parameter:
1654+
.. function:: prototype(vtbl_index, name[, paramflags[, iid]])
1655+
:noindex:
1656+
:module:
16821657

1658+
Returns a foreign function that will call a COM method. ``vtbl_index`` is the
1659+
index into the virtual function table, a small non-negative integer. *name* is
1660+
name of the COM method. *iid* is an optional pointer to the interface identifier
1661+
which is used in extended error reporting.
16831662

1684-
.. data:: 1
1685-
:noindex:
1663+
COM methods use a special calling convention: They require a pointer to the COM
1664+
interface as first argument, in addition to those parameters that are specified
1665+
in the :attr:`argtypes` tuple.
16861666

1687-
Specifies an input parameter to the function.
1667+
The optional *paramflags* parameter creates foreign function wrappers with much
1668+
more functionality than the features described above.
16881669

1670+
*paramflags* must be a tuple of the same length as :attr:`argtypes`.
16891671

1690-
.. data:: 2
1691-
:noindex:
1672+
Each item in this tuple contains further information about a parameter, it must
1673+
be a tuple containing one, two, or three items.
16921674

1693-
Output parameter. The foreign function fills in a value.
1675+
The first item is an integer containing a combination of direction
1676+
flags for the parameter:
16941677

1678+
1
1679+
Specifies an input parameter to the function.
16951680

1696-
.. data:: 4
1697-
:noindex:
1681+
2
1682+
Output parameter. The foreign function fills in a value.
16981683

1699-
Input parameter which defaults to the integer zero.
1684+
4
1685+
Input parameter which defaults to the integer zero.
17001686

1701-
The optional second item is the parameter name as string. If this is specified,
1702-
the foreign function can be called with named parameters.
1687+
The optional second item is the parameter name as string. If this is specified,
1688+
the foreign function can be called with named parameters.
17031689

1704-
The optional third item is the default value for this parameter.
1690+
The optional third item is the default value for this parameter.
17051691

17061692
This example demonstrates how to wrap the Windows ``MessageBoxA`` function so
17071693
that it supports default parameters and named arguments. The C declaration from
@@ -1714,16 +1700,14 @@ the windows header file is this::
17141700
LPCSTR lpCaption,
17151701
UINT uType);
17161702

1717-
Here is the wrapping with ``ctypes``:
1703+
Here is the wrapping with ``ctypes``::
17181704

1719-
::
1720-
1721-
>>> from ctypes import c_int, WINFUNCTYPE, windll
1722-
>>> from ctypes.wintypes import HWND, LPCSTR, UINT
1723-
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
1724-
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
1725-
>>> MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
1726-
>>>
1705+
>>> from ctypes import c_int, WINFUNCTYPE, windll
1706+
>>> from ctypes.wintypes import HWND, LPCSTR, UINT
1707+
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
1708+
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
1709+
>>> MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
1710+
>>>
17271711

17281712
The MessageBox foreign function can now be called in these ways::
17291713

@@ -1741,16 +1725,14 @@ function retrieves the dimensions of a specified window by copying them into
17411725
HWND hWnd,
17421726
LPRECT lpRect);
17431727

1744-
Here is the wrapping with ``ctypes``:
1745-
1746-
::
1728+
Here is the wrapping with ``ctypes``::
17471729

1748-
>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
1749-
>>> from ctypes.wintypes import BOOL, HWND, RECT
1750-
>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
1751-
>>> paramflags = (1, "hwnd"), (2, "lprect")
1752-
>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
1753-
>>>
1730+
>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
1731+
>>> from ctypes.wintypes import BOOL, HWND, RECT
1732+
>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
1733+
>>> paramflags = (1, "hwnd"), (2, "lprect")
1734+
>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
1735+
>>>
17541736

17551737
Functions with output parameters will automatically return the output parameter
17561738
value if there is a single one, or a tuple containing the output parameter
@@ -1766,6 +1748,7 @@ do the error checking, and raises an exception when the api call failed::
17661748
... if not result:
17671749
... raise WinError()
17681750
... return args
1751+
...
17691752
>>> GetWindowRect.errcheck = errcheck
17701753
>>>
17711754

@@ -1780,7 +1763,7 @@ instead, the normal processing will no longer take place::
17801763
... raise WinError()
17811764
... rc = args[1]
17821765
... return rc.left, rc.top, rc.bottom, rc.right
1783-
>>>
1766+
...
17841767
>>> GetWindowRect.errcheck = errcheck
17851768
>>>
17861769

@@ -1866,6 +1849,33 @@ Utility functions
18661849
servers with ctypes. It is called from the DllGetClassObject function that the
18671850
``_ctypes`` extension dll exports.
18681851

1852+
.. function:: find_library(name)
1853+
:module: ctypes.util
1854+
1855+
Try to find a library and return a pathname. `name` is the library name without
1856+
any prefix like `lib`, suffix like ``.so``, ``.dylib`` or version number (this
1857+
is the form used for the posix linker option :option:`-l`). If no library can
1858+
be found, returns ``None``.
1859+
1860+
The exact functionality is system dependent.
1861+
1862+
.. versionchanged:: 2.6
1863+
Windows only: ``find_library("m")`` or
1864+
``find_library("c")`` return the result of a call to
1865+
``find_msvcrt()``.
1866+
1867+
.. function:: find_msvcrt()
1868+
:module: ctypes.util
1869+
1870+
Windows only: return the filename of the VC runtype library used
1871+
by Python, and by the extension modules. If the name of the
1872+
library cannot be determined, ``None`` is returned.
1873+
1874+
If you need to free memory, for example, allocated by an extension
1875+
module with a call to the ``free(void *)``, it is important that you
1876+
use the function in the same library that allocated the memory.
1877+
1878+
.. versionadded:: 2.6
18691879

18701880
.. function:: FormatError([code])
18711881

@@ -2154,6 +2164,7 @@ These are the fundamental ctypes data types:
21542164
optional float initializer. On platforms where ``sizeof(long
21552165
double) == sizeof(double)`` it is an alias to :class:`c_double`.
21562166

2167+
.. versionadded:: 2.6
21572168

21582169
.. class:: c_float
21592170

0 commit comments

Comments
 (0)