Skip to content

Commit

Permalink
linuxisms
Browse files Browse the repository at this point in the history
  • Loading branch information
jquast committed Jun 1, 2014
1 parent aa427e0 commit ffd6085
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 48 deletions.
23 changes: 12 additions & 11 deletions pexpect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,10 @@ def _spawn(self, command, args=[]):
self.setwinsize(24, 80)

if not self.echo:
# disable echo on slave_fd for (some) systems *after* fork,
# where it is required by systems that were not handled priori
try:
self.setecho(self.echo)
except:
except OSError:
# Linux, etc. cannot set from child.
pass

if self.ignore_sighup:
Expand All @@ -669,11 +668,12 @@ def _spawn(self, command, args=[]):

# Parent
self.setwinsize(24, 80)
try:
self.setecho(self.echo)
except OSError:
# Solaris, etc. cannot set echo from master
pass
if not self.echo:
try:
self.setecho(self.echo)
except OSError:
# Solaris, etc. cannot set echo from master
pass
self.terminated = False
self.closed = False

Expand All @@ -683,9 +683,10 @@ def _svr4_openpty(self):
Open a pseudo-terminal, returning open fd's for both master and slave.
This is an alternative to the built-in os.openpty() and libc openpty(3),
which does not exist, or work at all, on systems implementing UNIX SVR4
(streams), such as Solaris 8+, Linux, OSF/1, HP-UX, AIX.
This is an alternative to the built-in os.openpty() and libc
openpty(3), which does not exist, or work at all, on systems
implementing UNIX SVR4 (streams), such as Solaris 8+, Linux, OSF/1,
HP-UX, AIX.
These systems do not open each of ``/dev/pty[p-zP-T][0-9a-f]``, seeking
the first sucessfully opened file descriptor (BSD), but, instead open a
Expand Down
110 changes: 73 additions & 37 deletions tests/test_expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,60 +142,66 @@ def _expect_order (self, p):
# ^
want_index = table.index(ONE)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

table = [JUNK, pexpect.TIMEOUT, ONE, TWO, THREE, pexpect.EOF, ]
# ^
want_index = table.index(TWO)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

table = [JUNK, pexpect.TIMEOUT, ONE, TWO, THREE, pexpect.EOF, ]
# ^
want_index = table.index(THREE)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

table = [pexpect.EOF, TWO, THREE, FOUR, ]
# ^
want_index = table.index(FOUR)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

table = [TWO, THREE, FOUR, pexpect.EOF]
# ^
want_index = table.index(pexpect.EOF)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

# it is possible to re-expect EOF multiple times
want_index = table.index(pexpect.EOF)
index = p.expect(table)
assert index == want_index, ('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

def test_waitnoecho(self):

Expand Down Expand Up @@ -329,38 +335,68 @@ def test_expect_index(self):
p = pexpect.spawn('cat', echo=False)
self._expect_index(p)

def test_expect_index_exact (self):
def test_expect_index_exact(self):
'''Like test_expect_index(), but using expect_exact().
'''
p = pexpect.spawn('cat', echo=False)
p.expect = p.expect_exact
self._expect_index(p)

def _expect_index (self, p):
def _expect_index(self, p):
p.sendline(b'ONE')
table = [b'junk', b'JUNK', b'ONE', pexpect.EOF]
want_index = table.index(b'ONE')
self.assertEqual(p.expect(table), want_index)
index = p.expect(table)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

p.sendline(b'TWO')
table = [pexpect.TIMEOUT, b'TWO', b'three', b'four', pexpect.EOF]
want_index = table.index(b'TWO')
self.assertEqual(p.expect(table), want_index)
index = p.expect(table)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

p.sendline(b'THREE')
table = [b'junk', pexpect.TIMEOUT, b'THREE', b'ONE', pexpect.EOF]
want_index = table.index(b'THREE')
self.assertEqual(p.expect(table), want_index)
index = p.expect(table)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

p.sendline(b'going down ...')
table = [b'junk', b'JUNK', b'ONE', pexpect.EOF, pexpect.TIMEOUT]
want_index = table.index(pexpect.TIMEOUT)
self.assertEqual(p.expect(table, timeout=3), want_index)
index = p.expect(table)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

p.sendeof()
table = [b'junk', b'JUNK', b'ONE', pexpect.TIMEOUT, pexpect.EOF]
want_index = table.index(pexpect.EOF)
self.assertEqual(p.expect(table), want_index)
index = p.expect(table)
self.assertEqual(index, want_index,
('got', index, table[index],
'wanted', want_index, table[want_index],
'before', p.before,
'after', p.after,
'buffer', p.buffer))

def test_expect_text_to_subprocess(self):
the_old_way = subprocess.Popen(args=['ls', '-1Sai', '/bin'],
Expand Down

0 comments on commit ffd6085

Please sign in to comment.