[Tutor] Incorrect padding errors; base64 on UNIX vs Windows

Gareth unixguru@newsguy.com
Thu Dec 19 16:24:06 2002


I have two scripts running as part of a set of automation tests. One sends
various attachments to a mail server; the other retrieves the mail and
processes it for attachments. (The idea being to exercise an underlying proxy
on client machines.)

If I run the retrieval script on UNIX, it retrieves the mail and detaches all
the attachments. Each attachment matches the original file when diffed.
Perfect. Couldn't ask for anything more.

On Windows (2000 SP3), however, I constantly receive the following once the
mail has been downloaded and is being processed for attachments:
Traceback (most recent call last):
  File "gm2.py", line 110, in ?
  File "gm2.py", line 31, in writeparts
  File "C:\Python22\lib\mhlib.py", line 731, in getbody
    return self.getbodyparts()
  File "C:\Python22\lib\mhlib.py", line 723, in getbodyparts
    part = SubMessage(self.folder, n, mf)
  File "C:\Python22\lib\mhlib.py", line 744, in __init__
    self.body = Message.getbodytext(self)
  File "C:\Python22\lib\mhlib.py", line 705, in getbodytext
    mimetools.decode(self.fp, output, encoding)
  File "C:\Python22\lib\mimetools.py", line 137, in decode
    return base64.decode(input, output)
  File "C:\Python22\lib\base64.py", line 31, in decode
    s = binascii.a2b_base64(line)
binascii.Error: Incorrect padding

I did wonder if it might have been something in the proxy that's actually
being tested, which might have been reassembling attachments incorrectly.
Happens on a vanilla Windows system, though.

So, my question becomes: what's different in the base64 handling on Win32
versus UNIX? Why does Win32 raise padding errors for the exact same message,
but UNIX sails through fine? Even my Python distributions are pretty close:
2.2.0 on UNIX; 2.2.1 on Windows.

Is there something I can/should do if the platform is Win32 to make sure the
padding is correct? Is this a spurious error? The attachments were all binary
- didn't want to deal with possible CR/LF/EOF issues just yet. :-)

(I haven't included the .py files involved, since they're pretty much lifted
directly from copyrighted books. However, refer to Programming Python (2nd
Ed.), page 630 to see how I do the decode. I tried decoding files attached via
the other script, and also files sent from real mail clients - same result.)

Any insight into how to go about addressing this would be appreciated. I'm at
my wits' end with this problem, and just seem to keep going around in circles
(ie making the problem worse). Google didn't offer any insight, other than to
report this used to be broken some time ago (around 1.5.2 if I recall), and
subsequently fixed.

Thanks
Gareth