[Python-checkins] python/dist/src/Modules timemodule.c,2.128,2.129

mhammond@users.sourceforge.net mhammond@users.sourceforge.net
Mon, 15 Jul 2002 18:29:21 -0700


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

Modified Files:
	timemodule.c 
Log Message:
Fix bug 581232 - [Windows] Can not interrupt time.sleep()

time.sleep() will now be interrupted on the main thread when Ctrl+C is pressed.  Other threads are never interrupted.

Index: timemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/timemodule.c,v
retrieving revision 2.128
retrieving revision 2.129
diff -C2 -d -r2.128 -r2.129
*** timemodule.c	30 Jun 2002 15:26:09 -0000	2.128
--- timemodule.c	16 Jul 2002 01:29:19 -0000	2.129
***************
*** 29,33 ****
--- 29,50 ----
  #else
  #ifdef MS_WINDOWS
+ #define WIN32_LEAN_AND_MEAN
  #include <windows.h>
+ #include "pythread.h"
+ 
+ /* helper to allow us to interrupt sleep() on Windows*/
+ static HANDLE hInterruptEvent = NULL;
+ static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
+ {
+ 	SetEvent(hInterruptEvent);
+ 	/* allow other default handlers to be called.
+ 	   Default Python handler will setup the
+ 	   KeyboardInterrupt exception.
+ 	*/
+ 	return FALSE;
+ }
+ static long main_thread;
+ 
+ 
  #if defined(__BORLANDC__)
  /* These overrides not needed for Win32 */
***************
*** 681,685 ****
  #endif /* __CYGWIN__ */
  #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
! 
          PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
  	Py_INCREF(&StructTimeType);
--- 698,710 ----
  #endif /* __CYGWIN__ */
  #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
! #ifdef MS_WINDOWS
! 	/* Helper to allow interrupts for Windows.
! 	   If Ctrl+C event delivered while not sleeping
! 	   it will be ignored.
! 	*/
! 	main_thread = PyThread_get_thread_ident();
! 	hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
! 	SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
! #endif /* MS_WINDOWS */
          PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
  	Py_INCREF(&StructTimeType);
***************
*** 776,782 ****
  			return -1;
  		}
- 		/* XXX Can't interrupt this sleep */
  		Py_BEGIN_ALLOW_THREADS
! 		Sleep((unsigned long)millisecs);
  		Py_END_ALLOW_THREADS
  	}
--- 801,825 ----
  			return -1;
  		}
  		Py_BEGIN_ALLOW_THREADS
! 		/* allow sleep(0) to maintain win32 semantics, and as decreed by
! 		   Guido, only the main thread can be interrupted. */
! 		if ((unsigned long)millisecs==0 || main_thread != PyThread_get_thread_ident())
! 			Sleep((unsigned long)millisecs);
! 		else {
! 			DWORD rc;
! 			ResetEvent(hInterruptEvent);
! 			rc = WaitForSingleObject(hInterruptEvent, (unsigned long)millisecs);
! 			if (rc==WAIT_OBJECT_0) {
! 				/* yield to make sure real Python signal handler called */
! 				Sleep(1);
! 				Py_BLOCK_THREADS
! 				/* PyErr_SetFromErrno() does the "right thing" wrt signals
! 				   if errno=EINTR
! 				*/
! 				errno = EINTR;
! 				PyErr_SetFromErrno(PyExc_IOError);
! 				return -1;
! 			}
! 		}
  		Py_END_ALLOW_THREADS
  	}