1994-05-23 09:37:57 -03:00
|
|
|
|
|
|
|
/* dl module */
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
#include "Python.h"
|
1994-05-23 09:37:57 -03:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2006-07-09 22:18:57 -03:00
|
|
|
#ifdef __VMS
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1994-05-23 09:37:57 -03:00
|
|
|
#ifndef RTLD_LAZY
|
|
|
|
#define RTLD_LAZY 1
|
|
|
|
#endif
|
|
|
|
|
2000-07-25 09:56:38 -03:00
|
|
|
typedef void *PyUnivPtr;
|
1994-05-23 09:37:57 -03:00
|
|
|
typedef struct {
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyUnivPtr *dl_handle;
|
1994-05-23 09:37:57 -03:00
|
|
|
} dlobject;
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject Dltype;
|
1994-05-23 09:37:57 -03:00
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *Dlerror;
|
1994-05-23 09:37:57 -03:00
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2000-07-10 08:56:03 -03:00
|
|
|
newdlobject(PyUnivPtr *handle)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
dlobject *xp;
|
|
|
|
xp = PyObject_New(dlobject, &Dltype);
|
|
|
|
if (xp == NULL)
|
|
|
|
return NULL;
|
|
|
|
xp->dl_handle = handle;
|
|
|
|
return (PyObject *)xp;
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-10 08:56:03 -03:00
|
|
|
dl_dealloc(dlobject *xp)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
if (xp->dl_handle != NULL)
|
|
|
|
dlclose(xp->dl_handle);
|
|
|
|
PyObject_Del(xp);
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2002-03-31 11:43:28 -04:00
|
|
|
dl_close(dlobject *xp)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
if (xp->dl_handle != NULL) {
|
|
|
|
dlclose(xp->dl_handle);
|
|
|
|
xp->dl_handle = NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2000-07-10 08:56:03 -03:00
|
|
|
dl_sym(dlobject *xp, PyObject *args)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
char *name;
|
|
|
|
PyUnivPtr *func;
|
|
|
|
if (PyString_Check(args)) {
|
|
|
|
name = PyString_AS_STRING(args);
|
|
|
|
} else {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected string, found %.200s",
|
|
|
|
Py_TYPE(args)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
func = dlsym(xp->dl_handle, name);
|
|
|
|
if (func == NULL) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
return PyInt_FromLong((long)func);
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2000-07-10 08:56:03 -03:00
|
|
|
dl_call(dlobject *xp, PyObject *args)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject *name;
|
|
|
|
long (*func)(long, long, long, long, long,
|
|
|
|
long, long, long, long, long);
|
|
|
|
long alist[10];
|
|
|
|
long res;
|
|
|
|
Py_ssize_t i;
|
|
|
|
Py_ssize_t n = PyTuple_Size(args);
|
|
|
|
if (n < 1) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "at least a name is needed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
name = PyTuple_GetItem(args, 0);
|
|
|
|
if (!PyString_Check(name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"function name must be a string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
func = (long (*)(long, long, long, long, long,
|
|
|
|
long, long, long, long, long))
|
|
|
|
dlsym(xp->dl_handle, PyString_AsString(name));
|
|
|
|
if (func == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, dlerror());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (n-1 > 10) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"too many arguments (max 10)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 1; i < n; i++) {
|
|
|
|
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)
|
|
|
|
alist[i-1] = (long) ((char *)NULL);
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"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]);
|
|
|
|
return PyInt_FromLong(res);
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyMethodDef dlobject_methods[] = {
|
2010-05-09 12:15:40 -03:00
|
|
|
{"call", (PyCFunction)dl_call, METH_VARARGS},
|
|
|
|
{"sym", (PyCFunction)dl_sym, METH_O},
|
|
|
|
{"close", (PyCFunction)dl_close, METH_NOARGS},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
1994-05-23 09:37:57 -03:00
|
|
|
};
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2000-07-10 08:56:03 -03:00
|
|
|
dl_getattr(dlobject *xp, char *name)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyTypeObject Dltype = {
|
2010-05-09 12:15:40 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"dl.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*/
|
1994-05-23 09:37:57 -03:00
|
|
|
};
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyObject *
|
2000-07-10 08:56:03 -03:00
|
|
|
dl_open(PyObject *self, PyObject *args)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
char *name;
|
|
|
|
int mode;
|
|
|
|
PyUnivPtr *handle;
|
|
|
|
if (sizeof(int) != sizeof(long) ||
|
|
|
|
sizeof(long) != sizeof(char *)) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
2002-01-01 16:18:30 -04:00
|
|
|
"module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
|
2010-05-09 12:15:40 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-01-01 16:18:30 -04:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
if (PyArg_ParseTuple(args, "z:open", &name))
|
|
|
|
mode = RTLD_LAZY;
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_ParseTuple(args, "zi:open", &name, &mode))
|
|
|
|
return NULL;
|
1994-05-23 09:37:57 -03:00
|
|
|
#ifndef RTLD_NOW
|
2010-05-09 12:15:40 -03:00
|
|
|
if (mode != RTLD_LAZY) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "mode must be 1");
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-05-23 09:37:57 -03:00
|
|
|
#endif
|
2010-05-09 12:15:40 -03:00
|
|
|
}
|
|
|
|
handle = dlopen(name, mode);
|
|
|
|
if (handle == NULL) {
|
|
|
|
char *errmsg = dlerror();
|
|
|
|
if (!errmsg)
|
|
|
|
errmsg = "dlopen() error";
|
|
|
|
PyErr_SetString(Dlerror, errmsg);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-09 22:18:57 -03:00
|
|
|
#ifdef __VMS
|
2010-05-09 12:15:40 -03:00
|
|
|
/* Under OpenVMS dlopen doesn't do any check, just save the name
|
|
|
|
* for later use, so we have to check if the file is readable,
|
|
|
|
* the name can be a logical or a file from SYS$SHARE.
|
|
|
|
*/
|
|
|
|
if (access(name, R_OK)) {
|
|
|
|
char fname[strlen(name) + 20];
|
|
|
|
strcpy(fname, "SYS$SHARE:");
|
|
|
|
strcat(fname, name);
|
|
|
|
strcat(fname, ".EXE");
|
|
|
|
if (access(fname, R_OK)) {
|
|
|
|
dlclose(handle);
|
|
|
|
PyErr_SetString(Dlerror,
|
|
|
|
"File not found or protection violation");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-07-09 22:18:57 -03:00
|
|
|
#endif
|
2010-05-09 12:15:40 -03:00
|
|
|
return newdlobject(handle);
|
1994-05-23 09:37:57 -03:00
|
|
|
}
|
|
|
|
|
1996-12-13 16:33:44 -04:00
|
|
|
static PyMethodDef dl_methods[] = {
|
2010-05-09 12:15:40 -03:00
|
|
|
{"open", dl_open, METH_VARARGS},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1994-05-23 09:37:57 -03:00
|
|
|
};
|
|
|
|
|
2000-09-13 13:26:10 -03:00
|
|
|
/* 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)
|
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject *v = PyInt_FromLong((long) value);
|
|
|
|
if (!v || PyDict_SetItemString(d, name, v))
|
|
|
|
PyErr_Clear();
|
2000-09-13 13:26:10 -03:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
Py_XDECREF(v);
|
2000-09-13 13:26:10 -03:00
|
|
|
}
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2000-07-21 03:00:07 -03:00
|
|
|
initdl(void)
|
1994-05-23 09:37:57 -03:00
|
|
|
{
|
2010-05-09 12:15:40 -03:00
|
|
|
PyObject *m, *d, *x;
|
1994-05-23 09:37:57 -03:00
|
|
|
|
2008-05-10 18:20:19 -03:00
|
|
|
if (PyErr_WarnPy3k("the dl module has been removed in "
|
2008-05-10 22:09:32 -03:00
|
|
|
"Python 3.0; use the ctypes module instead", 2) < 0)
|
2010-05-09 12:15:40 -03:00
|
|
|
return;
|
2008-05-10 18:20:19 -03:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
/* Initialize object type */
|
|
|
|
Py_TYPE(&Dltype) = &PyType_Type;
|
2001-02-27 16:54:23 -04:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule("dl", dl_methods);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
1994-05-23 09:37:57 -03:00
|
|
|
|
2010-05-09 12:15:40 -03:00
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
|
|
|
|
PyDict_SetItemString(d, "error", x);
|
|
|
|
x = PyInt_FromLong((long)RTLD_LAZY);
|
|
|
|
PyDict_SetItemString(d, "RTLD_LAZY", x);
|
2000-09-13 13:26:10 -03:00
|
|
|
#define INSINT(X) insint(d,#X,X)
|
1994-05-23 09:37:57 -03:00
|
|
|
#ifdef RTLD_NOW
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_NOW);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_NOLOAD
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_NOLOAD);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_GLOBAL
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_GLOBAL);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_LOCAL
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_LOCAL);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_PARENT
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_PARENT);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_GROUP
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_GROUP);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_WORLD
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_WORLD);
|
2000-09-13 13:26:10 -03:00
|
|
|
#endif
|
|
|
|
#ifdef RTLD_NODELETE
|
2010-05-09 12:15:40 -03:00
|
|
|
INSINT(RTLD_NODELETE);
|
1994-05-23 09:37:57 -03:00
|
|
|
#endif
|
|
|
|
}
|