473,769 Members | 5,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sys.stderr.writ e and sys.exit

Is the same use _sys.stderr.wri te('error message'); sys.exit(1)_ than
_sys.exit('erro r 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 9317
"GinTon" <jo*******@goog lemail.comwrite s:
Is the same use _sys.stderr.wri te('error message'); sys.exit(1)_
than _sys.exit('erro r 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.writ e('error message')
... sys.exit(1)
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getv alue()
... 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.getv alue()
... 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*******@goog lemail.comwrite s:
Is the same use
> >>sys.stderr.wr ite('error message'); sys.exit(1)
than
> >>sys.exit('err or 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.writ e('error message')
... sys.exit(1)
... except SystemExit, e:
... print "stderr contains:", sys.stderr.getv alue()
... 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.getv alue()
... 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.net com.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
2048
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 it, import the module. That's it. Upon import it will assign sys.stderr. In the normal case, your code is perfect so nothing ever gets written to stderr, and the module won't do much of anything. Upon the first write to stderr, if any, the...
4
9606
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
2028
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 would otherwise be lost in console-less programs. Graphical applications should not require console windows, but they do need a workable destination for sys.stderr.
2
1084
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
2297
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
1257
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. However, I see that doctest (1) doesn't capture anything from sys.stderr, and (2) unlike the normal interpreter, generates a traceback for SystemExit. Here's what some of my tests look like and the doctest output I get:: ========== in my...
37
5074
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 redirect stderr to a temporary file (or /dev/null but is there an equivalent under Windows? Is it "nul:") and then to *restore* the default stderr so that standard library functions that write to stderr produce output again.
5
3107
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
6697
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 own stderr? Say I have a program called foo: #!/usr/bin/python
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.