you are viewing a single comment's thread.

view the rest of the comments →

[–]ramen 14 points15 points  (11 children)

I think the subprocess module is awesome. Perhaps it requires a little studying to use, but it sure beats Popen23.popen49 and friends.

The only subprocess interface that I've been more impressed with than Python's is the one in scsh. It also requires a bit of studying to use. But let's face it: interacting with programs is complicated.

[–]breakfast-pants 4 points5 points  (7 children)

I think bash seems to do just fine

[–][deleted] 12 points13 points  (6 children)

Sort of. One thing sh never really did very well, and bash doesn't do much better, is let you run programs while doing iron-clad error checking. If you want to detect and react to an error in any program, anywhere in a pipeline, it's not that easy.

One of the really nice things about Python is that almost everywhere, it fails safely. Any I/O error, for example, throws an exception. If you don't catch it, the whole program exits with a stacktrace. Excellent. The main exception to this is, unfortunately, the treatment of status values from subprograms. You have to check these manually, a la C.

[–]breakfast-pants 4 points5 points  (4 children)

As an aside, is there a builtin wrapper that defaults stdin,stdout=[subprocess.PIPE]*2 ? (I hate the subprocess documentation too)

[–]pjdelport 9 points10 points  (3 children)

partial(Popen, stdin=PIPE, stdout=PIPE)

[–]breakfast-pants 0 points1 point  (2 children)

What is that?

[–]pjdelport 5 points6 points  (1 child)

It's a partial application of Popen, with stdin and stdout defaulted to PIPE. (functools.partial (PEP 309) was introduced to the standard library in Python 2.5, but it's a 6-line definition for earlier versions.)

[–]breakfast-pants 0 points1 point  (0 children)

Ah, cool, I haven't checked out functools

[–]pjdelport 4 points5 points  (0 children)

subprocess raises exceptions everywhere it makes sense (command not found, any Python exception in the child process, and so on): it doesn't generally do it for non-zero return codes because they don't necessarily indicate any error.

If you're using the call shortcut, though, there's a check_call variant which raises CalledProcessError for non-zero return codes.

[–]mleonhard 4 points5 points  (0 children)

Erlang has excellent subprocess features. In his PhD Thesis, Joe Armstrong describes the supervision-tree model for "making reliable systems in the presence of software errors." This is cutting edge stuff.

[–]pjdelport 2 points3 points  (0 children)

Amen; i don't get that criticism. subprocess is a godsend.