[issue34736] Confusing base64.b64decode output

Mark Dickinson report at bugs.python.org
Wed Sep 19 09:00:53 EDT 2018


New submission from Mark Dickinson <dickinsm at gmail.com>:

The following error message in Python 3.7 is confusing and unhelpful:

    >>> s = "FS9qzW_oliGH_Yo="
    >>> base64.b64decode(s)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/base64.py", line 87, in b64decode
        return binascii.a2b_base64(s)
    binascii.Error: Invalid base64-encoded string: length cannot be 1 more than a multiple of 4
    >>> len(s) % 4
    0

This had me staring at the string s and scratching my head, thinking: "s doesn't have length 1 more than a multiple of 4, either with or without the "=" padding byte. What's this talking about?"

The actual user error here is a failure to use the "altchars" argument correctly:

>>> base64.b64decode(s, altchars="-_")
b'\x15/j\xcdo\xe8\x96!\x87\xfd\x8a'

The error message in versions prior to Python 3.7 is no more helpful, but it's not quite as misleading, either.

Python 3.6.6 (default, Jun 28 2018, 05:43:53) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> s = "FS9qzW_oliGH_Yo="
>>> base64.b64decode(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

Related: #1466065, #33770

----------
messages: 325757
nosy: mark.dickinson
priority: normal
severity: normal
status: open
title: Confusing base64.b64decode output
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34736>
_______________________________________


More information about the Python-bugs-list mailing list