From eb7de3e6b7134b71a2548f76fe45110d7732f17b Mon Sep 17 00:00:00 2001 From: Per Cederqvist <ceder@lysator.liu.se> Date: Sun, 19 Oct 2003 11:38:55 +0000 Subject: [PATCH] (COOKED): Removed. (_saved_mode): New variable. (_save): New function. Arrange to call _restore via the atexit module. (_restore): New function. (user.__init__): Default to not modifying the termios setting. Call _save before changing them. (user.close): Use _restore to restore the settings. --- pcl_expect/user.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/pcl_expect/user.py b/pcl_expect/user.py index 2c43ddd..f5f8771 100644 --- a/pcl_expect/user.py +++ b/pcl_expect/user.py @@ -1,3 +1,4 @@ +import atexit import sys import tty import termios @@ -8,25 +9,42 @@ __all__ = [ "user", "RAW", "CBREAK", - "COOKED", ] RAW = 0 CBREAK = 1 -COOKED = 2 + +_saved_mode = None + +def _save(): + global _saved_mode + + if _saved_mode is None: + _saved_mode = termios.tcgetattr(sys.stdin.fileno()) + + atexit.register(_restore) + +def _restore(): + global _saved_mode + + if _saved_mode is not None: + termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, _saved_mode) + _saved_mode = None + + class user(pcl_expect.expectable): - def __init__(self, mode = CBREAK): + def __init__(self, mode = None): + fd = sys.stdin.fileno() - self.__mode = termios.tcgetattr(fd) - if mode == COOKED: - # Let's hope the fd already is in cooked mode... - pass - elif mode == CBREAK: + + if mode == CBREAK: + _save() tty.setcbreak(fd) elif mode == RAW: + _save() tty.setraw(fd) - else: + elif mode != None: raise pcl_expect.BadArgs() pcl_expect.expectable.__init__(self, fd) @@ -36,4 +54,5 @@ class user(pcl_expect.expectable): sys.stdout.flush() def close(self): - termios.tcsetattr(self.fileno(), termios.TCSAFLUSH, self.__mode) + pcl_expect.expectable.close(self) + _restore() -- GitLab