Problem with StreamReaderWriter on 3.6.3? SOLVED

Peter pacqa100 at yahoo.com.au
Fri Oct 20 06:46:46 EDT 2017


Thanks MRAB, your solution works a treat.

I'm replying to the list so others can know that this solution works. 
Note that sys.stderr.detach() is only available in >= 3.1, so one might 
need to do some version checking to get it to work properly in both 
versions. It also can mess up with the buffering and therefore the order 
of the output of stderr vs stdout.

Thanks again.

Peter


On 20/10/2017 10:19 AM, MRAB wrote:
> 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