diff upreckon/unix.py @ 218:65b5c9390010

With _unix, Escape presses now cancel test data unarchiving
author Oleg Oshmyan <chortos@inbox.lv>
date Mon, 22 Aug 2011 22:34:09 +0300
parents d5b6708c1955
children d9eb7d958b6d
line wrap: on
line diff
--- a/upreckon/unix.py	Sun Aug 21 01:24:29 2011 +0300
+++ b/upreckon/unix.py	Mon Aug 22 22:34:09 2011 +0300
@@ -308,3 +308,21 @@
 			tty.setcbreak(sys.stdin.fileno())
 			def pause():
 				sys.stdin.read(1)
+else:
+	try:
+		from signal import signal, SIGIO, SIG_DFL
+		from select import select
+	except ImportError:
+		pass
+	else:
+		def sigio_handler(signum, frame):
+			if select((sys.stdin,), (), (), 0)[0]:
+				if os.read(sys.stdin.fileno(), 1) == '\33'.encode('ascii'):
+					remove_escape_handler()
+					raise CanceledByUser
+		def install_escape_handler():
+			signal(SIGIO, sigio_handler)
+			sigio_handler(SIGIO, None)
+		def remove_escape_handler():
+			signal(SIGIO, SIG_DFL)
+		__all__ += 'install_escape_handler', 'remove_escape_handler'