mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	add explicit support for cancelling a running script (CFM-based MacPython had this built-in)
This commit is contained in:
		
							parent
							
								
									476736eed6
								
							
						
					
					
						commit
						eb64af9201
					
				
					 1 changed files with 25 additions and 2 deletions
				
			
		| 
						 | 
					@ -619,16 +619,39 @@ class Editor(W.Window):
 | 
				
			||||||
			savedir = os.getcwd()
 | 
								savedir = os.getcwd()
 | 
				
			||||||
			os.chdir(dir)
 | 
								os.chdir(dir)
 | 
				
			||||||
			sys.path.insert(0, dir)
 | 
								sys.path.insert(0, dir)
 | 
				
			||||||
		else:
 | 
							self._scriptDone = False
 | 
				
			||||||
			cwdindex = None
 | 
							if sys.platform == "darwin":
 | 
				
			||||||
 | 
								# On MacOSX, MacPython doesn't poll for command-period
 | 
				
			||||||
 | 
								# (cancel), so to enable the user to cancel a running
 | 
				
			||||||
 | 
								# script, we have to spawn a thread which does the
 | 
				
			||||||
 | 
								# polling. It will send a SIGINT to the main thread
 | 
				
			||||||
 | 
								# (in which the script is running) when the user types
 | 
				
			||||||
 | 
								# command-period.
 | 
				
			||||||
 | 
								from threading import Thread
 | 
				
			||||||
 | 
								t = Thread(target=self._userCancelledMonitor,
 | 
				
			||||||
 | 
										name="UserCancelledMonitor")
 | 
				
			||||||
 | 
								t.start()
 | 
				
			||||||
		try:
 | 
							try:
 | 
				
			||||||
			execstring(pytext, globals, locals, file, self.debugging, 
 | 
								execstring(pytext, globals, locals, file, self.debugging, 
 | 
				
			||||||
					modname, self.profiling)
 | 
										modname, self.profiling)
 | 
				
			||||||
		finally:
 | 
							finally:
 | 
				
			||||||
 | 
								self._scriptDone = True
 | 
				
			||||||
			if self.path:
 | 
								if self.path:
 | 
				
			||||||
				os.chdir(savedir)
 | 
									os.chdir(savedir)
 | 
				
			||||||
				del sys.path[0]
 | 
									del sys.path[0]
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						def _userCancelledMonitor(self):
 | 
				
			||||||
 | 
							import time
 | 
				
			||||||
 | 
							from signal import SIGINT
 | 
				
			||||||
 | 
							while not self._scriptDone:
 | 
				
			||||||
 | 
								if Evt.CheckEventQueueForUserCancel():
 | 
				
			||||||
 | 
									# Send a SIGINT signal to ourselves.
 | 
				
			||||||
 | 
									# This gets delivered to the main thread,
 | 
				
			||||||
 | 
									# cancelling the running script.
 | 
				
			||||||
 | 
									os.kill(os.getpid(), SIGINT)
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								time.sleep(0.25)
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	def getenvironment(self):
 | 
						def getenvironment(self):
 | 
				
			||||||
		if self.path:
 | 
							if self.path:
 | 
				
			||||||
			file = self.path
 | 
								file = self.path
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue