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