[Python-bugs-list] mimetools.{encode,decode} should handle trivial encodings (PR#273)

skip@mojam.com skip@mojam.com
Tue, 4 Apr 2000 16:06:10 -0400 (EDT)


Full_Name: Skip Montanaro
Version: 1.5.2+/1.6a1
OS: Linux
Submission from: beluga.mojam.com (199.249.165.173)


mimetools.{encode,decode} handle a number of different content transer
encodings, but don't handle trivial encodings like 7bit and 8bit.  It seems
to me that it would simplify callers' code if these functions handled these
common cases.  For example, a loop over a MultiFile object code be coded as

    file = multifile.MultiFile(sys.stdin)
    file.push(boundary)
    while file.next():
        submessage = mimetools.Message(file)
        try:
            data = mimetools.decode(file.read(), submessage.getencoding())
        except ValueError:
            print "unknown encoding:", submessage.getencoding()
            continue
        process_data(data)
     file.pop()

with the expectation that hitting "except ValueError" would actually be
exceptional.  Because 7bit and 8bit encodings are not currently handled, all
calling code has to handle the relatively common (and trivial) cases.

Here's a patch that just does a noop for encoding and decoding 7bit/8bit
data.

*** /tmp/mimetools.py.~1.16~qKA6Tr	Tue Apr  4 13:16:41 2000
--- /tmp/mimetools.pyqKAHex	Tue Apr  4 13:16:41 2000
***************
*** 140,145 ****
--- 140,147 ----
  	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  		import uu
  		return uu.decode(input, output)
+ 	if encoding in ('7bit', '8bit'):
+ 		output.write(input.read())
  	if decodetab.has_key(encoding):
  		pipethrough(input, decodetab[encoding], output)
  	else:
***************
*** 157,162 ****
--- 159,166 ----
  	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  		import uu
  		return uu.encode(input, output)
+ 	if encoding in ('7bit', '8bit'):
+ 		output.write(input.read())
  	if encodetab.has_key(encoding):
  		pipethrough(input, encodetab[encoding], output)
  	else:



I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

Skip Montanaro
skip@mojam.com