By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

sys.stderr.write and sys.exit

P: n/a
Is the same use _sys.stderr.write('error message'); sys.exit(1)_ than
_sys.exit('error message')_ ?

Note: help(sys.exit)
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is numeric, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system exit
status will be one (i.e., failure).

Nov 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"GinTon" <jo*******@googlemail.comwrites:
Is the same use _sys.stderr.write('error message'); sys.exit(1)_
than _sys.exit('error message')_ ?
(Note: The underscore '_' is a valid character in Python code, so I
was quite confused by what you wrote and had to read it several times
to see that you were intending the underscores not to be part of the
code. Better to show a line of code on its own line in your message.)

Code that wants to catch SystemExit will get a different exception
object in each case::
>>import sys
from StringIO import StringIO
sys.stderr = StringIO()
>>try:
... sys.stderr.write('error message')
... sys.exit(1)
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getvalue()
... print "e.code is:", e.code
...
stderr contains: error message
e.code is: 1
>>try:
... sys.exit('error message')
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getvalue()
... print "e.code is:", e.code
...
stderr contains: error message
e.code is: error message

I quite often catch SystemExit in unit tests, or other code that is
inspecting a program module.

--
\ "I have a large seashell collection, which I keep scattered on |
`\ the beaches all over the world. Maybe you've seen it." -- |
_o__) Steven Wright |
Ben Finney

Nov 23 '06 #2

P: n/a
Thanks Ben Finney. So it's understood very well.

Ben Finney ha escrito:
"GinTon" <jo*******@googlemail.comwrites:
Is the same use
> >>sys.stderr.write('error message'); sys.exit(1)
than
> >>sys.exit('error message') ?

Code that wants to catch SystemExit will get a different exception
object in each case::
>>import sys
>>from StringIO import StringIO
>>sys.stderr = StringIO()
>>try:
... sys.stderr.write('error message')
... sys.exit(1)
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getvalue()
... print "e.code is:", e.code
...
stderr contains: error message
e.code is: 1
>>try:
... sys.exit('error message')
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getvalue()
... print "e.code is:", e.code
...
stderr contains: error message
e.code is: error message

I quite often catch SystemExit in unit tests, or other code that is
inspecting a program module.
Nov 23 '06 #3

P: n/a
Dennis Lee Bieber <wl*****@ix.netcom.comwrites:
Ben Finney wrote:
(Note: The underscore '_' is a valid character in Python code, so
I was quite confused by what you wrote and had to read it several
times to see that you were intending the underscores not to be
part of the code. Better to show a line of code on its own line in
your message.)

Old "rich text" email format effectors... *word* =bolded, /word/
=italic, _word_ =underscored.
Yes, I use those (or similar) myself, but only in natural language
where the context makes it clear that the punctuation characters are
not intended to be part of the content. When showing program code,
it's a poor choice to use punctuation characters for such markup,
since it's not clear which ones are part of the code.

--
\ "It's a good thing we have gravity or else when birds died |
`\ they'd just stay right up there. Hunters would be all |
_o__) confused." -- Steven Wright |
Ben Finney

Nov 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.