cpython/Modules/dlmodule.c

257 lines
5.3 KiB
C
Raw Normal View History

1994-05-23 20:37:57 +08:00
/* dl module */
1996-12-14 04:33:44 +08:00
#include "Python.h"
1994-05-23 20:37:57 +08:00
#include <dlfcn.h>
#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif
typedef void *PyUnivPtr;
1994-05-23 20:37:57 +08:00
typedef struct {
1996-12-14 04:33:44 +08:00
PyObject_HEAD
PyUnivPtr *dl_handle;
1994-05-23 20:37:57 +08:00
} dlobject;
static PyTypeObject Dltype;
1994-05-23 20:37:57 +08:00
1996-12-14 04:33:44 +08:00
static PyObject *Dlerror;
1994-05-23 20:37:57 +08:00
1996-12-14 04:33:44 +08:00
static PyObject *
2000-07-10 19:56:03 +08:00
newdlobject(PyUnivPtr *handle)
1994-05-23 20:37:57 +08:00
{
dlobject *xp;
xp = PyObject_New(dlobject, &Dltype);
1994-05-23 20:37:57 +08:00
if (xp == NULL)
return NULL;
xp->dl_handle = handle;
1996-12-14 04:33:44 +08:00
return (PyObject *)xp;
1994-05-23 20:37:57 +08:00
}
static void
2000-07-10 19:56:03 +08:00
dl_dealloc(dlobject *xp)
1994-05-23 20:37:57 +08:00
{
if (xp->dl_handle != NULL)
dlclose(xp->dl_handle);
PyObject_Del(xp);
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
static PyObject *
dl_close(dlobject *xp)
1994-05-23 20:37:57 +08:00
{
if (xp->dl_handle != NULL) {
dlclose(xp->dl_handle);
xp->dl_handle = NULL;
}
1996-12-14 04:33:44 +08:00
Py_INCREF(Py_None);
return Py_None;
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
static PyObject *
2000-07-10 19:56:03 +08:00
dl_sym(dlobject *xp, PyObject *args)
1994-05-23 20:37:57 +08:00
{
char *name;
1996-12-14 04:33:44 +08:00
PyUnivPtr *func;
if (PyString_Check(args)) {
name = PyString_AS_STRING(args);
} else {
PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
args->ob_type->tp_name);
1994-05-23 20:37:57 +08:00
return NULL;
}
1994-05-23 20:37:57 +08:00
func = dlsym(xp->dl_handle, name);
if (func == NULL) {
1996-12-14 04:33:44 +08:00
Py_INCREF(Py_None);
return Py_None;
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
return PyInt_FromLong((long)func);
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
static PyObject *
2000-07-10 19:56:03 +08:00
dl_call(dlobject *xp, PyObject *args)
1994-05-23 20:37:57 +08:00
{
1996-12-14 04:33:44 +08:00
PyObject *name;
2000-10-12 05:44:02 +08:00
long (*func)(long, long, long, long, long,
long, long, long, long, long);
1994-05-23 20:37:57 +08:00
long alist[10];
long res;
int i;
1996-12-14 04:33:44 +08:00
int n = PyTuple_Size(args);
1994-05-23 20:37:57 +08:00
if (n < 1) {
1996-12-14 04:33:44 +08:00
PyErr_SetString(PyExc_TypeError, "at least a name is needed");
1994-05-23 20:37:57 +08:00
return NULL;
}
1996-12-14 04:33:44 +08:00
name = PyTuple_GetItem(args, 0);
if (!PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"function name must be a string");
1994-05-23 20:37:57 +08:00
return NULL;
}
func = (long (*)(long, long, long, long, long,
long, long, long, long, long))
dlsym(xp->dl_handle, PyString_AsString(name));
1994-05-23 20:37:57 +08:00
if (func == NULL) {
1996-12-14 04:33:44 +08:00
PyErr_SetString(PyExc_ValueError, dlerror());
1994-05-23 20:37:57 +08:00
return NULL;
}
if (n-1 > 10) {
1996-12-14 04:33:44 +08:00
PyErr_SetString(PyExc_TypeError,
"too many arguments (max 10)");
1994-05-23 20:37:57 +08:00
return NULL;
}
for (i = 1; i < n; i++) {
1996-12-14 04:33:44 +08:00
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)
1994-05-23 20:37:57 +08:00
alist[i-1] = (long) ((char *)NULL);
else {
1996-12-14 04:33:44 +08:00
PyErr_SetString(PyExc_TypeError,
1994-05-23 20:37:57 +08:00
"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]);
1996-12-14 04:33:44 +08:00
return PyInt_FromLong(res);
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
static PyMethodDef dlobject_methods[] = {
{"call", (PyCFunction)dl_call, METH_VARARGS},
{"sym", (PyCFunction)dl_sym, METH_O},
{"close", (PyCFunction)dl_close, METH_NOARGS},
1996-12-14 04:33:44 +08:00
{NULL, NULL} /* Sentinel */
1994-05-23 20:37:57 +08:00
};
1996-12-14 04:33:44 +08:00
static PyObject *
2000-07-10 19:56:03 +08:00
dl_getattr(dlobject *xp, char *name)
1994-05-23 20:37:57 +08:00
{
1996-12-14 04:33:44 +08:00
return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
1994-05-23 20:37:57 +08:00
}
1996-12-14 04:33:44 +08:00
static PyTypeObject Dltype = {
PyObject_HEAD_INIT(NULL)
1994-05-23 20:37:57 +08:00
0, /*ob_size*/
"dl.dl", /*tp_name*/
1994-05-23 20:37:57 +08:00
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*/
};
1996-12-14 04:33:44 +08:00
static PyObject *
2000-07-10 19:56:03 +08:00
dl_open(PyObject *self, PyObject *args)
1994-05-23 20:37:57 +08:00
{
char *name;
int mode;
1996-12-14 04:33:44 +08:00
PyUnivPtr *handle;
2002-01-02 04:18:30 +08:00
if (sizeof(int) != sizeof(long) ||
sizeof(long) != sizeof(char *)) {
PyErr_SetString(PyExc_SystemError,
"module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
return NULL;
}
if (PyArg_ParseTuple(args, "z:open", &name))
1994-05-23 20:37:57 +08:00
mode = RTLD_LAZY;
else {
1996-12-14 04:33:44 +08:00
PyErr_Clear();
if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
1994-05-23 20:37:57 +08:00
return NULL;
#ifndef RTLD_NOW
if (mode != RTLD_LAZY) {
1996-12-14 04:33:44 +08:00
PyErr_SetString(PyExc_ValueError, "mode must be 1");
1994-05-23 20:37:57 +08:00
return NULL;
}
#endif
}
handle = dlopen(name, mode);
if (handle == NULL) {
1996-12-14 04:33:44 +08:00
PyErr_SetString(Dlerror, dlerror());
1994-05-23 20:37:57 +08:00
return NULL;
}
return newdlobject(handle);
}
1996-12-14 04:33:44 +08:00
static PyMethodDef dl_methods[] = {
{"open", dl_open, METH_VARARGS},
1994-05-23 20:37:57 +08:00
{NULL, NULL} /* sentinel */
};
/* 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);
}
PyMODINIT_FUNC
initdl(void)
1994-05-23 20:37:57 +08:00
{
1996-12-14 04:33:44 +08:00
PyObject *m, *d, *x;
1994-05-23 20:37:57 +08:00
/* Initialize object type */
Dltype.ob_type = &PyType_Type;
1994-05-23 20:37:57 +08:00
/* Create the module and add the functions */
1996-12-14 04:33:44 +08:00
m = Py_InitModule("dl", dl_methods);
if (m == NULL)
return;
1994-05-23 20:37:57 +08:00
/* Add some symbolic constants to the module */
1996-12-14 04:33:44 +08:00
d = PyModule_GetDict(m);
Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
1996-12-14 04:33:44 +08:00
PyDict_SetItemString(d, "error", x);
x = PyInt_FromLong((long)RTLD_LAZY);
PyDict_SetItemString(d, "RTLD_LAZY", x);
#define INSINT(X) insint(d,#X,X)
1994-05-23 20:37:57 +08:00
#ifdef RTLD_NOW
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);
1994-05-23 20:37:57 +08:00
#endif
}