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