diff options
author | Marc-André Lemburg <mal@egenix.com> | 2004-01-20 09:40:14 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2004-01-20 09:40:14 (GMT) |
commit | c83dddf7fe7449d13435874da973c04946650a3d (patch) | |
tree | d511c307a2904706435bd78ec4d1b7faca8edf14 /Lib/encodings/__init__.py | |
parent | 5c94d3307709726fd2a8e677e22c9370ec574a76 (diff) | |
download | cpython-c83dddf7fe7449d13435874da973c04946650a3d.zip cpython-c83dddf7fe7449d13435874da973c04946650a3d.tar.gz cpython-c83dddf7fe7449d13435874da973c04946650a3d.tar.bz2 |
Let the default encodings search function lookup aliases before trying the codec import. This allows applications to install codecs which override (non-special-cased) builtin codecs.
Diffstat (limited to 'Lib/encodings/__init__.py')
-rw-r--r-- | Lib/encodings/__init__.py | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 666afad..d2e9523 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -27,7 +27,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com). """#" -import codecs, exceptions, types +import codecs, exceptions, types, aliases _cache = {} _unknown = '--unknown--' @@ -38,6 +38,7 @@ _norm_encoding_map = (' . ' ' ' ' ' ' ') +_aliases = aliases.aliases class CodecRegistryError(exceptions.LookupError, exceptions.SystemError): @@ -74,23 +75,31 @@ def search_function(encoding): # Import the module: # - # First look in the encodings package, then try to lookup the - # encoding in the aliases mapping and retry the import using the - # default import module lookup scheme with the alias name. + # First try to find an alias for the normalized encoding + # name and lookup the module using the aliased name, then try to + # lookup the module using the standard import scheme, i.e. first + # try in the encodings package, then at top-level. # - modname = normalize_encoding(encoding) - try: - mod = __import__('encodings.' + modname, - globals(), locals(), _import_tail) - except ImportError: - import aliases - modname = (aliases.aliases.get(modname) or - aliases.aliases.get(modname.replace('.', '_')) or - modname) + norm_encoding = normalize_encoding(encoding) + aliased_encoding = _aliases.get(norm_encoding) or \ + _aliases.get(norm_encoding.replace('.', '_')) + if aliased_encoding is not None: + modnames = [aliased_encoding, + norm_encoding] + else: + modnames = [norm_encoding] + for modname in modnames: + if not modname: + continue try: - mod = __import__(modname, globals(), locals(), _import_tail) + mod = __import__(modname, + globals(), locals(), _import_tail) except ImportError: - mod = None + pass + else: + break + else: + mod = None try: getregentry = mod.getregentry @@ -125,10 +134,9 @@ def search_function(encoding): except AttributeError: pass else: - import aliases for alias in codecaliases: - if not aliases.aliases.has_key(alias): - aliases.aliases[alias] = modname + if not _aliases.has_key(alias): + _aliases[alias] = modname # Return the registry entry return entry |