Daniel Lenski <dlen...@gmail.com> added the comment:

I recently ran into this bug as well.

For those looking for a reliable workaround, here's an implementation of a 
'decode_header_to_string' function which should Just Work™ in all possible 
cases:

    #!/usr/bin/python3
    import email.header

    # Workaround for https://bugs.python.org/issue22833
    def decode_header_to_string(header):
        '''Decodes an email message header (possibly RFC2047-encoded)
        into a string, while working around 
https://bugs.python.org/issue22833'''

        return ''.join(
            alleged_string if isinstance(alleged_string, str) else 
alleged_string.decode(
                alleged_charset or 'ascii')
            for alleged_string, alleged_charset in 
email.header.decode_header(header))


    for header in ('=?utf-8?B?ZsOzbw==',
                   '=?ascii?Q?hello?==?utf-8?B?ZsOzbw==?=',
                   'bar=?ascii?Q?hello?==?utf-8?B?ZsOzbw==?=',
                   'plain string',):
        print("Header value: %r" % header)
        print("email.header.decode_header(...) -> %r" % 
email.header.decode_header(header))
        print("decode_header_to_string(...)    -> %r" % 
decode_header_to_string(header))
        print("-------")

Outputs:

    Header value: '=?utf-8?B?ZsOzbw=='
    email.header.decode_header(...) -> [('=?utf-8?B?ZsOzbw==', None)]
    decode_header_to_string(...)    -> '=?utf-8?B?ZsOzbw=='
    -------
    Header value: '=?ascii?Q?hello?==?utf-8?B?ZsOzbw==?='
    email.header.decode_header(...) -> [(b'hello', 'ascii'), (b'f\xc3\xb3o', 
'utf-8')]
    decode_header_to_string(...)    -> 'hellofóo'
    -------
    Header value: 'bar=?ascii?Q?hello?==?utf-8?B?ZsOzbw==?='
    email.header.decode_header(...) -> [(b'bar', None), (b'hello', 'ascii'), 
(b'f\xc3\xb3o', 'utf-8')]
    decode_header_to_string(...)    -> 'barhellofóo'
    -------
    Header value: 'plain string'
    email.header.decode_header(...) -> [('plain string', None)]
    decode_header_to_string(...)    -> 'plain string'
    -------
    Header value: 'foo=?blah?Q??='
    email.header.decode_header(...) -> [(b'foo', None), (b'', 'blah')]
    decode_header_to_string(...)    -> 'foo'
    -------

----------
nosy: +dlenski

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue22833>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to