blob: 325724c8424f988ba3fe3e3007fcf3346c05a05d [file] [log] [blame]
Fred Drake4c8d83f1999-07-01 20:40:21 +00001\section{\module{dl} ---
2 Call C functions in shared objects}
3\declaremodule{extension}{dl}
4 \platform{Unix} %?????????? Anyone????????????
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drake4c8d83f1999-07-01 20:40:21 +00006\modulesynopsis{Call C functions in shared objects.}
7
8The \module{dl} module defines an interface to the
9\cfunction{dlopen()} function, which is the most common interface on
10\UNIX{} platforms for handling dynamically linked libraries. It allows
Thomas Woutersf8316632000-07-16 19:01:10 +000011the program to call arbitrary functions in such a library.
Fred Drake4c8d83f1999-07-01 20:40:21 +000012
Georg Brandl8cb30772006-01-20 09:34:29 +000013\warning{The \module{dl} module bypasses the Python type system and
14error handling. If used incorrectly it may cause segmentation faults,
15crashes or other incorrect behaviour.}
16
Fred Drake0aa811c2001-10-20 04:24:09 +000017\note{This module will not work unless
18\code{sizeof(int) == sizeof(long) == sizeof(char *)}
Fred Drake4c8d83f1999-07-01 20:40:21 +000019If this is not the case, \exception{SystemError} will be raised on
Fred Drake0aa811c2001-10-20 04:24:09 +000020import.}
Fred Drake4c8d83f1999-07-01 20:40:21 +000021
22The \module{dl} module defines the following function:
23
24\begin{funcdesc}{open}{name\optional{, mode\code{ = RTLD_LAZY}}}
25Open a shared object file, and return a handle. Mode
26signifies late binding (\constant{RTLD_LAZY}) or immediate binding
27(\constant{RTLD_NOW}). Default is \constant{RTLD_LAZY}. Note that some
Thomas Woutersf8316632000-07-16 19:01:10 +000028systems do not support \constant{RTLD_NOW}.
Fred Drake4c8d83f1999-07-01 20:40:21 +000029
Georg Brandl296152e2006-01-23 21:33:03 +000030Return value is a \class{dlobject}.
Fred Drake4c8d83f1999-07-01 20:40:21 +000031\end{funcdesc}
32
33The \module{dl} module defines the following constants:
34
35\begin{datadesc}{RTLD_LAZY}
36Useful as an argument to \function{open()}.
37\end{datadesc}
38
39\begin{datadesc}{RTLD_NOW}
40Useful as an argument to \function{open()}. Note that on systems
41which do not support immediate binding, this constant will not appear
42in the module. For maximum portability, use \function{hasattr()} to
43determine if the system supports immediate binding.
44\end{datadesc}
45
46The \module{dl} module defines the following exception:
47
48\begin{excdesc}{error}
Thomas Woutersf8316632000-07-16 19:01:10 +000049Exception raised when an error has occurred inside the dynamic loading
Fred Drake4c8d83f1999-07-01 20:40:21 +000050and linking routines.
51\end{excdesc}
52
53Example:
54
55\begin{verbatim}
56>>> import dl, time
57>>> a=dl.open('/lib/libc.so.6')
58>>> a.call('time'), time.time()
59(929723914, 929723914.498)
60\end{verbatim}
61
62This example was tried on a Debian GNU/Linux system, and is a good
63example of the fact that using this module is usually a bad alternative.
64
65\subsection{Dl Objects \label{dl-objects}}
66
67Dl objects, as returned by \function{open()} above, have the
68following methods:
69
70\begin{methoddesc}{close}{}
71Free all resources, except the memory.
72\end{methoddesc}
73
74\begin{methoddesc}{sym}{name}
75Return the pointer for the function named \var{name}, as a number, if
76it exists in the referenced shared object, otherwise \code{None}. This
77is useful in code like:
78
79\begin{verbatim}
80>>> if a.sym('time'):
81... a.call('time')
82... else:
83... time.time()
84\end{verbatim}
85
86(Note that this function will return a non-zero number, as zero is the
87\NULL{} pointer)
88\end{methoddesc}
89
90\begin{methoddesc}{call}{name\optional{, arg1\optional{, arg2\ldots}}}
91Call the function named \var{name} in the referenced shared object.
92The arguments must be either Python integers, which will be
93passed as is, Python strings, to which a pointer will be passed,
Fred Drakec37b65e2001-11-28 07:26:15 +000094or \code{None}, which will be passed as \NULL. Note that
Fred Drake4c8d83f1999-07-01 20:40:21 +000095strings should only be passed to functions as \ctype{const char*}, as
96Python will not like its string mutated.
97
98There must be at most 10 arguments, and arguments not given will be
99treated as \code{None}. The function's return value must be a C
100\ctype{long}, which is a Python integer.
101\end{methoddesc}