def safe_decode_hdr(msg=None, name=None, hdr=None, charset=None):
 if (hdr is None):
  value = ((msg and msg[name]) or '')
  charset = (charset or msg.get_content_charset() or 'utf-8')
 else:
  value = hdr
  charset = (charset or 'utf-8')
 if (not isinstance(value, unicode)):
  value = try_decode(value, charset, replace='?')
 if (('=?' in value) and ('?=' in value)):
  try:
   value = value.encode('utf-8').replace('"', '')
   pairs = decode_header(value)
   value = ' '.join([try_decode(t, (cs or charset)) for (t, cs) in pairs])
  except email.errors.HeaderParseError:
   pass
 return value.replace('\r', ' ').replace(' DCTB ', ' ').replace('\n', ' ')

def safe_decode_hdr ( msg = None , name = None , hdr = None , charset = None ) : if ( hdr is None ) : value = ( ( msg and msg [ name ] ) or '' ) charset = ( charset or msg . get_content_charset ( ) or 'utf-8' ) else : value = hdr charset = ( charset or 'utf-8' ) if ( not isinstance ( value , unicode ) ) : value = try_decode ( value , charset , replace = '?' ) if ( ( '=?' in value ) and ( '?=' in value ) ) : try : value = value . encode ( 'utf-8' ) . replace ( '"' , '' ) pairs = decode_header ( value ) value = ' ' . join ( [ try_decode ( t , ( cs or charset ) ) for ( t , cs ) in pairs ] ) except email . errors . HeaderParseError : pass return value . replace ( '\r' , ' ' ) . replace ( ' DCTB ' , ' ' ) . replace ( '\n' , ' ' )
this method stubbornly tries to decode header data and convert to pythonic unicode strings .