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