sys.stdout assign to- bug

Discussion in 'Python' started by castironpi@gmail.com, Mar 12, 2008.

  1. Guest

    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?
     
    , Mar 12, 2008
    #1
    1. Advertising

  2. Guest

    > 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?
     
    , Mar 13, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    670
    Roman Neuhauser
    Apr 4, 2005
  2. Replies:
    2
    Views:
    666
    velle
    Jan 5, 2006
  3. Replies:
    0
    Views:
    491
  4. Qiangning Hong

    sys.stdout.write()'s bug or doc bug?

    Qiangning Hong, Dec 26, 2008, in forum: Python
    Replies:
    4
    Views:
    513
    Steven D'Aprano
    Jan 14, 2009
  5. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    581
    Nobody
    Jan 11, 2010
Loading...

Share This Page