]> git.phdru.name Git - bookmarks_db.git/commitdiff
Upgrade subproc.py to version 1.15
authorOleg Broytman <phd@phdru.name>
Sun, 29 Jun 2014 16:26:05 +0000 (20:26 +0400)
committerOleg Broytman <phd@phdru.name>
Sun, 29 Jun 2014 16:26:05 +0000 (20:26 +0400)
subproc.py

index 39f82a63243cb628afc177f69f325c11d99f3d7c..d9eba6151c1e0208c174f29cfe9494c11d3e9efb 100644 (file)
@@ -1,6 +1,7 @@
 """Run a subprocess and communicate with it via stdin, stdout, and stderr.
 
-Requires that platform supports, eg, posix-style os.pipe and os.fork.
+Requires that platform supports, eg, posix-style 'os.pipe' and 'os.fork'
+routines.
 
 Subprocess class features:
 
@@ -11,14 +12,11 @@ Subprocess class features:
  - provides detection of subprocess startup failure
 
  - Subprocess objects have nice, informative string rep (as every good object
-   ought).
+   ought)."""
 
- - RecordFile class provides record-oriented IO for file-like stream objects.
-"""
+__version__ = "Revision: 1.15 "
 
-__version__ = "Revision: 1.7 "
-
-# Id: subproc.py,v 1.7 1998
+# 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
@@ -91,6 +89,7 @@ class Subprocess:
         """Fork a subprocess with designated COMMAND (default, self.cmd)."""
         if cmd: self.cmd = cmd
         else: cmd = self.cmd
+        cmd = string.split(self.cmd)
         pRc, cWp = os.pipe()            # parent-read-child, child-write-parent
         cRp, pWc = os.pipe()            # child-read-parent, parent-write-child
         pRe, cWe = os.pipe()            # parent-read-error, child-write-error
@@ -112,7 +111,16 @@ class Subprocess:
                     try: os.close(i)
                     except os.error: pass
 
-            self.run_cmd(cmd)
+            try:
+                os.execvp(cmd[0], cmd)
+                os._exit(1)                     # Shouldn't get here
+
+            except os.error, e:
+                if self.control_stderr:
+                    os.dup2(parentErr, 2)       # Reconnect to parent's stdout
+                sys.stderr.write("**execvp failed, '%s'**\n" %
+                                 str(e))
+                os._exit(1)
             os._exit(1)                 # Shouldn't get here.
 
         else:           ### PARENT ###
@@ -146,21 +154,6 @@ class Subprocess:
                 # Child may have exited, but not in error, so we won't say
                 # anything more at this point.
 
-    def run_cmd(self, cmd):
-        cmd = string.split(self.cmd)
-
-        try:
-            os.execvp(cmd[0], cmd)
-            os._exit(1)                     # Shouldn't get here
-
-        except os.error, e:
-            if self.control_stderr:
-                os.dup2(parentErr, 2)       # Reconnect to parent's stdout
-            sys.stderr.write("**execvp failed, '%s'**\n" %
-                             str(e))
-            os._exit(1)
-
-
     ### Write input to subprocess ###
 
     def write(self, str):
@@ -211,9 +204,8 @@ class Subprocess:
             return self.readPendingChars()
         got = ''
         while n:
-            got0 = self.readPendingChars(n)
-            got = got + got0
-            n = n - len(got0)
+            got = got + self.readPendingChars(n)
+            n = n - len(got)
         return got      
     def readPendingChars(self, max=None):
         """Read all currently pending subprocess output as a single string."""
@@ -344,13 +336,6 @@ class Subprocess:
         status = self.status()
         return '<Subprocess ' + status + ', at ' + hex(id(self))[2:] + '>'
 
-# The name of the class is a pun; it is short for "Process", but it also appeals
-# to the word "Procedure"
-class Subproc(Subprocess):
-    def run_cmd(self, cmd):
-        apply(cmd[0], cmd[1:])
-        os._exit(1)
-
 #############################################################################
 #####                 Non-blocking read operations                      #####
 #############################################################################
@@ -416,11 +401,15 @@ class ReadBuf:
 
         got = ""
         if self.buf:
-            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]: