sys.stdout assign to- bug

C

castironpi

I'm actually intimidated enough by a few tries I make to say something
on Python-Ideas, that I thought I'd run this by youguys first.

import sys
class ThreadedOut:
def __init__( self, old ):
self._old= old
def write( self, s ):
self._old.write( s )
sys.stdout= ThreadedOut( sys.stdout )
0

Python 3.0a2 WinXP, on the console. 'a' is undeclared but error
message isn't thrown. With 'sys.stdout= Thr...' commented:
Traceback (most recent call last):
0

But the docs say:

stdout and stderr needn't be built-in file objects: any object is
acceptable as long as it has a write() method that takes a string
argument.

What's the catch?
 
C

castironpi

import sys
class ThreadedOut:
        def __init__( self, old ):
                self._old= old
        def write( self, s ):
                self._old.write( s )
sys.stdout= ThreadedOut( sys.stdout )

Python 3.0a2 WinXP, on the console.  'a' is undeclared but error
message isn't thrown.  With 'sys.stdout= Thr...' commented:
stdout and stderr needn't be built-in file objects: any object is
acceptable as long as it has a write() method that takes a string
argument.

Adding

def flush( self ):
self._old.flush()

fixed it. Can we get that in the docs?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top