blob: 91619956e99ab62f6ad25ad258b2116de9e46c72 [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
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
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
62dl_funcptr _PyImport_GetDynLoadFunc(const char *name, const char *funcname,
63 const char *pathname, FILE *fp)
64{
65 dl_funcptr p;
66
67 /*
68 ** Dynamic loading of CFM shared libraries on the Mac. The
69 ** code has become more convoluted than it was, because we
70 ** want to be able to put multiple modules in a single
71 ** file. For this reason, we have to determine the fragment
72 ** name, and we cannot use the library entry point but we have
73 ** to locate the correct init routine "by hand".
74 */
75 FSSpec libspec;
76 CFragConnectionID connID;
77 Ptr mainAddr;
78 Str255 errMessage;
79 OSErr err;
80#ifndef USE_GUSI
81 Boolean isfolder, didsomething;
82#endif
83 char buf[512];
84 Str63 fragname;
85 Ptr symAddr;
86 CFragSymbolClass class;
87
88 /* First resolve any aliases to find the real file */
89#ifdef USE_GUSI
90 err = Path2FSSpec(pathname, &libspec);
91#else
92 (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec);
93 err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething);
94#endif
95 if ( err ) {
96 sprintf(buf, "%.255s: %.200s",
97 pathname, PyMac_StrError(err));
98 PyErr_SetString(PyExc_ImportError, buf);
99 return NULL;
100 }
101 /* Next, determine the fragment name,
102 by stripping '.slb' and 'module' */
103 memcpy(fragname+1, libspec.name+1, libspec.name[0]);
104 fragname[0] = libspec.name[0];
105 if( strncmp((char *)(fragname+1+fragname[0]-4),
106 ".slb", 4) == 0 )
107 fragname[0] -= 4;
108 if ( strncmp((char *)(fragname+1+fragname[0]-6),
109 "module", 6) == 0 )
110 fragname[0] -= 6;
111 /* Load the fragment
112 (or return the connID if it is already loaded */
113 err = GetDiskFragment(&libspec, 0, 0, fragname,
114 kLoadCFrag, &connID, &mainAddr,
115 errMessage);
116 if ( err ) {
117 sprintf(buf, "%.*s: %.200s",
118 errMessage[0], errMessage+1,
119 PyMac_StrError(err));
120 PyErr_SetString(PyExc_ImportError, buf);
121 return NULL;
122 }
123 /* Locate the address of the correct init function */
124 err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
125 if ( err ) {
126 sprintf(buf, "%s: %.200s",
127 funcname, PyMac_StrError(err));
128 PyErr_SetString(PyExc_ImportError, buf);
129 return NULL;
130 }
131 p = (dl_funcptr)symAddr;
132
133 return p;
134}