Help save net neutrality! Learn more.
Close

#45 Windows stderr doesn't have flush()

closed-accepted
nobody
None
5
2004-04-26
2004-04-18
No

When compiling to a window program (not console), the
sys.stderr and sys.stdout wrappers do not implement
.flush(). This is very easy to demonstrate. Simply
change the hello.py script by adding:

sys.stdout.flush()
sys.stderr.flush()

at the end.
And then change setup.py from:
windows = ["test_wx.py"],
console = ["hello.py"],
to:
windows = ["test_wx.py", "hello.py"],

Then when you go to run the hello.py module you will get:
AttributeError: 'Blackhole' object has no attribute 'flush'
or
AttributeError: 'Stderr' object has no attribute 'flush'

It would be nicer if Blackhole and Stderr had a flush()
even if it was a no-op. (Because most file objects have
one).

As a simple workaround I wrote the following:

import sys
def _emptyflush(): pass
if not isinstance(sys.stdout, file) and not
hasattr(sys.stdout, 'flush'):
sys.stdout.__dict__['flush'] = _emptyflush
if not isinstance(sys.stderr, file) and not
hasattr(sys.stderr, 'flush'):
sys.stderr.__dict__['flush'] = _emptyflush

I tried doing type() but they are classes, not
instances, so I'm not sure how to look for it. And if I
look for Blackhole/Stderr, they don't exist in normal
python. This at least seems to always work.

I ran into this because the default configuration for
the python logging mechanism writes to stderr. And if
you create a warning (log.warning) or greater, it tries
to flush the log to force it to get written. This fails
with a no flush attribute.

Discussion

  • Thomas Heller

    Thomas Heller - 2004-04-26

    Logged In: YES
    user_id=11105

    FYI: The Blackhole and Stderr objects live in
    lib/site-packages/py2exe/boot_common.py.
    I've added flush methods to them in CVS.
    Thanks for the report.

     
  • Thomas Heller

    Thomas Heller - 2004-04-26
    • status: open --> closed-accepted
     

Log in to post a comment.