Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e2260f

Browse files
committed
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.
1 parent f2f3e9b commit 1e2260f

2 files changed

Lines changed: 416 additions & 224 deletions

File tree

Mac/Python/macglue.c

Lines changed: 0 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -685,230 +685,6 @@ SIOUXDoAboutBox(void)
685685
DisposeDialog(theDialog);
686686
}
687687

688-
/*
689-
** Returns true if the argument has a resource fork, and it contains
690-
** a 'PYC ' resource of the correct name
691-
*/
692-
int
693-
PyMac_FindResourceModule(obj, module, filename)
694-
PyStringObject *obj;
695-
char *module;
696-
char *filename;
697-
{
698-
FSSpec fss;
699-
FInfo finfo;
700-
short oldrh, filerh;
701-
int ok;
702-
Handle h;
703-
704-
#ifdef INTERN_STRINGS
705-
/*
706-
** If we have interning find_module takes care of interning all
707-
** sys.path components. We then keep a record of all sys.path
708-
** components for which GetFInfo has failed (usually because the
709-
** component in question is a folder), and we don't try opening these
710-
** as resource files again.
711-
*/
712-
#define MAXPATHCOMPONENTS 32
713-
static PyStringObject *not_a_file[MAXPATHCOMPONENTS];
714-
static int max_not_a_file = 0;
715-
int i;
716-
717-
if ( obj->ob_sinterned ) {
718-
for( i=0; i< max_not_a_file; i++ )
719-
if ( obj == not_a_file[i] )
720-
return 0;
721-
}
722-
#endif /* INTERN_STRINGS */
723-
724-
if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
725-
/*
726-
** Special case: the application itself. Use a shortcut to
727-
** forestall opening and closing the application numerous times
728-
** (which is dead slow when running from CDROM)
729-
*/
730-
oldrh = CurResFile();
731-
UseResFile(PyMac_AppRefNum);
732-
filerh = -1;
733-
} else {
734-
if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ||
735-
FSpGetFInfo(&fss, &finfo) != noErr ) {
736-
#ifdef INTERN_STRINGS
737-
if ( max_not_a_file < MAXPATHCOMPONENTS && obj->ob_sinterned )
738-
not_a_file[max_not_a_file++] = obj;
739-
#endif /* INTERN_STRINGS */
740-
/* doesn't exist or is folder */
741-
return 0;
742-
}
743-
oldrh = CurResFile();
744-
filerh = FSpOpenResFile(&fss, fsRdPerm);
745-
if ( filerh == -1 )
746-
return 0;
747-
UseResFile(filerh);
748-
}
749-
SetResLoad(0);
750-
h = Get1NamedResource('PYC ', Pstring(module));
751-
SetResLoad(1);
752-
ok = (h != NULL);
753-
if ( filerh != -1 )
754-
CloseResFile(filerh);
755-
UseResFile(oldrh);
756-
return ok;
757-
}
758-
759-
/*
760-
** Load the specified module from a resource
761-
*/
762-
PyObject *
763-
PyMac_LoadResourceModule(module, filename)
764-
char *module;
765-
char *filename;
766-
{
767-
FSSpec fss;
768-
FInfo finfo;
769-
short oldrh, filerh;
770-
Handle h;
771-
OSErr err;
772-
PyObject *m, *co;
773-
long num, size;
774-
775-
if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
776-
/*
777-
** Special case: the application itself. Use a shortcut to
778-
** forestall opening and closing the application numerous times
779-
** (which is dead slow when running from CDROM)
780-
*/
781-
oldrh = CurResFile();
782-
UseResFile(PyMac_AppRefNum);
783-
filerh = -1;
784-
} else {
785-
if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr )
786-
goto error;
787-
if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
788-
goto error;
789-
oldrh = CurResFile();
790-
filerh = FSpOpenResFile(&fss, fsRdPerm);
791-
if ( filerh == -1 ) {
792-
err = ResError();
793-
goto error;
794-
}
795-
UseResFile(filerh);
796-
}
797-
h = Get1NamedResource('PYC ', Pstring(module));
798-
if ( h == NULL ) {
799-
err = ResError();
800-
goto error;
801-
}
802-
HLock(h);
803-
/*
804-
** XXXX The next few lines are intimately tied to the format of pyc
805-
** files. I'm not sure whether this code should be here or in import.c -- Jack
806-
*/
807-
size = GetHandleSize(h);
808-
if ( size < 8 ) {
809-
PyErr_SetString(PyExc_ImportError, "Resource too small");
810-
co = NULL;
811-
} else {
812-
num = (*h)[0] & 0xff;
813-
num = num | (((*h)[1] & 0xff) << 8);
814-
num = num | (((*h)[2] & 0xff) << 16);
815-
num = num | (((*h)[3] & 0xff) << 24);
816-
if ( num != PyImport_GetMagicNumber() ) {
817-
PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource");
818-
co = NULL;
819-
} else {
820-
co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
821-
}
822-
}
823-
HUnlock(h);
824-
if ( filerh != -1 )
825-
CloseResFile(filerh);
826-
UseResFile(oldrh);
827-
if ( co ) {
828-
m = PyImport_ExecCodeModule(module, co);
829-
Py_DECREF(co);
830-
} else {
831-
m = NULL;
832-
}
833-
if (Py_VerboseFlag)
834-
fprintf(stderr, "import %s # pyc resource from %s\n",
835-
module, filename);
836-
return m;
837-
error:
838-
{
839-
char buf[512];
840-
841-
sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
842-
PyErr_SetString(PyExc_ImportError, buf);
843-
return NULL;
844-
}
845-
}
846-
847-
/*
848-
** Look for a module in a single folder. Upon entry buf and len
849-
** point to the folder to search, upon exit they refer to the full
850-
** pathname of the module found (if any).
851-
*/
852-
struct filedescr *
853-
PyMac_FindModuleExtension(char *buf, int *lenp, char *module)
854-
{
855-
struct filedescr *fdp;
856-
unsigned char fnbuf[64];
857-
int modnamelen = strlen(module);
858-
FSSpec fss;
859-
short refnum;
860-
long dirid;
861-
862-
/*
863-
** Copy the module name to the buffer (already :-terminated)
864-
** We also copy the first suffix, if this matches immedeately we're
865-
** lucky and return immedeately.
866-
*/
867-
if ( !_PyImport_Filetab[0].suffix )
868-
return 0;
869-
870-
#if 0
871-
/* Pre 1.5a4 */
872-
strcpy(buf+*lenp, module);
873-
strcpy(buf+*lenp+modnamelen, _PyImport_Filetab[0].suffix);
874-
#else
875-
strcpy(buf+*lenp, _PyImport_Filetab[0].suffix);
876-
#endif
877-
if ( FSMakeFSSpec(0, 0, Pstring(buf), &fss) == noErr )
878-
return _PyImport_Filetab;
879-
/*
880-
** We cannot check for fnfErr (unfortunately), it can mean either that
881-
** the file doesn't exist (fine, we try others) or the path leading to it.
882-
*/
883-
refnum = fss.vRefNum;
884-
dirid = fss.parID;
885-
if ( refnum == 0 || dirid == 0 ) /* Fail on nonexistent dir */
886-
return 0;
887-
/*
888-
** We now have the folder parameters. Setup the field for the filename
889-
*/
890-
if ( modnamelen > 54 ) return 0; /* Leave room for extension */
891-
strcpy((char *)fnbuf+1, module);
892-
893-
for( fdp = _PyImport_Filetab+1; fdp->suffix; fdp++ ) {
894-
strcpy((char *)fnbuf+1+modnamelen, fdp->suffix);
895-
fnbuf[0] = strlen((char *)fnbuf+1);
896-
if (Py_VerboseFlag > 1)
897-
fprintf(stderr, "# trying %s%s\n", buf, fdp->suffix);
898-
if ( FSMakeFSSpec(refnum, dirid, fnbuf, &fss) == noErr ) {
899-
/* Found it. */
900-
#if 0
901-
strcpy(buf+*lenp+modnamelen, fdp->suffix);
902-
#else
903-
strcpy(buf+*lenp, fdp->suffix);
904-
#endif
905-
*lenp = strlen(buf);
906-
return fdp;
907-
}
908-
}
909-
return 0;
910-
}
911-
912688
#if 0
913689
int
914690
PyMac_FileExists(char *name)

0 commit comments

Comments
 (0)