You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
binascii b2a functions accept strings (unicode) as data #48637
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'
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.
[...] 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') [...]
loewis commented on Nov 30, 2008
loewismannequin
Mannequin
More actions
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.
Activity
terryjreedy commentedon Nov 22, 2008
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'.
I also tried b2a_uu.
Is this a bug?
birkenfeld commentedon Nov 22, 2008
I vote yes.
pitrou commentedon Nov 29, 2008
It's not /exactly/ nonsense, it seems to assume an utf8 encoding pass is
necessary:
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 asbinascii.a2b_base64('YWFh\n')(that is, b'aaa'), butbinascii.b2a_base64('aaa')should raise a TypeError rather thanapplying 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.
pitrou commentedon Nov 29, 2008
Hmm, I obviously meant:
[...] In other words, it might/should be ok for
binascii.a2b_base64('YWFh\n')to return the same asbinascii.a2b_base64(b'YWFh\n')(that is, b'aaa') [...]loewis commentedon Nov 30, 2008
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.
warsaw commentedon Nov 30, 2008
Martin, the patch looks okay to me. I vote for applying it.
loewis commentedon Dec 2, 2008
Committed as r67472.