Skip to content

Commit

Permalink
Closes issue #86 and issue #100
Browse files Browse the repository at this point in the history
Fallback to using stdout, and, when both stdin
and stdout are *both* closed, catch ValueError
and use the same constants as when the attached
process is not a terminal.
  • Loading branch information
jquast committed Aug 24, 2014
1 parent 8d96042 commit e2ff2f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
11 changes: 8 additions & 3 deletions pexpect/__init__.py
Expand Up @@ -498,12 +498,17 @@ def __init__(self, command, args=[], timeout=30, maxread=2000,
# inherit EOF and INTR definitions from controlling process.
try:
from termios import VEOF, VINTR
fd = sys.__stdin__.fileno()
try:
fd = sys.__stdin__.fileno()
except ValueError:
# ValueError: I/O operation on closed file
fd = sys.__stdout__.fileno()
self._INTR = ord(termios.tcgetattr(fd)[6][VINTR])
self._EOF = ord(termios.tcgetattr(fd)[6][VEOF])
except (ImportError, OSError, IOError, termios.error):
except (ImportError, OSError, IOError, ValueError, termios.error):
# unless the controlling process is also not a terminal,
# such as cron(1). Fall-back to using CEOF and CINTR.
# such as cron(1), or when stdin and stdout are both closed.
# Fall-back to using CEOF and CINTR. There
try:
from termios import CEOF, CINTR
(self._INTR, self._EOF) = (CINTR, CEOF)
Expand Down
37 changes: 36 additions & 1 deletion tests/test_expect.py
Expand Up @@ -18,11 +18,13 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
import multiprocessing
import unittest
import subprocess
import time
import signal
import sys
import os

import pexpect
from . import PexpectTestCase
Expand Down Expand Up @@ -542,7 +544,40 @@ def noop(x, y):
signal.alarm(1)
p1.expect('END')

def test_stdin_closed(self):
'''
Ensure pexpect continues to operate even when stdin is closed
'''
class Closed_stdin_proc(multiprocessing.Process):
def run(self):
sys.__stdin__.close()
cat = pexpect.spawn('cat')
cat.sendeof()
cat.expect(pexpect.EOF)

proc = Closed_stdin_proc()
proc.start()
proc.join()
assert proc.exitcode == 0

def test_stdin_stdout_closed(self):
'''
Ensure pexpect continues to operate even when stdin and stdout is closed
'''
class Closed_stdin_stdout_proc(multiprocessing.Process):
def run(self):
sys.__stdin__.close()
sys.__stdout__.close()
cat = pexpect.spawn('cat')
cat.sendeof()
cat.expect(pexpect.EOF)

proc = Closed_stdin_stdout_proc()
proc.start()
proc.join()
assert proc.exitcode == 0

if __name__ == '__main__':
unittest.main()

suite = unittest.makeSuite(ExpectTestCase,'test')
suite = unittest.makeSuite(ExpectTestCase, 'test')

0 comments on commit e2ff2f4

Please sign in to comment.