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