X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=subproc.py;h=b5944f90f87a8c1a9a5536c267ebd1d1b87e4f72;hb=22d6a7d40652eb92265cb03f2a719974e86bcb4f;hp=0280aad6c9fcc10fa0d87c562f44fb6b08eacf3a;hpb=c88cb7a75e7caf1d67466cfa107981d95115fa0c;p=bookmarks_db.git diff --git a/subproc.py b/subproc.py old mode 100644 new mode 100755 index 0280aad..b5944f9 --- a/subproc.py +++ b/subproc.py @@ -1,3 +1,5 @@ +#! /usr/bin/env python + """Run a subprocess and communicate with it via stdin, stdout, and stderr. Requires that platform supports, eg, posix-style 'os.pipe' and 'os.fork' @@ -40,13 +42,18 @@ __version__ = "Revision: 1.15 " # # ken.manheimer@nist.gov +# This is a modified version by Oleg Broytman . +# The original version is still preserved at +# https://www.python.org/ftp/python/contrib-09-Dec-1999/System/subproc.tar.gz import sys, os, string, time, types import select import signal -SubprocessError = 'SubprocessError' +class SubprocessError(Exception): + pass + # You may need to increase execvp_grace_seconds, if you have a large or slow # path to search: execvp_grace_seconds = 0.5 @@ -136,7 +143,9 @@ class Subprocess: except os.error as error: errno, msg = error if errno == 10: + self.pid = None raise SubprocessError("Subprocess '%s' failed." % self.cmd) + self.pid = None raise SubprocessError("Subprocess failed[%d]: %s" % (errno, msg)) if pid == self.pid: # child exited already @@ -148,6 +157,7 @@ class Subprocess: "child killed by signal %d with a return code of %d" % (sig, rc)) if rc: + self.pid = None raise SubprocessError( "child exited with return code %d" % rc) # Child may have exited, but not in error, so we won't say @@ -318,7 +328,11 @@ class Subprocess: (sig[0], self.pid, self.cmd, hex(id(self))[2:]))) for i in self.pipefiles: - os.close(i) + try: + fp = os.fdopen(i).close() + except OSError: + pass + del self.pipefiles[:] self.pid = 0 return None # ===> time.sleep(.1) @@ -643,15 +657,15 @@ class Ph: ############################################################################# def test(p=0): - print("\tOpening subprocess:") - p = Subprocess('cat', 1) # set to expire noisily... - print(p) print("\tOpening bogus subprocess, should fail:") try: b = Subprocess('/', 1) print("\tOops! Null-named subprocess startup *succeeded*?!?") except SubprocessError: print("\t...yep, it failed.") + print("\tOpening cat subprocess:") + p = Subprocess('cat', 1) # set to expire noisily... + print(p) print('\tWrite, then read, two newline-teriminated lines, using readline:') p.write('first full line written\n'); p.write('second.\n') print(repr(p.readline())) @@ -681,3 +695,6 @@ def test(p=0): del p print("\tDone.") return None + +if __name__ == '__main__': + test()