[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

sys.stdout assign to- bug

Aaron Brady

3/12/2008 10:55:00 AM

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 )

>>> a
>>> 0
0

Python 3.0a2 WinXP, on the console. 'a' is undeclared but error
message isn't thrown. With 'sys.stdout= Thr...' commented:

>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 0
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?
1 Answer

Aaron Brady

3/13/2008 7:10:00 AM

0

> 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?