This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Python / dynload_shlib.c
blobe8a04ce500a00771416db7a7d4518f793af2ae58
2 /* Support for dynamic loading of extension modules */
4 #include "Python.h"
5 #include "importdl.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
10 #if defined(__NetBSD__)
11 #include <sys/param.h>
12 #if (NetBSD < 199712)
13 #include <nlist.h>
14 #include <link.h>
15 #define dlerror() "error in dynamic linking"
16 #endif
17 #endif /* NetBSD */
19 #ifdef HAVE_DLFCN_H
20 #include <dlfcn.h>
21 #endif
23 #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
24 #define LEAD_UNDERSCORE "_"
25 #else
26 #define LEAD_UNDERSCORE ""
27 #endif
30 const struct filedescr _PyImport_DynLoadFiletab[] = {
31 #ifdef __CYGWIN__
32 {".dll", "rb", C_EXTENSION},
33 {"module.dll", "rb", C_EXTENSION},
34 #else
35 {".so", "rb", C_EXTENSION},
36 {"module.so", "rb", C_EXTENSION},
37 #endif
38 {0, 0}
41 static struct {
42 dev_t dev;
43 ino_t ino;
44 void *handle;
45 } handles[128];
46 static int nhandles = 0;
49 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
50 const char *pathname, FILE *fp)
52 dl_funcptr p;
53 void *handle;
54 char funcname[258];
55 char pathbuf[260];
56 int dlopenflags=0;
58 if (strchr(pathname, '/') == NULL) {
59 /* Prefix bare filename with "./" */
60 PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
61 pathname = pathbuf;
64 PyOS_snprintf(funcname, sizeof(funcname),
65 LEAD_UNDERSCORE "init%.200s", shortname);
67 if (fp != NULL) {
68 int i;
69 struct stat statb;
70 fstat(fileno(fp), &statb);
71 for (i = 0; i < nhandles; i++) {
72 if (statb.st_dev == handles[i].dev &&
73 statb.st_ino == handles[i].ino) {
74 p = (dl_funcptr) dlsym(handles[i].handle,
75 funcname);
76 return p;
79 if (nhandles < 128) {
80 handles[nhandles].dev = statb.st_dev;
81 handles[nhandles].ino = statb.st_ino;
85 dlopenflags = PyThreadState_Get()->interp->dlopenflags;
87 if (Py_VerboseFlag)
88 printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags);
90 handle = dlopen(pathname, dlopenflags);
92 if (handle == NULL) {
93 PyErr_SetString(PyExc_ImportError, dlerror());
94 return NULL;
96 if (fp != NULL && nhandles < 128)
97 handles[nhandles++].handle = handle;
98 p = (dl_funcptr) dlsym(handle, funcname);
99 return p;