source: vendor/python/2.5/Doc/lib/libdl.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.4 KB
Line 
1\section{\module{dl} ---
2 Call C functions in shared objects}
3\declaremodule{extension}{dl}
4 \platform{Unix} %?????????? Anyone????????????
5\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
6\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
11the program to call arbitrary functions in such a library.
12
13\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
17\note{This module will not work unless
18\code{sizeof(int) == sizeof(long) == sizeof(char *)}
19If this is not the case, \exception{SystemError} will be raised on
20import.}
21
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
28systems do not support \constant{RTLD_NOW}.
29
30Return value is a \class{dlobject}.
31\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}
49Exception raised when an error has occurred inside the dynamic loading
50and 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,
94or \code{None}, which will be passed as \NULL. Note that
95strings 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}
Note: See TracBrowser for help on using the repository browser.