Commit c83dddf7 authored by Marc-André Lemburg's avatar Marc-André Lemburg

Let the default encodings search function lookup aliases before trying the...

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.
parent 5c94d330
...@@ -27,7 +27,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com). ...@@ -27,7 +27,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
"""#" """#"
import codecs, exceptions, types import codecs, exceptions, types, aliases
_cache = {} _cache = {}
_unknown = '--unknown--' _unknown = '--unknown--'
...@@ -38,6 +38,7 @@ _norm_encoding_map = (' . ' ...@@ -38,6 +38,7 @@ _norm_encoding_map = (' . '
' ' ' '
' ' ' '
' ') ' ')
_aliases = aliases.aliases
class CodecRegistryError(exceptions.LookupError, class CodecRegistryError(exceptions.LookupError,
exceptions.SystemError): exceptions.SystemError):
...@@ -74,22 +75,30 @@ def search_function(encoding): ...@@ -74,22 +75,30 @@ def search_function(encoding):
# Import the module: # Import the module:
# #
# First look in the encodings package, then try to lookup the # First try to find an alias for the normalized encoding
# encoding in the aliases mapping and retry the import using the # name and lookup the module using the aliased name, then try to
# default import module lookup scheme with the alias name. # lookup the module using the standard import scheme, i.e. first
# try in the encodings package, then at top-level.
# #
modname = normalize_encoding(encoding) 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: try:
mod = __import__('encodings.' + modname, mod = __import__(modname,
globals(), locals(), _import_tail) globals(), locals(), _import_tail)
except ImportError: except ImportError:
import aliases pass
modname = (aliases.aliases.get(modname) or else:
aliases.aliases.get(modname.replace('.', '_')) or break
modname) else:
try:
mod = __import__(modname, globals(), locals(), _import_tail)
except ImportError:
mod = None mod = None
try: try:
...@@ -125,10 +134,9 @@ def search_function(encoding): ...@@ -125,10 +134,9 @@ def search_function(encoding):
except AttributeError: except AttributeError:
pass pass
else: else:
import aliases
for alias in codecaliases: for alias in codecaliases:
if not aliases.aliases.has_key(alias): if not _aliases.has_key(alias):
aliases.aliases[alias] = modname _aliases[alias] = modname
# Return the registry entry # Return the registry entry
return entry return entry
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment