dlmodule.c 5 KB
Newer Older
1 2 3

/* dl module */

Roger E. Masse's avatar
Roger E. Masse committed
4
#include "Python.h"
5 6 7 8 9 10 11

#include <dlfcn.h>

#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif

12
typedef void *PyUnivPtr;
13
typedef struct {
Roger E. Masse's avatar
Roger E. Masse committed
14 15
	PyObject_HEAD
	PyUnivPtr *dl_handle;
16 17
} dlobject;

Roger E. Masse's avatar
Roger E. Masse committed
18
staticforward PyTypeObject Dltype;
19

Roger E. Masse's avatar
Roger E. Masse committed
20
static PyObject *Dlerror;
21

Roger E. Masse's avatar
Roger E. Masse committed
22
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
23
newdlobject(PyUnivPtr *handle)
24 25
{
	dlobject *xp;
26
	xp = PyObject_New(dlobject, &Dltype);
27 28 29
	if (xp == NULL)
		return NULL;
	xp->dl_handle = handle;
Roger E. Masse's avatar
Roger E. Masse committed
30
	return (PyObject *)xp;
31 32 33
}

static void
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
34
dl_dealloc(dlobject *xp)
35 36 37
{
	if (xp->dl_handle != NULL)
		dlclose(xp->dl_handle);
38
	PyObject_Del(xp);
39 40
}

Roger E. Masse's avatar
Roger E. Masse committed
41
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
42
dl_close(dlobject *xp, PyObject *args)
43
{
Roger E. Masse's avatar
Roger E. Masse committed
44
	if (!PyArg_Parse(args, ""))
45 46 47 48 49
		return NULL;
	if (xp->dl_handle != NULL) {
		dlclose(xp->dl_handle);
		xp->dl_handle = NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
50 51
	Py_INCREF(Py_None);
	return Py_None;
52 53
}

Roger E. Masse's avatar
Roger E. Masse committed
54
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
55
dl_sym(dlobject *xp, PyObject *args)
56 57
{
	char *name;
Roger E. Masse's avatar
Roger E. Masse committed
58 59
	PyUnivPtr *func;
	if (!PyArg_Parse(args, "s", &name))
60 61 62
		return NULL;
	func = dlsym(xp->dl_handle, name);
	if (func == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
63 64
		Py_INCREF(Py_None);
		return Py_None;
65
	}
Roger E. Masse's avatar
Roger E. Masse committed
66
	return PyInt_FromLong((long)func);
67 68
}

Roger E. Masse's avatar
Roger E. Masse committed
69
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
70
dl_call(dlobject *xp, PyObject *args)
71
{
Roger E. Masse's avatar
Roger E. Masse committed
72
	PyObject *name;
73 74
	long (*func)(long, long, long, long, long,
                     long, long, long, long, long);
75 76 77
	long alist[10];
	long res;
	int i;
Roger E. Masse's avatar
Roger E. Masse committed
78
	int n = PyTuple_Size(args);
79
	if (n < 1) {
Roger E. Masse's avatar
Roger E. Masse committed
80
		PyErr_SetString(PyExc_TypeError, "at least a name is needed");
81 82
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
83 84 85 86
	name = PyTuple_GetItem(args, 0);
	if (!PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError,
				"function name must be a string");
87 88
		return NULL;
	}
Roger E. Masse's avatar
Roger E. Masse committed
89
	func = dlsym(xp->dl_handle, PyString_AsString(name));
90
	if (func == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
91
		PyErr_SetString(PyExc_ValueError, dlerror());
92 93 94
		return NULL;
	}
	if (n-1 > 10) {
Roger E. Masse's avatar
Roger E. Masse committed
95 96
		PyErr_SetString(PyExc_TypeError,
				"too many arguments (max 10)");
97 98 99
		return NULL;
	}
	for (i = 1; i < n; i++) {
Roger E. Masse's avatar
Roger E. Masse committed
100 101 102 103 104 105
		PyObject *v = PyTuple_GetItem(args, i);
		if (PyInt_Check(v))
			alist[i-1] = PyInt_AsLong(v);
		else if (PyString_Check(v))
			alist[i-1] = (long)PyString_AsString(v);
		else if (v == Py_None)
106 107
			alist[i-1] = (long) ((char *)NULL);
		else {
Roger E. Masse's avatar
Roger E. Masse committed
108
			PyErr_SetString(PyExc_TypeError,
109 110 111 112 113 114 115 116
				   "arguments must be int, string or None");
			return NULL;
		}
	}
	for (; i <= 10; i++)
		alist[i-1] = 0;
	res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
		      alist[5], alist[6], alist[7], alist[8], alist[9]);
Roger E. Masse's avatar
Roger E. Masse committed
117
	return PyInt_FromLong(res);
118 119
}

