summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Quast <contact@jeffquast.com>2015-04-26 13:35:49 -0700
committerJeff Quast <contact@jeffquast.com>2015-04-26 13:35:49 -0700
commit1ac9cb6cf63687c30e4304b134f88d0cefc5a37f (patch)
tree1b42710624892a8acc46b19019de10a6e440e75a
parent82d4937b73a2fc49824e1f60fa0e036731a03135 (diff)
downloadpexpect-git-1ac9cb6cf63687c30e4304b134f88d0cefc5a37f.tar.gz
Tests cases and documentation to supplement wait()
This matches the same branch of ptyprocess, noexception-on-wait-after-terminate, which allows calling the wait() method multiple times without raising an exception.
-rw-r--r--pexpect/pty_spawn.py9
-rwxr-xr-xtests/test_isalive.py27
2 files changed, 27 insertions, 9 deletions
diff --git a/pexpect/pty_spawn.py b/pexpect/pty_spawn.py
index 0ba1e0c..fe2cc0c 100644
--- a/pexpect/pty_spawn.py
+++ b/pexpect/pty_spawn.py
@@ -614,10 +614,17 @@ class spawn(SpawnBase):
not read any data from the child, so this will block forever if the
child has unread output and has terminated. In other words, the child
may have printed output then called exit(), but, the child is
- technically still alive until its output is read by the parent. '''
+ technically still alive until its output is read by the parent.
+
+ This method is non-blocking if :meth:`wait` has already been called
+ previously or :meth:`isalive` method returns False. It simply returns
+ the previously determined exit status.
+ '''
ptyproc = self.ptyproc
with _wrap_ptyprocess_err():
+ # exception may occur if "Is some other process attempting
+ # "job control with our child pid?"
exitstatus = ptyproc.wait()
self.status = ptyproc.status
self.exitstatus = ptyproc.exitstatus
diff --git a/tests/test_isalive.py b/tests/test_isalive.py
index 5168a52..cd79d09 100755
--- a/tests/test_isalive.py
+++ b/tests/test_isalive.py
@@ -25,22 +25,33 @@ import sys
import time
from . import PexpectTestCase
+
class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
+ """Various tests for the running status of processes."""
- def test_expect_wait (self):
- '''This tests that calling wait on a finished process works as expected.
- '''
- p = pexpect.spawn('sleep 3')
+ def test_expect_wait(self):
+ """Ensure consistency in wait() and isalive()."""
+ p = pexpect.spawn('sleep 1')
assert p.isalive()
- p.wait()
+ assert p.wait() == 0
assert not p.isalive()
+ # In previous versions of ptyprocess/pexpect, calling wait() a second
+ # time would raise an exception, but not since v4.0
+ assert p.wait() == 0
+ def test_expect_wait_after_termination(self):
+ """Ensure wait on a process terminated by kill -9."""
p = pexpect.spawn('sleep 3')
assert p.isalive()
p.kill(9)
time.sleep(1)
- with self.assertRaises(pexpect.ExceptionPexpect):
- p.wait()
+
+ # when terminated, the exitstatus is None, but p.signalstatus
+ # and p.terminated reflects that the kill -9 nature.
+ assert p.wait() is None
+ assert p.signalstatus == 9
+ assert p.terminated == True
+ assert not p.isalive()
def test_signal_wait(self):
'''Test calling wait with a process terminated by a signal.'''
@@ -102,7 +113,7 @@ class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
p = pexpect.spawn('cat')
assert p.isalive()
assert p.isalive()
- p.kill(9)
+ p.sendeof()
p.expect(pexpect.EOF)
assert not p.isalive()
assert not p.isalive()