473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

sys.stderr.write and sys.exit

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
3 9266
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bryan Olson | last post by:
Here's a module to show stderr output from console-less Python apps, and stay out of the way otherwise. I plan to make a ASPN recipe of it, but I thought I'd run it by this group first. To use...
4
by: | last post by:
I want to write all produced error messages (error, warning, notify, etc...) to STDERR. Is this possible without writing my own error handling routine?
0
by: Bryan Olson | last post by:
New and improved! Love Python's stack-tracing error messages, but hate the way GUI applications throw the messages away and crash silently? Here's a module to show Python error messages that...
2
by: ndessai | last post by:
Hi All, I had a project which used to redirect stderr to a file in VC 6. (working fine) When I ported it to VC 7 it does not put messages in the file. Any ideas??? Thanks,
3
by: Moretti | last post by:
Hello, I would like to be able to change the standard error path. How can I do this in a script if I want to send error messages to /dev/null by example ? Thanks -- Sebastien Moretti
0
by: Steven Bethard | last post by:
I have an optparse-like module and though I have a full unittest-style suite of tests for it, I'd also like to be able to run doctest on the documentation to make sure my examples all work. ...
37
by: Vince C. | last post by:
Hi all. I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using MinGW 3.4.2. I'd like to temporarily disable standard functions to write to stderr, i.e. for instance...
5
by: sakismat | last post by:
please help how can I get stderr from processes ($proc) in my screen without waiting the other processes to end #!/usr/bin/php <?php $con = mysql_connect("localhost", "user"); if (!$con)
1
by: Lincoln Yeoh | last post by:
Hi, I've just started to learn python (I've been using perl for some years). How do I redirect ALL stderr stuff to syslog, even stderr from external programs that don't explicitly change their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.