Simple process IO capture (Was: "shell-commands" and python!) - process.py (0/1)

Ignacio Vazquez-Abrams ignacio at openservices.net
Mon Sep 24 22:38:29 CEST 2001


On Mon, 24 Sep 2001, Paul Moore wrote:

How about these changes:

class Error:
  def __init__(self, code=0):
    self.code=code
  def __str__(self):
    return 'Error code %s' % self.code

retval = 0

>
>  [snip]
>
> def run(cmd, mode = 't', input = None,
>         capture_stderr = None,
>         raise_exception = None):

      global retval

>     if capture_stderr:
>         (i, o) = _popen4(cmd, mode)
>     else:
>         (i, o) = _popen2(cmd, mode)
>
>     if input:
>         i.write(input)
>     i.close()
>
>     ret = o.read()

      retval = o.close()

>     if raise_exception:

          if retval: raise Error(retval)

>     return ret

with those changes you can use proocess.retval instead of process.retval()
(not a big change, but why have extra code?), and you can get the return code
from the exception more easily.

In fact, you may also want to default the run() function to raising an
exception rather than just setting retval; force the user to have to
explicitly override the Pythonic behaviour.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>






More information about the Python-list mailing list