Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1842

Show
Ignore:
Timestamp:
01/12/08 23:07:38
Author:
fumanchu
Message:

Moved win32 console ctrl handler into a separate plugin, so that it's able to be disabled for those who 1) aren't working in a console, or 2) aren't using the Bus event loop.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r1837 r1842  
    183183    from cherrypy.restsrv import win32 as _restsrvwin 
    184184    engine = _restsrvwin.Win32Bus() 
     185    _console_control_handler = _restsrvwin.ConsoleCtrlHandler(engine) 
     186    # If you don't want a ConsoleControlHandler, 
     187    # unsubscribe this before calling engine.start(). 
     188    _console_control_handler.subscribe() 
    185189except ImportError: 
    186190    engine = restsrv.bus 
  • trunk/cherrypy/restsrv/win32.py

    r1830 r1842  
    99import win32serviceutil 
    1010 
    11 from cherrypy.restsrv import wspbus 
     11from cherrypy.restsrv import wspbus, plugins 
     12 
     13 
     14class ConsoleCtrlHandler(plugins.SimplePlugin): 
     15    """A WSPBus plugin for handling Win32 console events (like Ctrl-C).""" 
     16     
     17    def __init__(self, bus): 
     18        self.is_set = False 
     19        plugins.SimplePlugin.__init__(self, bus) 
     20     
     21    def start(self): 
     22        if self.is_set: 
     23            return 
     24         
     25        result = win32api.SetConsoleCtrlHandler(self.handle, 1) 
     26        if result == 0: 
     27            self.bus.log('Could not SetConsoleCtrlHandler (error %r)' % 
     28                         win32api.GetLastError()) 
     29        else: 
     30            self.is_set = True 
     31     
     32    def stop(self): 
     33        if not self.is_set: 
     34            return 
     35         
     36        try: 
     37            result = win32api.SetConsoleCtrlHandler(self.handle, 0) 
     38        except ValueError: 
     39            # "ValueError: The object has not been registered" 
     40            result = 1 
     41         
     42        if result == 0: 
     43            self.bus.log('Could not remove SetConsoleCtrlHandler (error %r)' % 
     44                         win32api.GetLastError()) 
     45        else: 
     46            self.is_set = False 
     47     
     48    def handle(self, event): 
     49        """Handle console control events (like Ctrl-C).""" 
     50        if event in (win32con.CTRL_C_EVENT, win32con.CTRL_LOGOFF_EVENT, 
     51                     win32con.CTRL_BREAK_EVENT, win32con.CTRL_SHUTDOWN_EVENT, 
     52                     win32con.CTRL_CLOSE_EVENT): 
     53            self.bus.log('Console event %s: shutting down bus' % event) 
     54             
     55            # Remove self immediately so repeated Ctrl-C doesn't re-call it. 
     56            try: 
     57                self.stop() 
     58            except ValueError: 
     59                pass 
     60             
     61            self.bus.exit() 
     62            # 'First to return True stops the calls' 
     63            return 1 
     64        return 0 
    1265 
    1366 
     
    1669     
    1770    Instead of using time.sleep for blocking, this bus uses native 
    18     win32event objects. 
     71    win32event objects. It also responds to console events. 
    1972    """ 
    2073     
    2174    def __init__(self): 
    2275        self.events = {} 
    23         result = win32api.SetConsoleCtrlHandler(self._console_event, 1) 
    24         if result == 0: 
    25             self.log('Could not SetConsoleCtrlHandler (error %r)' % 
    26                      win32api.GetLastError()) 
    2776        wspbus.Bus.__init__(self) 
    28      
    29     def _console_event(self, event): 
    30         """The handler for console control events (like Ctrl-C).""" 
    31         if event in (win32con.CTRL_C_EVENT, win32con.CTRL_LOGOFF_EVENT, 
    32                      win32con.CTRL_BREAK_EVENT, win32con.CTRL_SHUTDOWN_EVENT, 
    33                      win32con.CTRL_CLOSE_EVENT): 
    34             self.log('Console event %s: shutting down bus' % event) 
    35              
    36             # Remove this CtrlHandler so repeated Ctrl-C doesn't re-call it. 
    37             try: 
    38                 result = win32api.SetConsoleCtrlHandler(self._console_event, 0) 
    39                 if result == 0: 
    40                     self.log('Could not remove SetConsoleCtrlHandler (error %r)' % 
    41                              win32api.GetLastError()) 
    42             except ValueError: 
    43                 pass 
    44              
    45             self.exit() 
    46             # 'First to return True stops the calls' 
    47             return 1 
    48         return 0 
    4977     
    5078    def _get_state_event(self, state): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets