Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Modules / dlmodule.c
blob09556814f2dc779e7e397ca1745416c7de0f1847
2 /* dl module */
4 #include "Python.h"
6 #include <dlfcn.h>
8 #ifndef RTLD_LAZY
9 #define RTLD_LAZY 1
10 #endif
12 typedef void *PyUnivPtr;
13 typedef struct {
14 PyObject_HEAD
15 PyUnivPtr *dl_handle;
16 } dlobject;
18 static PyTypeObject Dltype;
20 static PyObject *Dlerror;
22 static PyObject *
23 newdlobject(PyUnivPtr *handle)
25 dlobject *xp;
26 xp = PyObject_New(dlobject, &Dltype);
27 if (xp == NULL)
28 return NULL;
29 xp->dl_handle = handle;
30 return (PyObject *)xp;
33 static void
34 dl_dealloc(dlobject *xp)
36 if (xp->dl_handle != NULL)
37 dlclose(xp->dl_handle);
38 PyObject_Del(xp);
41 static PyObject *
42 dl_close(dlobject *xp)
44 if (xp->dl_handle != NULL) {
45 dlclose(xp->dl_handle);
46 xp->dl_handle = NULL;
48 Py_INCREF(Py_None);
49 return Py_None;
52 static PyObject *
53 dl_sym(dlobject *xp, PyObject *args)
55 char *name;
56 PyUnivPtr *func;
57 if (PyString_Check(args)) {
58 name = PyString_AS_STRING(args);
59 } else {
60 PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
61 args->ob_type->tp_name);
62 return NULL;
64 func = dlsym(xp->dl_handle, name);
65 if (func == NULL) {
66 Py_INCREF(Py_None);
67 return Py_None;
69 return PyInt_FromLong((long)func);
72 static PyObject *
73 dl_call(dlobject *xp, PyObject *args)
75 PyObject *name;
76 long (*func)(long, long, long, long, long,
77 long, long, long, long, long);
78 long alist[10];
79 long res;
80 int i;
81 int n = PyTuple_Size(args);
82 if (n < 1) {
83 PyErr_SetString(PyExc_TypeError, "at least a name is needed");
84 return NULL;
86 name = PyTuple_GetItem(args, 0);
87 if (!PyString_Check(name)) {
88 PyErr_SetString(PyExc_TypeError,
89 "function name must be a string");
90 return NULL;
92 func = (long (*)(long, long, long, long, long,
93 long, long, long, long, long))
94 dlsym(xp->dl_handle, PyString_AsString(name));
95 if (func == NULL) {
96 PyErr_SetString(PyExc_ValueError, dlerror());
97 return NULL;
99 if (n-1 > 10) {
100 PyErr_SetString(PyExc_TypeError,
101 "too many arguments (max 10)");
102 return NULL;
104 for (i = 1; i < n; i++) {
105 PyObject *v = PyTuple_GetItem(args, i);
106 if (PyInt_Check(v))
107 alist[i-1] = PyInt_AsLong(v);
108 else if (PyString_Check(v))
109 alist[i-1] = (long)PyString_AsString(v);
110 else if (v == Py_None)
111 alist[i-1] = (long) ((char *)NULL);
112 else {
113 PyErr_SetString(PyExc_TypeError,
114 "arguments must be int, string or None");
115 return NULL;
118 for (; i <= 10; i++)
119 alist[i-1] = 0;
120 res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
121 alist[5], alist[6], alist[7], alist[8], alist[9]);
122 return PyInt_FromLong(res);
125 static PyMethodDef dlobject_methods[] = {
126 {"call", (PyCFunction)dl_call, METH_VARARGS},
127 {"sym", (PyCFunction)dl_sym, METH_O},
128 {"close", (PyCFunction)dl_close, METH_NOARGS},
129 {NULL, NULL} /* Sentinel */
132 static PyObject *
133 dl_getattr(dlobject *xp, char *name)
135 return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
139 static PyTypeObject Dltype = {
140 PyObject_HEAD_INIT(NULL)
141 0, /*ob_size*/
142 "dl.dl", /*tp_name*/
143 sizeof(dlobject), /*tp_basicsize*/
144 0, /*tp_itemsize*/
145 /* methods */
146 (destructor)dl_dealloc, /*tp_dealloc*/
147 0, /*tp_print*/
148 (getattrfunc)dl_getattr,/*tp_getattr*/
149 0, /*tp_setattr*/
150 0, /*tp_compare*/
151 0, /*tp_repr*/
152 0, /*tp_as_number*/
153 0, /*tp_as_sequence*/
154 0, /*tp_as_mapping*/
155 0, /*tp_hash*/
158 static PyObject *
159 dl_open(PyObject *self, PyObject *args)
161 char *name;
162 int mode;
163 PyUnivPtr *handle;
164 if (sizeof(int) != sizeof(long) ||
165 sizeof(long) != sizeof(char *)) {
166 PyErr_SetString(PyExc_SystemError,
167 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
168 return NULL;
171 if (PyArg_ParseTuple(args, "z:open", &name))
172 mode = RTLD_LAZY;
173 else {
174 PyErr_Clear();
175 if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
176 return NULL;
177 #ifndef RTLD_NOW
178 if (mode != RTLD_LAZY) {
179 PyErr_SetString(PyExc_ValueError, "mode must be 1");
180 return NULL;
182 #endif
184 handle = dlopen(name, mode);
185 if (handle == NULL) {
186 PyErr_SetString(Dlerror, dlerror());
187 return NULL;
189 return newdlobject(handle);
192 static PyMethodDef dl_methods[] = {
193 {"open", dl_open, METH_VARARGS},
194 {NULL, NULL} /* sentinel */
197 /* From socketmodule.c
198 * Convenience routine to export an integer value.
200 * Errors are silently ignored, for better or for worse...
202 static void
203 insint(PyObject *d, char *name, int value)
205 PyObject *v = PyInt_FromLong((long) value);
206 if (!v || PyDict_SetItemString(d, name, v))
207 PyErr_Clear();
209 Py_XDECREF(v);
212 PyMODINIT_FUNC
213 initdl(void)
215 PyObject *m, *d, *x;
217 /* Initialize object type */
218 Dltype.ob_type = &PyType_Type;
220 /* Create the module and add the functions */
221 m = Py_InitModule("dl", dl_methods);
222 if (m == NULL)
223 return;
225 /* Add some symbolic constants to the module */
226 d = PyModule_GetDict(m);
227 Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
228 PyDict_SetItemString(d, "error", x);
229 x = PyInt_FromLong((long)RTLD_LAZY);
230 PyDict_SetItemString(d, "RTLD_LAZY", x);
231 #define INSINT(X) insint(d,#X,X)
232 #ifdef RTLD_NOW
233 INSINT(RTLD_NOW);
234 #endif
235 #ifdef RTLD_NOLOAD
236 INSINT(RTLD_NOLOAD);
237 #endif
238 #ifdef RTLD_GLOBAL
239 INSINT(RTLD_GLOBAL);
240 #endif
241 #ifdef RTLD_LOCAL
242 INSINT(RTLD_LOCAL);
243 #endif
244 #ifdef RTLD_PARENT
245 INSINT(RTLD_PARENT);
246 #endif
247 #ifdef RTLD_GROUP
248 INSINT(RTLD_GROUP);
249 #endif
250 #ifdef RTLD_WORLD
251 INSINT(RTLD_WORLD);
252 #endif
253 #ifdef RTLD_NODELETE
254 INSINT(RTLD_NODELETE);
255 #endif