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 | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 64 | __BEOS__ -- BeOS shared libraries - defined by the compiler |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 65 | |
| 66 | (The other WITH_* symbols are used only once, to set the |
| 67 | appropriate symbols.) |
| 68 | */ |
| 69 | |
| 70 | /* Configure dynamic linking */ |
| 71 | |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 72 | #ifdef __hpux |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 73 | #ifndef hpux |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 74 | #define hpux |
| 75 | #endif |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 78 | #ifdef hpux |
| 79 | #define DYNAMIC_LINK |
| 80 | #include <errno.h> |
| 81 | typedef void (*dl_funcptr)(); |
| 82 | #define _DL_FUNCPTR_DEFINED 1 |
| 83 | #define SHORT_EXT ".sl" |
| 84 | #define LONG_EXT "module.sl" |
| 85 | #endif |
| 86 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 87 | #if defined(PYOS_OS2) |
| 88 | #define DYNAMIC_LINK |
| 89 | #define INCL_DOSERRORS |
| 90 | #define INCL_DOSMODULEMGR |
| 91 | #include <os2.h> |
| 92 | typedef int (* APIENTRY dl_funcptr)(); |
| 93 | #define _DL_FUNCPTR_DEFINED 1 |
| 94 | #define SHORT_EXT ".pyd" |
| 95 | #define LONG_EXT ".dll" |
| 96 | #endif |
| 97 | |
Guido van Rossum | 15af20a | 1998-01-19 22:03:52 +0000 | [diff] [blame] | 98 | #if defined(__NetBSD__) && (NetBSD < 199712) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 99 | #define DYNAMIC_LINK |
| 100 | #define USE_SHLIB |
| 101 | |
| 102 | #define dlerror() "error in dynamic linking" |
| 103 | #endif |
| 104 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 105 | #ifdef MS_WINDOWS /* i.e. MS_WIN32 or MS_WIN16 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 106 | #define DYNAMIC_LINK |
| 107 | #include <windows.h> |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 108 | #include <direct.h> |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 109 | typedef FARPROC dl_funcptr; |
| 110 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | 859b16c | 1998-05-15 20:22:08 +0000 | [diff] [blame] | 111 | #ifdef _DEBUG |
| 112 | #define SHORT_EXT "_d.pyd" |
| 113 | #define LONG_EXT "_d.dll" |
| 114 | #else |
Guido van Rossum | 5fb1da7 | 1995-01-07 12:36:02 +0000 | [diff] [blame] | 115 | #define SHORT_EXT ".pyd" |
Guido van Rossum | e71a947 | 1996-04-09 02:39:15 +0000 | [diff] [blame] | 116 | #define LONG_EXT ".dll" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 117 | #endif |
Guido van Rossum | 859b16c | 1998-05-15 20:22:08 +0000 | [diff] [blame] | 118 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 75f288d | 1995-06-14 22:07:26 +0000 | [diff] [blame] | 120 | #ifdef NeXT |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 121 | #define DYNAMIC_LINK |
| 122 | #define USE_RLD |
Guido van Rossum | ab076fd | 1998-08-24 14:15:44 +0000 | [diff] [blame] | 123 | /* Define this to 1 if you want be able to load ObjC modules as well: |
| 124 | it switches between two different way of loading modules on the NeXT, |
| 125 | one that directly interfaces with the dynamic loader (rld_load(), which |
| 126 | does not correctly load ObjC object files), and another that uses the |
| 127 | ObjC runtime (objc_loadModules()) to do the job. |
| 128 | You'll have to add ``-ObjC'' to the compiler flags if you set this to 1. |
| 129 | */ |
| 130 | #define HANDLE_OBJC_MODULES 1 |
| 131 | #if HANDLE_OBJC_MODULES |
| 132 | #include <objc/Object.h> |
| 133 | #include <objc/objc-load.h> |
| 134 | #endif |
Guido van Rossum | 54dec59 | 1997-08-16 14:38:09 +0000 | [diff] [blame] | 135 | #define SHORT_EXT ".so" |
| 136 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
| 139 | #ifdef WITH_SGI_DL |
| 140 | #define DYNAMIC_LINK |
| 141 | #define USE_DL |
| 142 | #endif |
| 143 | |
| 144 | #ifdef WITH_DL_DLD |
| 145 | #define DYNAMIC_LINK |
| 146 | #define USE_DL |
| 147 | #endif |
| 148 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 149 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 150 | #define DYNAMIC_LINK |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 151 | #define SHORT_EXT ".slb" |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 152 | #ifdef __CFM68K__ |
| 153 | #define LONG_EXT ".CFM68K.slb" |
| 154 | #else |
| 155 | #define LONG_EXT ".ppc.slb" |
| 156 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 157 | #ifndef _DL_FUNCPTR_DEFINED |
| 158 | typedef void (*dl_funcptr)(); |
| 159 | #endif |
| 160 | #endif |
| 161 | |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 162 | #if !defined(DYNAMIC_LINK) && (defined(HAVE_DLOPEN) || defined(M_UNIX)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 163 | #define DYNAMIC_LINK |
| 164 | #define USE_SHLIB |
| 165 | #endif |
| 166 | |
| 167 | #ifdef _AIX |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 168 | #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] | 169 | #define DYNAMIC_LINK |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 170 | #define SHORT_EXT ".so" |
| 171 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 172 | #include <sys/ldr.h> |
| 173 | typedef void (*dl_funcptr)(); |
| 174 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 175 | static int aix_getoldmodules(void **); |
| 176 | static int aix_bindnewmodule(void *, void *); |
| 177 | static void aix_loaderror(char *); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 178 | #endif |
| 179 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 180 | #ifdef __BEOS__ |
| 181 | # undef USE_SHLIB /* probably not defined anyway */ |
| 182 | # define DYNAMIC_LINK |
| 183 | # define SHORT_EXT ".so" |
| 184 | # define LONG_EXT "module.so" |
| 185 | typedef void (*dl_funcptr)(void); |
| 186 | # define _DL_FUNCPTR_DEFINED |
| 187 | |
| 188 | # if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H) |
| 189 | # undef MAXPATHLEN |
| 190 | # endif |
| 191 | |
| 192 | # include <kernel/image.h> |
| 193 | # include <kernel/OS.h> |
| 194 | # include <stdlib.h> |
| 195 | # include <unistd.h> |
| 196 | |
| 197 | # ifdef WITH_THREAD |
| 198 | # include "thread.h" |
| 199 | static type_lock beos_dyn_lock; |
| 200 | # endif |
| 201 | |
| 202 | static PyObject *beos_dyn_images = NULL; |
| 203 | |
| 204 | static void beos_init_dyn( void ); |
| 205 | static void beos_cleanup_dyn( void ); |
| 206 | static void beos_nuke_dyn( PyObject *item ); |
| 207 | static void beos_add_dyn( char *pathname, image_id id ); |
| 208 | |
| 209 | /* External interface for finding image IDs; useful if you need to |
| 210 | * do your own symbol lookup in dynamically loaded modules. [Donn Cave] |
| 211 | * |
| 212 | * Hmm, could we hack up the Sun dlmodule instead for this sort of thing? |
| 213 | * That might be more generally useful. [chrish] |
| 214 | */ |
| 215 | image_id PyImport_BeImageID( char *name ); |
| 216 | #endif |
| 217 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 218 | #ifdef DYNAMIC_LINK |
| 219 | |
| 220 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 221 | #include <sys/types.h> |
| 222 | #include <sys/stat.h> |
Guido van Rossum | 15af20a | 1998-01-19 22:03:52 +0000 | [diff] [blame] | 223 | #if defined(__NetBSD__) && (NetBSD < 199712) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 224 | #include <nlist.h> |
| 225 | #include <link.h> |
| 226 | #else |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 227 | #ifdef HAVE_DLFCN_H |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 228 | #include <dlfcn.h> |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 229 | #endif |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 230 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 231 | #ifndef _DL_FUNCPTR_DEFINED |
| 232 | typedef void (*dl_funcptr)(); |
| 233 | #endif |
| 234 | #ifndef RTLD_LAZY |
| 235 | #define RTLD_LAZY 1 |
| 236 | #endif |
| 237 | #define SHORT_EXT ".so" |
| 238 | #define LONG_EXT "module.so" |
| 239 | #endif /* USE_SHLIB */ |
| 240 | |
| 241 | #if defined(USE_DL) || defined(hpux) |
| 242 | #include "dl.h" |
| 243 | #endif |
| 244 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 245 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 0a72e8d | 1995-10-23 13:54:01 +0000 | [diff] [blame] | 246 | #include <Aliases.h> |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 247 | #include <CodeFragments.h> |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | #ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */ |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 249 | #define CFragConnectionID ConnectionID |
| 250 | #define kLoadCFrag 0x01 |
| 251 | #endif |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 252 | #ifdef USE_GUSI |
| 253 | #include "TFileSpec.h" /* for Path2FSSpec() */ |
| 254 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 255 | #include <Files.h> |
| 256 | #include "macdefs.h" |
| 257 | #include "macglue.h" |
| 258 | #endif |
| 259 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 260 | #ifdef USE_RLD |
| 261 | #include <mach-o/rld.h> |
| 262 | #define FUNCNAME_PATTERN "_init%.200s" |
| 263 | #ifndef _DL_FUNCPTR_DEFINED |
| 264 | typedef void (*dl_funcptr)(); |
| 265 | #endif |
| 266 | #endif /* USE_RLD */ |
| 267 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 268 | extern char *Py_GetProgramName(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 269 | |
| 270 | #ifndef FUNCNAME_PATTERN |
Guido van Rossum | 1f74016 | 1998-04-13 15:27:21 +0000 | [diff] [blame] | 271 | #if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__BORLANDC__) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 272 | #define FUNCNAME_PATTERN "_init%.200s" |
| 273 | #else |
| 274 | #define FUNCNAME_PATTERN "init%.200s" |
| 275 | #endif |
| 276 | #endif |
| 277 | |
| 278 | #if !defined(SHORT_EXT) && !defined(LONG_EXT) |
| 279 | #define SHORT_EXT ".o" |
| 280 | #define LONG_EXT "module.o" |
| 281 | #endif /* !SHORT_EXT && !LONG_EXT */ |
| 282 | |
| 283 | #endif /* DYNAMIC_LINK */ |
| 284 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 285 | struct filedescr _PyImport_Filetab[] = { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 286 | #ifdef SHORT_EXT |
| 287 | {SHORT_EXT, "rb", C_EXTENSION}, |
| 288 | #endif /* !SHORT_EXT */ |
| 289 | #ifdef LONG_EXT |
| 290 | {LONG_EXT, "rb", C_EXTENSION}, |
| 291 | #endif /* !LONG_EXT */ |
| 292 | {".py", "r", PY_SOURCE}, |
| 293 | {".pyc", "rb", PY_COMPILED}, |
| 294 | {0, 0} |
| 295 | }; |
| 296 | |
Guido van Rossum | 3823420 | 1996-07-31 17:55:19 +0000 | [diff] [blame] | 297 | #ifdef NO_DYNAMIC_LINK |
| 298 | #undef DYNAMIC_LINK |
| 299 | #endif |
| 300 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 301 | PyObject * |
| 302 | _PyImport_LoadDynamicModule(name, pathname, fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 303 | char *name; |
| 304 | char *pathname; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 305 | FILE *fp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 306 | { |
| 307 | #ifndef DYNAMIC_LINK |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 308 | PyErr_SetString(PyExc_ImportError, |
| 309 | "dynamically linked modules not supported"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 310 | return NULL; |
| 311 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | PyObject *m, *d, *s; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 313 | char funcname[258]; |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 314 | char *lastdot, *shortname, *packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 315 | dl_funcptr p = NULL; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 316 | #ifdef USE_SHLIB |
| 317 | static struct { |
| 318 | dev_t dev; |
| 319 | ino_t ino; |
| 320 | void *handle; |
| 321 | } handles[128]; |
| 322 | static int nhandles = 0; |
Guido van Rossum | 0bbf253 | 1996-08-09 20:55:05 +0000 | [diff] [blame] | 323 | char pathbuf[260]; |
| 324 | if (strchr(pathname, '/') == NULL) { |
| 325 | /* Prefix bare filename with "./" */ |
| 326 | sprintf(pathbuf, "./%-.255s", pathname); |
| 327 | pathname = pathbuf; |
| 328 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 329 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 330 | if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { |
| 331 | Py_INCREF(m); |
| 332 | return m; |
| 333 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 334 | lastdot = strrchr(name, '.'); |
| 335 | if (lastdot == NULL) { |
| 336 | packagecontext = NULL; |
| 337 | shortname = name; |
| 338 | } |
| 339 | else { |
| 340 | packagecontext = name; |
| 341 | shortname = lastdot+1; |
| 342 | } |
| 343 | sprintf(funcname, FUNCNAME_PATTERN, shortname); |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 344 | #ifdef USE_SHLIB |
| 345 | if (fp != NULL) { |
| 346 | int i; |
| 347 | struct stat statb; |
| 348 | fstat(fileno(fp), &statb); |
| 349 | for (i = 0; i < nhandles; i++) { |
| 350 | if (statb.st_dev == handles[i].dev && |
| 351 | statb.st_ino == handles[i].ino) { |
| 352 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 353 | funcname); |
| 354 | goto got_it; |
| 355 | } |
| 356 | } |
| 357 | if (nhandles < 128) { |
| 358 | handles[nhandles].dev = statb.st_dev; |
| 359 | handles[nhandles].ino = statb.st_ino; |
| 360 | } |
| 361 | } |
| 362 | #endif /* USE_SHLIB */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 363 | #ifdef USE_MAC_DYNAMIC_LOADING |
| 364 | /* |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 365 | ** Dynamic loading of CFM shared libraries on the Mac. The |
| 366 | ** code has become more convoluted than it was, because we |
| 367 | ** want to be able to put multiple modules in a single |
| 368 | ** file. For this reason, we have to determine the fragment |
| 369 | ** name, and we cannot use the library entry point but we have |
| 370 | ** to locate the correct init routine "by hand". |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 371 | */ |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 372 | { |
| 373 | FSSpec libspec; |
| 374 | CFragConnectionID connID; |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 375 | Ptr mainAddr; |
| 376 | Str255 errMessage; |
| 377 | OSErr err; |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 378 | #ifndef USE_GUSI |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 379 | Boolean isfolder, didsomething; |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 380 | #endif |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 381 | char buf[512]; |
| 382 | Str63 fragname; |
| 383 | Ptr symAddr; |
| 384 | CFragSymbolClass class; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 385 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 386 | /* First resolve any aliases to find the real file */ |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 387 | #ifdef USE_GUSI |
| 388 | err = Path2FSSpec(pathname, &libspec); |
| 389 | #else |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 390 | (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 391 | err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething); |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 392 | #endif |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 393 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 394 | sprintf(buf, "%.255s: %.200s", |
| 395 | pathname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 396 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 397 | return NULL; |
| 398 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 399 | /* Next, determine the fragment name, |
| 400 | by stripping '.slb' and 'module' */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 401 | memcpy(fragname+1, libspec.name+1, libspec.name[0]); |
| 402 | fragname[0] = libspec.name[0]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 403 | if( strncmp((char *)(fragname+1+fragname[0]-4), |
| 404 | ".slb", 4) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 405 | fragname[0] -= 4; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 406 | if ( strncmp((char *)(fragname+1+fragname[0]-6), |
| 407 | "module", 6) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 408 | fragname[0] -= 6; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 409 | /* Load the fragment |
| 410 | (or return the connID if it is already loaded */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 411 | err = GetDiskFragment(&libspec, 0, 0, fragname, |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 412 | kLoadCFrag, &connID, &mainAddr, |
| 413 | errMessage); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 414 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 415 | sprintf(buf, "%.*s: %.200s", |
| 416 | errMessage[0], errMessage+1, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 417 | PyMac_StrError(err)); |
| 418 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 419 | return NULL; |
| 420 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 421 | /* Locate the address of the correct init function */ |
| 422 | err = FindSymbol(connID, Pstring(funcname), &symAddr, &class); |
| 423 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 424 | sprintf(buf, "%s: %.200s", |
| 425 | funcname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 427 | return NULL; |
| 428 | } |
| 429 | p = (dl_funcptr)symAddr; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 430 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 431 | #endif /* USE_MAC_DYNAMIC_LOADING */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 432 | #ifdef USE_SHLIB |
| 433 | { |
| 434 | #ifdef RTLD_NOW |
| 435 | /* RTLD_NOW: resolve externals now |
| 436 | (i.e. core dump now if some are missing) */ |
Guido van Rossum | 6b07787 | 1998-05-18 13:42:45 +0000 | [diff] [blame] | 437 | void *handle = dlopen(pathname, RTLD_NOW); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 438 | #else |
| 439 | void *handle; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | if (Py_VerboseFlag) |
Guido van Rossum | 08052c7 | 1997-12-02 20:43:18 +0000 | [diff] [blame] | 441 | printf("dlopen(\"%s\", %d);\n", pathname, |
Guido van Rossum | 6b07787 | 1998-05-18 13:42:45 +0000 | [diff] [blame] | 442 | RTLD_LAZY); |
| 443 | handle = dlopen(pathname, RTLD_LAZY); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 444 | #endif /* RTLD_NOW */ |
| 445 | if (handle == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 446 | PyErr_SetString(PyExc_ImportError, dlerror()); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 449 | if (fp != NULL && nhandles < 128) |
| 450 | handles[nhandles++].handle = handle; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 451 | p = (dl_funcptr) dlsym(handle, funcname); |
| 452 | } |
| 453 | #endif /* USE_SHLIB */ |
| 454 | #ifdef _AIX |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 455 | /* |
| 456 | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
| 457 | -- of the shared module unresolved. Thus we have to resolve them |
| 458 | -- explicitely with loadbind. The new module is loaded, then we |
| 459 | -- resolve its symbols using the list of already loaded modules |
| 460 | -- (only those that belong to the python executable). Get these |
| 461 | -- with loadquery(L_GETINFO). |
| 462 | */ |
| 463 | { |
| 464 | static void *staticmodlistptr = NULL; |
| 465 | |
| 466 | if (!staticmodlistptr) |
| 467 | if (aix_getoldmodules(&staticmodlistptr) == -1) |
| 468 | return NULL; |
| 469 | p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0); |
| 470 | if (p == NULL) { |
| 471 | aix_loaderror(pathname); |
| 472 | return NULL; |
| 473 | } |
| 474 | if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) { |
| 475 | aix_loaderror(pathname); |
| 476 | return NULL; |
| 477 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 478 | } |
| 479 | #endif /* _AIX */ |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 480 | #ifdef MS_WIN32 |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 481 | { |
| 482 | HINSTANCE hDLL; |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 483 | char pathbuf[260]; |
| 484 | if (strchr(pathname, SEP) == NULL && |
| 485 | strchr(pathname, ALTSEP) == NULL) |
| 486 | { |
| 487 | /* Prefix bare filename with ".\" */ |
| 488 | char *p = pathbuf; |
| 489 | *p = '\0'; |
| 490 | _getcwd(pathbuf, sizeof pathbuf); |
| 491 | if (*p != '\0' && p[1] == ':') |
| 492 | p += 2; |
| 493 | sprintf(p, ".\\%-.255s", pathname); |
| 494 | pathname = pathbuf; |
| 495 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 496 | hDLL = LoadLibrary(pathname); |
| 497 | if (hDLL==NULL){ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 498 | char errBuf[256]; |
| 499 | unsigned int errorCode; |
| 500 | |
| 501 | /* Get an error string from Win32 error code */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 502 | char theInfo[256]; /* Pointer to error text |
| 503 | from system */ |
| 504 | int theLength; /* Length of error text */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 505 | |
| 506 | errorCode = GetLastError(); |
| 507 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 508 | theLength = FormatMessage( |
| 509 | FORMAT_MESSAGE_FROM_SYSTEM, /* flags */ |
| 510 | NULL, /* message source */ |
| 511 | errorCode, /* the message (error) ID */ |
| 512 | 0, /* default language environment */ |
| 513 | (LPTSTR) theInfo, /* the buffer */ |
| 514 | sizeof(theInfo), /* the buffer size */ |
| 515 | NULL); /* no additional format args. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 517 | /* Problem: could not get the error message. |
| 518 | This should not happen if called correctly. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 519 | if (theLength == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 520 | sprintf(errBuf, |
| 521 | "DLL load failed with error code %d", |
| 522 | errorCode); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 523 | } else { |
| 524 | int len; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 525 | /* For some reason a \r\n |
| 526 | is appended to the text */ |
| 527 | if (theLength >= 2 && |
| 528 | theInfo[theLength-2] == '\r' && |
| 529 | theInfo[theLength-1] == '\n') { |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 530 | theLength -= 2; |
| 531 | theInfo[theLength] = '\0'; |
| 532 | } |
| 533 | strcpy(errBuf, "DLL load failed: "); |
| 534 | len = strlen(errBuf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 535 | strncpy(errBuf+len, theInfo, |
| 536 | sizeof(errBuf)-len); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 537 | errBuf[sizeof(errBuf)-1] = '\0'; |
| 538 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 540 | return NULL; |
| 541 | } |
| 542 | p = GetProcAddress(hDLL, funcname); |
| 543 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 544 | #endif /* MS_WIN32 */ |
| 545 | #ifdef MS_WIN16 |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 546 | { |
| 547 | HINSTANCE hDLL; |
Guido van Rossum | a5e1b00 | 1998-06-27 21:53:17 +0000 | [diff] [blame] | 548 | char pathbuf[16]; |
| 549 | if (strchr(pathname, SEP) == NULL && |
| 550 | strchr(pathname, ALTSEP) == NULL) |
| 551 | { |
| 552 | /* Prefix bare filename with ".\" */ |
| 553 | sprintf(pathbuf, ".\\%-.13s", pathname); |
| 554 | pathname = pathbuf; |
| 555 | } |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 556 | hDLL = LoadLibrary(pathname); |
| 557 | if (hDLL < HINSTANCE_ERROR){ |
| 558 | char errBuf[256]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 559 | sprintf(errBuf, |
| 560 | "DLL load failed with error code %d", hDLL); |
| 561 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
| 564 | p = GetProcAddress(hDLL, funcname); |
| 565 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 566 | #endif /* MS_WIN16 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 567 | |
| 568 | #if defined(PYOS_OS2) |
| 569 | { |
| 570 | APIRET rc; |
| 571 | HMODULE hDLL; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 572 | char failreason[256]; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 573 | |
| 574 | rc = DosLoadModule(failreason, |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 575 | sizeof(failreason), |
| 576 | pathname, |
| 577 | &hDLL); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 578 | |
| 579 | if (rc != NO_ERROR) { |
| 580 | char errBuf[256]; |
| 581 | sprintf(errBuf, |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 582 | "DLL load failed, rc = %d, problem '%s': %s", |
| 583 | rc, failreason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 584 | PyErr_SetString(PyExc_ImportError, errBuf); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 588 | rc = DosQueryProcAddr(hDLL, 0L, funcname, &p); |
| 589 | if (rc != NO_ERROR) |
| 590 | p = NULL; /* Signify Failure to Acquire Entrypoint */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 591 | } |
| 592 | #endif /* PYOS_OS2 */ |
| 593 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 594 | #ifdef USE_DL |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 595 | p = dl_loadmod(Py_GetProgramName(), pathname, funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 596 | #endif /* USE_DL */ |
| 597 | #ifdef USE_RLD |
| 598 | { |
| 599 | NXStream *errorStream; |
| 600 | struct mach_header *new_header; |
| 601 | const char *filenames[2]; |
| 602 | long ret; |
| 603 | unsigned long ptr; |
| 604 | |
| 605 | errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY); |
| 606 | filenames[0] = pathname; |
| 607 | filenames[1] = NULL; |
Guido van Rossum | ab076fd | 1998-08-24 14:15:44 +0000 | [diff] [blame] | 608 | |
| 609 | #if HANDLE_OBJC_MODULES |
| 610 | |
| 611 | /* The following very bogus line of code ensures that |
| 612 | objc_msgSend, etc are linked into the binary. Without |
| 613 | it, dynamic loading of a module that includes objective-c |
| 614 | method calls will fail with "undefined symbol _objc_msgSend()". |
| 615 | This remains true even in the presence of the -ObjC flag |
| 616 | to the compiler |
| 617 | */ |
| 618 | |
| 619 | [Object name]; |
| 620 | |
| 621 | /* objc_loadModules() dynamically loads the object files |
| 622 | indicated by the paths in filenames. If there are any |
| 623 | errors generated during loading -- typically due to the |
| 624 | inability to find particular symbols -- an error message |
| 625 | will be written to errorStream. |
| 626 | It returns 0 if the module is successfully loaded, 1 |
| 627 | otherwise. |
| 628 | */ |
| 629 | |
| 630 | ret = !objc_loadModules(filenames, errorStream, |
| 631 | NULL, &new_header, NULL); |
| 632 | |
| 633 | #else /* !HANDLE_OBJC_MODULES */ |
| 634 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 635 | ret = rld_load(errorStream, &new_header, |
| 636 | filenames, NULL); |
| 637 | |
Guido van Rossum | ab076fd | 1998-08-24 14:15:44 +0000 | [diff] [blame] | 638 | #endif /* HANDLE_OBJC_MODULES */ |
| 639 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 640 | /* extract the error messages for the exception */ |
| 641 | if(!ret) { |
| 642 | char *streamBuf; |
| 643 | int len, maxLen; |
| 644 | |
| 645 | NXPutc(errorStream, (char)0); |
| 646 | |
| 647 | NXGetMemoryBuffer(errorStream, |
| 648 | &streamBuf, &len, &maxLen); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | PyErr_SetString(PyExc_ImportError, streamBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if(ret && rld_lookup(errorStream, funcname, &ptr)) |
| 653 | p = (dl_funcptr) ptr; |
| 654 | |
| 655 | NXCloseMemory(errorStream, NX_FREEBUFFER); |
| 656 | |
| 657 | if(!ret) |
| 658 | return NULL; |
| 659 | } |
| 660 | #endif /* USE_RLD */ |
| 661 | #ifdef hpux |
| 662 | { |
| 663 | shl_t lib; |
| 664 | int flags; |
| 665 | |
Guido van Rossum | 3afb595 | 1996-12-05 23:15:35 +0000 | [diff] [blame] | 666 | flags = BIND_FIRST | BIND_DEFERRED; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 667 | if (Py_VerboseFlag) { |
| 668 | flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | BIND_NONFATAL | BIND_VERBOSE; |
Guido van Rossum | bb71ab6 | 1998-07-08 13:47:12 +0000 | [diff] [blame] | 670 | printf("shl_load %s\n",pathname); |
| 671 | } |
| 672 | lib = shl_load(pathname, flags, 0); |
| 673 | /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */ |
| 674 | if (lib == NULL) { |
| 675 | char buf[256]; |
| 676 | if (Py_VerboseFlag) |
| 677 | perror(pathname); |
| 678 | sprintf(buf, "Failed to load %.200s", pathname); |
| 679 | PyErr_SetString(PyExc_ImportError, buf); |
| 680 | return NULL; |
| 681 | } |
| 682 | if (Py_VerboseFlag) |
| 683 | printf("shl_findsym %s\n", funcname); |
| 684 | shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p); |
| 685 | if (p == NULL && Py_VerboseFlag) |
| 686 | perror(funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 687 | } |
| 688 | #endif /* hpux */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 689 | #ifdef __BEOS__ |
| 690 | { |
| 691 | image_id the_id; |
| 692 | status_t retval; |
| 693 | char fullpath[PATH_MAX]; |
| 694 | |
| 695 | if( Py_VerboseFlag ) { |
| 696 | printf( "load_add_on( %s )\n", pathname ); |
| 697 | } |
| 698 | |
| 699 | /* Hmm, this old bug appears to have regenerated itself; if the |
| 700 | * path isn't absolute, load_add_on() will fail. Reported to Be |
| 701 | * April 21, 1998. |
| 702 | */ |
| 703 | if( pathname[0] != '/' ) { |
| 704 | (void)getcwd( fullpath, PATH_MAX ); |
| 705 | (void)strncat( fullpath, "/", PATH_MAX ); |
| 706 | (void)strncat( fullpath, pathname, PATH_MAX ); |
| 707 | |
| 708 | if( Py_VerboseFlag ) { |
| 709 | printf( "load_add_on( %s )\n", fullpath ); |
| 710 | } |
| 711 | } else { |
| 712 | (void)strcpy( fullpath, pathname ); |
| 713 | } |
| 714 | |
| 715 | the_id = load_add_on( fullpath ); |
| 716 | if( the_id < B_NO_ERROR ) { |
| 717 | /* It's too bad load_add_on() doesn't set errno or something... |
| 718 | */ |
| 719 | char buff[256]; /* hate hard-coded string sizes... */ |
| 720 | |
| 721 | if( Py_VerboseFlag ) { |
| 722 | printf( "load_add_on( %s ) failed", fullpath ); |
| 723 | } |
| 724 | |
| 725 | switch( the_id ) { |
| 726 | case B_ERROR: |
| 727 | sprintf( buff, "BeOS: Failed to load %.200s", fullpath ); |
| 728 | break; |
| 729 | default: |
| 730 | sprintf( buff, "Unknown error loading %.200s", fullpath ); |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | PyErr_SetString( PyExc_ImportError, buff ); |
| 735 | return NULL; |
| 736 | } |
| 737 | |
| 738 | if( Py_VerboseFlag ) { |
| 739 | printf( "get_image_symbol( %s )\n", funcname ); |
| 740 | } |
| 741 | |
| 742 | retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p ); |
| 743 | if( retval != B_NO_ERROR || p == NULL ) { |
| 744 | /* That's bad, we can't find that symbol in the module... |
| 745 | */ |
| 746 | char buff[256]; /* hate hard-coded string sizes... */ |
| 747 | |
| 748 | if( Py_VerboseFlag ) { |
| 749 | printf( "get_image_symbol( %s ) failed", funcname ); |
| 750 | } |
| 751 | |
| 752 | switch( retval ) { |
| 753 | case B_BAD_IMAGE_ID: |
| 754 | sprintf( buff, "can't load init function for dynamic module: " |
| 755 | "Invalid image ID for %.180s", fullpath ); |
| 756 | break; |
| 757 | case B_BAD_INDEX: |
| 758 | sprintf( buff, "can't load init function for dynamic module: " |
| 759 | "Bad index for %.180s", funcname ); |
| 760 | break; |
| 761 | default: |
| 762 | sprintf( buff, "can't load init function for dynamic module: " |
| 763 | "Unknown error looking up %.180s", funcname ); |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | retval = unload_add_on( the_id ); |
| 768 | |
| 769 | PyErr_SetString( PyExc_ImportError, buff ); |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | /* Save the module name and image ID for later so we can clean up |
| 774 | * gracefully. |
| 775 | */ |
| 776 | beos_add_dyn( name, the_id ); |
| 777 | } |
| 778 | #endif /* __BEOS__ */ |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 779 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 780 | got_it: |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 781 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 782 | if (p == NULL) { |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 783 | PyErr_Format(PyExc_ImportError, |
Guido van Rossum | 42e8e5d | 1998-01-19 22:23:08 +0000 | [diff] [blame] | 784 | "dynamic module does not define init function (%.200s)", |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 785 | funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 786 | return NULL; |
| 787 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 788 | _Py_PackageContext = packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 789 | (*p)(); |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 790 | _Py_PackageContext = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 791 | if (PyErr_Occurred()) |
| 792 | return NULL; |
| 793 | if (_PyImport_FixupExtension(name, pathname) == NULL) |
| 794 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 795 | |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 796 | m = PyDict_GetItemString(PyImport_GetModuleDict(), name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 797 | if (m == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 798 | PyErr_SetString(PyExc_SystemError, |
| 799 | "dynamic module not initialized properly"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 800 | return NULL; |
| 801 | } |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 802 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 803 | d = PyModule_GetDict(m); |
| 804 | s = PyString_FromString(pathname); |
| 805 | if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) |
| 806 | PyErr_Clear(); /* Not important enough to report */ |
| 807 | Py_XDECREF(s); |
| 808 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 809 | fprintf(stderr, |
| 810 | "import %s # dynamically loaded from %s\n", |
| 811 | name, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 812 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 813 | return m; |
| 814 | #endif /* DYNAMIC_LINK */ |
| 815 | } |
| 816 | |
| 817 | |
| 818 | #ifdef _AIX |
| 819 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 820 | #include <ctype.h> /* for isdigit() */ |
| 821 | #include <errno.h> /* for global errno */ |
| 822 | #include <string.h> /* for strerror() */ |
| 823 | #include <stdlib.h> /* for malloc(), free() */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 824 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 825 | typedef struct Module { |
| 826 | struct Module *next; |
| 827 | void *entry; |
| 828 | } Module, *ModulePtr; |
| 829 | |
| 830 | static int |
| 831 | aix_getoldmodules(modlistptr) |
| 832 | void **modlistptr; |
| 833 | { |
| 834 | register ModulePtr modptr, prevmodptr; |
| 835 | register struct ld_info *ldiptr; |
| 836 | register char *ldibuf; |
| 837 | register int errflag, bufsize = 1024; |
| 838 | register unsigned int offset; |
| 839 | |
| 840 | /* |
| 841 | -- Get the list of loaded modules into ld_info structures. |
| 842 | */ |
| 843 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 844 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 845 | return -1; |
| 846 | } |
| 847 | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
| 848 | && errno == ENOMEM) { |
| 849 | free(ldibuf); |
| 850 | bufsize += 1024; |
| 851 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 852 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 853 | return -1; |
| 854 | } |
| 855 | } |
| 856 | if (errflag == -1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 857 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 858 | return -1; |
| 859 | } |
| 860 | /* |
| 861 | -- Make the modules list from the ld_info structures. |
| 862 | */ |
| 863 | ldiptr = (struct ld_info *)ldibuf; |
| 864 | prevmodptr = NULL; |
| 865 | do { |
Guido van Rossum | 3b31cd2 | 1997-10-10 17:40:00 +0000 | [diff] [blame] | 866 | if (strstr(ldiptr->ldinfo_filename, "python") == NULL) { |
| 867 | /* |
| 868 | -- Extract only the modules containing "python" as a |
| 869 | -- substring, like the "python[version]" executable or |
| 870 | -- "libpython[version].a" in case python is embedded. |
| 871 | */ |
| 872 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 873 | ldiptr = (struct ld_info *)((unsigned int) |
| 874 | ldiptr + offset); |
| 875 | continue; |
| 876 | } |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 877 | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 878 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 879 | while (*modlistptr) { |
| 880 | modptr = (ModulePtr)*modlistptr; |
| 881 | *modlistptr = (void *)modptr->next; |
| 882 | free(modptr); |
| 883 | } |
| 884 | return -1; |
| 885 | } |
| 886 | modptr->entry = ldiptr->ldinfo_dataorg; |
| 887 | modptr->next = NULL; |
| 888 | if (prevmodptr == NULL) |
| 889 | *modlistptr = (void *)modptr; |
| 890 | else |
| 891 | prevmodptr->next = modptr; |
| 892 | prevmodptr = modptr; |
| 893 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 894 | ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset); |
| 895 | } while (offset); |
| 896 | free(ldibuf); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | static int |
| 901 | aix_bindnewmodule(newmoduleptr, modlistptr) |
| 902 | void *newmoduleptr; |
| 903 | void *modlistptr; |
| 904 | { |
| 905 | register ModulePtr modptr; |
| 906 | |
| 907 | /* |
| 908 | -- Bind the new module with the list of loaded modules. |
| 909 | */ |
| 910 | for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next) |
| 911 | if (loadbind(0, modptr->entry, newmoduleptr) != 0) |
| 912 | return -1; |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | static void |
| 917 | aix_loaderror(pathname) |
| 918 | char *pathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 919 | { |
| 920 | |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 921 | char *message[1024], errbuf[1024]; |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 922 | register int i,j; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 923 | |
| 924 | struct errtab { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 925 | int errNo; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 926 | char *errstr; |
| 927 | } load_errtab[] = { |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 928 | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 929 | {L_ERROR_NOLIB, "can't load library:"}, |
| 930 | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
| 931 | {L_ERROR_RLDBAD, |
| 932 | "RLD index out of range or bad relocation type:"}, |
| 933 | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
| 934 | {L_ERROR_MEMBER, |
| 935 | "file not an archive or does not contain requested member:"}, |
| 936 | {L_ERROR_TYPE, "symbol table mismatch:"}, |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 937 | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 938 | {L_ERROR_SYSTEM, "System error:"}, |
| 939 | {L_ERROR_ERRNO, NULL} |
| 940 | }; |
| 941 | |
| 942 | #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) |
| 943 | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
| 944 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 945 | sprintf(errbuf, "from module %.200s ", pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 946 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 947 | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 948 | ERRBUF_APPEND(strerror(errno)); |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 949 | ERRBUF_APPEND("\n"); |
| 950 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 951 | for(i = 0; message[i] && *message[i]; i++) { |
| 952 | int nerr = atoi(message[i]); |
| 953 | for (j=0; j<LOAD_ERRTAB_LEN ; j++) { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 954 | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 955 | ERRBUF_APPEND(load_errtab[j].errstr); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 956 | } |
| 957 | while (isdigit(*message[i])) message[i]++ ; |
| 958 | ERRBUF_APPEND(message[i]); |
| 959 | ERRBUF_APPEND("\n"); |
| 960 | } |
| 961 | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 962 | PyErr_SetString(PyExc_ImportError, errbuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | |
| 966 | #endif /* _AIX */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 967 | |
| 968 | #ifdef __BEOS__ |
| 969 | /* ---------------------------------------------------------------------- |
| 970 | * BeOS dynamic loading support |
| 971 | * |
| 972 | * This uses shared libraries, but BeOS has its own way of doing things |
| 973 | * (much easier than dlfnc.h, from the look of things). We'll use a |
| 974 | * Python Dictionary object to store the images_ids so we can be very |
| 975 | * nice and unload them when we exit. |
| 976 | * |
| 977 | * Note that this is thread-safe. Probably irrelevent, because of losing |
| 978 | * systems... Python probably disables threads while loading modules. |
| 979 | * Note the use of "probably"! Better to be safe than sorry. [chrish] |
| 980 | * |
| 981 | * As of 1.5.1 this should also work properly when you've configured |
| 982 | * Python without thread support; the 1.5 version required it, which wasn't |
| 983 | * very friendly. Note that I haven't tested it without threading... why |
| 984 | * would you want to avoid threads on BeOS? [chrish] |
| 985 | */ |
| 986 | |
| 987 | /* |
| 988 | * Initialize our dictionary, and the dictionary mutex. |
| 989 | */ |
| 990 | static void beos_init_dyn( void ) |
| 991 | { |
| 992 | /* We're protected from a race condition here by the atomic init_count |
| 993 | * variable. |
| 994 | */ |
| 995 | static int32 init_count = 0; |
| 996 | int32 val; |
| 997 | |
| 998 | val = atomic_add( &init_count, 1 ); |
| 999 | if( beos_dyn_images == NULL && val == 0 ) { |
| 1000 | beos_dyn_images = PyDict_New(); |
| 1001 | #ifdef WITH_THREAD |
| 1002 | beos_dyn_lock = PyThread_allocate_lock(); |
| 1003 | #endif |
| 1004 | atexit( beos_cleanup_dyn ); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* atexit() handler that'll call unload_add_on() for every item in the |
| 1009 | * dictionary. |
| 1010 | */ |
| 1011 | static void beos_cleanup_dyn( void ) |
| 1012 | { |
| 1013 | if( beos_dyn_images ) { |
| 1014 | int idx; |
| 1015 | int list_size; |
| 1016 | PyObject *id_list; |
| 1017 | |
| 1018 | #ifdef WITH_THREAD |
| 1019 | PyThread_acquire_lock( beos_dyn_lock, 1 ); |
| 1020 | #endif |
| 1021 | |
| 1022 | id_list = PyDict_Values( beos_dyn_images ); |
| 1023 | |
| 1024 | list_size = PyList_Size( id_list ); |
| 1025 | for( idx = 0; idx < list_size; idx++ ) { |
| 1026 | PyObject *the_item; |
| 1027 | |
| 1028 | the_item = PyList_GetItem( id_list, idx ); |
| 1029 | beos_nuke_dyn( the_item ); |
| 1030 | } |
| 1031 | |
| 1032 | PyDict_Clear( beos_dyn_images ); |
| 1033 | |
| 1034 | #ifdef WITH_THREAD |
| 1035 | PyThread_free_lock( beos_dyn_lock ); |
| 1036 | #endif |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* Whack an item; the item is an image_id in disguise, so we'll call |
| 1041 | * unload_add_on() for it. |
| 1042 | */ |
| 1043 | static void beos_nuke_dyn( PyObject *item ) |
| 1044 | { |
| 1045 | status_t retval; |
| 1046 | |
| 1047 | if( item ) { |
| 1048 | image_id id = (image_id)PyInt_AsLong( item ); |
| 1049 | |
| 1050 | retval = unload_add_on( id ); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * Add an image_id to the dictionary; the module name of the loaded image |
| 1056 | * is the key. Note that if the key is already in the dict, we unload |
| 1057 | * that image; this should allow reload() to work on dynamically loaded |
| 1058 | * modules (super-keen!). |
| 1059 | */ |
| 1060 | static void beos_add_dyn( char *name, image_id id ) |
| 1061 | { |
| 1062 | int retval; |
| 1063 | PyObject *py_id; |
| 1064 | |
| 1065 | if( beos_dyn_images == NULL ) { |
| 1066 | beos_init_dyn(); |
| 1067 | } |
| 1068 | |
| 1069 | #ifdef WITH_THREAD |
| 1070 | retval = PyThread_acquire_lock( beos_dyn_lock, 1 ); |
| 1071 | #endif |
| 1072 | |
| 1073 | /* If there's already an object with this key in the dictionary, |
| 1074 | * we're doing a reload(), so let's nuke it. |
| 1075 | */ |
| 1076 | py_id = PyDict_GetItemString( beos_dyn_images, name ); |
| 1077 | if( py_id ) { |
| 1078 | beos_nuke_dyn( py_id ); |
| 1079 | retval = PyDict_DelItemString( beos_dyn_images, name ); |
| 1080 | } |
| 1081 | |
| 1082 | py_id = PyInt_FromLong( (long)id ); |
| 1083 | if( py_id ) { |
| 1084 | retval = PyDict_SetItemString( beos_dyn_images, name, py_id ); |
| 1085 | } |
| 1086 | |
| 1087 | #ifdef WITH_THREAD |
| 1088 | PyThread_release_lock( beos_dyn_lock ); |
| 1089 | #endif |
| 1090 | } |
| 1091 | |
| 1092 | /* Given a module name, return the image_id (if it's a dynamically loaded |
| 1093 | * module). [Donn Cave] |
| 1094 | */ |
| 1095 | image_id PyImport_BeImageID( char *name ) |
| 1096 | { |
| 1097 | int retval; |
| 1098 | PyObject *py_id; |
| 1099 | long id; |
| 1100 | |
| 1101 | if( !beos_dyn_images ) { |
| 1102 | return B_ERROR; |
| 1103 | } |
| 1104 | |
| 1105 | #ifdef WITH_THREAD |
| 1106 | retval = PyThread_acquire_lock( beos_dyn_lock, 1 ); |
| 1107 | #endif |
| 1108 | |
| 1109 | py_id = PyDict_GetItemString( beos_dyn_images, name ); |
| 1110 | if( py_id ) { |
| 1111 | id = PyInt_AsLong( py_id ); |
| 1112 | } else { |
| 1113 | id = B_ERROR; |
| 1114 | } |
| 1115 | |
| 1116 | #ifdef WITH_THREAD |
| 1117 | PyThread_release_lock( beos_dyn_lock ); |
| 1118 | #endif |
| 1119 | |
| 1120 | return (image_id)id; |
| 1121 | } |
| 1122 | #endif /* __BEOS__ */ |