[issue11243] email/message.py str conversion

Steffen Daode Nurpmeso report at bugs.python.org
Wed Mar 16 16:07:04 CET 2011


Steffen Daode Nurpmeso <sdaoden at googlemail.com> added the comment:

On Wed, Mar 16, 2011 at 01:58:40PM +0000, R. David Murray wrote:
> Steffen, these look like different kinds of errors than the one you reported in this ticket.
> If they are, could you open a new issue?  Either way, simple reproducers would be the most helpful.

I'm on db73857669fb, email/message.py is patched with your code, 
and email/header.py is patched with email-header.2.diff. 
11243-test.1.py will traceback:

15:53 ~/tmp $ python3 11243-test.1.py 
Traceback (most recent call last):
  File "11243-test.1.py", line 17, in <module>
    msg[f] = email.header.make_header(email.header.decode_header(b))
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/header.py", line 154, in make_header
    h.append(s, charset)
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/header.py", line 279, in append
    s.encode(output_charset, errors)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 7: ordinal not in range(128)

I'll be down the next couple of hours, but in the meanwhile that's 
all i can do anyway...
And well, i won't open a new issue due to the stuff from msg131102, 
because that happen(ed) if the commented out code from 
email-header.2.diff is applied, which is non-real-life code-flow? 
(Though: a Message is happily read in via 
email.feedparser.BytesFeedParser() and finally adjusted via 
header.make_header(header.decode_header(b)) because you've asked 
me to do so, and just as is done by 11243-test.1.py.)

----------
Added file: http://bugs.python.org/file21240/email-header.2.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11243>
_______________________________________
-------------- next part --------------
diff --git a/Lib/email/header.py b/Lib/email/header.py
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -70,7 +70,7 @@
     occurs (e.g. a base64 decoding exception).
     """
     # If no encoding, just return the header with no charset.
-    if not ecre.search(header):
+    if not ecre.search(str(header)):
         return [(header, None)]
     # First step is to parse all the encoded parts into triplets of the form
     # (encoded_string, encoding, charset).  For unencoded strings, the last
@@ -265,6 +265,10 @@
             charset = self._charset
         elif not isinstance(charset, Charset):
             charset = Charset(charset)
+        # Steffen is out now again - but this can *really* be a Header here!
+        #if isinstance(s, Header):
+        #    s = str(s)
+        #    errors = 'replace'
         if not isinstance(s, str):
             input_charset = charset.input_codec or 'us-ascii'
             s = s.decode(input_charset, errors)


More information about the Python-bugs-list mailing list