Hi Stephen,

>  def find_headers(mail):
> -    return reduce(operator.__concat__,
> -                  ['%s: %s\n' % (k, Header(v, header_name=k,
> -                                           continuation_ws='\t').encode())
> -                   for (k, v) in list(mail.items())])
> +    headers = {key: decode_header(value) for key, value in 
> list(mail.items())}
> +    return '\n'.join(['%s: %s' % (key, make_header(value[0], header_name=key,
> +                                                   continuation_wd='\t'))
> +                      for key, value in headers])

This works beautifully in Python3. In Python2, not so much:


patchwork@652f47a766fc:~/patchwork$ python2
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from email.header import decode_header, make_header
>>> snowman_header = u'Snowman: \u2603'
>>> snowman_utf_8 = snowman_header.encode('utf-8')
>>> print(snowman_utf_8)
Snowman: ☃
>>> print(decode_header(snowman_header))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/email/header.py", line 73, in decode_header
    header = str(header)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2603' in position 
9: ordinal not in range(128)
>>> print(decode_header(snowman_utf_8))
[('Snowman: \xe2\x98\x83', None)]
>>> print(make_header(decode_header(snowman_utf_8)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/email/header.py", line 139, in make_header
    h.append(s, charset)
  File "/usr/lib/python2.7/email/header.py", line 267, in append
    ustr = unicode(s, incodec, errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9: ordinal 
not in range(128)
>>> 

So I suppose some half-breed of the two approaches is the way to go.

Regards,
Daniel
_______________________________________________
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork

Reply via email to