Make the code that ensures that python internal encodings are not ASCII more robust

This commit is contained in:
Kovid Goyal 2014-04-13 21:25:37 +05:30
parent 0c82e9c0d2
commit 7f9a2eb60f

View File

@ -12,7 +12,7 @@ import sys
is_narrow_build = sys.maxunicode < 0x10ffff
# Setup code {{{
import sys
import codecs
from calibre.constants import plugins
from calibre.utils.config_base import tweaks
@ -30,25 +30,26 @@ del err
icu_unicode_version = getattr(_icu, 'unicode_version', None)
_nmodes = {m:getattr(_icu, 'UNORM_'+m, None) for m in ('NFC', 'NFD', 'NFKC', 'NFKD', 'NONE', 'DEFAULT', 'FCD')}
ascii_encodings = {b'ansi_x3.4-1968', b'ascii'}
# Ensure that the python internal filesystem and default encodings are not ASCII
def is_ascii(name):
try:
return codecs.lookup(name).name == b'ascii'
except (TypeError, LookupError):
return True
try:
senc = sys.getdefaultencoding()
if not senc or senc.lower() in ascii_encodings:
if is_ascii(sys.getdefaultencoding()):
_icu.set_default_encoding(b'utf-8')
del senc
except:
import traceback
traceback.print_exc()
try:
fenc = sys.getfilesystemencoding()
if not fenc or fenc.lower() in ascii_encodings:
if is_ascii(sys.getfilesystemencoding()):
_icu.set_filesystem_encoding(b'utf-8')
del fenc
except:
import traceback
traceback.print_exc()
del ascii_encodings
del is_ascii
def collator():
global _collator, _locale