blob: ff42619119a6b425c948d16883123e3d83d17345 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`dl` --- Call C functions in shared objects
3================================================
4
5.. module:: dl
6 :platform: Unix
7 :synopsis: Call C functions in shared objects.
8.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
9
10
11.. % ?????????? Anyone????????????
12
13The :mod:`dl` module defines an interface to the :cfunc:`dlopen` function, which
14is the most common interface on Unix platforms for handling dynamically linked
15libraries. It allows the program to call arbitrary functions in such a library.
16
17.. warning::
18
19 The :mod:`dl` module bypasses the Python type system and error handling. If
20 used incorrectly it may cause segmentation faults, crashes or other incorrect
21 behaviour.
22
23.. note::
24
25 This module will not work unless ``sizeof(int) == sizeof(long) == sizeof(char
26 *)`` If this is not the case, :exc:`SystemError` will be raised on import.
27
28The :mod:`dl` module defines the following function:
29
30
31.. function:: open(name[, mode=RTLD_LAZY])
32
33 Open a shared object file, and return a handle. Mode signifies late binding
34 (:const:`RTLD_LAZY`) or immediate binding (:const:`RTLD_NOW`). Default is
35 :const:`RTLD_LAZY`. Note that some systems do not support :const:`RTLD_NOW`.
36
37 Return value is a :class:`dlobject`.
38
39The :mod:`dl` module defines the following constants:
40
41
42.. data:: RTLD_LAZY
43
44 Useful as an argument to :func:`open`.
45
46
47.. data:: RTLD_NOW
48
49 Useful as an argument to :func:`open`. Note that on systems which do not
50 support immediate binding, this constant will not appear in the module. For
51 maximum portability, use :func:`hasattr` to determine if the system supports
52 immediate binding.
53
54The :mod:`dl` module defines the following exception:
55
56
57.. exception:: error
58
59 Exception raised when an error has occurred inside the dynamic loading and
60 linking routines.
61
62Example::
63
64 >>> import dl, time
65 >>> a=dl.open('/lib/libc.so.6')
66 >>> a.call('time'), time.time()
67 (929723914, 929723914.498)
68
69This example was tried on a Debian GNU/Linux system, and is a good example of
70the fact that using this module is usually a bad alternative.
71
72
73.. _dl-objects:
74
75Dl Objects
76----------
77
78Dl objects, as returned by :func:`open` above, have the following methods:
79
80
81.. method:: dl.close()
82
83 Free all resources, except the memory.
84
85
86.. method:: dl.sym(name)
87
88 Return the pointer for the function named *name*, as a number, if it exists in
89 the referenced shared object, otherwise ``None``. This is useful in code like::
90
91 >>> if a.sym('time'):
92 ... a.call('time')
93 ... else:
94 ... time.time()
95
96 (Note that this function will return a non-zero number, as zero is the *NULL*
97 pointer)
98
99
100.. method:: dl.call(name[, arg1[, arg2...]])
101
102 Call the function named *name* in the referenced shared object. The arguments
103 must be either Python integers, which will be passed as is, Python strings, to
104 which a pointer will be passed, or ``None``, which will be passed as *NULL*.
105 Note that strings should only be passed to functions as :ctype:`const char\*`,
106 as Python will not like its string mutated.
107
108 There must be at most 10 arguments, and arguments not given will be treated as
109 ``None``. The function's return value must be a C :ctype:`long`, which is a
110 Python integer.
111