[ python-Bugs-824417 ] exception with Message.get_filename()

SourceForge.net noreply at sourceforge.net
Fri Nov 21 11:29:30 EST 2003

Bugs item #824417, was opened at 2003-10-15 17:39
Message generated for change (Comment added) made by bwarsaw
You can respond by visiting: 

Category: Python Library
Group: Python 2.2.3
>Status: Closed
>Resolution: Out of Date
Priority: 5
Submitted By: Stuart D. Gathman (customdesigned)
Assigned to: Barry A. Warsaw (bwarsaw)
Summary: exception with Message.get_filename()

Initial Comment:
The following scriptlet:
import sys
import email

msg = email.message_from_file(sys.stdin)
for part in msg.walk():
  print part.get_params()
  print part.get_filename()
gets an exception when fed the attached email
(extracted from a real life non-spam message).

$ python2 t.py <test1
[('multipart/mixed', ''), ('boundary',
[('multipart/alternative', ''), ('boundary',
[('text/plain', '')]
[('text/html', ''), ('charset', 'utf-8')]
[('application/rtf', ''), ('name', (None, None, '14676
World Transportation Systems OF, from arrival TIA
terminal to door and from Durres port to TIA.rtf'))]
Traceback (most recent call last):
  File "t.py", line 7, in ?
    print part.get_filename()
  File "/usr/lib/python2.2/email/Message.py", line 711,
in get_filename
    return unicode(newvalue[2], newvalue[0])
TypeError: unicode() argument 2 must be string, not None


>Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-11-21 11:29

Logged In: YES 

This has been fixed in cvs for Python 2.2.4 if that ever
gets released.  It also works fine for Python 2.3.2.


Comment By: Stuart D. Gathman (customdesigned)
Date: 2003-10-15 18:05

Logged In: YES 

The documentation for the get_param method suggests the
following code when the value is a tuple:

            param = msg.get_param('foo')
            if isinstance(param, tuple):
                param = unicode(param[2], param[0])

The get_filename method follows this advice, but it doesn't
work when the encoding is None.

I fixed this by changing Message.get_filename to this:

  def get_filename(self, failobj=None):
      """Return the filename associated with the payload if
      The filename is extracted from the Content-Disposition
      `filename' parameter, and it is unquoted.
      missing = []
      filename = self.get_param('filename', missing,
      if filename is missing:
          return failobj
      if isinstance(filename, TupleType):
          # It's an RFC 2231 encoded parameter
          newvalue = _unquotevalue(filename)
          if newvalue[0]:
            return unicode(newvalue[2], newvalue[0])
          return unicode(newvalue[2])
          newvalue = _unquotevalue(filename.strip())
          return newvalue


You can respond by visiting: 

More information about the Python-bugs-list mailing list