Kaydet (Commit) 1e2260fc authored tarafından Jack Jansen's avatar Jack Jansen

All import-related code has moved to macimport.c.

There's also new support for importing code fragments: if a file on
sys.path contains a PYD resource with resourcename equal to the name
of the module to be imported this PYD resource should contain a
(pascal) string with the name of a code fragment to load. This allows
freezing Python programs without access to source or a development
environment.
üst f2f3e9bc
......@@ -685,230 +685,6 @@ SIOUXDoAboutBox(void)
DisposeDialog(theDialog);
}
/*
** Returns true if the argument has a resource fork, and it contains
** a 'PYC ' resource of the correct name
*/
int
PyMac_FindResourceModule(obj, module, filename)
PyStringObject *obj;
char *module;
char *filename;
{
FSSpec fss;
FInfo finfo;
short oldrh, filerh;
int ok;
Handle h;
#ifdef INTERN_STRINGS
/*
** If we have interning find_module takes care of interning all
** sys.path components. We then keep a record of all sys.path
** components for which GetFInfo has failed (usually because the
** component in question is a folder), and we don't try opening these
** as resource files again.
*/
#define MAXPATHCOMPONENTS 32
static PyStringObject *not_a_file[MAXPATHCOMPONENTS];
static int max_not_a_file = 0;
int i;
if ( obj->ob_sinterned ) {
for( i=0; i< max_not_a_file; i++ )
if ( obj == not_a_file[i] )
return 0;
}
#endif /* INTERN_STRINGS */
if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
/*
** Special case: the application itself. Use a shortcut to
** forestall opening and closing the application numerous times
** (which is dead slow when running from CDROM)
*/
oldrh = CurResFile();
UseResFile(PyMac_AppRefNum);
filerh = -1;
} else {
if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ||
FSpGetFInfo(&fss, &finfo) != noErr ) {
#ifdef INTERN_STRINGS
if ( max_not_a_file < MAXPATHCOMPONENTS && obj->ob_sinterned )
not_a_file[max_not_a_file++] = obj;
#endif /* INTERN_STRINGS */
/* doesn't exist or is folder */
return 0;
}
oldrh = CurResFile();
filerh = FSpOpenResFile(&fss, fsRdPerm);
if ( filerh == -1 )
return 0;
UseResFile(filerh);
}
SetResLoad(0);
h = Get1NamedResource('PYC ', Pstring(module));
SetResLoad(1);
ok = (h != NULL);
if ( filerh != -1 )
CloseResFile(filerh);
UseResFile(oldrh);
return ok;
}
/*
** Load the specified module from a resource
*/
PyObject *
PyMac_LoadResourceModule(module, filename)
char *module;
char *filename;
{
FSSpec fss;
FInfo finfo;
short oldrh, filerh;
Handle h;
OSErr err;
PyObject *m, *co;
long num, size;
if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
/*
** Special case: the application itself. Use a shortcut to
** forestall opening and closing the application numerous times
** (which is dead slow when running from CDROM)
*/
oldrh = CurResFile();
UseResFile(PyMac_AppRefNum);
filerh = -1;
} else {
if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr )
goto error;
if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
goto error;
oldrh = CurResFile();
filerh = FSpOpenResFile(&fss, fsRdPerm);
if ( filerh == -1 ) {
err = ResError();
goto error;
}
UseResFile(filerh);
}
h = Get1NamedResource('PYC ', Pstring(module));
if ( h == NULL ) {
err = ResError();
goto error;
}
HLock(h);
/*
** XXXX The next few lines are intimately tied to the format of pyc
** files. I'm not sure whether this code should be here or in import.c -- Jack
*/
size = GetHandleSize(h);
if ( size < 8 ) {
PyErr_SetString(PyExc_ImportError, "Resource too small");
co = NULL;
} else {
num = (*h)[0] & 0xff;
num = num | (((*h)[1] & 0xff) << 8);
num = num | (((*h)[2] & 0xff) << 16);
num = num | (((*h)[3] & 0xff) << 24);
if ( num != PyImport_GetMagicNumber() ) {
PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource");
co = NULL;
} else {
co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
}
}
HUnlock(h);
if ( filerh != -1 )
CloseResFile(filerh);
UseResFile(oldrh);
if ( co ) {
m = PyImport_ExecCodeModule(module, co);
Py_DECREF(co);
} else {
m = NULL;
}
if (Py_VerboseFlag)
fprintf(stderr, "import %s # pyc resource from %s\n",
module, filename);
return m;
error:
{
char buf[512];
sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
PyErr_SetString(PyExc_ImportError, buf);
return NULL;
}
}
/*
** Look for a module in a single folder. Upon entry buf and len
** point to the folder to search, upon exit they refer to the full
** pathname of the module found (if any).
*/
struct filedescr *
PyMac_FindModuleExtension(char *buf, int *lenp, char *module)
{
struct filedescr *fdp;
unsigned char fnbuf[64];
int modnamelen = strlen(module);
FSSpec fss;
short refnum;
long dirid;
/*
** Copy the module name to the buffer (already :-terminated)
** We also copy the first suffix, if this matches immedeately we're
** lucky and return immedeately.
*/
if ( !_PyImport_Filetab[0].suffix )
return 0;
#if 0
/* Pre 1.5a4 */
strcpy(buf+*lenp, module);
strcpy(buf+*lenp+modnamelen, _PyImport_Filetab[0].suffix);
#else
strcpy(buf+*lenp, _PyImport_Filetab[0].suffix);
#endif
if ( FSMakeFSSpec(0, 0, Pstring(buf), &fss) == noErr )
return _PyImport_Filetab;
/*
** We cannot check for fnfErr (unfortunately), it can mean either that
** the file doesn't exist (fine, we try others) or the path leading to it.
*/
refnum = fss.vRefNum;
dirid = fss.parID;
if ( refnum == 0 || dirid == 0 ) /* Fail on nonexistent dir */
return 0;
/*
** We now have the folder parameters. Setup the field for the filename
*/
if ( modnamelen > 54 ) return 0; /* Leave room for extension */
strcpy((char *)fnbuf+1, module);
for( fdp = _PyImport_Filetab+1; fdp->suffix; fdp++ ) {
strcpy((char *)fnbuf+1+modnamelen, fdp->suffix);
fnbuf[0] = strlen((char *)fnbuf+1);
if (Py_VerboseFlag > 1)
fprintf(stderr, "# trying %s%s\n", buf, fdp->suffix);
if ( FSMakeFSSpec(refnum, dirid, fnbuf, &fss) == noErr ) {
/* Found it. */
#if 0
strcpy(buf+*lenp+modnamelen, fdp->suffix);
#else
strcpy(buf+*lenp, fdp->suffix);
#endif
*lenp = strlen(buf);
return fdp;
}
}
return 0;
}
#if 0
int
PyMac_FileExists(char *name)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment