base64 anomaly

eichin at metacarta.com eichin at metacarta.com
Fri Feb 28 02:35:36 EST 2003


Tracing down an obscure SOAP.py problem, I found this:

>>> import base64
>>> base64.encodestring("")
''
>>> base64.decodestring("")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/base64.py", line 44, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Cannot decode empty input

Huh, it can't handle it's own input? really? Let's check again:

>>> base64.decodestring(base64.encodestring(""))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/base64.py", line 44, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Cannot decode empty input

Yep.... It turns out, however, that the underlying functions *are*
reversible, it is in how they get called:

>>> import binascii
>>> binascii.a2b_base64("")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
binascii.Error: Cannot decode empty input
>>> binascii.b2a_base64("")
'\n'
>>> binascii.a2b_base64("\n")
''

Note that binascii.encode/decode (which take files) manage to avoid
this, mostly by accident - if they don't get any input, they don't
even call the underlying functions.

The trivial fix would be to add
   if not s: return ""
to binascii.decodestring, before the call to a2b_base64.  It appears
that python 2.3 has the same error.

So: does anyone more experienced agree that this is an error, and if
so, where should I report it?

			_Mark_ <eichin at metacarta.com>




More information about the Python-list mailing list