Python 3 is killing Python

Terry Reedy tjreedy at udel.edu
Thu Jul 17 00:47:37 CEST 2014


On 7/16/2014 5:02 PM, Terry Reedy wrote:
> On 7/16/2014 3:49 AM, Steven D'Aprano wrote:
>
>> There are certainly use-cases for stdin and stdout to use bytes, but
>> there are also use-cases for them to deal with strings. I'll certainly
>> grant you that there ought to be an easy way to get access to the binary
>> streams,
>
> As has been discussed before on this list, there is in 3.x.
> https://docs.python.org/3/library/sys.html#sys.stdin
>
>  >>> b=sys.stdin.buffer.readline()
> a line
>  >>> b
> b'a line\r\n'
>
> In other words, 3.x text mode (which essentially nothing to do with 2.x
> 'text' mode), is a wrapped binary mode that gives users the *choice* to
> read bytes or text.

One can also convert a stream permanently with .detach()
 >>> import sys
 >>> sys.stdin = sys.stdin.detach()
 >>> b = sys.stdin.readline()
a line
 >>> b
b'a line\r\n'

This does diable the input() function ;-).
 >>> b = input()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: '_io.BufferedReader' object has no attribute 'errors'

-- 
Terry Jan Reedy




More information about the Python-list mailing list