473,794 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redirecting stderr to null and revert

Hello,
I have some function that output too much stuff to stderr when called,
and I have no control over the function itself. So I thought as a
workaround, I redirect stderr to /dev/null before calling that
function, and then revert the stderr back after calling that
function.
I have something like this:

def nullStderr():
sys.stderr.flus h()
err = open('/dev/null', 'a+', 0)
os.dup2(err.fil eno(), sys.stderr.file no())

def revertStderr():
sys.stderr = sys.__stderr__

Then when calling the function, I want to do:
nullStderr()
noisyFunction(f oo, bar)
revertStderr()

The problem is the "revertStderr() " doesn't work, ie. I still get no
stderr back after calling it (stderr still redirects to /dev/null). I
want stderr back so that when the script fails in different places
later I can get error messages. What am I missing here ? Thanks a lot
for any help.

RDB

Aug 6 '07 #1
1 3079
reubendb wrote:
def nullStderr():
sys.stderr.flus h()
err = open('/dev/null', 'a+', 0)
os.dup2(err.fil eno(), sys.stderr.file no())

def revertStderr():
sys.stderr = sys.__stderr__
You're doing the redirection at one level and
trying to revert it at a different level.

If this is a Python function that's doing its
output by writing to sys.stderr, there's a
much simpler way to do the redirection:

sys.stderr = open('/dev/null', 'w')

(that's the Python builtin function 'open',
not the one in os). Then your revertStderr
function will work.

BTW I'd arrange for the reversion to be done
in a try-finally, e.g.

nullStderr()
try:
do_something()
finally:
revertStderr()

so you won't get stuck with a redirected stderr
if an exception occurs, and thereby not be able
to see the traceback!

--
Greg
Aug 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3762
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My problem is the C++ lib does write stuffs on its stdout, and I would like to print those messages in a Tk frame. When I run the computation, it has it's own thread. So my question is : how van I redirect the dll's stdout to something I can
3
3435
by: Laszlo Zsolt Nagy | last post by:
Hello, I have this code: s = smtplib.SMTP() s.set_debuglevel(1) s.connect(host=smtp_host) s.set_debuglevel(0) log("Connected, sending e-mail") sys.stdout.flush()
9
1767
by: Fuzzyman | last post by:
Hello, I'm trying to redirect standard out in a single namespace. I can replace sys.stdout with a custom object - but that affects all namespaces. There will be code running simultaneously that could import sys afterwards - so I don't want to make the change in the sys module.
18
9263
by: praetor.michael | last post by:
I have a DLL written in C that writes to stderr. I have a win32 console application that makes calls to the DLL. In the console app I redirect stderr to a file using freopen. The problem I'm having is that none of the messages sent to stderr from the DLL are getting written to the file but if I write to stderr from the console application it gets written to the file no problem. Any ideas??
116
4642
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however, when I run it, it stalls and does nothing. I'm wondering if there's something obvious that I'm missing. My code is below and any help would be appreciated. Thanks, Dave
10
4070
by: Guillaume Dargaud | last post by:
Hello all, I have some error checking using the function 'perror', which writes messages on stderr. I'd like to send all error messages to a file instead. Is there some way to do this, short of replacing all the perror calls with a custom function ? -- Guillaume Dargaud http://www.gdargaud.net/
1
1730
by: grbgooglefan | last post by:
I am in a perculiar situation. I want to use PyRun_SimpleString for creating Python functions in embedded Python in C++. But there could be cases when Python function code compilation could fail & PyRun_SimpleString will return -1 as return status. At this time, it prints the error message to sys.stderr. So, we do not have any control or cannot use that message to show to user to correct the errors in the function code. I could assign an...
1
6702
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
1
3328
by: n00b | last post by:
greetings, i would like to redirect stdout/err to a mysql table and would like a) some peer review and b) suggestions for hardening the approach for a general purpose class. thank you very much. import sys import MySQLdb class DBLogger(object): def __init__(self):
0
9672
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
10435
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
10213
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...
0
9037
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...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
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
5436
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.