classification
Title: rot_13 codec not working
Type: behavior Stage:
Components: Documentation, Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: codecs missing: base64 bz2 hex zlib hex_codec ...
View: 7475
Assigned To: docs@python Nosy List: cool-RR, docs@python, ezio.melotti, petri.lehtinen
Priority: normal Keywords:

Created on 2011-12-14 08:56 by cool-RR, last changed 2011-12-14 10:48 by petri.lehtinen. This issue is now closed.

Messages (5)
msg149431 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-12-14 08:56
The `rot_13` codec is supposed to work like this, no?

>>> 'qwerty'.encode('utf-8')
b'qwerty'
>>> 'qwerty'.encode('rot_13')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    'qwerty'.encode('rot_13')
TypeError: encoder did not return a bytes object (type=str)

>>> b'qwerty'.decode('utf-8')
'qwerty'
>>> b'qwerty'.decode('rot_13')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    b'qwerty'.decode('rot_13')
  File "C:\Python32\lib\encodings\rot_13.py", line 19, in decode
    return (input.translate(rot13_map), len(input))
AttributeError: 'memoryview' object has no attribute 'translate'
msg149434 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-12-14 09:08
See #7475.
msg149436 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-12-14 09:30
Ram Rachum wrote:
> The `rot_13` codec is supposed to work like this, no?

No it isn't. In Python 3, str.encode() always encodes to bytes and bytes.decode() always decodes to str. IOW, str.encode() encodes text (Unicode) to data (bytes), and bytes.decode() decodes data to text. ROT-13 is not a character encoding, so it cannot be used in this manner.

It's still available in the codecs module, though:

>>> import codecs
>>> codecs.lookup('rot-13').encode('qwerty')
('djregl', 6)

Other text->text and bytes->bytes codecs are also available there.
msg149437 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-12-14 09:47
Then I suggest replacing this error message:

    encoder did not return a bytes object (type=str)

and this one:

    'memoryview' object has no attribute 'translate'

With something like:

    Please use `codecs.lookup('rot-13').encode`
msg149438 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-12-14 10:48
Issue #7475 discusses fixing the error messages, too.
History
Date User Action Args
2011-12-14 10:48:42petri.lehtinensetstatus: open -> closed
superseder: codecs missing: base64 bz2 hex zlib hex_codec ...
messages: + msg149438

components: + Library (Lib)
resolution: duplicate
stage: resolved ->
2011-12-14 09:47:55cool-RRsetstatus: closed -> open

assignee: docs@python
components: + Documentation, - Library (Lib)

nosy: + docs@python
messages: + msg149437
resolution: not a bug -> (no value)
2011-12-14 09:30:35petri.lehtinensetstatus: open -> closed

type: behavior

nosy: + petri.lehtinen
messages: + msg149436
resolution: not a bug
stage: resolved
2011-12-14 09:08:07ezio.melottisetnosy: + ezio.melotti
messages: + msg149434
2011-12-14 08:56:36cool-RRcreate