473,395 Members | 2,783 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,395 software developers and data experts.

Catching stderr output from graphical apps


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 module will launch a
new process, and that process will show the stderr output in a
window. The window will live until dismissed; I hate, hate, hate
those vanishing-consoles-with-critical-information.
The code shows some arguably-cool tricks. To fit everthing in
one file, the module runs the Python interpreter on itself; it
uses the "if __name__ == '__main__'" idiom to behave radically
differently upon import versus direct execution. It uses TkInter
for the window, but that's in a new process; it does not import
TkInter into your application.

To try it out, save it to a file -- I call it "errorwindow.py" -
- and import it into some subsequently-incorrect code. For
example:

import errorwindow

a = 3 + 1 + nonesuchdefined

should cause a window to appear, showing the traceback of a
Python NameError.

--
--Bryan
----------------------------------------------------------------

"""

Import this module into graphical Python apps to provide a
sys.stderr. No functions to call, just import it. It uses
only facilities in the Python standard distribution.

If nothing is ever written to stderr, then the module just
sits there and stays out of your face. Upon write to stderr,
it launches a new process, piping it error stream. The new
process throws up a window showing the error messages.

"""

import sys
import os
import thread

if __name__ == '__main__':

from Tkinter import *
import Queue
queue = Queue.Queue(99)
def read_stdin(app):
while 1:
data = os.read(sys.stdin.fileno(), 2048)
queue.put(data)
if not data:
break
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title("Error Stream from run of %s" % sys.argv[-1])
self.pack(fill=BOTH, expand=YES)
self.logwidget = Text(self)
self.logwidget.pack(side=TOP, fill=BOTH, expand=YES)
# Disallow key entry, but allow copy with <Control-c>
self.logwidget.bind('<Key>', lambda x: 'break')
self.logwidget.bind('<Control-c>', lambda x: None)
self.after(200, self.start_thread, ())
def start_thread(self, _):
thread.start_new_thread(read_stdin, (self,))
self.after(200, self.check_q, ())
def check_q(self, _):
go = True
while go:
try:
data = queue.get_nowait()
if not data:
self.logwidget.configure(foreground ='#0000AA')
data = "\n==== File Closed ====\n"
go = False
self.logwidget.insert(END, data)
self.logwidget.see(END)
except Queue.Empty:
self.after(200, self.check_q, ())
go = False
app = Application()
app.mainloop()

else:

class ErrorPipe(object):
def __init__(self):
self.lock = thread.allocate_lock()
self.empty = True
def on_first_write(self):
command = "%s %s %s" % (sys.executable, __file__, sys.argv[0])
self.pipe = os.popen(command, 'w')
def _write(self, data):
self.pipe.write(data)
os.fsync(self.pipe)
def write(self, data):
self.lock.acquire()
try:
if self.empty:
self.on_first_write()
self.empty = False
self._write(data)
finally:
self.lock.release()
sys.stderr = ErrorPipe()

Aug 10 '05 #1
9 2029
Bryan Olson wrote:
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.


For what it's worth, I believe you've basically duplicated the
functionality available in wxPython in the standard App object.

-Peter
Aug 10 '05 #2
Peter Hansen wrote:
Bryan Olson wrote:
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.


For what it's worth, I believe you've basically duplicated the
functionality available in wxPython in the standard App object.


I'm not a wxPython user, but what I've heard is that it tries to
write stderr to a regular window within the process. Crashing
apps vanish from the screen, taking their error windows with
them. Most any GUI toolkit has to return to its event-loop
before it will update the screen, preventing it from showing
messages of fatal errors.
--
--Bryan
Aug 10 '05 #3
gry
Python 2.3.3, Tkinter.__version__'$Revision: 1.177 $'

Hmm, the error window pops up with appropriate title, but contains no
text.
I stuck an unbuffered write to a log file in ErrorPipe.write and got
only one line: Traceback (most recent call last):$

Any idea what's wrong?

-- George

Aug 10 '05 #4
gr*@ll.mit.edu wrote:
Python 2.3.3, Tkinter.__version__'$Revision: 1.177 $'

Hmm, the error window pops up with appropriate title, but contains no
text.
I stuck an unbuffered write to a log file in ErrorPipe.write and got
only one line: Traceback (most recent call last):$

Any idea what's wrong?


Darn. Are you on Windoze/Mac/Unix/, and what version?

If you can spare the time, could you please try:
import errorwindow
import sys

sys.stderr.write("Hello\nWorld.\n")

x = 7 + nosuchvariable
and tell me what happens?

Thanks.
--
--Bryan
Aug 10 '05 #5
gry
Linux -2.4.20 (x86), Python 2.3.3.
I did exactly as you suggested.
After the stderr.write, a window pops up with title "Error Stream from
run of errorwindow.pyc".
The window is otherwise blank.
Nothing more happens when I do the "x=7+nosuchvariable", I just get
the next python ">>>" prompt, but no error messages.
When I try to exit the python, e.g. sys.exit(), it hangs and I have to
Control-C.
After the control-C, the "Error Stream..." window goes away.

Any more suggestions? Perhaps we should take this to email, instead of
newsgroup?
This *is* something I would like to be able to use, if we can get it
working.

-- George

Aug 12 '05 #6

Thanks.

Yeah, guess I was naive to test on Windows and expect that kind
of process stuff to be portable. I'll be away from Linux for a
week or so, so this will take me a while.

Further bulletins as events warrant.

--
--Bryan
Aug 12 '05 #7
Bryan Olson wrote:

Thanks.

Yeah, guess I was naive to test on Windows and expect that kind
of process stuff to be portable. I'll be away from Linux for a
week or so, so this will take me a while.

Further bulletins as events warrant.


If you can get a cross-platform solution, please re-annoucne it; this
sounds like a really neat module to have handy for graphical programs.
Aug 12 '05 #8
Christopher Subich wrote:
Bryan Olson wrote:
Thanks.

Yeah, guess I was naive to test on Windows and expect that kind
of process stuff to be portable. I'll be away from Linux for a
week or so, so this will take me a while.

Further bulletins as events warrant.


If you can get a cross-platform solution, please re-annoucne it; this
sounds like a really neat module to have handy for graphical programs.


Look at py.io[1]. It seems to have implemented a probably-cross-platform
solution. Please check it out and let c.l.py and the py mailing list
know if it works on Windows.

[1] http://codespeak.net/py/current/doc/home.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 13 '05 #9
Robert Kern wrote:
Christopher Subich wrote:
If you can get a cross-platform solution, please re-annoucne it; this
sounds like a really neat module to have handy for graphical programs.

Look at py.io[1]. It seems to have implemented a probably-cross-platform
solution. Please check it out and let c.l.py and the py mailing list
know if it works on Windows.

[1] http://codespeak.net/py/current/doc/home.html


Thanks guys. I found I had a bootable Linux system. With some
stuff from py.io, and *without* the fsync(), this one worked on
Windows and Linux in my not-so-extensive testing.

--
--Bryan

"""

Import this module into graphical Python apps to provide a
sys.stderr. No functions to call, just import it. It uses
only facilities in the Python standard distribution.

If nothing is ever written to stderr, then the module just
sits there and stays out of your face. Upon write to stderr,
it launches a new process, piping it error stream. The new
process throws up a window showing the error messages.

"""

import sys
import os
import thread

import time

if __name__ == '__main__':

from Tkinter import *
import Queue
queue = Queue.Queue(99)
def read_stdin(app):
fd = os.dup(sys.stdin.fileno())
infile = os.fdopen(fd, 'r', 0)
while 1:
data = os.read(infile.fileno(), 2048)
queue.put(data)
if not data:
break
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title("Error Stream from run of %s" % sys.argv[-1])
self.pack(fill=BOTH, expand=YES)
self.logwidget = Text(self)
self.logwidget.pack(side=TOP, fill=BOTH, expand=YES)
# Disallow key entry, but allow copy with <Control-c>
self.logwidget.bind('<Key>', lambda x: 'break')
self.logwidget.bind('<Control-c>', lambda x: None)
self.after(200, self.start_thread, ())
def start_thread(self, _):
thread.start_new_thread(read_stdin, (self,))
self.after(200, self.check_q, ())
def check_q(self, _):
go = True
while go:
try:
data = queue.get_nowait()
if not data:
self.logwidget.configure(foreground ='#0000AA')
data = "\n==== File Closed ====\n"
go = False
self.logwidget.insert(END, data)
self.logwidget.see(END)
except Queue.Empty:
self.after(200, self.check_q, ())
go = False
app = Application()
app.mainloop()

else:

class ErrorPipe(object):
def __init__(self):
self.lock = thread.allocate_lock()
self.pipe = None
def on_first_write(self):
command = "%s %s %s" % (sys.executable, __file__, sys.argv[0])
self.rawpipe = os.popen(command, 'w')
fd = os.dup(self.rawpipe.fileno())
self.pipe = os.fdopen(fd, 'w', 0)
def write(self, data):
self.lock.acquire()
try:
if not self.pipe:
self.on_first_write()
self.pipe.write(data)
finally:
self.lock.release()
sys.stderr = ErrorPipe()
# sys.stdout = ErrorPipe()
Aug 13 '05 #10

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

Similar topics

6
by: Farshid Lashkari | last post by:
Hi, My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush...
21
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
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...
7
by: Andre | last post by:
Hi, I have a program that sends some output to stdout and some to stderr. I need to separate the two using the command-line so that I direct stderr output to a file, say fileA.txt, and stdout...
5
by: Philip Hölzenspies | last post by:
Hi All, I have been crunching my brains on this one all day. I use this library that creates output I want, but it can only write it to a file or the stdout. The problem is that I don't want to...
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
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...
8
by: r0g | last post by:
Hi There, I'm trying to migrate a locally hosted page from windows to a private Ubuntu development server and one of the key bits of functionality (which used to work fine in Windows) was the...
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
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
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,...
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,...
0
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...
0
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...

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.