openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Python / dynload_shlib.c
blob61674ba555a2575cb7d8bacdfefd54f605bb041f
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 #else
22 #if defined(PYOS_OS2) && defined(PYCC_GCC)
23 #include "dlfcn.h"
24 #endif
25 #endif
27 #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
28 #define LEAD_UNDERSCORE "_"
29 #else
30 #define LEAD_UNDERSCORE ""
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 #if defined(PYOS_OS2) && defined(PYCC_GCC)
40 {".pyd", "rb", C_EXTENSION},
41 {".dll", "rb", C_EXTENSION},
42 #else
43 {".so", "rb", C_EXTENSION},
44 {"module.so", "rb", C_EXTENSION},
45 #endif
46 #endif
47 {0, 0}
50 static struct {
51 dev_t dev;
52 ino_t ino;
53 void *handle;
54 } handles[128];
55 static int nhandles = 0;
58 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
59 const char *pathname, FILE *fp)
61 dl_funcptr p;
62 void *handle;
63 char funcname[258];
64 char pathbuf[260];
65 int dlopenflags=0;
67 if (strchr(pathname, '/') == NULL) {
68 /* Prefix bare filename with "./" */
69 PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
70 pathname = pathbuf;
73 PyOS_snprintf(funcname, sizeof(funcname),
74 LEAD_UNDERSCORE "init%.200s", shortname);
76 if (fp != NULL) {
77 int i;
78 struct stat statb;
79 fstat(fileno(fp), &statb);
80 for (i = 0; i < nhandles; i++) {
81 if (statb.st_dev == handles[i].dev &&
82 statb.st_ino == handles[i].ino) {
83 p = (dl_funcptr) dlsym(handles[i].handle,
84 funcname);
85 return p;
88 if (nhandles < 128) {
89 handles[nhandles].dev = statb.st_dev;
90 handles[nhandles].ino = statb.st_ino;
94 #if !(defined(PYOS_OS2) && defined(PYCC_GCC))
95 dlopenflags = PyThreadState_Get()->interp->dlopenflags;
96 #endif
98 if (Py_VerboseFlag)
99 printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags);
101 handle = dlopen(pathname, dlopenflags);
103 if (handle == NULL) {
104 PyErr_SetString(PyExc_ImportError, dlerror());
105 return NULL;
107 if (fp != NULL && nhandles < 128)
108 handles[nhandles++].handle = handle;
109 p = (dl_funcptr) dlsym(handle, funcname);
110 return p;