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