Commit 0e120327 authored by Roger E. Masse's avatar Roger E. Masse

Renamed.

parent 7eee08d0
...@@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* dl module */ /* dl module */
#include "allobjects.h" #include "Python.h"
#include "modsupport.h"
#include <dlfcn.h> #include <dlfcn.h>
...@@ -40,25 +39,26 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -40,25 +39,26 @@ PERFORMANCE OF THIS SOFTWARE.
#define RTLD_LAZY 1 #define RTLD_LAZY 1
#endif #endif
typedef ANY *PyUnivPtr;
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
ANY *dl_handle; PyUnivPtr *dl_handle;
} dlobject; } dlobject;
staticforward typeobject Dltype; staticforward PyTypeObject Dltype;
static object *Dlerror; static PyObject *Dlerror;
static object * static PyObject *
newdlobject(handle) newdlobject(handle)
ANY *handle; PyUnivPtr *handle;
{ {
dlobject *xp; dlobject *xp;
xp = NEWOBJ(dlobject, &Dltype); xp = PyObject_NEW(dlobject, &Dltype);
if (xp == NULL) if (xp == NULL)
return NULL; return NULL;
xp->dl_handle = handle; xp->dl_handle = handle;
return (object *)xp; return (PyObject *)xp;
} }
static void static void
...@@ -67,80 +67,82 @@ dl_dealloc(xp) ...@@ -67,80 +67,82 @@ dl_dealloc(xp)
{ {
if (xp->dl_handle != NULL) if (xp->dl_handle != NULL)
dlclose(xp->dl_handle); dlclose(xp->dl_handle);
DEL(xp); PyMem_DEL(xp);
} }
static object * static PyObject *
dl_close(xp, args) dl_close(xp, args)
dlobject *xp; dlobject *xp;
object *args; PyObject *args;
{ {
if (!getargs(args, "")) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (xp->dl_handle != NULL) { if (xp->dl_handle != NULL) {
dlclose(xp->dl_handle); dlclose(xp->dl_handle);
xp->dl_handle = NULL; xp->dl_handle = NULL;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
dl_sym(xp, args) dl_sym(xp, args)
dlobject *xp; dlobject *xp;
object *args; PyObject *args;
{ {
char *name; char *name;
ANY *func; PyUnivPtr *func;
if (!getargs(args, "s", &name)) if (!PyArg_Parse(args, "s", &name))
return NULL; return NULL;
func = dlsym(xp->dl_handle, name); func = dlsym(xp->dl_handle, name);
if (func == NULL) { if (func == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
return newintobject((long)func); return PyInt_FromLong((long)func);
} }
static object * static PyObject *
dl_call(xp, args) dl_call(xp, args)
dlobject *xp; dlobject *xp;
object *args; /* (varargs) */ PyObject *args; /* (varargs) */
{ {
object *name; PyObject *name;
long (*func)(); long (*func)();
long alist[10]; long alist[10];
long res; long res;
int i; int i;
int n = gettuplesize(args); int n = PyTuple_Size(args);
if (n < 1) { if (n < 1) {
err_setstr(TypeError, "at least a name is needed"); PyErr_SetString(PyExc_TypeError, "at least a name is needed");
return NULL; return NULL;
} }
name = gettupleitem(args, 0); name = PyTuple_GetItem(args, 0);
if (!is_stringobject(name)) { if (!PyString_Check(name)) {
err_setstr(TypeError, "function name must be a string"); PyErr_SetString(PyExc_TypeError,
"function name must be a string");
return NULL; return NULL;
} }
func = dlsym(xp->dl_handle, getstringvalue(name)); func = dlsym(xp->dl_handle, PyString_AsString(name));
if (func == NULL) { if (func == NULL) {
err_setstr(ValueError, dlerror()); PyErr_SetString(PyExc_ValueError, dlerror());
return NULL; return NULL;
} }
if (n-1 > 10) { if (n-1 > 10) {
err_setstr(TypeError, "too many arguments (max 10)"); PyErr_SetString(PyExc_TypeError,
"too many arguments (max 10)");
return NULL; return NULL;
} }
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
object *v = gettupleitem(args, i); PyObject *v = PyTuple_GetItem(args, i);
if (is_intobject(v)) if (PyInt_Check(v))
alist[i-1] = getintvalue(v); alist[i-1] = PyInt_AsLong(v);
else if (is_stringobject(v)) else if (PyString_Check(v))
alist[i-1] = (long)getstringvalue(v); alist[i-1] = (long)PyString_AsString(v);
else if (v == None) else if (v == Py_None)
alist[i-1] = (long) ((char *)NULL); alist[i-1] = (long) ((char *)NULL);
else { else {
err_setstr(TypeError, PyErr_SetString(PyExc_TypeError,
"arguments must be int, string or None"); "arguments must be int, string or None");
return NULL; return NULL;
} }
...@@ -149,27 +151,27 @@ dl_call(xp, args) ...@@ -149,27 +151,27 @@ dl_call(xp, args)
alist[i-1] = 0; alist[i-1] = 0;
res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4], res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4],
alist[5], alist[6], alist[7], alist[8], alist[9]); alist[5], alist[6], alist[7], alist[8], alist[9]);
return newintobject(res); return PyInt_FromLong(res);
} }
static struct methodlist dlobject_methods[] = { static PyMethodDef dlobject_methods[] = {
{"call", (method)dl_call, 1 /* varargs */}, {"call", (PyCFunction)dl_call, 1 /* varargs */},
{"sym", (method)dl_sym}, {"sym", (PyCFunction)dl_sym},
{"close", (method)dl_close}, {"close", (PyCFunction)dl_close},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
static object * static PyObject *
dl_getattr(xp, name) dl_getattr(xp, name)
dlobject *xp; dlobject *xp;
char *name; char *name;
{ {
return findmethod(dlobject_methods, (object *)xp, name); return Py_FindMethod(dlobject_methods, (PyObject *)xp, name);
} }
static typeobject Dltype = { static PyTypeObject Dltype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"dl", /*tp_name*/ "dl", /*tp_name*/
sizeof(dlobject), /*tp_basicsize*/ sizeof(dlobject), /*tp_basicsize*/
...@@ -187,36 +189,36 @@ static typeobject Dltype = { ...@@ -187,36 +189,36 @@ static typeobject Dltype = {
0, /*tp_hash*/ 0, /*tp_hash*/
}; };
static object * static PyObject *
dl_open(self, args) dl_open(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
char *name; char *name;
int mode; int mode;
ANY *handle; PyUnivPtr *handle;
if (getargs(args, "z", &name)) if (PyArg_Parse(args, "z", &name))
mode = RTLD_LAZY; mode = RTLD_LAZY;
else { else {
err_clear(); PyErr_Clear();
if (!getargs(args, "(zi)", &name, &mode)) if (!PyArg_Parse(args, "(zi)", &name, &mode))
return NULL; return NULL;
#ifndef RTLD_NOW #ifndef RTLD_NOW
if (mode != RTLD_LAZY) { if (mode != RTLD_LAZY) {
err_setstr(ValueError, "mode must be 1"); PyErr_SetString(PyExc_ValueError, "mode must be 1");
return NULL; return NULL;
} }
#endif #endif
} }
handle = dlopen(name, mode); handle = dlopen(name, mode);
if (handle == NULL) { if (handle == NULL) {
err_setstr(Dlerror, dlerror()); PyErr_SetString(Dlerror, dlerror());
return NULL; return NULL;
} }
return newdlobject(handle); return newdlobject(handle);
} }
static struct methodlist dl_methods[] = { static PyMethodDef dl_methods[] = {
{"open", dl_open}, {"open", dl_open},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -224,28 +226,28 @@ static struct methodlist dl_methods[] = { ...@@ -224,28 +226,28 @@ static struct methodlist dl_methods[] = {
void void
initdl() initdl()
{ {
object *m, *d, *x; PyObject *m, *d, *x;
if (sizeof(int) != sizeof(long) || if (sizeof(int) != sizeof(long) ||
sizeof(long) != sizeof(char *)) sizeof(long) != sizeof(char *))
fatal( Py_FatalError(
"module dl requires sizeof(int) == sizeof(long) == sizeof(char*)"); "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
/* Create the module and add the functions */ /* Create the module and add the functions */
m = initmodule("dl", dl_methods); m = Py_InitModule("dl", dl_methods);
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = getmoduledict(m); d = PyModule_GetDict(m);
Dlerror = x = newstringobject("dl.error"); Dlerror = x = PyString_FromString("dl.error");
dictinsert(d, "error", x); PyDict_SetItemString(d, "error", x);
x = newintobject((long)RTLD_LAZY); x = PyInt_FromLong((long)RTLD_LAZY);
dictinsert(d, "RTLD_LAZY", x); PyDict_SetItemString(d, "RTLD_LAZY", x);
#ifdef RTLD_NOW #ifdef RTLD_NOW
x = newintobject((long)RTLD_NOW); x = PyInt_FromLong((long)RTLD_NOW);
dictinsert(d, "RTLD_NOW", x); PyDict_SetItemString(d, "RTLD_NOW", x);
#endif #endif
/* Check for errors */ /* Check for errors */
if (err_occurred()) if (PyErr_Occurred())
fatal("can't initialize module dl"); Py_FatalError("can't initialize module dl");
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment