Mercurial > cpython-lru_cache-weakref
annotate Modules/dlmodule.c @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | fd25030223b0 |
| children |
| rev | line source |
|---|---|
| 1650 | 1 |
| 2 /* dl module */ | |
| 3 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
4 #include "Python.h" |
| 1650 | 5 |
| 6 #include <dlfcn.h> | |
| 7 | |
|
39043
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
8 #ifdef __VMS |
|
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
9 #include <unistd.h> |
|
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
10 #endif |
|
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
11 |
| 1650 | 12 #ifndef RTLD_LAZY |
| 13 #define RTLD_LAZY 1 | |
| 14 #endif | |
| 15 | |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13651
diff
changeset
|
16 typedef void *PyUnivPtr; |
| 1650 | 17 typedef struct { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
18 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
19 PyUnivPtr *dl_handle; |
| 1650 | 20 } dlobject; |
| 21 | |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22710
diff
changeset
|
22 static PyTypeObject Dltype; |
| 1650 | 23 |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
24 static PyObject *Dlerror; |
| 1650 | 25 |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
26 static PyObject * |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
27 newdlobject(PyUnivPtr *handle) |
| 1650 | 28 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
29 dlobject *xp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
30 xp = PyObject_New(dlobject, &Dltype); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
31 if (xp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
32 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
33 xp->dl_handle = handle; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
34 return (PyObject *)xp; |
| 1650 | 35 } |
| 36 | |
| 37 static void | |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
38 dl_dealloc(dlobject *xp) |
| 1650 | 39 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
40 if (xp->dl_handle != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
41 dlclose(xp->dl_handle); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
42 PyObject_Del(xp); |
| 1650 | 43 } |
| 44 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
45 static PyObject * |
|
22710
8a225bd8763a
Convert METH_OLDARGS -> METH_VARARGS: also PyArg_Parse -> PyArg_ParseTuple
Neal Norwitz <nnorwitz@gmail.com>
parents:
21757
diff
changeset
|
46 dl_close(dlobject *xp) |
| 1650 | 47 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
48 if (xp->dl_handle != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
49 dlclose(xp->dl_handle); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
50 xp->dl_handle = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
51 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
52 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
53 return Py_None; |
| 1650 | 54 } |
| 55 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
56 static PyObject * |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
57 dl_sym(dlobject *xp, PyObject *args) |
| 1650 | 58 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
59 char *name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
60 PyUnivPtr *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
61 if (PyString_Check(args)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
62 name = PyString_AS_STRING(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
63 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
64 PyErr_Format(PyExc_TypeError, "expected string, found %.200s", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
65 Py_TYPE(args)->tp_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
66 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
67 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
68 func = dlsym(xp->dl_handle, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
69 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
70 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
71 return Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
72 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
73 return PyInt_FromLong((long)func); |
| 1650 | 74 } |
| 75 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
76 static PyObject * |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
77 dl_call(dlobject *xp, PyObject *args) |
| 1650 | 78 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
79 PyObject *name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
80 long (*func)(long, long, long, long, long, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
81 long, long, long, long, long); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
82 long alist[10]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
83 long res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
84 Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
85 Py_ssize_t n = PyTuple_Size(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
86 if (n < 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
87 PyErr_SetString(PyExc_TypeError, "at least a name is needed"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
88 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
89 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
90 name = PyTuple_GetItem(args, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
91 if (!PyString_Check(name)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
92 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
93 "function name must be a string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
94 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
95 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
96 func = (long (*)(long, long, long, long, long, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
97 long, long, long, long, long)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
98 dlsym(xp->dl_handle, PyString_AsString(name)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
99 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
100 PyErr_SetString(PyExc_ValueError, dlerror()); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
101 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
102 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
103 if (n-1 > 10) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
104 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
105 "too many arguments (max 10)"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
106 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
107 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
108 for (i = 1; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
109 PyObject *v = PyTuple_GetItem(args, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
110 if (PyInt_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
111 alist[i-1] = PyInt_AsLong(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
112 else if (PyString_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
113 alist[i-1] = (long)PyString_AsString(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
114 else if (v == Py_None) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
115 alist[i-1] = (long) ((char *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
116 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
117 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
118 "arguments must be int, string or None"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
119 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
120 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
121 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
122 for (; i <= 10; i++) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
123 alist[i-1] = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
124 res = (*func)(alist[0], alist[1], alist[2], alist[3], alist[4], |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
125 alist[5], alist[6], alist[7], alist[8], alist[9]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
126 return PyInt_FromLong(res); |
| 1650 | 127 } |
| 128 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
129 static PyMethodDef dlobject_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
130 {"call", (PyCFunction)dl_call, METH_VARARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
131 {"sym", (PyCFunction)dl_sym, METH_O}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
132 {"close", (PyCFunction)dl_close, METH_NOARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
133 {NULL, NULL} /* Sentinel */ |
| 1650 | 134 }; |
| 135 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
136 static PyObject * |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
137 dl_getattr(dlobject *xp, char *name) |
| 1650 | 138 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
139 return Py_FindMethod(dlobject_methods, (PyObject *)xp, name); |
| 1650 | 140 } |
| 141 | |
| 142 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
143 static PyTypeObject Dltype = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
144 PyVarObject_HEAD_INIT(NULL, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
145 "dl.dl", /*tp_name*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
146 sizeof(dlobject), /*tp_basicsize*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
147 0, /*tp_itemsize*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
148 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
149 (destructor)dl_dealloc, /*tp_dealloc*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
150 0, /*tp_print*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
151 (getattrfunc)dl_getattr,/*tp_getattr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
152 0, /*tp_setattr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
153 0, /*tp_compare*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
154 0, /*tp_repr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
155 0, /*tp_as_number*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
156 0, /*tp_as_sequence*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
157 0, /*tp_as_mapping*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
158 0, /*tp_hash*/ |
| 1650 | 159 }; |
| 160 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
161 static PyObject * |
|
13499
046799a08057
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
162 dl_open(PyObject *self, PyObject *args) |
| 1650 | 163 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
164 char *name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
165 int mode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
166 PyUnivPtr *handle; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
167 if (sizeof(int) != sizeof(long) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
168 sizeof(long) != sizeof(char *)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
169 PyErr_SetString(PyExc_SystemError, |
|
21610
a9e06ffaf7fb
Patch #497126: Always compile dl.
Martin v. Löwis <martin@v.loewis.de>
parents:
21313
diff
changeset
|
170 "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)"); |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
171 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
172 } |
|
21610
a9e06ffaf7fb
Patch #497126: Always compile dl.
Martin v. Löwis <martin@v.loewis.de>
parents:
21313
diff
changeset
|
173 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
174 if (PyArg_ParseTuple(args, "z:open", &name)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
175 mode = RTLD_LAZY; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
176 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
177 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
178 if (!PyArg_ParseTuple(args, "zi:open", &name, &mode)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
179 return NULL; |
| 1650 | 180 #ifndef RTLD_NOW |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
181 if (mode != RTLD_LAZY) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
182 PyErr_SetString(PyExc_ValueError, "mode must be 1"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
183 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
184 } |
| 1650 | 185 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
186 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
187 handle = dlopen(name, mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
188 if (handle == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
189 char *errmsg = dlerror(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
190 if (!errmsg) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
191 errmsg = "dlopen() error"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
192 PyErr_SetString(Dlerror, errmsg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
193 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
194 } |
|
39043
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
195 #ifdef __VMS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
196 /* Under OpenVMS dlopen doesn't do any check, just save the name |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
197 * for later use, so we have to check if the file is readable, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
198 * the name can be a logical or a file from SYS$SHARE. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
199 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
200 if (access(name, R_OK)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
201 char fname[strlen(name) + 20]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
202 strcpy(fname, "SYS$SHARE:"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
203 strcat(fname, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
204 strcat(fname, ".EXE"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
205 if (access(fname, R_OK)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
206 dlclose(handle); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
207 PyErr_SetString(Dlerror, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
208 "File not found or protection violation"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
209 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
210 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
211 } |
|
39043
c67b23397f30
Patch #1516912: improve Modules support for OpenVMS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38760
diff
changeset
|
212 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
213 return newdlobject(handle); |
| 1650 | 214 } |
| 215 | |
|
4725
8669edb1d9c5
Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4503
diff
changeset
|
216 static PyMethodDef dl_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
217 {"open", dl_open, METH_VARARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
218 {NULL, NULL} /* sentinel */ |
| 1650 | 219 }; |
| 220 | |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
221 /* From socketmodule.c |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
222 * Convenience routine to export an integer value. |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
223 * |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
224 * Errors are silently ignored, for better or for worse... |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
225 */ |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
226 static void |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
227 insint(PyObject *d, char *name, int value) |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
228 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
229 PyObject *v = PyInt_FromLong((long) value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
230 if (!v || PyDict_SetItemString(d, name, v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
231 PyErr_Clear(); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
232 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
233 Py_XDECREF(v); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
234 } |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
235 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
24280
diff
changeset
|
236 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13499
diff
changeset
|
237 initdl(void) |
| 1650 | 238 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
239 PyObject *m, *d, *x; |
| 1650 | 240 |
|
46840
b6b2cea5d306
Deprecate the dl module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
44350
diff
changeset
|
241 if (PyErr_WarnPy3k("the dl module has been removed in " |
|
46855
892f9b2959c9
Flesh out the 3.0 deprecation to suggest using the ctypes module.
Brett Cannon <bcannon@gmail.com>
parents:
46840
diff
changeset
|
242 "Python 3.0; use the ctypes module instead", 2) < 0) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
243 return; |
|
46840
b6b2cea5d306
Deprecate the dl module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
44350
diff
changeset
|
244 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
245 /* Initialize object type */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
246 Py_TYPE(&Dltype) = &PyType_Type; |
|
16804
8156879fe60d
Patch #404680: disables the nis module and enables the dl module when
Andrew M. Kuchling <amk@amk.ca>
parents:
16735
diff
changeset
|
247 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
248 /* Create the module and add the functions */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
249 m = Py_InitModule("dl", dl_methods); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
250 if (m == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
251 return; |
| 1650 | 252 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
253 /* Add some symbolic constants to the module */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
254 d = PyModule_GetDict(m); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
255 Dlerror = x = PyErr_NewException("dl.error", NULL, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
256 PyDict_SetItemString(d, "error", x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
257 x = PyInt_FromLong((long)RTLD_LAZY); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
258 PyDict_SetItemString(d, "RTLD_LAZY", x); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
259 #define INSINT(X) insint(d,#X,X) |
| 1650 | 260 #ifdef RTLD_NOW |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
261 INSINT(RTLD_NOW); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
262 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
263 #ifdef RTLD_NOLOAD |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
264 INSINT(RTLD_NOLOAD); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
265 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
266 #ifdef RTLD_GLOBAL |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
267 INSINT(RTLD_GLOBAL); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
268 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
269 #ifdef RTLD_LOCAL |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
270 INSINT(RTLD_LOCAL); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
271 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
272 #ifdef RTLD_PARENT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
273 INSINT(RTLD_PARENT); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
274 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
275 #ifdef RTLD_GROUP |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
276 INSINT(RTLD_GROUP); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
277 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
278 #ifdef RTLD_WORLD |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
279 INSINT(RTLD_WORLD); |
|
14542
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
280 #endif |
|
a45ca1f7f241
Add several dl.RTLD_ constants. Closes bug 110842.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
281 #ifdef RTLD_NODELETE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
47967
diff
changeset
|
282 INSINT(RTLD_NODELETE); |
| 1650 | 283 #endif |
| 284 } |
