- try:
- codecs.lookup('ascii')[1](s)
- return s
- except UnicodeError:
- pass
- try:
- codecs.lookup('utf-8')[1](s)
- h = email.Header.Header(s, 'utf-8', 998)
- return str(h)
- except UnicodeError:
- h = email.Header.Header(s, 'iso-8859-1', 998)
- return str(h)
+ for enc in ['ascii', 'utf-8', 'iso-8859-1']:
+ try:
+ h = email.Header.Header(s, enc, 998)
+ return str(h)
+ except UnicodeError:
+ pass
+
+ # If we get here, we're boned beyond belief
+ return ''