Suggested tweak to base64.py

Tim Roberts timr at probo.com
Sun Dec 30 16:21:43 EST 2001


Once in a while, I use the base64.py standard library module as a
standalone Base64 decoder.  However, there is a slight problem with this.
Most base64 attachments are encoded because they were binary files to begin
with.  The base64.py test() module writes its results to stdout, which on
Windows is a text file by default.  CR/LF and Ctrl-Z confusion ensues.

I'd like to suggest the following four lines be inserted just before the
end of base64.test():

    ...
        if o == '-t': test1(); return
+   if func == decode and sys.platform == 'win32':
+       import os
+       import msvcrt
+       msvcrt.setmode( sys.stdout.fileno(), os.O_BINARY )
    if args and args[0] != '-':
        func(open(args[0], 'rb'), sys.stdout)
    else:
        func(sys.stdin, sys.stdout)
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list