summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2003-05-08 03:37:10 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2003-05-08 03:37:10 +0000
commit080caf259a074f3176c0bd72b366cc7eb79da5db (patch)
tree5af9f03029659513d21a68a1608d1884596f26d6
parent3677e721fb208749f5de4e87fb294e28442852e6 (diff)
downloadpexpect-080caf259a074f3176c0bd72b366cc7eb79da5db.tar.gz
islive(), will you ever work?
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@196 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/pexpect.py33
-rwxr-xr-xpexpect/tests/test_destructor.py19
-rwxr-xr-xpexpect/tests/test_filedescriptor.py10
3 files changed, 26 insertions, 36 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index be671a2..349ef2f 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -257,7 +257,8 @@ class spawn:
self.log_file = fileobject
def read_nonblocking (self, size = -1, timeout = None):
- """This reads at most size characters from the child application.
+ """
+ This reads at most size characters from the child application.
It includes a timeout. If the read does not complete within the
timeout period then a TIMEOUT exception is raised.
If the end of file is read then an EOF exception will be raised.
@@ -439,8 +440,10 @@ class spawn:
This returns 1 if the child process appears to be running or 0 if not.
This also sets the exitstatus attribute.
It can take literally SECONDS for Solaris to return the right status.
+ This is the most wiggly part of Pexpect, but I think I've almost got
+ it nailed down.
"""
- # I don't want to use signals. Signals on UNIX suck and they
+ # I can't use signals. Signals on UNIX suck and they
# mess up Python pipes (setting SIGCHLD to SIGIGNORE).
# If this class was created from an existing file descriptor then
@@ -459,20 +462,18 @@ class spawn:
# I have to do this twice for Solaris.
# I can't even believe that I figured this out...
- try:
- pid, status = os.waitpid(self.pid, os.WNOHANG)
- #print 'Solaris sucks'
- except OSError:
- # Non-Solaris platforms raise an exception that is
- # quietly ignored here. This is harmless... I think :-)
- pass
-
- # If status is still 0 after two calls to waitpid() then
- # the process really is alive. This seems to work on all platforms.
- if status == 0:
- return 1
+ if pid == 0 and status == 0:
+ try:
+ pid, status = os.waitpid(self.pid, os.WNOHANG)
+ #print 'Solaris sucks'
+ except OSError: # This is crufty. When does this happen?
+ return 0
+ # If pid and status is still 0 after two calls to waitpid() then
+ # the process really is alive. This seems to work on all platforms.
+ if pid == 0 and status == 0:
+ return 1
- # I didn't OR this together because I want hooks for debugging.
+ # I do not OR this together because I want hooks for debugging.
if os.WIFEXITED (status):
self.exitstatus = os.WEXITSTATUS(status)
return 0
@@ -481,7 +482,7 @@ class spawn:
elif os.WIFSIGNALED (status):
return 0
else:
- return 1
+ return 0 # Can I ever get here?
def kill(self, sig):
"""This sends the given signal to the child application.
diff --git a/pexpect/tests/test_destructor.py b/pexpect/tests/test_destructor.py
index 86bae08..fe071d7 100755
--- a/pexpect/tests/test_destructor.py
+++ b/pexpect/tests/test_destructor.py
@@ -1,28 +1,25 @@
#!/usr/bin/env python
import pexpect
import unittest
+import PexpectTestCase
import gc
import time
-class TestCaseDestructor(unittest.TestCase):
- #def runTest (self):
-
+class TestCaseDestructor(PexpectTestCase.PexpectTestCase):
def test_destructor (self):
- p1 = pexpect.spawn('ls -l')
- p2 = pexpect.spawn('ls -l')
- p3 = pexpect.spawn('ls -l')
- p4 = pexpect.spawn('ls -l')
+ p1 = pexpect.spawn('python hello_world.py')
+ p2 = pexpect.spawn('python hello_world.py')
+ p3 = pexpect.spawn('python hello_world.py')
+ p4 = pexpect.spawn('python hello_world.py')
fd_t1 = (p1.child_fd,p2.child_fd,p3.child_fd,p4.child_fd)
p1.expect(pexpect.EOF)
p2.expect(pexpect.EOF)
p3.expect(pexpect.EOF)
p4.expect(pexpect.EOF)
- #print p1.before, p2.before, p3.before, p4.before
p1.kill(9)
p2.kill(9)
p3.kill(9)
p4.kill(9)
- time.sleep(1) # Some platforms are slow at gc... Solaris!
p1 = None
p2 = None
p3 = None
@@ -33,19 +30,17 @@ class TestCaseDestructor(unittest.TestCase):
p2 = pexpect.spawn('ls -l')
p3 = pexpect.spawn('ls -l')
p4 = pexpect.spawn('ls -l')
- time.sleep(1) # Some platforms are slow at gc... Solaris!
fd_t2 = (p1.child_fd,p2.child_fd,p3.child_fd,p4.child_fd)
p1.kill(9)
p2.kill(9)
p3.kill(9)
p4.kill(9)
- time.sleep(1) # Some platforms are slow at gc... Solaris!
del (p1)
del (p2)
del (p3)
del (p4)
gc.collect()
- time.sleep(1) # Some platforms are slow at gc... Solaris!
+ time.sleep(1)
p1 = pexpect.spawn('ls -l')
p2 = pexpect.spawn('ls -l')
p3 = pexpect.spawn('ls -l')
diff --git a/pexpect/tests/test_filedescriptor.py b/pexpect/tests/test_filedescriptor.py
index 205d898..5509dc9 100755
--- a/pexpect/tests/test_filedescriptor.py
+++ b/pexpect/tests/test_filedescriptor.py
@@ -1,17 +1,11 @@
#!/usr/bin/env python
import pexpect
import unittest
+import PexpectTestCase
import sys
import os
-class ExpectTestCase(unittest.TestCase):
- def setUp(self):
- self.original_path = os.getcwd()
- newpath = os.path.join (os.environ['PROJECT_PEXPECT_HOME'], 'tests')
- os.chdir (newpath)
- def tearDown(self):
- os.chdir (self.original_path)
-
+class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def test_fd (self):
fd = os.open ('TESTDATA.txt', os.O_RDONLY)
s = pexpect.spawn (fd)