Skip to content
Snippets Groups Projects
Commit eb7de3e6 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(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.
parent 51dbc0ea
Branches
Tags
No related merge requests found
import atexit
import sys import sys
import tty import tty
import termios import termios
...@@ -8,25 +9,42 @@ __all__ = [ ...@@ -8,25 +9,42 @@ __all__ = [
"user", "user",
"RAW", "RAW",
"CBREAK", "CBREAK",
"COOKED",
] ]
RAW = 0 RAW = 0
CBREAK = 1 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): class user(pcl_expect.expectable):
def __init__(self, mode = CBREAK): def __init__(self, mode = None):
fd = sys.stdin.fileno() fd = sys.stdin.fileno()
self.__mode = termios.tcgetattr(fd)
if mode == COOKED: if mode == CBREAK:
# Let's hope the fd already is in cooked mode... _save()
pass
elif mode == CBREAK:
tty.setcbreak(fd) tty.setcbreak(fd)
elif mode == RAW: elif mode == RAW:
_save()
tty.setraw(fd) tty.setraw(fd)
else: elif mode != None:
raise pcl_expect.BadArgs() raise pcl_expect.BadArgs()
pcl_expect.expectable.__init__(self, fd) pcl_expect.expectable.__init__(self, fd)
...@@ -36,4 +54,5 @@ class user(pcl_expect.expectable): ...@@ -36,4 +54,5 @@ class user(pcl_expect.expectable):
sys.stdout.flush() sys.stdout.flush()
def close(self): def close(self):
termios.tcsetattr(self.fileno(), termios.TCSAFLUSH, self.__mode) pcl_expect.expectable.close(self)
_restore()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment