[Python-checkins] CVS: python/dist/src/Mac/Python macglue.c,1.106,1.107

Jack Jansen jackjansen@users.sourceforge.net
Fri, 09 Nov 2001 16:41:45 -0800


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

Modified Files:
	macglue.c 
Log Message:
Fixed various problems with command-dot handling (some very old):
- Don't scan for cmd-. unless in the foreground
- Scan before switching out to other processes, not after
- don't scan if SchedParams.check_interrupt is false (!)
  - But: do scan if we're blocked on I/O

One problem remains: in the last case KeyboardInterrupt is raised
too late.

Index: macglue.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Python/macglue.c,v
retrieving revision 1.106
retrieving revision 1.107
diff -C2 -d -r1.106 -r1.107
*** macglue.c	2001/11/01 23:17:35	1.106
--- macglue.c	2001/11/10 00:41:43	1.107
***************
*** 299,308 ****
  #endif
  
- 	if (interrupted) return -1;
  
  	if ( msg == SP_AUTO_SPIN )
  		maxsleep = 0;
! 	if ( msg==SP_SLEEP||msg==SP_SELECT )
  		maxsleep = arg;
  
  	PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */
--- 299,317 ----
  #endif
  
  
  	if ( msg == SP_AUTO_SPIN )
  		maxsleep = 0;
! 	if ( msg==SP_SLEEP||msg==SP_SELECT ) {
  		maxsleep = arg;
+ 		/*
+ 		** We force-scan for interrupts. Not pretty, but otherwise
+ 		** a program may hang in select or sleep forever.
+ 		*/
+ 		scan_event_queue(1);
+ 	}
+ 	if (interrupted) {
+ 		interrupted = 0;
+ 		return -1;
+ 	}
  
  	PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */
***************
*** 454,469 ****
  }
  
  /*
  ** This routine scans the event queue looking for cmd-.
- ** This is the only way to get an interrupt under THINK (since it
- ** doesn't do SIGINT handling), but is also used under MW, when
- ** the full-fledged event loop is disabled. This way, we can at least
- ** interrupt a runaway python program.
  */
  static void
! scan_event_queue(flush)
! 	int flush;
  {
  #if !TARGET_API_MAC_OS8
  	if ( CheckEventQueueForUserCancel() )
  		interrupted = 1;
--- 463,496 ----
  }
  
+ /* Check whether we are in the foreground */
+ static int
+ PyMac_InForeground(void)
+ {
+ 	static ProcessSerialNumber ours;
+ 	static inited;
+ 	ProcessSerialNumber curfg;
+ 	Boolean eq;
+ 	
+ 	if ( inited == 0 ) {
+ 		(void)GetCurrentProcess(&ours);
+ 		inited = 1;
+ 	}
+ 	if ( GetFrontProcess(&curfg) < 0 )
+ 		eq = 1;
+ 	else if ( SameProcess(&ours, &curfg, &eq) < 0 )
+ 		eq = 1;
+ 	return (int)eq;
+ }
+ 
  /*
  ** This routine scans the event queue looking for cmd-.
  */
  static void
! scan_event_queue(force)
! 	int force;
  {
  #if !TARGET_API_MAC_OS8
+ 	if ( interrupted || (!schedparams.check_interrupt && !force) )
+ 		return;
  	if ( CheckEventQueueForUserCancel() )
  		interrupted = 1;
***************
*** 471,474 ****
--- 498,503 ----
  	register EvQElPtr q;
  	
+ 	if ( interrupted || (!schedparams.check_interrupt && !force) || !PyMac_InForeground() )
+ 		return;
  	q = (EvQElPtr) LMGetEventQueue()->qHead;
  	
***************
*** 489,504 ****
  PyErr_CheckSignals()
  {
  	if (schedparams.enabled) {
! 		if ( (unsigned long)LMGetTicks() > schedparams.next_check ) {
! 			if ( PyMac_Yield() < 0)
! 				return -1;
! 			schedparams.next_check = (unsigned long)LMGetTicks()
! 					 + schedparams.check_interval;
  			if (interrupted) {
- 				scan_event_queue(1);	/* Eat events up to cmd-. */
  				interrupted = 0;
  				PyErr_SetNone(PyExc_KeyboardInterrupt);
  				return -1;
  			}
  		}
  	}
--- 518,537 ----
  PyErr_CheckSignals()
  {
+ 	int xxx, xxx_old;
+ 	
  	if (schedparams.enabled) {
! 		if ( interrupted || (unsigned long)LMGetTicks() > schedparams.next_check ) {
! 			scan_event_queue(0);
  			if (interrupted) {
  				interrupted = 0;
  				PyErr_SetNone(PyExc_KeyboardInterrupt);
  				return -1;
  			}
+ 			if ( PyMac_Yield() < 0)
+ 				return -1;
+ 			xxx = LMGetTicks();
+ 			xxx_old = schedparams.next_check;
+ 			schedparams.next_check = (unsigned long)LMGetTicks()
+ 					 + schedparams.check_interval;
  		}
  	}
***************
*** 509,534 ****
  PyOS_InterruptOccurred()
  {
! 	scan_event_queue(1);
! 	return interrupted;
! }
! 
! /* Check whether we are in the foreground */
! static int
! PyMac_InForeground(void)
! {
! 	static ProcessSerialNumber ours;
! 	static inited;
! 	ProcessSerialNumber curfg;
! 	Boolean eq;
! 	
! 	if ( inited == 0 ) {
! 		(void)GetCurrentProcess(&ours);
! 		inited = 1;
! 	}
! 	if ( GetFrontProcess(&curfg) < 0 )
! 		eq = 1;
! 	else if ( SameProcess(&ours, &curfg, &eq) < 0 )
! 		eq = 1;
! 	return (int)eq;
  }
  #endif
--- 542,550 ----
  PyOS_InterruptOccurred()
  {
! 	scan_event_queue(0);
! 	if ( !interrupted )
! 		return 0;
! 	interrupted = 0;
! 	return 1;
  }
  #endif
***************
*** 617,629 ****
  	
  	in_here++;
- 	/*
- 	** First check for interrupts, if wanted.
- 	** This sets a flag that will be picked up at an appropriate
- 	** moment in the mainloop.
- 	*/
- 	if (schedparams.check_interrupt)
- 		scan_event_queue(0);
- 	
- 	/* XXXX Implementing an idle routine goes here */
  		
  	/*
--- 633,636 ----