]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Python/dynload_shlib.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Python / dynload_shlib.c
CommitLineData
4710c53d 1\r
2/* Support for dynamic loading of extension modules */\r
3\r
4#include "Python.h"\r
5#include "importdl.h"\r
6\r
7#include <sys/types.h>\r
8#include <sys/stat.h>\r
9\r
10#if defined(__NetBSD__)\r
11#include <sys/param.h>\r
12#if (NetBSD < 199712)\r
13#include <nlist.h>\r
14#include <link.h>\r
15#define dlerror() "error in dynamic linking"\r
16#endif\r
17#endif /* NetBSD */\r
18\r
19#ifdef HAVE_DLFCN_H\r
20#include <dlfcn.h>\r
21#else\r
22#if defined(PYOS_OS2) && defined(PYCC_GCC)\r
23#include "dlfcn.h"\r
24#endif\r
25#endif\r
26\r
27#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)\r
28#define LEAD_UNDERSCORE "_"\r
29#else\r
30#define LEAD_UNDERSCORE ""\r
31#endif\r
32\r
33\r
34const struct filedescr _PyImport_DynLoadFiletab[] = {\r
35#ifdef __CYGWIN__\r
36 {".dll", "rb", C_EXTENSION},\r
37 {"module.dll", "rb", C_EXTENSION},\r
38#else\r
39#if defined(PYOS_OS2) && defined(PYCC_GCC)\r
40 {".pyd", "rb", C_EXTENSION},\r
41 {".dll", "rb", C_EXTENSION},\r
42#else\r
43#ifdef __VMS\r
44 {".exe", "rb", C_EXTENSION},\r
45 {".EXE", "rb", C_EXTENSION},\r
46 {"module.exe", "rb", C_EXTENSION},\r
47 {"MODULE.EXE", "rb", C_EXTENSION},\r
48#else\r
49 {".so", "rb", C_EXTENSION},\r
50 {"module.so", "rb", C_EXTENSION},\r
51#endif\r
52#endif\r
53#endif\r
54 {0, 0}\r
55};\r
56\r
57static struct {\r
58 dev_t dev;\r
59#ifdef __VMS\r
60 ino_t ino[3];\r
61#else\r
62 ino_t ino;\r
63#endif\r
64 void *handle;\r
65} handles[128];\r
66static int nhandles = 0;\r
67\r
68\r
69dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,\r
70 const char *pathname, FILE *fp)\r
71{\r
72 dl_funcptr p;\r
73 void *handle;\r
74 char funcname[258];\r
75 char pathbuf[260];\r
76 int dlopenflags=0;\r
77\r
78 if (strchr(pathname, '/') == NULL) {\r
79 /* Prefix bare filename with "./" */\r
80 PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);\r
81 pathname = pathbuf;\r
82 }\r
83\r
84 PyOS_snprintf(funcname, sizeof(funcname),\r
85 LEAD_UNDERSCORE "init%.200s", shortname);\r
86\r
87 if (fp != NULL) {\r
88 int i;\r
89 struct stat statb;\r
90 fstat(fileno(fp), &statb);\r
91 for (i = 0; i < nhandles; i++) {\r
92 if (statb.st_dev == handles[i].dev &&\r
93 statb.st_ino == handles[i].ino) {\r
94 p = (dl_funcptr) dlsym(handles[i].handle,\r
95 funcname);\r
96 return p;\r
97 }\r
98 }\r
99 if (nhandles < 128) {\r
100 handles[nhandles].dev = statb.st_dev;\r
101#ifdef __VMS\r
102 handles[nhandles].ino[0] = statb.st_ino[0];\r
103 handles[nhandles].ino[1] = statb.st_ino[1];\r
104 handles[nhandles].ino[2] = statb.st_ino[2];\r
105#else\r
106 handles[nhandles].ino = statb.st_ino;\r
107#endif\r
108 }\r
109 }\r
110\r
111#if !(defined(PYOS_OS2) && defined(PYCC_GCC))\r
112 dlopenflags = PyThreadState_GET()->interp->dlopenflags;\r
113#endif\r
114\r
115 if (Py_VerboseFlag)\r
116 PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname,\r
117 dlopenflags);\r
118\r
119#ifdef __VMS\r
120 /* VMS currently don't allow a pathname, use a logical name instead */\r
121 /* Concatenate 'python_module_' and shortname */\r
122 /* so "import vms.bar" will use the logical python_module_bar */\r
123 /* As C module use only one name space this is probably not a */\r
124 /* important limitation */\r
125 PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",\r
126 shortname);\r
127 pathname = pathbuf;\r
128#endif\r
129\r
130 handle = dlopen(pathname, dlopenflags);\r
131\r
132 if (handle == NULL) {\r
133 const char *error = dlerror();\r
134 if (error == NULL)\r
135 error = "unknown dlopen() error";\r
136 PyErr_SetString(PyExc_ImportError, error);\r
137 return NULL;\r
138 }\r
139 if (fp != NULL && nhandles < 128)\r
140 handles[nhandles++].handle = handle;\r
141 p = (dl_funcptr) dlsym(handle, funcname);\r
142 return p;\r
143}\r