I need some help interpreting this error

Terry Reedy tjreedy at udel.edu
Wed Feb 17 12:27:03 EST 2021


On 2/17/2021 10:40 AM, Chris Green wrote:
> I'm running this using Python 3.7 on a Linux system.
> 
> Most of the time (i.e. for a couple of days now) the program has been
> satifactorily delivering mail messages, hundreds of them.  However one
> mail message has provoked the following error:-
> 
>      chris at cheddar$ tail mail.err
>      Traceback (most recent call last):
>        File "/home/chris/.mutt/bin/filter.py", line 95, in <module>
>          if sbstrip in msghdr["subject"]:
>      TypeError: argument of type 'Header' is not iterable
> 
> 
> But msghdr["subject"] is surely just a string isn't it?

Obviously not.

>  Why is it
> complaining about something of type 'Header'?

Because you tried to iterate it.  Header is defined in email.header 
(find 'class Header').  It has a __str__ to turn one into a string.  I 
have never read the email doc so I have no idea if 'subject' being a 
Header is a bug.

Grepping email package for case-sensitive word 'Header' also returns 3 
comment saying that something might be a Header, so stringify it.  I 
have the impression that these might have been added after the original 
code, perhaps in response to error reports.  In any case, you can do the 
same.

> As I said the program has been running happily for a couple of days
> with no errors, I guess it must be something strange in a mail that
> has broken things - but what?

To determine that, look at (after logging or saving) the raw email and 
maybe the resulting Message (using msg.as_string).

> # Read the message from standard input and make a message object from it
> #
> msg = mailbox.MaildirMessage(sys.stdin.buffer.read())

raw = sys.stdin.buffer.read()  # So can save
msg = mailbox.MaildirMessage(raw)

> msghdr["subject"] = msg.get("Subject", "unknown")
...
>          if sbstrip in msghdr["subject"]:

Replace with

           sub = msghdr["subject"]
           if 'Header' in str(sub.__class__):
# Or import email.message.class and use isinstance
                # stringify or skip or ???
            else:

>              msg.replace_header("Subject", msghdr["subject"].replace(sbstrip, 

-- 
Terry Jan Reedy



More information about the Python-list mailing list