[Python-ideas] Python 3000 TIOBE -3%

Terry Reedy tjreedy at udel.edu
Tue Feb 14 23:29:24 CET 2012


On 2/14/2012 6:39 AM, Carl M. Johnson wrote:
>
> On Feb 13, 2012, at 10:45 PM, Nick Coghlan wrote:
>
>> (Of course, if sys.stdout.encoding is "UTF-8", then you're right, those
>> characters will just be displayed as gibberish, as they would in the
>> latin-1 case. I guess its only on Windows and in any other locations
>> with a more restrictive default stdout encoding that errors are
>> particularly likely).
>
> I don't think that's right. I think that by default Python refuses to turn surrogate characters into UTF-8:
>
>>>> bytes(range(256)).decode("ascii", errors="surrogateescape")
> '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\udc80\udc81\udc82\udc83\udc84\udc85\udc86\udc87\udc88\udc89\udc8a\udc8b\udc8c\udc8d\udc8e\udc8f\udc90\udc91\udc92\udc93\udc94\udc95\udc96\udc97\udc98\udc99\udc9a\udc9b\udc9c\udc9d\udc9e\udc9f\udca0\udca1\udca2\udca3\udca4\udca5\udca6\udca7\udca8\udca9\udcaa\udcab\udcac\udcad\udcae\udcaf\udcb0\udcb1\udcb2\udcb3\udcb4\udcb5\udcb6\udcb7\udcb8\udcb9\udcba\udcbb\udcbc\udcbd\udcbe\udcbf\udcc0\udcc1\udcc2\udcc3\udcc4\udcc5\udcc6\udcc7\udcc8\udcc9\udcca\udccb\udccc\udccd\udcce\udccf\udcd0\udcd1\udcd2\udcd3\udcd4\udcd5\udcd6\udcd7\udcd8\udcd9\udcda\udcdb\udcdc\udcdd\udcde\udcdf
>   \udce0\udce1\udce2\udce3\udce4\udce5\udce6\udce7\udce8\udce9\udcea\udceb\udcec\udced\udcee\udcef\udcf0\udcf1\udcf2\udcf3\udcf4\udcf5\udcf6\udcf7\udcf8\udcf9\udcfa\udcfb\udcfc\udcfd\udcfe\udcff'

While this is a Py3 str object, it is not unicode. Unicode only only 
allows proper surrogate codeunit pairs. Py2 allowed mal-formed unicode 
objects and that was not changed in Py3 -- or 3.3. It seems appropriate 
that bytes that are meaningless to ascii should be translated to 
codeunits that are meaningless (by themselves) to unicode.

>>>> _.encode("utf-8")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> UnicodeEncodeError: 'utf-8' codec can't encode character '\udc80' in position 128: surrogates not allowed

utf-8 only encodes proper unicode.

>>>> _.encode("utf-8", errors="surrogateescape")
> b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'

The result is not utf-8 and it would be better not to use 'utf-8' 
instead of 'ascii' in the expression. The above encodes to ascii + 
uninterpreted high-bit-set bytes.

 >>> s=bytes(range(256)).decode("ascii", errors="surrogateescape")
 >>> u=s.encode("utf-8", errors="surrogateescape")
 >>> a=s.encode("ascii", errors="surrogateescape")
 >>> u == a
True

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list