<NME> u2u_decode.py
<BEF> #!/usr/bin/env python
# coding: utf-8
"""
u2u_decode.py

unstructured rfc2047 header to unicode
"""
import re
from email.header import decode_header, make_header

# check spaces between encoded_words (and strip them)
sre = re.compile(r'\?=[ \t]+=\?')
from email.utils import parseaddr



def clean_spaces(m):
    """Replace unencoded spaces in string

    :param str m: a match object
    :return: the cleaned string
ascii_re = re.compile(r"=[\dA-F]{2,3}", re.I)



def decode_mime(m):
    """substitute matching encoded_word with unicode equiv.
    """
    h = decode_header(clean_spaces(m))
    try:
        u = unicode(make_header(h))
    except UnicodeDecodeError:
        return m.group(0)
    return u

    """Strip non printable characters."""
    code = int(m.group(0)[1:], 16)
    if code < 20:
        return ""
    return m.group(0)


def decode_mime(m):
    """Substitute matching encoded_word with unicode equiv."""
    h = decode_header(clean_spaces(m))
    try:
        u = smart_text(make_header(h))
    except (LookupError, UnicodeDecodeError):
        return m.group(0)
    return u


def clean_header(header):
    """Clean header function."""
    header = "".join(header.splitlines())
    header = sre.sub("?==?", header)
    return ascii_re.sub(clean_non_printable_char, header)


def u2u_decode(s):
    """utility function for (final) decoding of mime header

    note: resulting string is in one line (no \n within)
    note2: spaces between enc_words are stripped (see RFC2047)
    """
    return mre.sub(decode_mime, clean_header(s)).strip(" \r\t\n")


def decode_address(value):
    """Special function for address decoding.

    We need a dedicated processing because RFC1342 explicitely says
    address MUST NOT contain encoded-word:

      These are the ONLY locations where an encoded-word may appear.  In
      particular, an encoded-word MUST NOT appear in any portion of an
      "address".  In addition, an encoded-word MUST NOT be used in a
      Received header field.
    """
    phrase, address = parseaddr(clean_header(value))
    if phrase:
        phrase = mre.sub(decode_mime, phrase)
    return phrase, address

<MSG> Catch LookupError exception.

fix #944

<DFF> @@ -1,12 +1,9 @@
-#!/usr/bin/env python
 # coding: utf-8
-"""
-u2u_decode.py
 
-unstructured rfc2047 header to unicode
-"""
-import re
+"""Unstructured rfc2047 header to unicode."""
+
 from email.header import decode_header, make_header
+import re
 
 # check spaces between encoded_words (and strip them)
 sre = re.compile(r'\?=[ \t]+=\?')
@@ -15,7 +12,7 @@ mre = re.compile(r'=\?[^?]*?\?[bq]\?[^?\t]*?\?=', re.I)
 
 
 def clean_spaces(m):
-    """Replace unencoded spaces in string
+    """Replace unencoded spaces in string.
 
     :param str m: a match object
     :return: the cleaned string
@@ -24,12 +21,11 @@ def clean_spaces(m):
 
 
 def decode_mime(m):
-    """substitute matching encoded_word with unicode equiv.
-    """
+    """Substitute matching encoded_word with unicode equiv."""
     h = decode_header(clean_spaces(m))
     try:
         u = unicode(make_header(h))
-    except UnicodeDecodeError:
+    except (LookupError, UnicodeDecodeError):
         return m.group(0)
     return u
 

Catch LookupError exception.