1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
42 typedef ANY
*PyUnivPtr
;
48 staticforward PyTypeObject Dltype
;
50 static PyObject
*Dlerror
;
57 xp
= PyObject_NEW(dlobject
, &Dltype
);
60 xp
->dl_handle
= handle
;
61 return (PyObject
*)xp
;
68 if (xp
->dl_handle
!= NULL
)
69 dlclose(xp
->dl_handle
);
78 if (!PyArg_Parse(args
, ""))
80 if (xp
->dl_handle
!= NULL
) {
81 dlclose(xp
->dl_handle
);
95 if (!PyArg_Parse(args
, "s", &name
))
97 func
= dlsym(xp
->dl_handle
, name
);
102 return PyInt_FromLong((long)func
);
108 PyObject
*args
; /* (varargs) */
115 int n
= PyTuple_Size(args
);
117 PyErr_SetString(PyExc_TypeError
, "at least a name is needed");
120 name
= PyTuple_GetItem(args
, 0);
121 if (!PyString_Check(name
)) {
122 PyErr_SetString(PyExc_TypeError
,
123 "function name must be a string");
126 func
= dlsym(xp
->dl_handle
, PyString_AsString(name
));
128 PyErr_SetString(PyExc_ValueError
, dlerror());
132 PyErr_SetString(PyExc_TypeError
,
133 "too many arguments (max 10)");
136 for (i
= 1; i
< n
; i
++) {
137 PyObject
*v
= PyTuple_GetItem(args
, i
);
139 alist
[i
-1] = PyInt_AsLong(v
);
140 else if (PyString_Check(v
))
141 alist
[i
-1] = (long)PyString_AsString(v
);
142 else if (v
== Py_None
)
143 alist
[i
-1] = (long) ((char *)NULL
);
145 PyErr_SetString(PyExc_TypeError
,
146 "arguments must be int, string or None");
152 res
= (*func
)(alist
[0], alist
[1], alist
[2], alist
[3], alist
[4],
153 alist
[5], alist
[6], alist
[7], alist
[8], alist
[9]);
154 return PyInt_FromLong(res
);
157 static PyMethodDef dlobject_methods
[] = {
158 {"call", (PyCFunction
)dl_call
, 1 /* varargs */},
159 {"sym", (PyCFunction
)dl_sym
},
160 {"close", (PyCFunction
)dl_close
},
161 {NULL
, NULL
} /* Sentinel */
169 return Py_FindMethod(dlobject_methods
, (PyObject
*)xp
, name
);
173 static PyTypeObject Dltype
= {
174 PyObject_HEAD_INIT(&PyType_Type
)
177 sizeof(dlobject
), /*tp_basicsize*/
180 (destructor
)dl_dealloc
, /*tp_dealloc*/
182 (getattrfunc
)dl_getattr
,/*tp_getattr*/
187 0, /*tp_as_sequence*/
200 if (PyArg_Parse(args
, "z", &name
))
204 if (!PyArg_Parse(args
, "(zi)", &name
, &mode
))
207 if (mode
!= RTLD_LAZY
) {
208 PyErr_SetString(PyExc_ValueError
, "mode must be 1");
213 handle
= dlopen(name
, mode
);
214 if (handle
== NULL
) {
215 PyErr_SetString(Dlerror
, dlerror());
218 return newdlobject(handle
);
221 static PyMethodDef dl_methods
[] = {
223 {NULL
, NULL
} /* sentinel */
231 if (sizeof(int) != sizeof(long) ||
232 sizeof(long) != sizeof(char *)) {
233 PyErr_SetString(PyExc_SystemError
,
234 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
238 /* Create the module and add the functions */
239 m
= Py_InitModule("dl", dl_methods
);
241 /* Add some symbolic constants to the module */
242 d
= PyModule_GetDict(m
);
243 Dlerror
= x
= PyErr_NewException("dl.error", NULL
, NULL
);
244 PyDict_SetItemString(d
, "error", x
);
245 x
= PyInt_FromLong((long)RTLD_LAZY
);
246 PyDict_SetItemString(d
, "RTLD_LAZY", x
);
248 x
= PyInt_FromLong((long)RTLD_NOW
);
249 PyDict_SetItemString(d
, "RTLD_NOW", x
);