Problem with StreamReaderWriter on 3.6.3?

MRAB python at mrabarnett.plus.com
Thu Oct 19 19:19:17 EDT 2017


On 2017-10-19 22:46, Peter via Python-list wrote:
> I came across this code in Google cpplint.py, a Python script for
> linting C++ code. I was getting funny results with Python 3.6.3, but it
> worked fine under 2.7.13
> 
> I've tracked the problem to an issue with StreamReaderWriter; the
> traceback and error never shows under 3. The _cause_ of the error is
> clear (xrange not in Py3), but I need the raised exception to show.
> 
> I'm running Python 3.6.3 32bit on Windows 10. I also get the same
> results on Python 3.5.2 on Ubuntu (under WSL)
> 
> I'm not super familiar with rebinding stderr with codecs, but I'm
> guessing they are doing it because of some Unicode issue they may have
> been having.
> 
> I have a workaround - drop the rebinding - but it seems like there might
> be an error in StreamReaderWriter.
> Do other people see the same behaviour?
> Is there something I'm not seeing or understanding?
> Would I raise it on issue tracker?
> 
> Peter
> 
> ----------------------------------------------------------
> 
> import sys
> import codecs
> 
> sys.stderr = codecs.StreamReaderWriter(
>       sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'),
> 'replace')
> 
> # This should work fine in Py 2, but raise an exception in Py3
> # But instead Py3 "swallows" the exception and it is never seen
> print(xrange(1, 10))
> 
> # Although this line doesn't show in Py 3 (as the script has silently
> crashed)
> print("This line doesn't show in Py 3")
> 
> ----------------------------------------------------------
> 
StreamReaderWriter is being passed an encoder which returns bytes 
(UTF-8), but the output stream that is being passed, to which it will be 
writing those butes, i.e. the original sys.stderr, expects str.

I'd get the underlying byte stream of stderr using .detach():

sys.stderr = codecs.StreamReaderWriter(sys.stderr.detach(), 
codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace')



More information about the Python-list mailing list