cpython/Modules/dlmodule.c

252 lines
5.8 KiB
C
Raw Normal View History

1994-05-23 12:37:57 +00:00
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1994-05-23 12:37:57 +00:00
All Rights Reserved
1996-10-25 14:44:06 +00:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1994-05-23 12:37:57 +00:00
provided that the above copyright notice appear in all copies and that
1996-10-25 14:44:06 +00:00
both that copyright notice and this permission notice appear in
1994-05-23 12:37:57 +00:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 14:44:06 +00:00
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
1994-05-23 12:37:57 +00:00
******************************************************************/
/* dl module */
1996-12-13 20:33:44 +00:00
#include "Python.h"
1994-05-23 12:37:57 +00:00
#include <dlfcn.h>
#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif
1996-12-13 20:33:44 +00:00
typedef ANY *PyUnivPtr;
1994-05-23 12:37:57 +00:00
typedef struct {
1996-12-13 20:33:44 +00:00
PyObject_HEAD
PyUnivPtr *dl_handle;
1994-05-23 12:37:57 +00:00
} dlobject;
1996-12-13 20:33:44 +00:00
staticforward PyTypeObject Dltype;
1994-05-23 12:37:57 +00:00
1996-12-13 20:33:44 +00:00
static PyObject *Dlerror;
1994-05-23 12:37:57 +00:00
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
newdlobject(handle)
1996-12-13 20:33:44 +00:00
PyUnivPtr *handle;
1994-05-23 12:37:57 +00:00
{
dlobject *xp;
xp = PyObject_New(dlobject, &Dltype);
1994-05-23 12:37:57 +00:00
if (xp == NULL)
return NULL;
xp->dl_handle = handle;
1996-12-13 20:33:44 +00:00
return (PyObject *)xp;
1994-05-23 12:37:57 +00:00
}
static void
dl_dealloc(xp)
dlobject *xp;
{
if (xp->dl_handle != NULL)
dlclose(xp->dl_handle);
PyObject_Del(xp);
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
dl_close(xp, args)
dlobject *xp;
1996-12-13 20:33:44 +00:00
PyObject *args;
1994-05-23 12:37:57 +00:00
{
1996-12-13 20:33:44 +00:00
if (!PyArg_Parse(args, ""))
1994-05-23 12:37:57 +00:00
return NULL;
if (xp->dl_handle != NULL) {
dlclose(xp->dl_handle);
xp->dl_handle = NULL;
}
1996-12-13 20:33:44 +00:00
Py_INCREF(Py_None);
return Py_None;
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
dl_sym(xp, args)
dlobject *xp;
1996-12-13 20:33:44 +00:00
PyObject *args;
1994-05-23 12:37:57 +00:00
{
char *name;
1996-12-13 20:33:44 +00:00
PyUnivPtr *func;
if (!PyArg_Parse(args, "s", &name))
1994-05-23 12:37:57 +00:00
return NULL;
func = dlsym(xp->dl_handle, name);
if (func == NULL) {
1996-12-13 20:33:44 +00:00
Py_INCREF(Py_None);
return Py_None;
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
return PyInt_FromLong((long)func);
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
dl_call(xp, args)
dlobject *xp;
1996-12-13 20:33:44 +00:00
PyObject *args; /* (varargs) */
1994-05-23 12:37:57 +00:00
{
1996-12-13 20:33:44 +00:00
PyObject *name;
1994-05-23 12:37:57 +00:00
long (*func)();
long alist[10];
long res;
int i;
1996-12-13 20:33:44 +00:00
int n = PyTuple_Size(args);
1994-05-23 12:37:57 +00:00
if (n < 1) {
1996-12-13 20:33:44 +00:00
PyErr_SetString(PyExc_TypeError, "at least a name is needed");
1994-05-23 12:37:57 +00:00
return NULL;
}
1996-12-13 20:33:44 +00:00
name = PyTuple_GetItem(args, 0);
if (!PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"function name must be a string");
1994-05-23 12:37:57 +00:00
return NULL;
}
1996-12-13 20:33:44 +00:00
func = dlsym(xp->dl_handle, PyString_AsString(name));
1994-05-23 12:37:57 +00:00
if (func == NULL) {
1996-12-13 20:33:44 +00:00
PyErr_SetString(PyExc_ValueError, dlerror());
1994-05-23 12:37:57 +00:00
return NULL;
}
if (n-1 > 10) {
1996-12-13 20:33:44 +00:00
PyErr_SetString(PyExc_TypeError,
"too many arguments (max 10)");
1994-05-23 12:37:57 +00:00
return NULL;
}
for (i = 1; i < n; i++) {
1996-12-13 20:33:44 +00: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 12:37:57 +00:00
alist[i-1] = (long) ((char *)NULL);
else {
1996-12-13 20:33:44 +00:00
PyErr_SetString(PyExc_TypeError,
1994-05-23 12:37:57 +00: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-13 20:33:44 +00:00
return PyInt_FromLong(res);
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
static PyMethodDef dlobject_methods[] = {
{"call", (PyCFunction)dl_call, 1 /* varargs */},
{"sym", (PyCFunction)dl_sym},
{"close", (PyCFunction)dl_close},
{NULL, NULL} /* Sentinel */
1994-05-23 12:37:57 +00:00
};
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
dl_getattr(xp, name)
dlobject *xp;
char *name;
{
1996-12-13 20:33:44 +00:00
return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
1994-05-23 12:37:57 +00:00
}
1996-12-13 20:33:44 +00:00
static PyTypeObject Dltype = {
PyObject_HEAD_INIT(&PyType_Type)
1994-05-23 12:37:57 +00:00
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*/
};
1996-12-13 20:33:44 +00:00
static PyObject *
1994-05-23 12:37:57 +00:00
dl_open(self, args)
1996-12-13 20:33:44 +00:00
PyObject *self;
PyObject *args;
1994-05-23 12:37:57 +00:00
{
char *name;
int mode;
1996-12-13 20:33:44 +00:00
PyUnivPtr *handle;
if (PyArg_Parse(args, "z", &name))
1994-05-23 12:37:57 +00:00
mode = RTLD_LAZY;
else {
1996-12-13 20:33:44 +00:00
PyErr_Clear();
if (!PyArg_Parse(args, "(zi)", &name, &mode))
1994-05-23 12:37:57 +00:00
return NULL;
#ifndef RTLD_NOW
if (mode != RTLD_LAZY) {
1996-12-13 20:33:44 +00:00
PyErr_SetString(PyExc_ValueError, "mode must be 1");
1994-05-23 12:37:57 +00:00
return NULL;
}
#endif
}
handle = dlopen(name, mode);
if (handle == NULL) {
1996-12-13 20:33:44 +00:00
PyErr_SetString(Dlerror, dlerror());
1994-05-23 12:37:57 +00:00
return NULL;
}
return newdlobject(handle);
}
1996-12-13 20:33:44 +00:00
static PyMethodDef dl_methods[] = {
1994-05-23 12:37:57 +00:00
{"open", dl_open},
{NULL, NULL} /* sentinel */
};
void
initdl()
{
1996-12-13 20:33:44 +00:00
PyObject *m, *d, *x;
1994-05-23 12:37:57 +00:00
if (sizeof(int) != sizeof(long) ||
sizeof(long) != sizeof(char *)) {
PyErr_SetString(PyExc_SystemError,
1994-05-23 12:37:57 +00:00
"module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
return;
}
1994-05-23 12:37:57 +00:00
/* Create the module and add the functions */
1996-12-13 20:33:44 +00:00
m = Py_InitModule("dl", dl_methods);
1994-05-23 12:37:57 +00:00
/* Add some symbolic constants to the module */
1996-12-13 20:33:44 +00:00
d = PyModule_GetDict(m);
Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
1996-12-13 20:33:44 +00:00
PyDict_SetItemString(d, "error", x);
x = PyInt_FromLong((long)RTLD_LAZY);
PyDict_SetItemString(d, "RTLD_LAZY", x);
1994-05-23 12:37:57 +00:00
#ifdef RTLD_NOW
1996-12-13 20:33:44 +00:00
x = PyInt_FromLong((long)RTLD_NOW);
PyDict_SetItemString(d, "RTLD_NOW", x);
1994-05-23 12:37:57 +00:00
#endif
}