This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: exception with Message.get_filename()
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, customdesigned
Priority: normal Keywords:

Created on 2003-10-15 21:39 by customdesigned, last changed 2022-04-10 16:11 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test1 customdesigned, 2003-10-15 21:39 test input for scriptlet
Messages (3)
msg18645 - (view) Author: Stuart D. Gathman (customdesigned) Date: 2003-10-15 21:39
The following scriptlet:
-----------t.py-----------------
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',
'=-mkF0Ur/S0HaYfa60OEsM')]
None
[('multipart/alternative', ''), ('boundary',
'=-VowfKaQaEHb81enMCUlR')]
None
[('text/plain', '')]
None
[('text/html', ''), ('charset', 'utf-8')]
None
[('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
msg18646 - (view) Author: Stuart D. Gathman (customdesigned) Date: 2003-10-15 22:05
Logged In: YES 
user_id=142072

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
present.
    
      The filename is extracted from the Content-Disposition
header's
      `filename' parameter, and it is unquoted.
      """
      missing = []
      filename = self.get_param('filename', missing,
'content-disposition')
      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])
      else:
          newvalue = _unquotevalue(filename.strip())
          return newvalue
msg18647 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2003-11-21 16:29
Logged In: YES 
user_id=12800

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.
History
Date User Action Args
2022-04-10 16:11:45adminsetgithub: 39414
2003-10-15 21:39:27customdesignedcreate