简体   繁体   English

使用ROT13的codecs.decode中可能存在错误

[英]Possible Bug in codecs.decode using ROT13

Python 3.5.1 on Ubuntu Ubuntu上的Python 3.5.1

>>> from codecs import decode
>>> s = 'string'
>>> b = b'bytes'
>>> decode(b, 'utf8')
'bytes'
>>> decode(s, 'utf8')
Traceback (most recent call last):
  File "/usr/lib/python3.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
TypeError: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: decoding with 'utf8' codec failed (TypeError: a bytes-like object is required, not 'str')

so far everything behaves as expected. 到目前为止,一切都按预期进行。 But when I try to use ROT13 encoding I get: 但是,当我尝试使用ROT13编码时,我得到:

>>> decode(s, 'rot13')
'fgevat'
>>> decode(b, 'rot13')
Traceback (most recent call last):
  File "/usr/lib/python3.5/encodings/rot_13.py", line 18, in decode
    return (input.translate(rot13_map), len(input))
TypeError: a bytes-like object is required, not 'dict'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: decoding with 'rot13' codec failed (TypeError: a bytes-like object is required, not 'dict')

As @snakecharmerb points out ROT13 en/decoding is only supposed to work on strings. 正如@snakecharmerb指出的那样,ROT13编码/解码仅适用于字符串。
The exception Message though, is still wrong since it states, that a bytes-like object was expected even when a bytes-like object was actually passed, and mentions some dictionary the user did not create. 但是,异常Message仍然是错误的,因为它指出,即使实际上传递了类似字节的对象,也希望使用类似字节的对象,并且提到了用户未创建的一些词典。

Regarding the ROT13 codec, the Python 3.5 docs state : 关于ROT13编解码器, Python 3.5文档指出

The following codec provides a text transform: a str to str mapping. 以下编解码器提供了文本转换:str到str的映射。 It is not supported by str.encode() (which only produces bytes output). str.encode()不支持它(仅产生字节输出)。

that is, you can only pass unicode strings ( str objects) when encoding to ROT13 and you will only get str objects back. 也就是说,在编码到ROT13时,您只能传递unicode字符串( str对象),并且只会返回str对象。

This is different from Python 2.x, when ROT13 was treated the same as other codecs. 当ROT13与其他编解码器相同时,这与Python 2.x不同。 ROT13 was not ported to Python 3 initially, because ROT13 does not encode a unicode string to bytes - it just swaps letters around. ROT13最初并未移植到Python 3,因为ROT13不会将unicode字符串编码为字节-只是交换字母。 It as reinstated, as a text transform in Python 3.2 and 3.4. 恢复后,作为Python 3.2和3.4中的文本转换。 The full story is in this bug report . 完整的故事在这个bug报告中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM