Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* dl module */ |
| 12 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 14 | |
| 15 | #include <dlfcn.h> |
| 16 | |
| 17 | #ifndef RTLD_LAZY |
| 18 | #define RTLD_LAZY 1 |
| 19 | #endif |
| 20 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 21 | typedef ANY *PyUnivPtr; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 22 | typedef struct { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 23 | PyObject_HEAD |
| 24 | PyUnivPtr *dl_handle; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 25 | } dlobject; |
| 26 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 27 | staticforward PyTypeObject Dltype; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 28 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 29 | static PyObject *Dlerror; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 30 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 31 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 32 | newdlobject(PyUnivPtr *handle) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 33 | { |
| 34 | dlobject *xp; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 35 | xp = PyObject_New(dlobject, &Dltype); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 36 | if (xp == NULL) |
| 37 | return NULL; |
| 38 | xp->dl_handle = handle; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 39 | return (PyObject *)xp; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 43 | dl_dealloc(dlobject *xp) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 44 | { |
| 45 | if (xp->dl_handle != NULL) |
| 46 | dlclose(xp->dl_handle); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 47 | PyObject_Del(xp); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 50 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 51 | dl_close(dlobject *xp, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 52 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 53 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | if (xp->dl_handle != NULL) { |
| 56 | dlclose(xp->dl_handle); |
| 57 | xp->dl_handle = NULL; |
| 58 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 59 | Py_INCREF(Py_None); |
| 60 | return Py_None; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 63 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 64 | dl_sym(dlobject *xp, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 65 | { |
| 66 | char *name; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 67 | PyUnivPtr *func; |
| 68 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | func = dlsym(xp->dl_handle, name); |
| 71 | if (func == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 74 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 75 | return PyInt_FromLong((long)func); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 78 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 79 | dl_call(dlobject *xp, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 80 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 81 | PyObject *name; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 82 | long (*func)(); |
| 83 | long alist[10]; |
| 84 | long res; |
| 85 | int i; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 86 | int n = PyTuple_Size(args); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 87 | if (n < 1) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 88 | PyErr_SetString(PyExc_TypeError, "at least a name is needed"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 89 | return NULL; |
| 90 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 91 | name = PyTuple_GetItem(args, 0); |
| 92 | if (!PyString_Check(name)) { |
| 93 | PyErr_SetString(PyExc_TypeError, |
| 94 | "function name must be a string"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 97 | func = dlsym(xp->dl_handle, PyString_AsString(name)); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 98 | if (func == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 99 | PyErr_SetString(PyExc_ValueError, dlerror()); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | if (n-1 > 10) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 103 | PyErr_SetString(PyExc_TypeError, |
| 104 | "too many arguments (max 10)"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | for (i = 1; i < n; i++) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 108 | PyObject *v = PyTuple_GetItem(args, i); |
| 109 | if (PyInt_Check(v)) |
| 110 | alist[i-1] = PyInt_AsLong(v); |
| 111 | else if (PyString_Check(v)) |
| 112 | alist[i-1] = (long)PyString_AsString(v); |
| 113 | else if (v == Py_None) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 114 | alist[i-1] = (long) ((char *)NULL); |
| 115 | else { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 116 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 117 | "arguments must be int, string or None"); |
| 118 | return NULL; |
| 119 | } |
| 120 | } |
| 121 | for (; i <= 10; i++) |
| 122 | alist[i-1] = 0; |
| 123 | res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4], |
| 124 | alist[5], alist[6], alist[7], alist[8], alist[9]); |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 125 | return PyInt_FromLong(res); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 128 | static PyMethodDef dlobject_methods[] = { |
| 129 | {"call", (PyCFunction)dl_call, 1 /* varargs */}, |
| 130 | {"sym", (PyCFunction)dl_sym}, |
| 131 | {"close", (PyCFunction)dl_close}, |
| 132 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 135 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 136 | dl_getattr(dlobject *xp, char *name) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 137 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 138 | return Py_FindMethod(dlobject_methods, (PyObject *)xp, name); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 142 | static PyTypeObject Dltype = { |
| 143 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 144 | 0, /*ob_size*/ |
| 145 | "dl", /*tp_name*/ |
| 146 | sizeof(dlobject), /*tp_basicsize*/ |
| 147 | 0, /*tp_itemsize*/ |
| 148 | /* methods */ |
| 149 | (destructor)dl_dealloc, /*tp_dealloc*/ |
| 150 | 0, /*tp_print*/ |
| 151 | (getattrfunc)dl_getattr,/*tp_getattr*/ |
| 152 | 0, /*tp_setattr*/ |
| 153 | 0, /*tp_compare*/ |
| 154 | 0, /*tp_repr*/ |
| 155 | 0, /*tp_as_number*/ |
| 156 | 0, /*tp_as_sequence*/ |
| 157 | 0, /*tp_as_mapping*/ |
| 158 | 0, /*tp_hash*/ |
| 159 | }; |
| 160 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 161 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 162 | dl_open(PyObject *self, PyObject *args) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 163 | { |
| 164 | char *name; |
| 165 | int mode; |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 166 | PyUnivPtr *handle; |
| 167 | if (PyArg_Parse(args, "z", &name)) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 168 | mode = RTLD_LAZY; |
| 169 | else { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 170 | PyErr_Clear(); |
| 171 | if (!PyArg_Parse(args, "(zi)", &name, &mode)) |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 172 | return NULL; |
| 173 | #ifndef RTLD_NOW |
| 174 | if (mode != RTLD_LAZY) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 175 | PyErr_SetString(PyExc_ValueError, "mode must be 1"); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | #endif |
| 179 | } |
| 180 | handle = dlopen(name, mode); |
| 181 | if (handle == NULL) { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 182 | PyErr_SetString(Dlerror, dlerror()); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
| 185 | return newdlobject(handle); |
| 186 | } |
| 187 | |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 188 | static PyMethodDef dl_methods[] = { |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 189 | {"open", dl_open}, |
| 190 | {NULL, NULL} /* sentinel */ |
| 191 | }; |
| 192 | |
| 193 | void |
| 194 | initdl() |
| 195 | { |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 196 | PyObject *m, *d, *x; |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 197 | |
| 198 | if (sizeof(int) != sizeof(long) || |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 199 | sizeof(long) != sizeof(char *)) { |
Guido van Rossum | e795197 | 1997-12-16 23:58:15 +0000 | [diff] [blame] | 200 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 201 | "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)"); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 204 | |
| 205 | /* Create the module and add the functions */ |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 206 | m = Py_InitModule("dl", dl_methods); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 207 | |
| 208 | /* Add some symbolic constants to the module */ |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 209 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 210 | Dlerror = x = PyErr_NewException("dl.error", NULL, NULL); |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 211 | PyDict_SetItemString(d, "error", x); |
| 212 | x = PyInt_FromLong((long)RTLD_LAZY); |
| 213 | PyDict_SetItemString(d, "RTLD_LAZY", x); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 214 | #ifdef RTLD_NOW |
Roger E. Masse | 0e12032 | 1996-12-13 20:33:44 +0000 | [diff] [blame] | 215 | x = PyInt_FromLong((long)RTLD_NOW); |
| 216 | PyDict_SetItemString(d, "RTLD_NOW", x); |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 217 | #endif |
Guido van Rossum | 34162a1 | 1994-05-23 12:37:57 +0000 | [diff] [blame] | 218 | } |