[Python-checkins] CVS: python/dist/src/Modules posixmodule.c,2.182,2.183

Mark Hammond mhammond@users.sourceforge.net
Tue, 30 Jan 2001 23:30:31 -0800


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

Modified Files:
	posixmodule.c 
Log Message:
Partial fix to [ Bug #128685 ] popen on Win9x isnt smart enough about finding w9xpopen.exe.

"Partial" as the code uses sys.prefix in an attempt to locate 'w9xpopen.exe', but sys.prefix is not set if Python can't find it itself.  So this _still_ fails in Pythonwin, but I am committing the patch for 2 reasons:
* Embedded apps that set sys.prefix or use PYTHONHOME will work
* The exception raised on failure to find the executable is far more obvious

Index: posixmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.182
retrieving revision 2.183
diff -C2 -r2.182 -r2.183
*** posixmodule.c	2001/01/31 05:38:47	2.182
--- posixmodule.c	2001/01/31 07:30:29	2.183
***************
*** 2290,2294 ****
  }
  
! static int
  _PyPopenCreateProcess(char *cmdstring,
  		      HANDLE hStdin,
--- 2290,2294 ----
  }
  
! static BOOL
  _PyPopenCreateProcess(char *cmdstring,
  		      HANDLE hStdin,
***************
*** 2300,2304 ****
  	STARTUPINFO siStartInfo;
  	char *s1,*s2, *s3 = " /c ";
! 	const char *szConsoleSpawn = "w9xpopen.exe \"";
  	int i;
  	int x;
--- 2300,2304 ----
  	STARTUPINFO siStartInfo;
  	char *s1,*s2, *s3 = " /c ";
! 	const char *szConsoleSpawn = "w9xpopen.exe";
  	int i;
  	int x;
***************
*** 2322,2326 ****
  			 * KB: Q150956
  			 */
! 			char modulepath[256];
  			GetModuleFileName(NULL, modulepath, sizeof(modulepath));
  			for (i = x = 0; modulepath[i]; i++)
--- 2322,2327 ----
  			 * KB: Q150956
  			 */
! 			char modulepath[_MAX_PATH];
! 			struct stat statinfo;
  			GetModuleFileName(NULL, modulepath, sizeof(modulepath));
  			for (i = x = 0; modulepath[i]; i++)
***************
*** 2328,2341 ****
  					x = i+1;
  			modulepath[x] = '\0';
  			x = i + strlen(s3) + strlen(cmdstring) + 1 +
  				strlen(modulepath) + 
  				strlen(szConsoleSpawn) + 1;
  			s2 = (char *)_alloca(x);
  			ZeroMemory(s2, x);
  			sprintf(
  				s2,
! 				"%s%s%s%s%s\"",
  				modulepath,
- 				szConsoleSpawn,
  				s1,
  				s3,
--- 2329,2374 ----
  					x = i+1;
  			modulepath[x] = '\0';
+ 			/* Create the full-name to w9xpopen, so we can test it exists */
+ 			strncat(modulepath, 
+ 			        szConsoleSpawn, 
+ 			        (sizeof(modulepath)/sizeof(modulepath[0]))
+ 			               -strlen(modulepath));
+ 			if (stat(modulepath, &statinfo) != 0) {
+ 				/* Eeek - file-not-found - possibly an embedding 
+ 				   situation - see if we can locate it in sys.prefix 
+ 				*/
+ 				strncpy(modulepath, 
+ 				        Py_GetExecPrefix(), 
+ 				        sizeof(modulepath)/sizeof(modulepath[0]));
+ 				if (modulepath[strlen(modulepath)-1] != '\\')
+ 					strcat(modulepath, "\\");
+ 				strncat(modulepath, 
+ 				        szConsoleSpawn, 
+ 				        (sizeof(modulepath)/sizeof(modulepath[0]))
+ 				               -strlen(modulepath));
+ 				/* No where else to look - raise an easily identifiable
+ 				   error, rather than leaving Windows to report
+ 				   "file not found" - as the user is probably blissfully
+ 				   unaware this shim EXE is used, and it will confuse them.
+ 				   (well, it confused me for a while ;-)
+ 				*/
+ 				if (stat(modulepath, &statinfo) != 0) {
+ 					PyErr_Format(PyExc_RuntimeError, 
+ 					    "Can not locate '%s' which is needed "
+ 					    "for popen to work on this platform.",
+ 					    szConsoleSpawn);
+ 					return FALSE;
+ 				}
+ 			}
  			x = i + strlen(s3) + strlen(cmdstring) + 1 +
  				strlen(modulepath) + 
  				strlen(szConsoleSpawn) + 1;
+ 
  			s2 = (char *)_alloca(x);
  			ZeroMemory(s2, x);
  			sprintf(
  				s2,
! 				"%s \"%s%s%s\"",
  				modulepath,
  				s1,
  				s3,
***************
*** 2346,2351 ****
  	/* Could be an else here to try cmd.exe / command.com in the path
  	   Now we'll just error out.. */
! 	else
! 		return -1;
    
  	ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
--- 2379,2386 ----
  	/* Could be an else here to try cmd.exe / command.com in the path
  	   Now we'll just error out.. */
! 	else {
! 		PyErr_SetString(PyExc_RuntimeError, "Can not locate a COMSPEC environment variable to use as the shell");
! 		return FALSE;
! 	}
    
  	ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
***************
*** 2374,2377 ****
--- 2409,2413 ----
  		return TRUE;
  	}
+ 	win32_error("CreateProcess", NULL);
  	return FALSE;
  }
***************
*** 2565,2569 ****
  					    hChildStdoutWr,
  					    &hProcess))
! 			 return win32_error("CreateProcess", NULL);
  	 }
  	 else {
--- 2601,2605 ----
  					    hChildStdoutWr,
  					    &hProcess))
! 			 return NULL;
  	 }
  	 else {
***************
*** 2573,2577 ****
  					    hChildStderrWr,
  					    &hProcess))
! 			 return win32_error("CreateProcess", NULL);
  	 }
  
--- 2609,2613 ----
  					    hChildStderrWr,
  					    &hProcess))
! 			 return NULL;
  	 }