Skip to content

Commit

Permalink
miniterm: change cancel impl. for console, fixes #174
Browse files Browse the repository at this point in the history
the way select was used, was incompatible with the text io
wrapper used by python3 for sys.stdin
  • Loading branch information
zsquareplusc committed Dec 7, 2016
1 parent 0c7077a commit cab3dab
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions serial/tools/miniterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,12 @@ def cancel(self):
elif os.name == 'posix':
import atexit
import termios
import select
import fcntl

class Console(ConsoleBase):
def __init__(self):
super(Console, self).__init__()
self.fd = sys.stdin.fileno()
# an additional pipe is used in getkey, so that the cancel method
# can abort the waiting getkey method
self.pipe_r, self.pipe_w = os.pipe()
self.old = termios.tcgetattr(self.fd)
atexit.register(self.cleanup)
if sys.version_info < (3, 0):
Expand All @@ -159,17 +156,13 @@ def setup(self):
termios.tcsetattr(self.fd, termios.TCSANOW, new)

def getkey(self):
ready, _, _ = select.select([self.enc_stdin, self.pipe_r], [], [], None)
if self.pipe_r in ready:
os.read(self.pipe_r, 1)
return
c = self.enc_stdin.read(1)
if c == unichr(0x7f):
c = unichr(8) # map the BS key (which yields DEL) to backspace
return c

def cancel(self):
os.write(self.pipe_w, b"x")
fcntl.ioctl(self.fd, termios.TIOCSTI, b'\0')

def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
Expand Down

0 comments on commit cab3dab

Please sign in to comment.