1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
21 typedef void *PyUnivPtr
;
27 staticforward PyTypeObject Dltype
;
29 static PyObject
*Dlerror
;
32 newdlobject(PyUnivPtr
*handle
)
35 xp
= PyObject_New(dlobject
, &Dltype
);
38 xp
->dl_handle
= handle
;
39 return (PyObject
*)xp
;
43 dl_dealloc(dlobject
*xp
)
45 if (xp
->dl_handle
!= NULL
)
46 dlclose(xp
->dl_handle
);
51 dl_close(dlobject
*xp
, PyObject
*args
)
53 if (!PyArg_Parse(args
, ""))
55 if (xp
->dl_handle
!= NULL
) {
56 dlclose(xp
->dl_handle
);
64 dl_sym(dlobject
*xp
, PyObject
*args
)
68 if (!PyArg_Parse(args
, "s", &name
))
70 func
= dlsym(xp
->dl_handle
, name
);
75 return PyInt_FromLong((long)func
);
79 dl_call(dlobject
*xp
, PyObject
*args
)
86 int n
= PyTuple_Size(args
);
88 PyErr_SetString(PyExc_TypeError
, "at least a name is needed");
91 name
= PyTuple_GetItem(args
, 0);
92 if (!PyString_Check(name
)) {
93 PyErr_SetString(PyExc_TypeError
,
94 "function name must be a string");
97 func
= dlsym(xp
->dl_handle
, PyString_AsString(name
));
99 PyErr_SetString(PyExc_ValueError
, dlerror());
103 PyErr_SetString(PyExc_TypeError
,
104 "too many arguments (max 10)");
107 for (i
= 1; i
< n
; i
++) {
108 PyObject
*v
= PyTuple_GetItem(args
, i
);
110 alist
[i
-1] = PyInt_AsLong(v
);
111 else if (PyString_Check(v
))
112 alist
[i
-1] = (long)PyString_AsString(v
);
113 else if (v
== Py_None
)
114 alist
[i
-1] = (long) ((char *)NULL
);
116 PyErr_SetString(PyExc_TypeError
,
117 "arguments must be int, string or None");
123 res
= (*func
)(alist
[0], alist
[1], alist
[2], alist
[3], alist
[4],
124 alist
[5], alist
[6], alist
[7], alist
[8], alist
[9]);
125 return PyInt_FromLong(res
);
128 static PyMethodDef dlobject_methods
[] = {
129 {"call", (PyCFunction
)dl_call
, 1 /* varargs */},
130 {"sym", (PyCFunction
)dl_sym
},
131 {"close", (PyCFunction
)dl_close
},
132 {NULL
, NULL
} /* Sentinel */
136 dl_getattr(dlobject
*xp
, char *name
)
138 return Py_FindMethod(dlobject_methods
, (PyObject
*)xp
, name
);
142 static PyTypeObject Dltype
= {
143 PyObject_HEAD_INIT(&PyType_Type
)
146 sizeof(dlobject
), /*tp_basicsize*/
149 (destructor
)dl_dealloc
, /*tp_dealloc*/
151 (getattrfunc
)dl_getattr
,/*tp_getattr*/
156 0, /*tp_as_sequence*/
162 dl_open(PyObject
*self
, PyObject
*args
)
167 if (PyArg_Parse(args
, "z", &name
))
171 if (!PyArg_Parse(args
, "(zi)", &name
, &mode
))
174 if (mode
!= RTLD_LAZY
) {
175 PyErr_SetString(PyExc_ValueError
, "mode must be 1");
180 handle
= dlopen(name
, mode
);
181 if (handle
== NULL
) {
182 PyErr_SetString(Dlerror
, dlerror());
185 return newdlobject(handle
);
188 static PyMethodDef dl_methods
[] = {
190 {NULL
, NULL
} /* sentinel */
198 if (sizeof(int) != sizeof(long) ||
199 sizeof(long) != sizeof(char *)) {
200 PyErr_SetString(PyExc_SystemError
,
201 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
205 /* Create the module and add the functions */
206 m
= Py_InitModule("dl", dl_methods
);
208 /* Add some symbolic constants to the module */
209 d
= PyModule_GetDict(m
);
210 Dlerror
= x
= PyErr_NewException("dl.error", NULL
, NULL
);
211 PyDict_SetItemString(d
, "error", x
);
212 x
= PyInt_FromLong((long)RTLD_LAZY
);
213 PyDict_SetItemString(d
, "RTLD_LAZY", x
);
215 x
= PyInt_FromLong((long)RTLD_NOW
);
216 PyDict_SetItemString(d
, "RTLD_NOW", x
);