mercurial/utils/procutil.py
changeset 38526 313a940d49a3
parent 38525 c153f440682f
child 39662 50f46b771921
child 39807 e5724be689b3
equal deleted inserted replaced
38525:c153f440682f 38526:313a940d49a3
   413             time.sleep(0.1)
   413             time.sleep(0.1)
   414         return pid
   414         return pid
   415     finally:
   415     finally:
   416         if prevhandler is not None:
   416         if prevhandler is not None:
   417             signal.signal(signal.SIGCHLD, prevhandler)
   417             signal.signal(signal.SIGCHLD, prevhandler)
       
   418 
       
   419 @contextlib.contextmanager
       
   420 def uninterruptable(warn):
       
   421     """Inhibit SIGINT handling on a region of code.
       
   422 
       
   423     Note that if this is called in a non-main thread, it turns into a no-op.
       
   424 
       
   425     Args:
       
   426       warn: A callable which takes no arguments, and returns True if the
       
   427             previous signal handling should be restored.
       
   428     """
       
   429 
       
   430     oldsiginthandler = [signal.getsignal(signal.SIGINT)]
       
   431     shouldbail = []
       
   432 
       
   433     def disabledsiginthandler(*args):
       
   434         if warn():
       
   435             signal.signal(signal.SIGINT, oldsiginthandler[0])
       
   436             del oldsiginthandler[0]
       
   437         shouldbail.append(True)
       
   438 
       
   439     try:
       
   440         try:
       
   441             signal.signal(signal.SIGINT, disabledsiginthandler)
       
   442         except ValueError:
       
   443             # wrong thread, oh well, we tried
       
   444             del oldsiginthandler[0]
       
   445         yield
       
   446     finally:
       
   447         if oldsiginthandler:
       
   448             signal.signal(signal.SIGINT, oldsiginthandler[0])
       
   449         if shouldbail:
       
   450             raise KeyboardInterrupt