]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/Lib/encodings/__init__.py
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 4/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / encodings / __init__.py
CommitLineData
3257aa99
DM
1""" Standard "encodings" Package\r
2\r
3 Standard Python encoding modules are stored in this package\r
4 directory.\r
5\r
6 Codec modules must have names corresponding to normalized encoding\r
7 names as defined in the normalize_encoding() function below, e.g.\r
8 'utf-8' must be implemented by the module 'utf_8.py'.\r
9\r
10 Each codec module must export the following interface:\r
11\r
12 * getregentry() -> codecs.CodecInfo object\r
13 The getregentry() API must a CodecInfo object with encoder, decoder,\r
14 incrementalencoder, incrementaldecoder, streamwriter and streamreader\r
15 atttributes which adhere to the Python Codec Interface Standard.\r
16\r
17 In addition, a module may optionally also define the following\r
18 APIs which are then used by the package's codec search function:\r
19\r
20 * getaliases() -> sequence of encoding name strings to use as aliases\r
21\r
22 Alias names returned by getaliases() must be normalized encoding\r
23 names as defined by normalize_encoding().\r
24\r
25Written by Marc-Andre Lemburg (mal@lemburg.com).\r
26\r
27(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.\r
28\r
29"""#"\r
30\r
31import codecs\r
32from encodings import aliases\r
33import __builtin__\r
34\r
35_cache = {}\r
36_unknown = '--unknown--'\r
37_import_tail = ['*']\r
38_norm_encoding_map = (' . '\r
39 '0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ '\r
40 ' abcdefghijklmnopqrstuvwxyz '\r
41 ' '\r
42 ' '\r
43 ' ')\r
44_aliases = aliases.aliases\r
45\r
46class CodecRegistryError(LookupError, SystemError):\r
47 pass\r
48\r
49def normalize_encoding(encoding):\r
50\r
51 """ Normalize an encoding name.\r
52\r
53 Normalization works as follows: all non-alphanumeric\r
54 characters except the dot used for Python package names are\r
55 collapsed and replaced with a single underscore, e.g. ' -;#'\r
56 becomes '_'. Leading and trailing underscores are removed.\r
57\r
58 Note that encoding names should be ASCII only; if they do use\r
59 non-ASCII characters, these must be Latin-1 compatible.\r
60\r
61 """\r
62 # Make sure we have an 8-bit string, because .translate() works\r
63 # differently for Unicode strings.\r
64 if hasattr(__builtin__, "unicode") and isinstance(encoding, unicode):\r
65 # Note that .encode('latin-1') does *not* use the codec\r
66 # registry, so this call doesn't recurse. (See unicodeobject.c\r
67 # PyUnicode_AsEncodedString() for details)\r
68 encoding = encoding.encode('latin-1')\r
69 return '_'.join(encoding.translate(_norm_encoding_map).split())\r
70\r
71def search_function(encoding):\r
72\r
73 # Cache lookup\r
74 entry = _cache.get(encoding, _unknown)\r
75 if entry is not _unknown:\r
76 return entry\r
77\r
78 # Import the module:\r
79 #\r
80 # First try to find an alias for the normalized encoding\r
81 # name and lookup the module using the aliased name, then try to\r
82 # lookup the module using the standard import scheme, i.e. first\r
83 # try in the encodings package, then at top-level.\r
84 #\r
85 norm_encoding = normalize_encoding(encoding)\r
86 aliased_encoding = _aliases.get(norm_encoding) or \\r
87 _aliases.get(norm_encoding.replace('.', '_'))\r
88 if aliased_encoding is not None:\r
89 modnames = [aliased_encoding,\r
90 norm_encoding]\r
91 else:\r
92 modnames = [norm_encoding]\r
93 for modname in modnames:\r
94 if not modname or '.' in modname:\r
95 continue\r
96 try:\r
97 # Import is absolute to prevent the possibly malicious import of a\r
98 # module with side-effects that is not in the 'encodings' package.\r
99 mod = __import__('encodings.' + modname, fromlist=_import_tail,\r
100 level=0)\r
101 except ImportError:\r
102 pass\r
103 else:\r
104 break\r
105 else:\r
106 mod = None\r
107\r
108 try:\r
109 getregentry = mod.getregentry\r
110 except AttributeError:\r
111 # Not a codec module\r
112 mod = None\r
113\r
114 if mod is None:\r
115 # Cache misses\r
116 _cache[encoding] = None\r
117 return None\r
118\r
119 # Now ask the module for the registry entry\r
120 entry = getregentry()\r
121 if not isinstance(entry, codecs.CodecInfo):\r
122 if not 4 <= len(entry) <= 7:\r
123 raise CodecRegistryError,\\r
124 'module "%s" (%s) failed to register' % \\r
125 (mod.__name__, mod.__file__)\r
126 if not hasattr(entry[0], '__call__') or \\r
127 not hasattr(entry[1], '__call__') or \\r
128 (entry[2] is not None and not hasattr(entry[2], '__call__')) or \\r
129 (entry[3] is not None and not hasattr(entry[3], '__call__')) or \\r
130 (len(entry) > 4 and entry[4] is not None and not hasattr(entry[4], '__call__')) or \\r
131 (len(entry) > 5 and entry[5] is not None and not hasattr(entry[5], '__call__')):\r
132 raise CodecRegistryError,\\r
133 'incompatible codecs in module "%s" (%s)' % \\r
134 (mod.__name__, mod.__file__)\r
135 if len(entry)<7 or entry[6] is None:\r
136 entry += (None,)*(6-len(entry)) + (mod.__name__.split(".", 1)[1],)\r
137 entry = codecs.CodecInfo(*entry)\r
138\r
139 # Cache the codec registry entry\r
140 _cache[encoding] = entry\r
141\r
142 # Register its aliases (without overwriting previously registered\r
143 # aliases)\r
144 try:\r
145 codecaliases = mod.getaliases()\r
146 except AttributeError:\r
147 pass\r
148 else:\r
149 for alias in codecaliases:\r
150 if alias not in _aliases:\r
151 _aliases[alias] = modname\r
152\r
153 # Return the registry entry\r
154 return entry\r
155\r
156# Register the search_function in the Python codec registry\r
157codecs.register(search_function)\r