Roger E. Masse's avatar
Roger E. Masse committed
120 121 122 123 124
static PyMethodDef dlobject_methods[] = {
	{"call",	(PyCFunction)dl_call,	1 /* varargs */},
	{"sym", 	(PyCFunction)dl_sym},
	{"close",	(PyCFunction)dl_close},
	{NULL,  	NULL}			 /* Sentinel */
125 126
};

Roger E. Masse's avatar
Roger E. Masse committed
127
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
128
dl_getattr(dlobject *xp, char *name)
129
{
Roger E. Masse's avatar
Roger E. Masse committed
130
	return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
131 132 133
}


Roger E. Masse's avatar
Roger E. Masse committed
134 135
static PyTypeObject Dltype = {
	PyObject_HEAD_INIT(&PyType_Type)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	0,			/*ob_size*/
	"dl",			/*tp_name*/
	sizeof(dlobject),	/*tp_basicsize*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)dl_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)dl_getattr,/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
};

Roger E. Masse's avatar
Roger E. Masse committed
153
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
154
dl_open(PyObject *self, PyObject *args)
155 156 157
{
	char *name;
	int mode;
Roger E. Masse's avatar
Roger E. Masse committed
158 159
	PyUnivPtr *handle;
	if (PyArg_Parse(args, "z", &name))
160 161
		mode = RTLD_LAZY;
	else {
Roger E. Masse's avatar
Roger E. Masse committed
162 163
		PyErr_Clear();
		if (!PyArg_Parse(args, "(zi)", &name, &mode))
164 165 166
			return NULL;
#ifndef RTLD_NOW
		if (mode != RTLD_LAZY) {
Roger E. Masse's avatar
Roger E. Masse committed
167
			PyErr_SetString(PyExc_ValueError, "mode must be 1");
168 169 170 171 172 173
			return NULL;
		}
#endif
	}
	handle = dlopen(name, mode);
	if (handle == NULL) {
Roger E. Masse's avatar
Roger E. Masse committed
174
		PyErr_SetString(Dlerror, dlerror());
175 176 177 178 179
		return NULL;
	}
	return newdlobject(handle);
}

Roger E. Masse's avatar
Roger E. Masse committed
180
static PyMethodDef dl_methods[] = {
181 182 183 184
	{"open",	dl_open},
	{NULL,		NULL}		/* sentinel */
};

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* From socketmodule.c
 * Convenience routine to export an integer value.
 *
 * Errors are silently ignored, for better or for worse...
 */
static void
insint(PyObject *d, char *name, int value)
{
	PyObject *v = PyInt_FromLong((long) value);
	if (!v || PyDict_SetItemString(d, name, v))
		PyErr_Clear();

	Py_XDECREF(v);
}

200
void
201
initdl(void)
202
{
Roger E. Masse's avatar
Roger E. Masse committed
203
	PyObject *m, *d, *x;
204 205

	if (sizeof(int) != sizeof(long) ||
206
	    sizeof(long) != sizeof(char *)) {
207
		PyErr_SetString(PyExc_SystemError,
208
 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
209 210
		return;
	}
211 212

	/* Create the module and add the functions */
Roger E. Masse's avatar
Roger E. Masse committed
213
	m = Py_InitModule("dl", dl_methods);
214 215

	/* Add some symbolic constants to the module */
Roger E. Masse's avatar
Roger E. Masse committed
216
	d = PyModule_GetDict(m);
217
	Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
Roger E. Masse's avatar
Roger E. Masse committed
218 219 220
	PyDict_SetItemString(d, "error", x);
	x = PyInt_FromLong((long)RTLD_LAZY);
	PyDict_SetItemString(d, "RTLD_LAZY", x);
221
#define INSINT(X)    insint(d,#X,X)
222
#ifdef RTLD_NOW
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        INSINT(RTLD_NOW);
#endif
#ifdef RTLD_NOLOAD
        INSINT(RTLD_NOLOAD);
#endif
#ifdef RTLD_GLOBAL
        INSINT(RTLD_GLOBAL);
#endif
#ifdef RTLD_LOCAL
        INSINT(RTLD_LOCAL);
#endif
#ifdef RTLD_PARENT
        INSINT(RTLD_PARENT);
#endif
#ifdef RTLD_GROUP
        INSINT(RTLD_GROUP);
#endif
#ifdef RTLD_WORLD
        INSINT(RTLD_WORLD);
#endif
#ifdef RTLD_NODELETE
        INSINT(RTLD_NODELETE);
245 246
#endif
}