Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Support for dynamic loading of extension modules */ |
| 33 | /* If no dynamic linking is supported, this file still generates some code! */ |
| 34 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 35 | #include "Python.h" |
Guido van Rossum | cecadc4 | 1998-04-10 23:45:14 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef HAVE_SYS_PARAM_H |
| 38 | /* osdefs.h will define MAXPATHLEN if it's not already defined. */ |
| 39 | #include <sys/param.h> |
| 40 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 41 | #include "osdefs.h" |
| 42 | #include "importdl.h" |
| 43 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | /* Explanation of some of the the various #defines used by dynamic linking... |
| 45 | |
| 46 | symbol -- defined for: |
| 47 | |
| 48 | DYNAMIC_LINK -- any kind of dynamic linking |
Guido van Rossum | 75f288d | 1995-06-14 22:07:26 +0000 | [diff] [blame] | 49 | USE_RLD -- NeXT dynamic linking |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 50 | USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl |
| 51 | USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries |
| 52 | _AIX -- AIX style dynamic linking |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 53 | MS_WIN32 -- Windows NT style dynamic linking (using DLLs) |
| 54 | MS_WIN16 -- Windows 16-bit dynamic linking (using DLLs) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 55 | PYOS_OS2 -- IBM OS/2 dynamic linking (using DLLs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 56 | _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 57 | USE_MAC_DYNAMIC_LOADING -- Mac CFM shared libraries |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 58 | SHORT_EXT -- short extension for dynamic module, e.g. ".so" |
| 59 | LONG_EXT -- long extension, e.g. "module.so" |
| 60 | hpux -- HP-UX Dynamic Linking - defined by the compiler |
Guido van Rossum | 15af20a | 1998-01-19 22:03:52 +0000 | [diff] [blame] | 61 | __NetBSD__ -- NetBSD shared libraries |
| 62 | (assuming dlerror() was introduced between 1.2 and 1.3) |
Guido van Rossum | 25e8529 | 1996-02-25 05:02:29 +0000 | [diff] [blame] | 63 | __FreeBSD__ -- FreeBSD shared libraries |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 64 | |
| 65 | (The other WITH_* symbols are used only once, to set the |
| 66 | appropriate symbols.) |
| 67 | */ |
| 68 | |
| 69 | /* Configure dynamic linking */ |
| 70 | |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 71 | #ifdef __hpux |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 72 | #ifndef hpux |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 73 | #define hpux |
| 74 | #endif |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 75 | #endif |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | #ifdef hpux |
| 78 | #define DYNAMIC_LINK |
| 79 | #include <errno.h> |
| 80 | typedef void (*dl_funcptr)(); |
| 81 | #define _DL_FUNCPTR_DEFINED 1 |
| 82 | #define SHORT_EXT ".sl" |
| 83 | #define LONG_EXT "module.sl" |
| 84 | #endif |
| 85 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 86 | #if defined(PYOS_OS2) |
| 87 | #define DYNAMIC_LINK |
| 88 | #define INCL_DOSERRORS |
| 89 | #define INCL_DOSMODULEMGR |
| 90 | #include <os2.h> |
| 91 | typedef int (* APIENTRY dl_funcptr)(); |
| 92 | #define _DL_FUNCPTR_DEFINED 1 |
| 93 | #define SHORT_EXT ".pyd" |
| 94 | #define LONG_EXT ".dll" |
| 95 | #endif |
| 96 | |
Guido van Rossum | 15af20a | 1998-01-19 22:03:52 +0000 | [diff] [blame] | 97 | #if defined(__NetBSD__) && (NetBSD < 199712) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 98 | #define DYNAMIC_LINK |
| 99 | #define USE_SHLIB |
| 100 | |
| 101 | #define dlerror() "error in dynamic linking" |
| 102 | #endif |
| 103 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 104 | #ifdef MS_WINDOWS /* i.e. MS_WIN32 or MS_WIN16 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 105 | #define DYNAMIC_LINK |
| 106 | #include <windows.h> |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 107 | #include <direct.h> |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 108 | typedef FARPROC dl_funcptr; |
| 109 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | 859b16c | 1998-05-15 20:22:08 +0000 | [diff] [blame] | 110 | #ifdef _DEBUG |
| 111 | #define SHORT_EXT "_d.pyd" |
| 112 | #define LONG_EXT "_d.dll" |
| 113 | #else |
Guido van Rossum | 5fb1da7 | 1995-01-07 12:36:02 +0000 | [diff] [blame] | 114 | #define SHORT_EXT ".pyd" |
Guido van Rossum | e71a947 | 1996-04-09 02:39:15 +0000 | [diff] [blame] | 115 | #define LONG_EXT ".dll" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 116 | #endif |
Guido van Rossum | 859b16c | 1998-05-15 20:22:08 +0000 | [diff] [blame] | 117 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 75f288d | 1995-06-14 22:07:26 +0000 | [diff] [blame] | 119 | #ifdef NeXT |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 120 | #define DYNAMIC_LINK |
| 121 | #define USE_RLD |
Guido van Rossum | 54dec59 | 1997-08-16 14:38:09 +0000 | [diff] [blame] | 122 | #define SHORT_EXT ".so" |
| 123 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
| 126 | #ifdef WITH_SGI_DL |
| 127 | #define DYNAMIC_LINK |
| 128 | #define USE_DL |
| 129 | #endif |
| 130 | |
| 131 | #ifdef WITH_DL_DLD |
| 132 | #define DYNAMIC_LINK |
| 133 | #define USE_DL |
| 134 | #endif |
| 135 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 136 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 137 | #define DYNAMIC_LINK |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 138 | #define SHORT_EXT ".slb" |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 139 | #ifdef __CFM68K__ |
| 140 | #define LONG_EXT ".CFM68K.slb" |
| 141 | #else |
| 142 | #define LONG_EXT ".ppc.slb" |
| 143 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 144 | #ifndef _DL_FUNCPTR_DEFINED |
| 145 | typedef void (*dl_funcptr)(); |
| 146 | #endif |
| 147 | #endif |
| 148 | |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 149 | #if !defined(DYNAMIC_LINK) && (defined(HAVE_DLOPEN) || defined(M_UNIX)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 150 | #define DYNAMIC_LINK |
| 151 | #define USE_SHLIB |
| 152 | #endif |
| 153 | |
| 154 | #ifdef _AIX |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 155 | #undef USE_SHLIB /* AIX 4.2 and higher have dlfcn.h but we don't want it */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 156 | #define DYNAMIC_LINK |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 157 | #define SHORT_EXT ".so" |
| 158 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 159 | #include <sys/ldr.h> |
| 160 | typedef void (*dl_funcptr)(); |
| 161 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 162 | static int aix_getoldmodules(void **); |
| 163 | static int aix_bindnewmodule(void *, void *); |
| 164 | static void aix_loaderror(char *); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
| 167 | #ifdef DYNAMIC_LINK |
| 168 | |
| 169 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 170 | #include <sys/types.h> |
| 171 | #include <sys/stat.h> |
Guido van Rossum | 15af20a | 1998-01-19 22:03:52 +0000 | [diff] [blame] | 172 | #if defined(__NetBSD__) && (NetBSD < 199712) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 173 | #include <nlist.h> |
| 174 | #include <link.h> |
| 175 | #else |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 176 | #ifdef HAVE_DLFCN_H |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 177 | #include <dlfcn.h> |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 178 | #endif |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 179 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 180 | #ifndef _DL_FUNCPTR_DEFINED |
| 181 | typedef void (*dl_funcptr)(); |
| 182 | #endif |
| 183 | #ifndef RTLD_LAZY |
| 184 | #define RTLD_LAZY 1 |
| 185 | #endif |
| 186 | #define SHORT_EXT ".so" |
| 187 | #define LONG_EXT "module.so" |
| 188 | #endif /* USE_SHLIB */ |
| 189 | |
| 190 | #if defined(USE_DL) || defined(hpux) |
| 191 | #include "dl.h" |
| 192 | #endif |
| 193 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 194 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 0a72e8d | 1995-10-23 13:54:01 +0000 | [diff] [blame] | 195 | #include <Aliases.h> |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 196 | #include <CodeFragments.h> |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 197 | #ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */ |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 198 | #define CFragConnectionID ConnectionID |
| 199 | #define kLoadCFrag 0x01 |
| 200 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 201 | #include <Files.h> |
| 202 | #include "macdefs.h" |
| 203 | #include "macglue.h" |
| 204 | #endif |
| 205 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 206 | #ifdef USE_RLD |
| 207 | #include <mach-o/rld.h> |
| 208 | #define FUNCNAME_PATTERN "_init%.200s" |
| 209 | #ifndef _DL_FUNCPTR_DEFINED |
| 210 | typedef void (*dl_funcptr)(); |
| 211 | #endif |
| 212 | #endif /* USE_RLD */ |
| 213 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 214 | extern char *Py_GetProgramName(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 215 | |
| 216 | #ifndef FUNCNAME_PATTERN |
Guido van Rossum | 1f74016 | 1998-04-13 15:27:21 +0000 | [diff] [blame] | 217 | #if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__BORLANDC__) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 218 | #define FUNCNAME_PATTERN "_init%.200s" |
| 219 | #else |
| 220 | #define FUNCNAME_PATTERN "init%.200s" |
| 221 | #endif |
| 222 | #endif |
| 223 | |
| 224 | #if !defined(SHORT_EXT) && !defined(LONG_EXT) |
| 225 | #define SHORT_EXT ".o" |
| 226 | #define LONG_EXT "module.o" |
| 227 | #endif /* !SHORT_EXT && !LONG_EXT */ |
| 228 | |
| 229 | #endif /* DYNAMIC_LINK */ |
| 230 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 231 | struct filedescr _PyImport_Filetab[] = { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 232 | #ifdef SHORT_EXT |
| 233 | {SHORT_EXT, "rb", C_EXTENSION}, |
| 234 | #endif /* !SHORT_EXT */ |
| 235 | #ifdef LONG_EXT |
| 236 | {LONG_EXT, "rb", C_EXTENSION}, |
| 237 | #endif /* !LONG_EXT */ |
| 238 | {".py", "r", PY_SOURCE}, |
| 239 | {".pyc", "rb", PY_COMPILED}, |
| 240 | {0, 0} |
| 241 | }; |
| 242 | |
Guido van Rossum | 3823420 | 1996-07-31 17:55:19 +0000 | [diff] [blame] | 243 | #ifdef NO_DYNAMIC_LINK |
| 244 | #undef DYNAMIC_LINK |
| 245 | #endif |
| 246 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | PyObject * |
| 248 | _PyImport_LoadDynamicModule(name, pathname, fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 249 | char *name; |
| 250 | char *pathname; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 251 | FILE *fp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 252 | { |
| 253 | #ifndef DYNAMIC_LINK |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 254 | PyErr_SetString(PyExc_ImportError, |
| 255 | "dynamically linked modules not supported"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 256 | return NULL; |
| 257 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 258 | PyObject *m, *d, *s; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 259 | char funcname[258]; |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 260 | char *lastdot, *shortname, *packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 261 | dl_funcptr p = NULL; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 262 | #ifdef USE_SHLIB |
| 263 | static struct { |
| 264 | dev_t dev; |
| 265 | ino_t ino; |
| 266 | void *handle; |
| 267 | } handles[128]; |
| 268 | static int nhandles = 0; |
Guido van Rossum | 0bbf253 | 1996-08-09 20:55:05 +0000 | [diff] [blame] | 269 | char pathbuf[260]; |
| 270 | if (strchr(pathname, '/') == NULL) { |
| 271 | /* Prefix bare filename with "./" */ |
| 272 | sprintf(pathbuf, "./%-.255s", pathname); |
| 273 | pathname = pathbuf; |
| 274 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 275 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 276 | if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { |
| 277 | Py_INCREF(m); |
| 278 | return m; |
| 279 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 280 | lastdot = strrchr(name, '.'); |
| 281 | if (lastdot == NULL) { |
| 282 | packagecontext = NULL; |
| 283 | shortname = name; |
| 284 | } |
| 285 | else { |
| 286 | packagecontext = name; |
| 287 | shortname = lastdot+1; |
| 288 | } |
| 289 | sprintf(funcname, FUNCNAME_PATTERN, shortname); |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 290 | #ifdef USE_SHLIB |
| 291 | if (fp != NULL) { |
| 292 | int i; |
| 293 | struct stat statb; |
| 294 | fstat(fileno(fp), &statb); |
| 295 | for (i = 0; i < nhandles; i++) { |
| 296 | if (statb.st_dev == handles[i].dev && |
| 297 | statb.st_ino == handles[i].ino) { |
| 298 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 299 | funcname); |
| 300 | goto got_it; |
| 301 | } |
| 302 | } |
| 303 | if (nhandles < 128) { |
| 304 | handles[nhandles].dev = statb.st_dev; |
| 305 | handles[nhandles].ino = statb.st_ino; |
| 306 | } |
| 307 | } |
| 308 | #endif /* USE_SHLIB */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 309 | #ifdef USE_MAC_DYNAMIC_LOADING |
| 310 | /* |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 311 | ** Dynamic loading of CFM shared libraries on the Mac. The |
| 312 | ** code has become more convoluted than it was, because we |
| 313 | ** want to be able to put multiple modules in a single |
| 314 | ** file. For this reason, we have to determine the fragment |
| 315 | ** name, and we cannot use the library entry point but we have |
| 316 | ** to locate the correct init routine "by hand". |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 317 | */ |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 318 | { |
| 319 | FSSpec libspec; |
| 320 | CFragConnectionID connID; |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 321 | Ptr mainAddr; |
| 322 | Str255 errMessage; |
| 323 | OSErr err; |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 324 | Boolean isfolder, didsomething; |
| 325 | char buf[512]; |
| 326 | Str63 fragname; |
| 327 | Ptr symAddr; |
| 328 | CFragSymbolClass class; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 329 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 330 | /* First resolve any aliases to find the real file */ |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 331 | (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 332 | err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething); |
| 333 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 334 | sprintf(buf, "%.255s: %.200s", |
| 335 | pathname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 336 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 337 | return NULL; |
| 338 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 339 | /* Next, determine the fragment name, |
| 340 | by stripping '.slb' and 'module' */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 341 | memcpy(fragname+1, libspec.name+1, libspec.name[0]); |
| 342 | fragname[0] = libspec.name[0]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 343 | if( strncmp((char *)(fragname+1+fragname[0]-4), |
| 344 | ".slb", 4) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 345 | fragname[0] -= 4; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 346 | if ( strncmp((char *)(fragname+1+fragname[0]-6), |
| 347 | "module", 6) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 348 | fragname[0] -= 6; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 349 | /* Load the fragment |
| 350 | (or return the connID if it is already loaded */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 351 | err = GetDiskFragment(&libspec, 0, 0, fragname, |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 352 | kLoadCFrag, &connID, &mainAddr, |
| 353 | errMessage); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 354 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 355 | sprintf(buf, "%.*s: %.200s", |
| 356 | errMessage[0], errMessage+1, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 357 | PyMac_StrError(err)); |
| 358 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 361 | /* Locate the address of the correct init function */ |
| 362 | err = FindSymbol(connID, Pstring(funcname), &symAddr, &class); |
| 363 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 364 | sprintf(buf, "%s: %.200s", |
| 365 | funcname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 366 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | p = (dl_funcptr)symAddr; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 370 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 371 | #endif /* USE_MAC_DYNAMIC_LOADING */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 372 | #ifdef USE_SHLIB |
| 373 | { |
| 374 | #ifdef RTLD_NOW |
| 375 | /* RTLD_NOW: resolve externals now |
| 376 | (i.e. core dump now if some are missing) */ |
Guido van Rossum | 6b07787 | 1998-05-18 13:42:45 +0000 | [diff] [blame] | 377 | void *handle = dlopen(pathname, RTLD_NOW); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 378 | #else |
| 379 | void *handle; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 380 | if (Py_VerboseFlag) |
Guido van Rossum | 08052c7 | 1997-12-02 20:43:18 +0000 | [diff] [blame] | 381 | printf("dlopen(\"%s\", %d);\n", pathname, |
Guido van Rossum | 6b07787 | 1998-05-18 13:42:45 +0000 | [diff] [blame] | 382 | RTLD_LAZY); |
| 383 | handle = dlopen(pathname, RTLD_LAZY); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 384 | #endif /* RTLD_NOW */ |
| 385 | if (handle == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 386 | PyErr_SetString(PyExc_ImportError, dlerror()); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 389 | if (fp != NULL && nhandles < 128) |
| 390 | handles[nhandles++].handle = handle; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 391 | p = (dl_funcptr) dlsym(handle, funcname); |
| 392 | } |
| 393 | #endif /* USE_SHLIB */ |
| 394 | #ifdef _AIX |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 395 | /* |
| 396 | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
| 397 | -- of the shared module unresolved. Thus we have to resolve them |
| 398 | -- explicitely with loadbind. The new module is loaded, then we |
| 399 | -- resolve its symbols using the list of already loaded modules |
| 400 | -- (only those that belong to the python executable). Get these |
| 401 | -- with loadquery(L_GETINFO). |
| 402 | */ |
| 403 | { |
| 404 | static void *staticmodlistptr = NULL; |
| 405 | |
| 406 | if (!staticmodlistptr) |
| 407 | if (aix_getoldmodules(&staticmodlistptr) == -1) |
| 408 | return NULL; |
| 409 | p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0); |
| 410 | if (p == NULL) { |
| 411 | aix_loaderror(pathname); |
| 412 | return NULL; |
| 413 | } |
| 414 | if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) { |
| 415 | aix_loaderror(pathname); |
| 416 | return NULL; |
| 417 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 418 | } |
| 419 | #endif /* _AIX */ |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 420 | #ifdef MS_WIN32 |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 421 | { |
| 422 | HINSTANCE hDLL; |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 423 | char pathbuf[260]; |
| 424 | if (strchr(pathname, SEP) == NULL && |
| 425 | strchr(pathname, ALTSEP) == NULL) |
| 426 | { |
| 427 | /* Prefix bare filename with ".\" */ |
| 428 | char *p = pathbuf; |
| 429 | *p = '\0'; |
| 430 | _getcwd(pathbuf, sizeof pathbuf); |
| 431 | if (*p != '\0' && p[1] == ':') |
| 432 | p += 2; |
| 433 | sprintf(p, ".\\%-.255s", pathname); |
| 434 | pathname = pathbuf; |
| 435 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 436 | hDLL = LoadLibrary(pathname); |
| 437 | if (hDLL==NULL){ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 438 | char errBuf[256]; |
| 439 | unsigned int errorCode; |
| 440 | |
| 441 | /* Get an error string from Win32 error code */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 442 | char theInfo[256]; /* Pointer to error text |
| 443 | from system */ |
| 444 | int theLength; /* Length of error text */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 445 | |
| 446 | errorCode = GetLastError(); |
| 447 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 448 | theLength = FormatMessage( |
| 449 | FORMAT_MESSAGE_FROM_SYSTEM, /* flags */ |
| 450 | NULL, /* message source */ |
| 451 | errorCode, /* the message (error) ID */ |
| 452 | 0, /* default language environment */ |
| 453 | (LPTSTR) theInfo, /* the buffer */ |
| 454 | sizeof(theInfo), /* the buffer size */ |
| 455 | NULL); /* no additional format args. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 456 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 457 | /* Problem: could not get the error message. |
| 458 | This should not happen if called correctly. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 459 | if (theLength == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 460 | sprintf(errBuf, |
| 461 | "DLL load failed with error code %d", |
| 462 | errorCode); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 463 | } else { |
| 464 | int len; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 465 | /* For some reason a \r\n |
| 466 | is appended to the text */ |
| 467 | if (theLength >= 2 && |
| 468 | theInfo[theLength-2] == '\r' && |
| 469 | theInfo[theLength-1] == '\n') { |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 470 | theLength -= 2; |
| 471 | theInfo[theLength] = '\0'; |
| 472 | } |
| 473 | strcpy(errBuf, "DLL load failed: "); |
| 474 | len = strlen(errBuf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 475 | strncpy(errBuf+len, theInfo, |
| 476 | sizeof(errBuf)-len); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 477 | errBuf[sizeof(errBuf)-1] = '\0'; |
| 478 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 479 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 480 | return NULL; |
| 481 | } |
| 482 | p = GetProcAddress(hDLL, funcname); |
| 483 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 484 | #endif /* MS_WIN32 */ |
| 485 | #ifdef MS_WIN16 |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 486 | { |
| 487 | HINSTANCE hDLL; |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 488 | char pathbuf[16]; |
| 489 | if (strchr(pathname, SEP) == NULL && |
| 490 | strchr(pathname, ALTSEP) == NULL) |
| 491 | { |
| 492 | /* Prefix bare filename with ".\" */ |
| 493 | sprintf(pathbuf, ".\\%-.13s", pathname); |
| 494 | pathname = pathbuf; |
| 495 | } |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 496 | hDLL = LoadLibrary(pathname); |
| 497 | if (hDLL < HINSTANCE_ERROR){ |
| 498 | char errBuf[256]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 499 | sprintf(errBuf, |
| 500 | "DLL load failed with error code %d", hDLL); |
| 501 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
| 504 | p = GetProcAddress(hDLL, funcname); |
| 505 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 506 | #endif /* MS_WIN16 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 507 | |
| 508 | #if defined(PYOS_OS2) |
| 509 | { |
| 510 | APIRET rc; |
| 511 | HMODULE hDLL; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 512 | char failreason[256]; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 513 | |
| 514 | rc = DosLoadModule(failreason, |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 515 | sizeof(failreason), |
| 516 | pathname, |
| 517 | &hDLL); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 518 | |
| 519 | if (rc != NO_ERROR) { |
| 520 | char errBuf[256]; |
| 521 | sprintf(errBuf, |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 522 | "DLL load failed, rc = %d, problem '%s': %s", |
| 523 | rc, failreason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 524 | PyErr_SetString(PyExc_ImportError, errBuf); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 528 | rc = DosQueryProcAddr(hDLL, 0L, funcname, &p); |
| 529 | if (rc != NO_ERROR) |
| 530 | p = NULL; /* Signify Failure to Acquire Entrypoint */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 531 | } |
| 532 | #endif /* PYOS_OS2 */ |
| 533 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 534 | #ifdef USE_DL |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 535 | p = dl_loadmod(Py_GetProgramName(), pathname, funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 536 | #endif /* USE_DL */ |
| 537 | #ifdef USE_RLD |
| 538 | { |
| 539 | NXStream *errorStream; |
| 540 | struct mach_header *new_header; |
| 541 | const char *filenames[2]; |
| 542 | long ret; |
| 543 | unsigned long ptr; |
| 544 | |
| 545 | errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY); |
| 546 | filenames[0] = pathname; |
| 547 | filenames[1] = NULL; |
| 548 | ret = rld_load(errorStream, &new_header, |
| 549 | filenames, NULL); |
| 550 | |
| 551 | /* extract the error messages for the exception */ |
| 552 | if(!ret) { |
| 553 | char *streamBuf; |
| 554 | int len, maxLen; |
| 555 | |
| 556 | NXPutc(errorStream, (char)0); |
| 557 | |
| 558 | NXGetMemoryBuffer(errorStream, |
| 559 | &streamBuf, &len, &maxLen); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 560 | PyErr_SetString(PyExc_ImportError, streamBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if(ret && rld_lookup(errorStream, funcname, &ptr)) |
| 564 | p = (dl_funcptr) ptr; |
| 565 | |
| 566 | NXCloseMemory(errorStream, NX_FREEBUFFER); |
| 567 | |
| 568 | if(!ret) |
| 569 | return NULL; |
| 570 | } |
| 571 | #endif /* USE_RLD */ |
| 572 | #ifdef hpux |
| 573 | { |
| 574 | shl_t lib; |
| 575 | int flags; |
| 576 | |
Guido van Rossum | 3afb595 | 1996-12-05 23:15:35 +0000 | [diff] [blame] | 577 | flags = BIND_FIRST | BIND_DEFERRED; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 578 | if (Py_VerboseFlag) { |
| 579 | flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 580 | BIND_NONFATAL | BIND_VERBOSE; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame^] | 581 | printf("shl_load %s\n",pathname); |
| 582 | } |
| 583 | lib = shl_load(pathname, flags, 0); |
| 584 | /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */ |
| 585 | if (lib == NULL) { |
| 586 | char buf[256]; |
| 587 | if (Py_VerboseFlag) |
| 588 | perror(pathname); |
| 589 | sprintf(buf, "Failed to load %.200s", pathname); |
| 590 | PyErr_SetString(PyExc_ImportError, buf); |
| 591 | return NULL; |
| 592 | } |
| 593 | if (Py_VerboseFlag) |
| 594 | printf("shl_findsym %s\n", funcname); |
| 595 | shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p); |
| 596 | if (p == NULL && Py_VerboseFlag) |
| 597 | perror(funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 598 | } |
| 599 | #endif /* hpux */ |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 600 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 601 | got_it: |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 602 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 603 | if (p == NULL) { |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 604 | PyErr_Format(PyExc_ImportError, |
Guido van Rossum | 42e8e5d | 1998-01-19 22:23:08 +0000 | [diff] [blame] | 605 | "dynamic module does not define init function (%.200s)", |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 606 | funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 607 | return NULL; |
| 608 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 609 | _Py_PackageContext = packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 610 | (*p)(); |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 611 | _Py_PackageContext = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 612 | if (PyErr_Occurred()) |
| 613 | return NULL; |
| 614 | if (_PyImport_FixupExtension(name, pathname) == NULL) |
| 615 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 617 | m = PyDict_GetItemString(PyImport_GetModuleDict(), name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 618 | if (m == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 619 | PyErr_SetString(PyExc_SystemError, |
| 620 | "dynamic module not initialized properly"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 623 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 624 | d = PyModule_GetDict(m); |
| 625 | s = PyString_FromString(pathname); |
| 626 | if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) |
| 627 | PyErr_Clear(); /* Not important enough to report */ |
| 628 | Py_XDECREF(s); |
| 629 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 630 | fprintf(stderr, |
| 631 | "import %s # dynamically loaded from %s\n", |
| 632 | name, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 634 | return m; |
| 635 | #endif /* DYNAMIC_LINK */ |
| 636 | } |
| 637 | |
| 638 | |
| 639 | #ifdef _AIX |
| 640 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 641 | #include <ctype.h> /* for isdigit() */ |
| 642 | #include <errno.h> /* for global errno */ |
| 643 | #include <string.h> /* for strerror() */ |
| 644 | #include <stdlib.h> /* for malloc(), free() */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 646 | typedef struct Module { |
| 647 | struct Module *next; |
| 648 | void *entry; |
| 649 | } Module, *ModulePtr; |
| 650 | |
| 651 | static int |
| 652 | aix_getoldmodules(modlistptr) |
| 653 | void **modlistptr; |
| 654 | { |
| 655 | register ModulePtr modptr, prevmodptr; |
| 656 | register struct ld_info *ldiptr; |
| 657 | register char *ldibuf; |
| 658 | register int errflag, bufsize = 1024; |
| 659 | register unsigned int offset; |
| 660 | |
| 661 | /* |
| 662 | -- Get the list of loaded modules into ld_info structures. |
| 663 | */ |
| 664 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 666 | return -1; |
| 667 | } |
| 668 | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
| 669 | && errno == ENOMEM) { |
| 670 | free(ldibuf); |
| 671 | bufsize += 1024; |
| 672 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 673 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 674 | return -1; |
| 675 | } |
| 676 | } |
| 677 | if (errflag == -1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 678 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 679 | return -1; |
| 680 | } |
| 681 | /* |
| 682 | -- Make the modules list from the ld_info structures. |
| 683 | */ |
| 684 | ldiptr = (struct ld_info *)ldibuf; |
| 685 | prevmodptr = NULL; |
| 686 | do { |
Guido van Rossum | 3b31cd2 | 1997-10-10 17:40:00 +0000 | [diff] [blame] | 687 | if (strstr(ldiptr->ldinfo_filename, "python") == NULL) { |
| 688 | /* |
| 689 | -- Extract only the modules containing "python" as a |
| 690 | -- substring, like the "python[version]" executable or |
| 691 | -- "libpython[version].a" in case python is embedded. |
| 692 | */ |
| 693 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 694 | ldiptr = (struct ld_info *)((unsigned int) |
| 695 | ldiptr + offset); |
| 696 | continue; |
| 697 | } |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 698 | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 700 | while (*modlistptr) { |
| 701 | modptr = (ModulePtr)*modlistptr; |
| 702 | *modlistptr = (void *)modptr->next; |
| 703 | free(modptr); |
| 704 | } |
| 705 | return -1; |
| 706 | } |
| 707 | modptr->entry = ldiptr->ldinfo_dataorg; |
| 708 | modptr->next = NULL; |
| 709 | if (prevmodptr == NULL) |
| 710 | *modlistptr = (void *)modptr; |
| 711 | else |
| 712 | prevmodptr->next = modptr; |
| 713 | prevmodptr = modptr; |
| 714 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 715 | ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset); |
| 716 | } while (offset); |
| 717 | free(ldibuf); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int |
| 722 | aix_bindnewmodule(newmoduleptr, modlistptr) |
| 723 | void *newmoduleptr; |
| 724 | void *modlistptr; |
| 725 | { |
| 726 | register ModulePtr modptr; |
| 727 | |
| 728 | /* |
| 729 | -- Bind the new module with the list of loaded modules. |
| 730 | */ |
| 731 | for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next) |
| 732 | if (loadbind(0, modptr->entry, newmoduleptr) != 0) |
| 733 | return -1; |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static void |
| 738 | aix_loaderror(pathname) |
| 739 | char *pathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 740 | { |
| 741 | |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 742 | char *message[1024], errbuf[1024]; |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 743 | register int i,j; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 744 | |
| 745 | struct errtab { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 746 | int errNo; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 747 | char *errstr; |
| 748 | } load_errtab[] = { |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 749 | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 750 | {L_ERROR_NOLIB, "can't load library:"}, |
| 751 | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
| 752 | {L_ERROR_RLDBAD, |
| 753 | "RLD index out of range or bad relocation type:"}, |
| 754 | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
| 755 | {L_ERROR_MEMBER, |
| 756 | "file not an archive or does not contain requested member:"}, |
| 757 | {L_ERROR_TYPE, "symbol table mismatch:"}, |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 758 | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 759 | {L_ERROR_SYSTEM, "System error:"}, |
| 760 | {L_ERROR_ERRNO, NULL} |
| 761 | }; |
| 762 | |
| 763 | #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) |
| 764 | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
| 765 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 766 | sprintf(errbuf, "from module %.200s ", pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 767 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 768 | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 769 | ERRBUF_APPEND(strerror(errno)); |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 770 | ERRBUF_APPEND("\n"); |
| 771 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 772 | for(i = 0; message[i] && *message[i]; i++) { |
| 773 | int nerr = atoi(message[i]); |
| 774 | for (j=0; j<LOAD_ERRTAB_LEN ; j++) { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 775 | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 776 | ERRBUF_APPEND(load_errtab[j].errstr); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 777 | } |
| 778 | while (isdigit(*message[i])) message[i]++ ; |
| 779 | ERRBUF_APPEND(message[i]); |
| 780 | ERRBUF_APPEND("\n"); |
| 781 | } |
| 782 | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 783 | PyErr_SetString(PyExc_ImportError, errbuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | |
| 787 | #endif /* _AIX */ |