Message189747
> However, the two intro paragraphs need a bit of work.
Yes, it's a help which I needed. Thank you.
However your wording is not entirely correct. In 2.7 binary-to-binary codecs and rot-13 works with Unicode strings (only ascii-compatible) as with bytes strings.
>>> u'Python'.encode('base64')
'UHl0aG9u\n'
>>> u'UHl0aG9u'.decode('base64')
'Python'
>>> u'Python\u20ac'.encode('base64')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython-2.7/Lib/encodings/base64_codec.py", line 24, in base64_encode
output = base64.encodestring(input)
File "/home/serhiy/py/cpython-2.7/Lib/base64.py", line 315, in encodestring
pieces.append(binascii.b2a_base64(chunk))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 6: ordinal not in range(128)
Rot-13 works as common text-to-binary encoding (encode returns str, decode returns unicode).
>>> u'Python'.encode('rot13')
'Clguba'
>>> u'Python'.decode('rot13')
u'Clguba'
>>> 'Python'.encode('rot13')
'Clguba'
>>> 'Python'.decode('rot13')
u'Clguba'
>>> u'Python\u20ac'.encode('rot13')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython-2.7/Lib/encodings/rot_13.py", line 17, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 6: character maps to <undefined>
>>> u'Python\u20ac'.decode('rot13')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython-2.7/Lib/encodings/rot_13.py", line 20, in decode
return codecs.charmap_decode(input,errors,decoding_map)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 6: ordinal not in range(128) |
|
Date |
User |
Action |
Args |
2013-05-21 12:39:04 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, lemburg, doerwalter, ncoghlan, docs@python |
2013-05-21 12:39:04 | serhiy.storchaka | set | messageid: <1369139944.85.0.436449420415.issue17844@psf.upfronthosting.co.za> |
2013-05-21 12:39:04 | serhiy.storchaka | link | issue17844 messages |
2013-05-21 12:39:04 | serhiy.storchaka | create | |
|