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

Skip to content

Commit eda7863

Browse files
committed
Make imports faster on the Mac, by
- Remembering whether sys.path components refer to files or folders, - Using mac-specific code to check for file existence, in stead of trying to fopen() each possible file. These mods need an accompanying mod to import.c.
1 parent 0130f0f commit eda7863

2 files changed

Lines changed: 106 additions & 8 deletions

File tree

Mac/Include/macglue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ void PyMac_HandleEvent Py_PROTO((EventRecord *, int)); /* Handle one event, if p
8181
void PyMac_InitMenuBar(void); /* Setup menu bar as we want it */
8282
void PyMac_RestoreMenuBar(void); /* Restore menu bar for ease of exiting */
8383

84-
int PyMac_FindResourceModule(char *, char *); /* Test for 'PYC ' resource in a file */
84+
int PyMac_FindResourceModule(PyStringObject *, char *, char *); /* Test for 'PYC ' resource in a file */
8585
PyObject * PyMac_LoadResourceModule(char *, char *); /* Load 'PYC ' resource from file */
86+
struct filedescr *PyMac_FindModuleExtension(char *, int *, char *); /* Look for module in single folder */
8687

8788
int PyMac_GetDirectory(FSSpec *dirfss, char *prompt); /* Ask user for a directory */
8889
void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList,

Mac/Python/macglue.c

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4141
#include "macglue.h"
4242
#include "marshal.h"
4343
#include "import.h"
44+
#include "importdl.h"
4445

4546
#include "pythonresources.h"
4647

@@ -609,8 +610,10 @@ PyMac_InitMenuBar()
609610
void
610611
PyMac_RestoreMenuBar()
611612
{
612-
if ( sioux_mbar )
613+
if ( sioux_mbar ) {
613614
SetMenuBar(sioux_mbar);
615+
DrawMenuBar();
616+
}
614617
}
615618

616619

@@ -647,7 +650,8 @@ SIOUXDoAboutBox(void)
647650
** a 'PYC ' resource of the correct name
648651
*/
649652
int
650-
PyMac_FindResourceModule(module, filename)
653+
PyMac_FindResourceModule(obj, module, filename)
654+
PyStringObject *obj;
651655
char *module;
652656
char *filename;
653657
{
@@ -656,7 +660,27 @@ char *filename;
656660
short oldrh, filerh;
657661
int ok;
658662
Handle h;
659-
663+
664+
#ifdef INTERN_STRINGS
665+
/*
666+
** If we have interning find_module takes care of interning all
667+
** sys.path components. We then keep a record of all sys.path
668+
** components for which GetFInfo has failed (usually because the
669+
** component in question is a folder), and we don't try opening these
670+
** as resource files again.
671+
*/
672+
#define MAXPATHCOMPONENTS 32
673+
static PyStringObject *not_a_file[MAXPATHCOMPONENTS];
674+
static int max_not_a_file = 0;
675+
int i;
676+
677+
if ( obj->ob_sinterned ) {
678+
for( i=0; i< max_not_a_file; i++ )
679+
if ( obj == not_a_file[i] )
680+
return 0;
681+
}
682+
#endif /* INTERN_STRINGS */
683+
660684
if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
661685
/*
662686
** Special case: the application itself. Use a shortcut to
@@ -667,10 +691,15 @@ char *filename;
667691
UseResFile(PyMac_AppRefNum);
668692
filerh = -1;
669693
} else {
670-
if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr )
671-
return 0; /* It doesn't exist */
672-
if ( FSpGetFInfo(&fss, &finfo) != noErr )
673-
return 0; /* shouldn't happen, I guess */
694+
if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ||
695+
FSpGetFInfo(&fss, &finfo) != noErr ) {
696+
#ifdef INTERN_STRINGS
697+
if ( max_not_a_file < MAXPATHCOMPONENTS && obj->ob_sinterned )
698+
not_a_file[max_not_a_file++] = obj;
699+
#endif /* INTERN_STRINGS */
700+
/* doesn't exist or is folder */
701+
return 0;
702+
}
674703
oldrh = CurResFile();
675704
filerh = FSpOpenResFile(&fss, fsRdPerm);
676705
if ( filerh == -1 )
@@ -772,6 +801,74 @@ char *filename;
772801
}
773802
}
774803

804+
/*
805+
** Look for a module in a single folder. Upon entry buf and len
806+
** point to the folder to search, upon exit they refer to the full
807+
** pathname of the module found (if any).
808+
*/
809+
struct filedescr *
810+
PyMac_FindModuleExtension(char *buf, int *lenp, char *module)
811+
{
812+
struct filedescr *fdp;
813+
unsigned char fnbuf[64];
814+
int modnamelen = strlen(module);
815+
FSSpec fss;
816+
short refnum;
817+
long dirid;
818+
819+
/*
820+
** Copy the module name to the buffer (already :-terminated)
821+
** We also copy the first suffix, if this matches immedeately we're
822+
** lucky and return immedeately.
823+
*/
824+
if ( !_PyImport_Filetab[0].suffix )
825+
return 0;
826+
827+
strcpy(buf+*lenp, module);
828+
strcpy(buf+*lenp+modnamelen, _PyImport_Filetab[0].suffix);
829+
if ( FSMakeFSSpec(0, 0, Pstring(buf), &fss) == noErr )
830+
return _PyImport_Filetab;
831+
/*
832+
** We cannot check for fnfErr (unfortunately), it can mean either that
833+
** the file doesn't exist (fine, we try others) or the path leading to it.
834+
*/
835+
refnum = fss.vRefNum;
836+
dirid = fss.parID;
837+
if ( refnum == 0 || dirid == 0 ) /* Fail on nonexistent dir */
838+
return 0;
839+
/*
840+
** We now have the folder parameters. Setup the field for the filename
841+
*/
842+
if ( modnamelen > 54 ) return 0; /* Leave room for extension */
843+
strcpy((char *)fnbuf+1, module);
844+
845+
for( fdp = _PyImport_Filetab+1; fdp->suffix; fdp++ ) {
846+
strcpy((char *)fnbuf+1+modnamelen, fdp->suffix);
847+
fnbuf[0] = strlen((char *)fnbuf+1);
848+
if (Py_VerboseFlag > 1)
849+
fprintf(stderr, "# trying %s%s\n", buf, fdp->suffix);
850+
if ( FSMakeFSSpec(refnum, dirid, fnbuf, &fss) == noErr ) {
851+
/* Found it. */
852+
strcpy(buf+*lenp+modnamelen, fdp->suffix);
853+
*lenp = strlen(buf);
854+
return fdp;
855+
}
856+
}
857+
return 0;
858+
}
859+
860+
#if 0
861+
int
862+
PyMac_FileExists(char *name)
863+
{
864+
FSSpec fss;
865+
866+
if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr )
867+
return 1;
868+
return 0;
869+
}
870+
#endif
871+
775872
/*
776873
** Helper routine for GetDirectory
777874
*/

0 commit comments

Comments
 (0)