blob: 85ff0144c76a4140f1cceebf34a0da43a0555c75 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE 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
Guido van Rossumb33aa1a2000-04-24 15:08:18 +000043#ifdef USE_GUSI1
Guido van Rossum22a1d361999-12-20 21:18:49 +000044#include "TFileSpec.h" /* for Path2FSSpec() */
45#endif
46#include <Files.h>
47#include "macdefs.h"
48#include "macglue.h"
49
50
51const 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 Rossum96a8fb71999-12-22 14:09:35 +000062dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000063 const char *pathname, FILE *fp)
64{
65 dl_funcptr p;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000066 char funcname[258];
Guido van Rossum22a1d361999-12-20 21:18:49 +000067
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;
Guido van Rossumb33aa1a2000-04-24 15:08:18 +000081#ifndef USE_GUSI1
Guido van Rossum22a1d361999-12-20 21:18:49 +000082 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 */
Guido van Rossumb33aa1a2000-04-24 15:08:18 +000090#ifdef USE_GUSI1
Guido van Rossum22a1d361999-12-20 21:18:49 +000091 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 Rossume817acd2000-02-14 17:58:25 +0000117 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 Rossum22a1d361999-12-20 21:18:49 +0000129 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 Rossum96a8fb71999-12-22 14:09:35 +0000137 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000138 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}