blob: 58c57d507417c7955fe55b2451c46c47ae8ff980 [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
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;
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);
117 if ( err ) {
118 sprintf(buf, "%.*s: %.200s",
119 errMessage[0], errMessage+1,
120 PyMac_StrError(err));
121 PyErr_SetString(PyExc_ImportError, buf);
122 return NULL;
123 }
124 /* Locate the address of the correct init function */
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000125 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000126 err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
127 if ( err ) {
128 sprintf(buf, "%s: %.200s",
129 funcname, PyMac_StrError(err));
130 PyErr_SetString(PyExc_ImportError, buf);
131 return NULL;
132 }
133 p = (dl_funcptr)symAddr;
134
135 return p;
136}