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
标题: binascii b2a functions accept strings (unicode) as data
类型: Stage:
Components: Extension Modules Versions: Python 3.0
process
状态: closed Resolution: accepted
Dependencies: 后续:
分配给: 抄送列表: barry, georg.brandl, loewis, pitrou, terry.reedy
优先级: release blocker 关键字: needs review, patch

Created on 2008-11-22 00:41 by terry.reedy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
reqbytes.diff loewis, 2008-11-30 13:48
Messages (7)
msg76226 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) 日期: 2008-11-22 00:41
Binascii b2a_xxx functions accept 'binary data' and return ascii-encoded
bytes.  The corresponding a2b_xxx functions turn the ascii-encoded bytes
back to 'binary data' (bytes).  If the binary data is bytes, these
should be inverses of each other.

Somewhat surprisingly to me (because the corresponding base64 module
functions raise "TypeError: expected bytes, not str") 3.0 strings
(unicode) are accepted as 'binary data', though they will not 'round-trip'.

Ascii chars almost do
>>> a='aaaa'
>>> c=b.b2a_base64(a)
>>> c
b'YWFhYQ==\n'
>>> d=b.a2b_base64(c)
>>> d
b'aaaa'

But general unicode chars generate nonsense.
>>> a='\u1000'
>>> c=b.b2a_base64(a)
>>> c
b'4YCA\n'
>>> d=b.a2b_base64(c)
>>> d
b'\xe1\x80\x80'

I also tried b2a_uu.

Is this a bug?
msg76233 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) 日期: 2008-11-22 08:16
I vote yes.
msg76628 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) 日期: 2008-11-29 22:48
It's not /exactly/ nonsense, it seems to assume an utf8 encoding pass is
necessary:

>>> b'\xe1\x80\x80'.decode('utf8') == '\u1000'
True

IMO, while accepting unicode strings instead of bytes for the a2b_xx
functions is understandable (because in practice only ASCII characters
are allowed), it is not acceptable for b2a_xx functions to accept
unicode strings instead of bytes.

In other words, it might/should be ok for
`binascii.a2b_base64('YWFh\n')` to return the same as
`binascii.a2b_base64('YWFh\n')` (that is, b'aaa'), but
`binascii.b2a_base64('aaa')` should raise a TypeError rather than
applying an utf8 encoding pass before doing the actual b2a encoding.

I think this must be fixed before 3.0 final, and is therefore a release
blocker.
msg76629 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) 日期: 2008-11-29 22:49
Hmm, I obviously meant:

[...] In other words, it might/should be ok for
`binascii.a2b_base64('YWFh\n')` to return the same as
`binascii.a2b_base64(b'YWFh\n')` (that is, b'aaa') [...]
msg76639 - (view) Author: Martin v. Löwis (loewis) * (Python committer) 日期: 2008-11-30 13:48
Here is a patches that fixes the problem.

Notice that this affects the email API; base64mime.body_encode now also
requires bytes (whereas quoprimime remains unchanged).

There are probably more functions that still incorrectly accept strings,
e.g. zlib.crc32.
msg76662 - (view) Author: Barry A. Warsaw (barry) * (Python committer) 日期: 2008-11-30 20:14
Martin, the patch looks okay to me.  I vote for applying it.
msg76724 - (view) Author: Martin v. Löwis (loewis) * (Python committer) 日期: 2008-12-02 06:00
Committed as r67472.
历史
日期 用户 动作 参数
2022-04-11 14:56:41admin修改github: 48637
2008-12-02 06:00:34loewis修改状态: open -> closed
消息: + msg76724
2008-11-30 20:14:38barry修改抄送: + barry
resolution: accepted
消息: + msg76662
2008-11-30 13:58:56loewis修改keywords: + needs review
2008-11-30 13:48:30loewis修改文件: + reqbytes.diff
抄送: + loewis
消息: + msg76639
keywords: + patch
2008-11-29 22:49:42pitrou修改消息: + msg76629
2008-11-29 22:48:03pitrou修改优先级: release blocker
抄送: + pitrou
消息: + msg76628
2008-11-22 08:16:40georg.brandl修改抄送: + georg.brandl
消息: + msg76233
2008-11-22 00:41:18terry.reedy创建