[Python-Dev] [IronPython] base64 module

Dino Viehland dinov at exchange.microsoft.com
Thu Apr 6 19:01:05 CEST 2006

Well, CPython at least still enforces the padding, even if it's ignoring the invalid characters.

Here's Seo's repro 'simplified' to go straight to binascii (just to get to the root API):

>>> import binascii
>>> binascii.a2b_base64('%')

And then sending a valid character, invalid padding:

>>> binascii.a2b_base64('A')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
binascii.Error: Incorrect padding

and then throwing in random invalid characters, and CPython ignores the invalid characters:

>>> binascii.a2b_base64('ABC=')
>>> binascii.a2b_base64('%%ABC=')
>>> binascii.a2b_base64('%%ABC=%!@##')
>>> binascii.a2b_base64('%%ABC=%!@##*#*()')

The documentation for binascii.a2b_base64 doesn't specify if it throws for anything either.

I would suspect that there's a reason why CPython is ignoring the invalid characters here.  If this is the expected behavior then I'm happy to make IronPython match this.  And at the very least we HAVE to fix the exception that gets thrown - I'm with Seo that it should be a ValueError but line between ValueError and TypeError is blurry at times anyway, and TypeError is what's documented.

Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Sanghyeon Seo
Sent: Wednesday, April 05, 2006 10:45 PM
To: python-dev at python.org; users at lists.ironpython.com
Subject: [IronPython] base64 module


base64 module documentation for b64decode function says, "TypeError is
raised if s were incorrectly padded or if there are non-alphabet
characters present in the string." But this doesn't seem to be the
case. Testcase:

import base64

Since % is a non-alphabet character, this should raise TypeError (btw,
shouldn't this be ValueError instead?), but Python 2.4.3 silently

I found this while experimenting with IronPython. IronPython 1.0 Beta 5 gives:

Traceback (most recent call last):
  File base64, line unknown, in b64decode
SystemError: cannot decode byte: '%'

It's not TypeError, but it doesn't silently ignore either.

Seo Sanghyeon
users mailing list
users at lists.ironpython.com

More information about the Python-Dev mailing list