__init__.py (2868B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\encodings\__init__.py 2 import codecs 3 from encodings import aliases 4 import __builtin__ 5 _cache = {} 6 _unknown = '--unknown--' 7 _import_tail = ['*'] 8 _norm_encoding_map = ' . 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz ' 9 _aliases = aliases.aliases 10 11 class CodecRegistryError(LookupError, SystemError): 12 pass 13 14 15 def normalize_encoding(encoding): 16 if hasattr(__builtin__, 'unicode') and isinstance(encoding, unicode): 17 encoding = encoding.encode('latin-1') 18 return '_'.join(encoding.translate(_norm_encoding_map).split()) 19 20 21 def search_function(encoding): 22 entry = _cache.get(encoding, _unknown) 23 if entry is not _unknown: 24 return entry 25 norm_encoding = normalize_encoding(encoding) 26 aliased_encoding = _aliases.get(norm_encoding) or _aliases.get(norm_encoding.replace('.', '_')) 27 if aliased_encoding is not None: 28 modnames = [aliased_encoding, norm_encoding] 29 else: 30 modnames = [norm_encoding] 31 for modname in modnames: 32 if not modname or '.' in modname: 33 continue 34 try: 35 mod = __import__('encodings.' + modname, fromlist=_import_tail, level=0) 36 except ImportError: 37 pass 38 else: 39 break 40 41 else: 42 mod = None 43 44 try: 45 getregentry = mod.getregentry 46 except AttributeError: 47 mod = None 48 49 if mod is None: 50 _cache[encoding] = None 51 return 52 entry = getregentry() 53 if not isinstance(entry, codecs.CodecInfo): 54 if not 4 <= len(entry) <= 7: 55 raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__) 56 if not hasattr(entry[0], '__call__') or not hasattr(entry[1], '__call__') or entry[2] is not None and not hasattr(entry[2], '__call__') or entry[3] is not None and not hasattr(entry[3], '__call__') or len(entry) > 4 and entry[4] is not None and not hasattr(entry[4], '__call__') or len(entry) > 5 and entry[5] is not None and not hasattr(entry[5], '__call__'): 57 raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__) 58 if len(entry) < 7 or entry[6] is None: 59 entry += (None,) * (6 - len(entry)) + (mod.__name__.split('.', 1)[1],) 60 entry = codecs.CodecInfo(*entry) 61 _cache[encoding] = entry 62 try: 63 codecaliases = mod.getaliases() 64 except AttributeError: 65 pass 66 else: 67 for alias in codecaliases: 68 if alias not in _aliases: 69 _aliases[alias] = modname 70 71 return entry 72 73 74 codecs.register(search_function)