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

Fredrik Lundh python-dev@python.org
Wed, 26 Jul 2000 10:29:15 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv13581/Modules

Modified Files:
	posixmodule.c 
Log Message:


- changed windows pclose to make sure we don't return before the
  underlying process has terminated
  (bug fix from David Bolen)


Index: posixmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.161
retrieving revision 2.162
diff -C2 -r2.161 -r2.162
*** posixmodule.c	2000/07/24 16:06:23	2.161
--- posixmodule.c	2000/07/26 17:29:12	2.162
***************
*** 2596,2604 ****
  static int _PyPclose(FILE *file)
  {
! 	int result = 0;
  	DWORD exit_code;
  	HANDLE hProcess;
  	PyObject *hProcessObj, *fileObj;
     
  	if (_PyPopenProcs) {
  		fileObj = PyLong_FromVoidPtr(file);
--- 2596,2609 ----
  static int _PyPclose(FILE *file)
  {
! 	int result;
  	DWORD exit_code;
  	HANDLE hProcess;
  	PyObject *hProcessObj, *fileObj;
     
+ 	/* Close the file handle first, to ensure it can't block the
+ 	 * child from exiting when we wait for it below.
+ 	 */
+ 	result = fclose(file);
+ 
  	if (_PyPopenProcs) {
  		fileObj = PyLong_FromVoidPtr(file);
***************
*** 2607,2624 ****
  			if (hProcessObj) {
  				hProcess = PyLong_AsVoidPtr(hProcessObj);
! 				if (GetExitCodeProcess(hProcess, &exit_code)) {
  					/* Possible truncation here in 16-bit environments, but
  					 * real exit codes are just the lower byte in any event.
  					 */
  					result = exit_code;
- 					if (result == STILL_ACTIVE)
- 						result = 0; /* Minimize confusion */
  				} else {
! 					/* No good way to bubble up an error, so instead we just
! 					 * return the Windows last error shifted above standard
! 					 * exit codes.	This will truncate in 16-bits but should
! 					 * be fine in 32 and at least distinguishes the problem.
  					 */
! 					result = (GetLastError() << 8);
  				}
  
--- 2612,2636 ----
  			if (hProcessObj) {
  				hProcess = PyLong_AsVoidPtr(hProcessObj);
! 				if (result != EOF &&
! 				    WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
! 				    GetExitCodeProcess(hProcess, &exit_code)) {
  					/* Possible truncation here in 16-bit environments, but
  					 * real exit codes are just the lower byte in any event.
  					 */
  					result = exit_code;
  				} else {
! 					/* Indicate failure - this will cause the file object
! 					 * to raise an I/O error and translate the last Win32
! 					 * error code from errno.  We do have a problem with
! 					 * last errors that overlap the normal errno table,
! 					 * but that's a consistent problem with the file object.
  					 */
! 					if (result != EOF) {
! 						/* If the error wasn't from the fclose(), then
! 						 * set errno for the file object error handling.
! 						 */
! 						errno = GetLastError();
! 					}
! 					result = -1;
  				}
  
***************
*** 2636,2640 ****
  	} /* if _PyPopenProcs */
  
- 	fclose(file);
  	return result;
  }
--- 2648,2651 ----