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