]> git.phdru.name Git - bookmarks_db.git/blobdiff - subproc.py
Fix(subproc.py): Clear pid to avoid repeated killing
[bookmarks_db.git] / subproc.py
old mode 100644 (file)
new mode 100755 (executable)
index c7fd116..57a9568
@@ -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'
@@ -16,7 +18,7 @@ Subprocess class features:
 
 __version__ = "Revision: 1.15 "
 
-# Id: subproc.py,v 1.15 1998/12/14 20:53:16 klm Exp 
+# Id: subproc.py,v 1.15 1998/12/14 20:53:16 klm Exp
 # Originally by ken manheimer, ken.manheimer@nist.gov, jan 1995.
 
 # Prior art: Initially based python code examples demonstrating usage of pipes
@@ -40,13 +42,18 @@ __version__ = "Revision: 1.15 "
 #
 # ken.manheimer@nist.gov
 
+# This is a modified version by Oleg Broytman <phd@phdru.name>.
+# 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
@@ -206,7 +216,7 @@ class Subprocess:
             got0 = self.readPendingChars(n)
             got = got + got0
             n = n - len(got0)
-        return got      
+        return got
     def readPendingChars(self, max=None):
         """Read all currently pending subprocess output as a single string."""
         return self.readbuf.readPendingChars(max)
@@ -401,15 +411,15 @@ class ReadBuf:
 
         got = ""
         if self.buf:
-             if (max > 0) and (len(self.buf) > max):
-                 got = self.buf[0:max]
-                 self.buf = self.buf[max:]
-             else:
-                 got, self.buf = self.buf, ''
-             return got                                         
+            if (max > 0) and (len(self.buf) > max):
+                got = self.buf[0:max]
+                self.buf = self.buf[max:]
+            else:
+                got, self.buf = self.buf, ''
+            return got
 
         if self.eof:
-             return ''
+            return ''
 
         sel = select.select([self.fd], [], [self.fd], 0)
         if sel[2]:
@@ -590,7 +600,7 @@ class Ph:
                 line = string.splitfields(line, ':')
                 it[string.strip(line[0])] = (
                     string.strip(string.join(line[1:])))
-        
+
     def getreply(self):
         """Consume next response from ph, returning list of lines or string
         err."""
@@ -643,15 +653,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 +691,6 @@ def test(p=0):
             del p
             print("\tDone.")
             return None
+
+if __name__ == '__main__':
+    test()