This commit was manufactured by cvs2svn to create tag 'r212'.
[python/dscho.git] / Python / dynload_shlib.c
blob170c18b7b9293e25d2a476103060ab6ad3ad8e3b
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
29 #ifndef RTLD_LAZY
30 #define RTLD_LAZY 1
31 #endif
34 const struct filedescr _PyImport_DynLoadFiletab[] = {
35 #ifdef __CYGWIN__
36 {".dll", "rb", C_EXTENSION},
37 {"module.dll", "rb", C_EXTENSION},
38 #else
39 {".so", "rb", C_EXTENSION},
40 {"module.so", "rb", C_EXTENSION},
41 #endif
42 {0, 0}
45 static struct {
46 dev_t dev;
47 ino_t ino;
48 void *handle;
49 } handles[128];
50 static int nhandles = 0;
53 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
54 const char *pathname, FILE *fp)
56 dl_funcptr p;
57 void *handle;
58 char funcname[258];
59 char pathbuf[260];
61 if (strchr(pathname, '/') == NULL) {
62 /* Prefix bare filename with "./" */
63 sprintf(pathbuf, "./%-.255s", pathname);
64 pathname = pathbuf;
67 sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname);
69 if (fp != NULL) {
70 int i;
71 struct stat statb;
72 fstat(fileno(fp), &statb);
73 for (i = 0; i < nhandles; i++) {
74 if (statb.st_dev == handles[i].dev &&
75 statb.st_ino == handles[i].ino) {
76 p = (dl_funcptr) dlsym(handles[i].handle,
77 funcname);
78 return p;
81 if (nhandles < 128) {
82 handles[nhandles].dev = statb.st_dev;
83 handles[nhandles].ino = statb.st_ino;
87 #ifdef RTLD_NOW
88 /* RTLD_NOW: resolve externals now
89 (i.e. core dump now if some are missing) */
90 handle = dlopen(pathname, RTLD_NOW);
91 #else
92 if (Py_VerboseFlag)
93 printf("dlopen(\"%s\", %d);\n", pathname,
94 RTLD_LAZY);
95 handle = dlopen(pathname, RTLD_LAZY);
96 #endif /* RTLD_NOW */
97 if (handle == NULL) {
98 PyErr_SetString(PyExc_ImportError, dlerror());
99 return NULL;
101 if (fp != NULL && nhandles < 128)
102 handles[nhandles++].handle = handle;
103 p = (dl_funcptr) dlsym(handle, funcname);
104 return p;