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:
at the end.
And then change setup.py from:
windows = ["test_wx.py"],
console = ["hello.py"],
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'
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
As a simple workaround I wrote the following:
def _emptyflush(): pass
if not isinstance(sys.stdout, file) and not
sys.stdout.__dict__['flush'] = _emptyflush
if not isinstance(sys.stderr, file) and not
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.
Log in to post a comment.