This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: rot_13 codec not working
类型: behavior Stage:
Components: Documentation, Library (Lib) Versions: Python 3.3
process
状态: closed Resolution: duplicate
Dependencies: 后续: codecs missing: base64 bz2 hex zlib hex_codec ...
View: 7475
分配给: docs@python 抄送列表: cool-RR, docs@python, ezio.melotti, petri.lehtinen
优先级: normal 关键字:

Created on 2011-12-14 08:56 by cool-RR, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg149431 - (view) Author: Ram Rachum (cool-RR) * 日期: 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) 日期: 2011-12-14 09:08
See #7475.
msg149436 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) 日期: 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) * 日期: 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) 日期: 2011-12-14 10:48
Issue #7475 discusses fixing the error messages, too.
历史
日期 用户 动作 参数
2022-04-11 14:57:24admin修改github: 57809
2011-12-14 10:48:42petri.lehtinen修改状态: open -> closed
后续: codecs missing: base64 bz2 hex zlib hex_codec ...
消息: + msg149438

components: + Library (Lib)
resolution: duplicate
stage: resolved ->
2011-12-14 09:47:55cool-RR修改状态: closed -> open

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

抄送: + docs@python
消息: + msg149437
resolution: not a bug -> (no value)
2011-12-14 09:30:35petri.lehtinen修改状态: open -> closed

type: behavior

抄送: + petri.lehtinen
消息: + msg149436
resolution: not a bug
stage: resolved
2011-12-14 09:08:07ezio.melotti修改抄送: + ezio.melotti
消息: + msg149434
2011-12-14 08:56:36cool-RR创建