Improve preferred encoding detection method

This commit is contained in:
Ricardo Garcia 2009-09-20 00:08:50 +02:00
parent 0833f1eb83
commit f94b636c3e
1 changed files with 9 additions and 9 deletions

View File

@ -33,15 +33,15 @@ def preferredencoding():
Returns the best encoding scheme for the system, based on Returns the best encoding scheme for the system, based on
locale.getpreferredencoding() and some further tweaks. locale.getpreferredencoding() and some further tweaks.
""" """
try: def yield_preferredencoding():
pref = locale.getpreferredencoding() try:
# Mac OSX systems have this problem sometimes pref = locale.getpreferredencoding()
if pref == '': u'TEST'.encode(pref)
return 'UTF-8' except:
return pref pref = 'UTF-8'
except: while True:
sys.stderr.write('WARNING: problem obtaining preferred encoding. Falling back to UTF-8.\n') yield pref
return 'UTF-8' return yield_preferredencoding().next()
class DownloadError(Exception): class DownloadError(Exception):
"""Download Error exception. """Download Error exception.