summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-09-23 17:32:17 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-09-23 17:32:17 +0000
commit26a3397ee56e9eaae8c9181bc050c2863661b923 (patch)
tree0c5e6e9f83778e58b4b0622b0887a501218bbc79
parent6553a969c08b7f7fd26d03355da0ab8601efc054 (diff)
downloadpexpect-26a3397ee56e9eaae8c9181bc050c2863661b923.tar.gz
Changed isAlive() method. Renamed to isalive() and heavily modified to
used a totally different technique to determine process status. git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@83 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/Makefile2
-rw-r--r--pexpect/alltests.py1
-rwxr-xr-xpexpect/examples/ftp.py4
-rwxr-xr-xpexpect/examples/python.py2
-rwxr-xr-xpexpect/hash.py6
-rw-r--r--pexpect/pexpect.py95
-rw-r--r--pexpect/setup.py2
-rwxr-xr-xpexpect/tests/test_constructor.py4
-rwxr-xr-xpexpect/tests/test_destructor.py18
-rwxr-xr-xpexpect/tests/test_expect.py14
-rwxr-xr-xpexpect/tests/test_is_alive.py22
-rwxr-xr-xpexpect/tests/test_log.py2
-rwxr-xr-xpexpect/tests/test_run_out_of_pty.py2
13 files changed, 92 insertions, 82 deletions
diff --git a/pexpect/Makefile b/pexpect/Makefile
index 0b82ce4..9d96945 100644
--- a/pexpect/Makefile
+++ b/pexpect/Makefile
@@ -1,6 +1,6 @@
SHELL = /bin/sh
-VERSION= 0.91
+VERSION= 0.92
#DOCGENERATOR= happydoc
DOCGENERATOR= pydoc -w
MANIFEST_LINES != cat MANIFEST
diff --git a/pexpect/alltests.py b/pexpect/alltests.py
index fcacfb0..8f605f0 100644
--- a/pexpect/alltests.py
+++ b/pexpect/alltests.py
@@ -17,6 +17,7 @@ modules_to_test = (
'test_screen'
)
+
def suite():
alltests = unittest.TestSuite()
for module in map(__import__, modules_to_test):
diff --git a/pexpect/examples/ftp.py b/pexpect/examples/ftp.py
index 69909d5..bc64f92 100755
--- a/pexpect/examples/ftp.py
+++ b/pexpect/examples/ftp.py
@@ -23,8 +23,8 @@ sys.stdout.write (child.after)
sys.stdout.flush()
child.interact() # Escape character defaults to ^]
-if child.isAlive():
+if child.isalive():
child.sendline('bye')
child.kill(1)
-print 'Is Alive: ', child.isAlive()
+print 'Is Alive: ', child.isalive()
diff --git a/pexpect/examples/python.py b/pexpect/examples/python.py
index 82b507f..22b541e 100755
--- a/pexpect/examples/python.py
+++ b/pexpect/examples/python.py
@@ -18,5 +18,5 @@ print 'Escape character is \'^]\'.'
print c.after,
c.interact()
c.kill(1)
-print 'isAlive:', c.isAlive()
+print 'is alive:', c.isalive()
diff --git a/pexpect/hash.py b/pexpect/hash.py
new file mode 100755
index 0000000..90d87f0
--- /dev/null
+++ b/pexpect/hash.py
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+import sys
+
+lines = sys.stdin.readlines()
+for line in lines:
+ sys.stdout.write ('#' + line)
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index ef6afd0..469db8d 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -130,19 +130,22 @@ class spawn:
if which(self.command) == None:
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
-
- # This is necessary for isAlive() to work. Without this there is
- # no portable way to tell if a child process is a zombie.
- # Checking waitpid with WNOHANG option does not work and
- # checking waitpid without it would block if the child is not a zombie.
- # With this children should exit completely without going into
- # a zombie state. Note that some UNIX flavors may send the signal
- # before the child's pty output buffer is empty, while others
- # may send the signal only when the buffer is empty.
- # In the later case, isAlive() will always return true until the
- # output buffer is empty. Use expect_eof() to consume all child output.
- # This is not the same as the Zombie (waiting to die) problem.
- #signal.signal(signal.SIGCHLD, signal.SIG_IGN)
+# I'm no longer using this technique to test if a process is alive.
+# First, it is not portable.
+# Second, it messes up other subsystems that use signals (such as pipes).
+# Third, signals on UNIX suck.
+# # This is necessary for isAlive() to work. Without this there is
+# # no portable way to tell if a child process is a zombie.
+# # Checking waitpid with WNOHANG option does not work and
+# # checking waitpid without it would block if the child is not a zombie.
+# # With this children should exit completely without going into
+# # a zombie state. Note that some UNIX flavors may send the signal
+# # before the child's pty output buffer is empty, while others
+# # may send the signal only when the buffer is empty.
+# # In the later case, isAlive() will always return true until the
+# # output buffer is empty. Use expect_eof() to consume all child output.
+# # This is not the same as the Zombie (waiting to die) problem.
+# #signal.signal(signal.SIGCHLD, signal.SIG_IGN)
try:
self.pid, self.child_fd = pty.fork()
@@ -461,15 +464,15 @@ class spawn:
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old) # restore state
- def isAlive(self):
+ def isalive(self):
"""This tests if the child process is running or not.
- It returns 1 if the child process appears to be running or
- 0 if not.
+ This returns 1 if the child process appears to be running or 0 if not.
"""
status = os.waitpid (self.pid, os.WNOHANG)
if status[0] == 0:
return 1
+ # I didn't OR this together because I want hooks for debugging.
if os.WIFEXITED (status[1]):
return 0
elif os.WIFSTOPPED (status[1]):
@@ -479,36 +482,36 @@ class spawn:
else:
return 1
- def isAliveOld(self):
- """This tests if the child process is running or not.
- It returns 1 if the child process appears to be running or
- 0 if not. This checks the process list to see if the pid is
- there. In theory, the original child could have died and the
- pid could have been reused by some other process. This is
- unlikely, but I can find no portable way to make sure.
- Also, this is not POSIX portable way to check, but
- UNIX provides no standard way to test if a given pid is
- running or not. By convention most modern UNIX systems will
- respond to signal 0.
- """
- old_signal = signal.getsignal (signal.SIGCHLD)
- if old_signal is None:
- raise ExceptionPexpect ('Existing signal handler is non-Python')
- #signal.signal(signal.SIGCHLD, signal.SIG_IGN)
- try:
- print "stuff2"
- os.kill (self.pid, 0)
- print "stuff3"
- signal.signal(signal.SIGCHLD, old_signal)
- return 1
- except OSError, e:
- print str(e)
- signal.signal(signal.SIGCHLD, old_signal)
- return 0
- ###return e.errno == errno.EPERM
- ### For some reason I got this exception printed even though
- ### I am explicitly catching OSError. Noah doth halucinate?
- ### OSError: [Errno 3] No such process
+# def isAlive(self):
+# """This tests if the child process is running or not.
+# It returns 1 if the child process appears to be running or
+# 0 if not. This checks the process list to see if the pid is
+# there. In theory, the original child could have died and the
+# pid could have been reused by some other process. This is
+# unlikely, but I can find no portable way to make sure.
+# Also, this is not POSIX portable way to check, but
+# UNIX provides no standard way to test if a given pid is
+# running or not. By convention most modern UNIX systems will
+# respond to signal 0.
+# """
+# old_signal = signal.getsignal (signal.SIGCHLD)
+# if old_signal is None:
+# raise ExceptionPexpect ('Existing signal handler is non-Python')
+# #signal.signal(signal.SIGCHLD, signal.SIG_IGN)
+# try:
+# print "stuff2"
+# os.kill (self.pid, 0)
+# print "stuff3"
+# signal.signal(signal.SIGCHLD, old_signal)
+# return 1
+# except OSError, e:
+# print str(e)
+# signal.signal(signal.SIGCHLD, old_signal)
+# return 0
+# ###return e.errno == errno.EPERM
+# ### For some reason I got this exception printed even though
+# ### I am explicitly catching OSError. Noah doth halucinate?
+# ### OSError: [Errno 3] No such process
def kill(self, sig):
"""This sends the given signal to the child application.
diff --git a/pexpect/setup.py b/pexpect/setup.py
index 0f66bee..b7836ae 100644
--- a/pexpect/setup.py
+++ b/pexpect/setup.py
@@ -4,7 +4,7 @@ $Date$
'''
from distutils.core import setup
setup (name='pexpect',
- version='0.91',
+ version='0.92',
py_modules=['pexpect'],
description='Pexpect, a pure Python Expect allows control of other applications.',
author='Noah Spurrier',
diff --git a/pexpect/tests/test_constructor.py b/pexpect/tests/test_constructor.py
index 85262fe..a302fcd 100755
--- a/pexpect/tests/test_constructor.py
+++ b/pexpect/tests/test_constructor.py
@@ -10,8 +10,8 @@ class TestCaseConstructor(unittest.TestCase):
the same results for different styles of invoking __init__().
This assumes that the root directory / is static during the test.
"""
- p1 = pexpect.spawn('/bin/ls -l /bin')
- p2 = pexpect.spawn('/bin/ls' ,['-l', '/bin'])
+ p1 = pexpect.spawn('ls -l')
+ p2 = pexpect.spawn('ls' ,['-l'])
p1.expect (pexpect.EOF)
p2.expect (pexpect.EOF)
assert (p1.before == p2.before)
diff --git a/pexpect/tests/test_destructor.py b/pexpect/tests/test_destructor.py
index 2c4d2d1..e6c1d27 100755
--- a/pexpect/tests/test_destructor.py
+++ b/pexpect/tests/test_destructor.py
@@ -7,25 +7,25 @@ class TestCaseDestructor(unittest.TestCase):
#def runTest (self):
def test_destructor (self):
- p1 = pexpect.spawn('/bin/ls -l')
- p2 = pexpect.spawn('/bin/ls -l')
- p3 = pexpect.spawn('/bin/ls -l')
+ p1 = pexpect.spawn('ls -l')
+ p2 = pexpect.spawn('ls -l')
+ p3 = pexpect.spawn('ls -l')
fd_t1 = (p1.child_fd,p2.child_fd,p3.child_fd)
p1 = None
p2 = None
p3 = None
gc.collect()
- p1 = pexpect.spawn('/bin/ls -l')
- p2 = pexpect.spawn('/bin/ls -l')
- p3 = pexpect.spawn('/bin/ls -l')
+ p1 = pexpect.spawn('ls -l')
+ p2 = pexpect.spawn('ls -l')
+ p3 = pexpect.spawn('ls -l')
fd_t2 = (p1.child_fd,p2.child_fd,p3.child_fd)
del (p1)
del (p3)
del (p2)
gc.collect()
- p1 = pexpect.spawn('/bin/ls -l')
- p2 = pexpect.spawn('/bin/ls -l')
- p3 = pexpect.spawn('/bin/ls -l')
+ p1 = pexpect.spawn('ls -l')
+ p2 = pexpect.spawn('ls -l')
+ p3 = pexpect.spawn('ls -l')
fd_t3 = (p1.child_fd,p2.child_fd,p3.child_fd)
assert (fd_t1 == fd_t2 == fd_t3)
diff --git a/pexpect/tests/test_expect.py b/pexpect/tests/test_expect.py
index a657d72..957b703 100755
--- a/pexpect/tests/test_expect.py
+++ b/pexpect/tests/test_expect.py
@@ -8,9 +8,9 @@ class ExpectTestCase(unittest.TestCase):
#def runTest (self):
def test_expect (self):
- the_old_way = commands.getoutput('/bin/ls -l')
+ the_old_way = commands.getoutput('ls -l')
- p = pexpect.spawn('/bin/ls -l')
+ p = pexpect.spawn('ls -l')
the_new_way = ''
try:
while 1:
@@ -23,9 +23,9 @@ class ExpectTestCase(unittest.TestCase):
assert the_old_way == the_new_way
def test_expect_exact (self):
- the_old_way = commands.getoutput('/bin/ls -l')
+ the_old_way = commands.getoutput('ls -l')
- p = pexpect.spawn('/bin/ls -l')
+ p = pexpect.spawn('ls -l')
try:
the_new_way = ''
while 1:
@@ -38,9 +38,9 @@ class ExpectTestCase(unittest.TestCase):
assert the_old_way == the_new_way
def test_expect_eof (self):
- the_old_way = commands.getoutput('/bin/ls -l')
+ the_old_way = commands.getoutput('ls -l')
- p = pexpect.spawn('/bin/ls -l')
+ p = pexpect.spawn('ls -l')
p.expect(pexpect.EOF) # This basically tells it to read everything.
the_new_way = p.before
the_new_way = the_new_way.replace('\r','')
@@ -49,7 +49,7 @@ class ExpectTestCase(unittest.TestCase):
assert the_old_way == the_new_way
def test_unexpected_eof (self):
- p = pexpect.spawn('/bin/ls -l')
+ p = pexpect.spawn('ls -l')
try:
p.expect('ZXYXZ') # Probably never see this in ls output.
except pexpect.EOF, e:
diff --git a/pexpect/tests/test_is_alive.py b/pexpect/tests/test_is_alive.py
index f132b14..3865fcd 100755
--- a/pexpect/tests/test_is_alive.py
+++ b/pexpect/tests/test_is_alive.py
@@ -5,28 +5,28 @@ import sys
class IsAliveTestCase(unittest.TestCase):
- def test_expect_is_alive1 (self):
- p = pexpect.spawn('/bin/ls')
+ def test_expect_isalive1 (self):
+ p = pexpect.spawn('ls')
p.expect(pexpect.EOF)
- if p.isAlive():
+ if p.isalive():
self.fail ('Child process is not dead. It should be.')
- def test_expect_is_alive2 (self):
- p = pexpect.spawn('/bin/cat')
- if not p.isAlive():
+ def test_expect_isalive2 (self):
+ p = pexpect.spawn('cat')
+ if not p.isalive():
self.fail ('Child process is not alive. It should be.')
p.kill(1)
p.expect(pexpect.EOF)
- if p.isAlive():
+ if p.isalive():
self.fail ('Child process is not dead. It should be.')
- def test_expect_is_alive3 (self):
- p = pexpect.spawn('/bin/cat')
- if not p.isAlive():
+ def test_expect_isalive3 (self):
+ p = pexpect.spawn('cat')
+ if not p.isalive():
self.fail ('Child process is not alive. It should be.')
p.kill(9)
p.expect(pexpect.EOF)
- if p.isAlive():
+ if p.isalive():
self.fail ('Child process is not dead. It should be.')
if __name__ == '__main__':
diff --git a/pexpect/tests/test_log.py b/pexpect/tests/test_log.py
index 96495df..4e4b170 100755
--- a/pexpect/tests/test_log.py
+++ b/pexpect/tests/test_log.py
@@ -10,7 +10,7 @@ class TestCaseLog(unittest.TestCase):
def test_log (self):
log_message = 'This is a test.'
filename = tempfile.mktemp()
- p = pexpect.spawn('/bin/echo', [log_message])
+ p = pexpect.spawn('echo', [log_message])
p.log_open (filename)
p.expect (pexpect.EOF)
p.log_close()
diff --git a/pexpect/tests/test_run_out_of_pty.py b/pexpect/tests/test_run_out_of_pty.py
index 082397b..18eac41 100755
--- a/pexpect/tests/test_run_out_of_pty.py
+++ b/pexpect/tests/test_run_out_of_pty.py
@@ -13,7 +13,7 @@ class ExpectTestCase(unittest.TestCase):
plist=[]
for count in range (0,1000):
try:
- plist.append (pexpect.spawn('/bin/ls -l'))
+ plist.append (pexpect.spawn('ls -l'))
except pexpect.ExceptionPexpect, e:
return
except Exception, e: