[IPython-dev] Re: Changes to Notebook Format

Robert Kern rkern at ucsd.edu
Thu Aug 4 02:29:12 EDT 2005


Hans Meine wrote:
> On Monday 01 August 2005 21:51, Fernando Perez wrote:
> 
>>mmh, scratch that.  Upon thinking a bit more, it seems to me that since our
>>output proxies have to trap all write() calls, [...]
> 
> BTW: Did you find a way to catch stderr output from binary extension modules?

Yes! I didn't have any luck with doing the file descriptor magic in pure 
Python, but a little bit of Pyrex (attached) works wonders. It works for 
Python (without touching the sys.stdout or sys.stderr objects), C's 
printf and fprintf, C++'s cout and cerr, and FORTRAN's WRITE. It needs 
to be rewritten to carefully handle errors, but the functionality is 
there. It works on OS X; I presume it will work on most sane UNIX's. I 
think it will probably also work on Windows, but I wouldn't care to 
wager real money on that belief.

Of course, redirecting stdout and stderr like this forgoes any 
interleaving.  ;-)

# test.py
import sys

import redirc
rout = redirc.Redirector(redirc.STDOUT)
rerr = redirc.Redirector(redirc.STDERR)

import ctest
import cxxtest
import ftest

rout.start()
rerr.start()
print "Python: stdout"
print >>sys.stderr, "Python: stderr"
ctest.stdout()
ctest.stderr()
cxxtest.stdout()
cxxtest.stderr()
ftest.stdo()
out = rout.stop()
err = rerr.stop()

print 'stdout:'
print repr(out)
print
print 'stderr:'
print repr(err)

# and the output:
stdout:
'Python: stdout\nC: stdout\nC++: stdout\n FORTRAN: stdout\n'

stderr:
'Python: stderr\nC: stderr\nC++: stderr\n'

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: redirc.pyx
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20050803/fc156b00/attachment.ksh>


More information about the IPython-dev mailing list