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

Donn Cave donn at drizzle.com
Wed Sep 26 01:48:48 EDT 2001


Quoth weeks at vitus.scs.agilent.com (Greg Weeks):
| I guess there isn't a lot of interest in this.  Still, I'm interested.
|
| Donn Cave (donn at drizzle.com) wrote:
| : Are you thinking about reading stderr for use in the exception?
|
| stderr is a messy issue, because there is no agreement on what stderr is
| for.  Some people use stderr only for fatal error messages.  Othere use
| stderr -- now renamed "diagnostic output" -- for any messages whatsoever
| intended for human readers.  The first group is like to want exceptions to
| include stderr text; the second group isn't.
|
| I'm in the first group, and I'm content to write a private utility that
| handles stderr as if it were used only for fatal error messages (even
| though I know this will get me into trouble with certain programs).  But
| I'd be hard-pressed to come up with a general solution.  Look at the
| evolution of the popen2 module, for instance.  And if you have a pipeline
| with multiple processes, it just gets that much worse.

In mine (I see ftp://ftp.u.washington.edu/public/donn/cmdproc.py is
a fairly current version) there are 4 functions: system, systemr,
expand and expandr - expand returns output, system doesn't, and
*r raises an exception from stderr _if the command exits non-zero_.

I don't think many programmers would agree that stderr should be
reserved for fatal errors, and it isn't ideally suited for use in
an exception, but in practice it works very well for the general
run of utility programs.  Use with discretion.  The other problem
is error exits of a more "non-perfect" than "fatal" nature - for
example, you say "ls a b", and a exists, so ls displays a valid
result, but b doesn't, so ls writes to stderr and exits 1.

It might be like porting C to Python, where the C code has been
ignoring error codes --- for (i=getdtablesize(); i >= 0; --i)
close(i); and now you have to handle a bunch of exceptions because
Python doesn't ignore errors.  Or you can make it an option to
ignore errors.

Some programs will misbehave and write errors to stdout and exit
with random values, but not many.  As for pipelines - in shells,
the process at the downstream end of the pipe returns the exit
status for the pipeline.  I have not had much need to construct
Python pipelines, though, and haven't thought about what would
be useful there.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list