diff --git a/ptyprocess/ptyprocess.py b/ptyprocess/ptyprocess.py index daf20b1..f1d1c0f 100644 --- a/ptyprocess/ptyprocess.py +++ b/ptyprocess/ptyprocess.py @@ -430,7 +430,12 @@ def isatty(self): methods such as setecho(), setwinsize(), getwinsize() may raise an IOError. ''' - return os.isatty(self.fd) + if self.closed or self.fd < 0: + return False + try: + return os.isatty(self.fd) + except OSError: + return False def waitnoecho(self, timeout=None): '''Wait until the terminal ECHO flag is set False. diff --git a/tests/test_spawn.py b/tests/test_spawn.py index 8aefff1..ce90f16 100755 --- a/tests/test_spawn.py +++ b/tests/test_spawn.py @@ -152,3 +152,9 @@ def test_pass_fds(self): with open(temp_file_name, 'r') as temp_file_r: assert temp_file_r.read() == 'hello' + + def test_isatty_when_closed(self): + p = PtyProcess.spawn(['true']) + self.assertTrue(p.isatty()) + p.close() + self.assertFalse(p.isatty())