1 """ Standard "encodings" Package
3 Standard Python encoding modules are stored in this package
6 Codec modules must have names corresponding to standard lower-case
7 encoding names with hyphens mapped to underscores, e.g. 'utf-8' is
8 implemented by the module 'utf_8.py'.
10 Each codec module must export the following interface:
12 * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
13 The getregentry() API must return callable objects which adhere to
14 the Python Codec Interface Standard.
16 In addition, a module may optionally also define the following
17 APIs which are then used by the package's codec search function:
19 * getaliases() -> sequence of encoding name strings to use as aliases
21 Alias names returned by getaliases() must be lower-case.
24 Written by Marc-Andre Lemburg (mal@lemburg.com).
26 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
33 _unknown
= '--unknown--'
35 def search_function(encoding
):
38 entry
= _cache
.get(encoding
,_unknown
)
39 if entry
is not _unknown
:
43 modname
= encoding
.replace('-', '_')
44 modname
= aliases
.aliases
.get(modname
,modname
)
46 mod
= __import__(modname
,globals(),locals(),'*')
47 except ImportError,why
:
48 _cache
[encoding
] = None
51 # Now ask the module for the registry entry
53 entry
= tuple(mod
.getregentry())
54 except AttributeError:
58 'module "%s.%s" failed to register' % \
63 'incompatible codecs in module "%s.%s"' % \
66 # Cache the encoding and its aliases
67 _cache
[encoding
] = entry
69 codecaliases
= mod
.getaliases()
70 except AttributeError:
73 for alias
in codecaliases
:
77 # Register the search_function in the Python codec registry
78 codecs
.register(search_function
)