diff --git a/pyrepl/unix_console.py b/pyrepl/unix_console.py index 281c200..afd107d 100644 --- a/pyrepl/unix_console.py +++ b/pyrepl/unix_console.py @@ -354,7 +354,11 @@ def move_cursor(self, x, y): def prepare(self): # per-readline preparations: - self.__svtermstate = tcgetattr(self.input_fd) + try: + self.__svtermstate = tcgetattr(self.input_fd) + except termios.error as exc: + raise EOFError("could not prepare fd %d: %s" % (self.input_fd, exc)) + self._prepared = True raw = self.__svtermstate.copy() raw.iflag |= termios.ICRNL raw.iflag &= ~(termios.BRKINT | termios.INPCK | @@ -386,6 +390,9 @@ def prepare(self): pass def restore(self): + if not hasattr(self, '_prepared'): + return + del self._prepared self.__maybe_write_code(self._rmkx) self.flushoutput() tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate) diff --git a/testing/test_unix_console.py b/testing/test_unix_console.py new file mode 100644 index 0000000..51bf874 --- /dev/null +++ b/testing/test_unix_console.py @@ -0,0 +1,12 @@ +import pytest + + +def test_eoferror(): + from pyrepl.unix_console import UnixConsole + + console = UnixConsole(f_in=99) + with pytest.raises( + EOFError, + match="^could not prepare fd 99: .*Bad file descriptor" + ): + console.prepare()