Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 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. |
| 16 | |
| 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. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Support for dynamic loading of extension modules */ |
| 33 | |
| 34 | #include "Python.h" |
| 35 | #include "importdl.h" |
| 36 | |
| 37 | #include <Aliases.h> |
| 38 | #include <CodeFragments.h> |
| 39 | #ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */ |
| 40 | #define CFragConnectionID ConnectionID |
| 41 | #define kLoadCFrag 0x01 |
| 42 | #endif |
| 43 | #ifdef USE_GUSI |
| 44 | #include "TFileSpec.h" /* for Path2FSSpec() */ |
| 45 | #endif |
| 46 | #include <Files.h> |
| 47 | #include "macdefs.h" |
| 48 | #include "macglue.h" |
| 49 | |
| 50 | |
| 51 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 52 | {".slb", "rb", C_EXTENSION}, |
| 53 | #ifdef __CFM68K__ |
| 54 | {".CFM68K.slb", "rb", C_EXTENSION}, |
| 55 | #else |
| 56 | {".ppc.slb", "rb", C_EXTENSION}, |
| 57 | #endif |
| 58 | {0, 0} |
| 59 | }; |
| 60 | |
| 61 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 62 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 63 | const char *pathname, FILE *fp) |
| 64 | { |
| 65 | dl_funcptr p; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 66 | char funcname[258]; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | ** Dynamic loading of CFM shared libraries on the Mac. The |
| 70 | ** code has become more convoluted than it was, because we |
| 71 | ** want to be able to put multiple modules in a single |
| 72 | ** file. For this reason, we have to determine the fragment |
| 73 | ** name, and we cannot use the library entry point but we have |
| 74 | ** to locate the correct init routine "by hand". |
| 75 | */ |
| 76 | FSSpec libspec; |
| 77 | CFragConnectionID connID; |
| 78 | Ptr mainAddr; |
| 79 | Str255 errMessage; |
| 80 | OSErr err; |
| 81 | #ifndef USE_GUSI |
| 82 | Boolean isfolder, didsomething; |
| 83 | #endif |
| 84 | char buf[512]; |
| 85 | Str63 fragname; |
| 86 | Ptr symAddr; |
| 87 | CFragSymbolClass class; |
| 88 | |
| 89 | /* First resolve any aliases to find the real file */ |
| 90 | #ifdef USE_GUSI |
| 91 | err = Path2FSSpec(pathname, &libspec); |
| 92 | #else |
| 93 | (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec); |
| 94 | err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething); |
| 95 | #endif |
| 96 | if ( err ) { |
| 97 | sprintf(buf, "%.255s: %.200s", |
| 98 | pathname, PyMac_StrError(err)); |
| 99 | PyErr_SetString(PyExc_ImportError, buf); |
| 100 | return NULL; |
| 101 | } |
| 102 | /* Next, determine the fragment name, |
| 103 | by stripping '.slb' and 'module' */ |
| 104 | memcpy(fragname+1, libspec.name+1, libspec.name[0]); |
| 105 | fragname[0] = libspec.name[0]; |
| 106 | if( strncmp((char *)(fragname+1+fragname[0]-4), |
| 107 | ".slb", 4) == 0 ) |
| 108 | fragname[0] -= 4; |
| 109 | if ( strncmp((char *)(fragname+1+fragname[0]-6), |
| 110 | "module", 6) == 0 ) |
| 111 | fragname[0] -= 6; |
| 112 | /* Load the fragment |
| 113 | (or return the connID if it is already loaded */ |
| 114 | err = GetDiskFragment(&libspec, 0, 0, fragname, |
| 115 | kLoadCFrag, &connID, &mainAddr, |
| 116 | errMessage); |
Guido van Rossum | e817acd | 2000-02-14 17:58:25 +0000 | [diff] [blame] | 117 | if ( err == cfragImportTooOldErr || err == cfragImportTooNewErr ) { |
| 118 | /* |
| 119 | ** Special-case code: if PythonCore is too old or too new this means |
| 120 | ** the dynamic module was meant for a different Python. |
| 121 | */ |
| 122 | if (errMessage[0] == 10 && strncmp((char *)errMessage+1, "PythonCore", 10) == 0 ) { |
| 123 | sprintf(buf, "Dynamic module was built for %s version of MacPython", |
| 124 | (err == cfragImportTooOldErr ? "a newer" : "an older")); |
| 125 | PyErr_SetString(PyExc_ImportError, buf); |
| 126 | return NULL; |
| 127 | } |
| 128 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 129 | if ( err ) { |
| 130 | sprintf(buf, "%.*s: %.200s", |
| 131 | errMessage[0], errMessage+1, |
| 132 | PyMac_StrError(err)); |
| 133 | PyErr_SetString(PyExc_ImportError, buf); |
| 134 | return NULL; |
| 135 | } |
| 136 | /* Locate the address of the correct init function */ |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 137 | sprintf(funcname, "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 138 | err = FindSymbol(connID, Pstring(funcname), &symAddr, &class); |
| 139 | if ( err ) { |
| 140 | sprintf(buf, "%s: %.200s", |
| 141 | funcname, PyMac_StrError(err)); |
| 142 | PyErr_SetString(PyExc_ImportError, buf); |
| 143 | return NULL; |
| 144 | } |
| 145 | p = (dl_funcptr)symAddr; |
| 146 | |
| 147 | return p; |
| 148 | } |