strange subprocess behavior when calling ps

Roger Davis rbd at hawaii.edu
Wed Nov 17 14:29:34 EST 2010


> Completely off topic but I think the try clause could be rewritten that way:
> ...
> Don't use bare except clause, you're masking syntax errors for instance,
> which will be flagged as 'unexpected error in generation ...".
> In a more general manner, if something unexpected happens it's better to
> just let the exception raise uncought. If you want to handle some
> errors, meaning you're kindof expecting them then add a explicit clause
> (like you did with KeyboardInterrupt).
>
> JM
>
> PS : "except Exception :" will catch most of the exceptions (all
> inheriting from that class). It's better than using a bare "except :"
> clause. (Exception won't catch SyntaxError)

Thanks for the suggestion JM, it is off-topic and, although I will
first just say that the exception mechanism is *not* one of the
reasons I use Python (and stop there with regard to the whole
exception mechanism and various usage strategies in general), I do
have a few specific questions about a couple of your statements if you
don't mind following up.

First, inserting a syntax error into my existing code does not hide a
SyntaxError exception as you have stated:

% cat pid.py
#!/usr/bin/python
import os
import sys
import subprocess

def main():

	psargs= ["/bin/ps", "-e"]
	try:
		ps= subprocess.Popen(psargs, stdout=subprocess.PIPE, close_fds=True)
		psout= ps.communicate()[0]
		pslines= psout.splitlines()
		if pslines not good Python talky-talk
		for line in pslines:
			print "%s" % line
	except KeyboardInterrupt:
		print "Keyboard interrupt received -- terminating."
		sys.stdout.flush()
		sys.exit(-1)
	except:
		print "%s: unexpected error in generation of system process list" %
prognm
		sys.stdout.flush()
		sys.exit(-1)

main()

% ./pid.py
  File "./pid.py", line 14
    if pslines not good Python talky-talk
                      ^
SyntaxError: invalid syntax

It appears that the interpreter is catching the syntax error before
the code is even executed.

Second, python.org's exception hierarchy documentation (Section 6.1 at
http://docs.python.org/library/exceptions.html) shows all exception
types except SystemExit, KeyboardInterrupt and GeneratorExit as being
descendants of Exception. This includes SyntaxError, a child of
StandardError which is itself a child of Exception. So, if I say
'except Exception:' then isn't that clause going to process any child
exception type of Exception (including SyntaxError) that I haven't
already covered in a separate except clause? (Except of course that my
interpreter doesn't seem to treat a syntax error as any kind of
exception at all!)

Finally, and this does not apply to your comments in particular, in
researching around about exception handling I often see the usage

   except Exception, e:

suggested, but can't for the life of me figure out what the heck the
', e' part does. Can anybody explain what this means and why it might
be desirable (or not)?

Thanks!





More information about the Python-list mailing list