strange subprocess behavior when calling ps

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Nov 18 05:50:39 EST 2010


Roger Davis wrote:
>> 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.
>
>   
That's right, 'cause my example is approximative. You would need to 
import that file.
A better example would be to fire a NameError exception.
In the above code of fix the syntax error, but replace "for line in 
pslines" by "for line in psLines". note the typo, very common mistake.
Then see how the error is hidden by your handler.

> 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?
You're absolutely right. Using Exception is bad, using bare except is 
even worse. Exception is slightly better than except because you can at 
least get the exception instance that has been raised, print it in your 
handler, so you don't loose information (well, with a bare except you 
can still inspect some sys objects to get info about the last exception 
but it's a bit hackish).

>  (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!
>
>
>   


Exception is the class, e is an instance of that class that has been 
actually fired, it contains informations, most of the time a message. 
This syntax has changed in python 3 I think, that would mean that you're 
right to be confused, this python 2 syntax is ... strange...

import sys

# what if someone writes sys.hihi ?
try:
    raise AttributeError("sys object has no attribute 'hihi'")
except Exception, e:
    print e.args
    print e.__class__
    print e.message
    print e.__doc__

output:
("sys object has no attribute 'hihi'",)
<type 'exceptions.AttributeError'>
sys object has no attribute 'hihi'
Attribute not found.


Apart from all these details, just keep in mind this golden rule:
- Don't use bare except clause.
And you'll be fine.

JM





More information about the Python-list mailing list