Issue 10807: `b'dGVzdA==\n'.decode('base64')` raise exception

Created on 2011-01-02 18:22 by yi.codeplayer, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7) msg125068 - (view) Author: yihuang (yi.codeplayer) Date: 2011-01-02 18:22
>>> b'dGVzdA==\n'.decode('base64')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "../Lib/encodings/base64_codec.py", line 20, in base64_decode
    return (base64.decodebytes(input), len(input))
  File "../Lib/base64.py", line 359, in decodebytes
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not memoryview
msg125072 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-02 18:59
base64, bz2, hex, quopri, rot13, uu and zlib codecs (reintroduced recently by r86934, issue #7475) cannot be used by str.encode/bytes.decode, but with .transform() and .untransform() methods of bytes and str objects. But these methods were removed by r87176.

The last solution to use base64 codec is:

>>> import codecs
>>> codecs.lookup('base64').decode(b'YWJj\n')[0]
b'abc'
>>> codecs.lookup('base64').encode(b'YWJj\n')[0]
b'abc'

Or simply use directly the base64 module:

>>> import base64
>>> base64.decodebytes(b'YWJj\n')
b'abc'
>>> base64.encodebytes(b'abc')
b'YWJj\n'

base64, bz2, hex, quopri, rot13, uu and zlib codecs should be removed from encodings.aliases (because they introduced a confusion for Python 2 users), or removed completly (because it's easier to use directly the related module, eg. base64 or zlib).
msg125074 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-02 19:22
issue10807.patch just disables hex, base64, ... codecs in aliases (so it's still possible to use they through codecs.lookup()).
msg125075 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-02 19:23
That does not look like the right patch...
msg125076 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-02 19:24
Ah yes :-)
msg125077 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-02 19:25
Looks good, please commit.
msg125084 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-01-02 19:55
> Looks good, please commit.

Ok, done: r87642
History Date User Action Args 2022-04-11 14:57:10adminsetgithub: 55016 2011-01-02 19:55:50vstinnersetstatus: open -> closed

messages: + msg125084
resolution: fixed
nosy: georg.brandl, belopolsky, vstinner, yi.codeplayer

2011-01-02 19:26:45belopolskysetnosy: + belopolsky
2011-01-02 19:25:19georg.brandlsetmessages: + msg125077 2011-01-02 19:24:18vstinnersetfiles: + issue10807.patch

messages: + msg125076

2011-01-02 19:24:06vstinnersetfiles: - issue10780.patch 2011-01-02 19:23:28georg.brandlsetmessages: + msg125075 2011-01-02 19:22:51vstinnersetfiles: + issue10780.patch

messages: + msg125074
keywords: + patch

2011-01-02 19:08:11vstinnersetpriority: normal -> release blocker
nosy: + georg.brandl
2011-01-02 18:59:59vstinnersetnosy: + vstinner
messages: + msg125072
2011-01-02 18:22:43yi.codeplayercreate