[Python-checkins] CVS: python/dist/src/Python import.c,2.169,2.170

Tim Peters tim_one@users.sourceforge.net
Thu, 01 Mar 2001 10:12:03 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv11859/python/dist/src/python

Modified Files:
	import.c 
Log Message:
More MacOSX fiddling.  As noted in a comment, I believe all variations
of these "search the directory" schemes (including this one) are still prone
to making mistakes.


Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.169
retrieving revision 2.170
diff -C2 -r2.169 -r2.170
*** import.c	2001/03/01 08:47:29	2.169
--- import.c	2001/03/01 18:12:00	2.170
***************
*** 985,995 ****
  }
  
! /* case_ok(buf, len, namelen, name)
!  * We've already done a successful stat() or fopen() on buf (a path of length
!  * len, exclusive of trailing null).  name is the last component of that path
!  * (a string of length namelen, exclusive of trailing null).
   * case_ok() is to return 1 if there's a case-sensitive match for
   * name, else 0.  case_ok() is also to return 1 if envar PYTHONCASEOK
   * exists.
   * case_ok() is used to implement case-sensitive import semantics even
   * on platforms with case-insensitive filesystems.  It's trivial to implement
--- 985,1006 ----
  }
  
! /* case_ok(char* buf, int len, int namelen, char* name)
!  * The arguments here are tricky, best shown by example:
!  *    /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
!  *    ^                      ^                   ^    ^
!  *    |--------------------- buf ---------------------|
!  *    |------------------- len ------------------|
!  *                           |------ name -------|
!  *                           |----- namelen -----|
!  * buf is the full path, but len only counts up to (& exclusive of) the
!  * extension.  name is the module name, also exclusive of extension.
!  *
!  * We've already done a successful stat() or fopen() on buf, so know that
!  * there's some match, possibly case-insensitive.
!  *
   * case_ok() is to return 1 if there's a case-sensitive match for
   * name, else 0.  case_ok() is also to return 1 if envar PYTHONCASEOK
   * exists.
+  *
   * case_ok() is used to implement case-sensitive import semantics even
   * on platforms with case-insensitive filesystems.  It's trivial to implement
***************
*** 1016,1020 ****
  #endif
  
! #elif defined(__MACH__) && defined(__APPLE__)
  #include <sys/types.h>
  #include <dirent.h>
--- 1027,1031 ----
  #endif
  
! #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
  #include <sys/types.h>
  #include <dirent.h>
***************
*** 1108,1133 ****
  	       strncmp(name, (char *)fss.name+1, namelen) == 0;
  
! /* new-fangled macintosh */
! #elif defined(__MACH__) && defined(__APPLE__)
  	DIR *dirp;
  	struct dirent *dp;
! 	char pathname[MAXPATHLEN + 1];
! 	const int pathlen = len - namelen - 1; /* don't want trailing SEP */
  
  	if (getenv("PYTHONCASEOK") != NULL)
  		return 1;
  
! 	/* Copy the path component into pathname; substitute "." if empty */
! 	if (pathlen <= 0) {
! 		pathname[0] = '.';
! 		pathname[1] = '\0';
  	}
  	else {
! 		assert(pathlen <= MAXPATHLEN);
! 		memcpy(pathname, buf, pathlen);
! 		pathname[pathlen] = '\0';
  	}
  	/* Open the directory and search the entries for an exact match. */
! 	dirp = opendir(pathname);
  	if (dirp) {
  		while ((dp = readdir(dirp)) != NULL) {
--- 1119,1151 ----
  	       strncmp(name, (char *)fss.name+1, namelen) == 0;
  
! /* new-fangled macintosh (macosx)
!  *
!  * XXX This seems prone to obscure errors, like suppose someone does
!  * XXX "import xyz", and in some directory there's both "XYZ.py" and
!  * XXX "xyz.txt".  fopen("xyz.py") will open XYZ.py, but when marching thru
!  * XXX the directory we'll eventually "succeed" on "xyz.txt" because the
!  * XXX extension is never checked.
!  */
! #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
  	DIR *dirp;
  	struct dirent *dp;
! 	char dirname[MAXPATHLEN + 1];
! 	const int dirlen = len - namelen - 1; /* don't want trailing SEP */
  
  	if (getenv("PYTHONCASEOK") != NULL)
  		return 1;
  
! 	/* Copy the dir component into dirname; substitute "." if empty */
! 	if (dirlen <= 0) {
! 		dirname[0] = '.';
! 		dirname[1] = '\0';
  	}
  	else {
! 		assert(dirlen <= MAXPATHLEN);
! 		memcpy(dirname, buf, dirlen);
! 		dirname[dirlen] = '\0';
  	}
  	/* Open the directory and search the entries for an exact match. */
! 	dirp = opendir(dirname);
  	if (dirp) {
  		while ((dp = readdir(dirp)) != NULL) {
***************
*** 1138,1142 ****
  						strlen(dp->d_name);
  #endif
! 			if (thislen == namelen && !strcmp(dp->d_name, name)) {
  				(void)closedir(dirp);
  				return 1; /* Found */
--- 1156,1161 ----
  						strlen(dp->d_name);
  #endif
! 			if (thislen >= namelen &&
! 			    strncmp(dp->d_name, name, namelen) == 0) {
  				(void)closedir(dirp);
  				return 1; /* Found */