def decode_header(header):
 if hasattr(header, '_chunks'):
  return [(_charset._encode(string, str(charset)), str(charset)) for (string, charset) in header._chunks]
 if (not ecre.search(header)):
  return [(header, None)]
 words = []
 for line in header.splitlines():
  parts = ecre.split(line)
  first = True
  while parts:
   unencoded = parts.pop(0)
   if first:
    unencoded = unencoded.lstrip()
    first = False
   if unencoded:
    words.append((unencoded, None, None))
   if parts:
    charset = parts.pop(0).lower()
    encoding = parts.pop(0).lower()
    encoded = parts.pop(0)
    words.append((encoded, encoding, charset))
 droplist = []
 for (n, w) in enumerate(words):
  if ((n > 1) and w[1] and words[(n - 2)][1] and words[(n - 1)][0].isspace()):
   droplist.append((n - 1))
 for d in reversed(droplist):
  del words[d]
 decoded_words = []
 for (encoded_string, encoding, charset) in words:
  if (encoding is None):
   decoded_words.append((encoded_string, charset))
  elif (encoding == 'q'):
   word = email.quoprimime.header_decode(encoded_string)
   decoded_words.append((word, charset))
  elif (encoding == 'b'):
   paderr = (len(encoded_string) % 4)
   if paderr:
    encoded_string += '==='[:(4 - paderr)]
   try:
    word = email.base64mime.decode(encoded_string)
   except binascii.Error:
    raise HeaderParseError('Base64 decoding error')
   else:
    decoded_words.append((word, charset))
  else:
   raise AssertionError(('Unexpected encoding: ' + encoding))
 collapsed = []
 last_word = last_charset = None
 for (word, charset) in decoded_words:
  if isinstance(word, unicode):
   word = bytes(word, 'raw-unicode-escape')
  if (last_word is None):
   last_word = word
   last_charset = charset
  elif (charset != last_charset):
   collapsed.append((last_word, last_charset))
   last_word = word
   last_charset = charset
  elif (last_charset is None):
   last_word += (' ' + word)
  else:
   last_word += word
 collapsed.append((last_word, last_charset))
 return collapsed

def decode_header ( header ) : if hasattr ( header , '_chunks' ) : return [ ( _charset . _encode ( string , str ( charset ) ) , str ( charset ) ) for ( string , charset ) in header . _chunks ] if ( not ecre . search ( header ) ) : return [ ( header , None ) ] words = [ ] for line in header . splitlines ( ) : parts = ecre . split ( line ) first = True while parts : unencoded = parts . pop ( 0 ) if first : unencoded = unencoded . lstrip ( ) first = False if unencoded : words . append ( ( unencoded , None , None ) ) if parts : charset = parts . pop ( 0 ) . lower ( ) encoding = parts . pop ( 0 ) . lower ( ) encoded = parts . pop ( 0 ) words . append ( ( encoded , encoding , charset ) ) droplist = [ ] for ( n , w ) in enumerate ( words ) : if ( ( n > 1 ) and w [ 1 ] and words [ ( n - 2 ) ] [ 1 ] and words [ ( n - 1 ) ] [ 0 ] . isspace ( ) ) : droplist . append ( ( n - 1 ) ) for d in reversed ( droplist ) : del words [ d ] decoded_words = [ ] for ( encoded_string , encoding , charset ) in words : if ( encoding is None ) : decoded_words . append ( ( encoded_string , charset ) ) elif ( encoding == 'q' ) : word = email . quoprimime . header_decode ( encoded_string ) decoded_words . append ( ( word , charset ) ) elif ( encoding == 'b' ) : paderr = ( len ( encoded_string ) % 4 ) if paderr : encoded_string += '===' [ : ( 4 - paderr ) ] try : word = email . base64mime . decode ( encoded_string ) except binascii . Error : raise HeaderParseError ( 'Base64 decoding error' ) else : decoded_words . append ( ( word , charset ) ) else : raise AssertionError ( ( 'Unexpected encoding: ' + encoding ) ) collapsed = [ ] last_word = last_charset = None for ( word , charset ) in decoded_words : if isinstance ( word , unicode ) : word = bytes ( word , 'raw-unicode-escape' ) if ( last_word is None ) : last_word = word last_charset = charset elif ( charset != last_charset ) : collapsed . append ( ( last_word , last_charset ) ) last_word = word last_charset = charset elif ( last_charset is None ) : last_word += ( ' ' + word ) else : last_word += word collapsed . append ( ( last_word , last_charset ) ) return collapsed
make sure the header is an unicode string .