MCPcopy Index your code
hub / github.com/python/cpython / search_function

Function search_function

Lib/encodings/__init__.py:68–154  ·  view source on GitHub ↗
(encoding)

Source from the content-addressed store, hash-verified

66 return _normalize_encoding(encoding)
67
68def search_function(encoding):
69
70 # Cache lookup
71 entry = _cache.get(encoding, _unknown)
72 if entry is not _unknown:
73 return entry
74
75 # Import the module:
76 #
77 # First try to find an alias for the normalized encoding
78 # name and lookup the module using the aliased name, then try to
79 # lookup the module using the standard import scheme, i.e. first
80 # try in the encodings package, then at top-level.
81 #
82 norm_encoding = normalize_encoding(encoding)
83 aliased_encoding = _aliases.get(norm_encoding) or \
84 _aliases.get(norm_encoding.replace('.', '_'))
85 if aliased_encoding is not None:
86 modnames = [aliased_encoding,
87 norm_encoding]
88 else:
89 modnames = [norm_encoding]
90 for modname in modnames:
91 if not modname or '.' in modname:
92 continue
93 try:
94 # Import is absolute to prevent the possibly malicious import of a
95 # module with side-effects that is not in the 'encodings' package.
96 mod = __import__('encodings.' + modname, fromlist=_import_tail,
97 level=0)
98 except ImportError:
99 # ImportError may occur because 'encodings.(modname)' does not exist,
100 # or because it imports a name that does not exist (see mbcs and oem)
101 pass
102 else:
103 break
104 else:
105 mod = None
106
107 try:
108 getregentry = mod.getregentry
109 except AttributeError:
110 # Not a codec module
111 mod = None
112
113 if mod is None:
114 # Cache misses
115 if len(_cache) >= _MAXCACHE:
116 _cache.clear()
117 _cache[encoding] = None
118 return None
119
120 # Now ask the module for the registry entry
121 entry = getregentry()
122 if not isinstance(entry, codecs.CodecInfo):
123 if not 4 <= len(entry) <= 7:
124 raise CodecRegistryError('module "%s" (%s) failed to register'
125 % (mod.__name__, mod.__file__))

Callers

nothing calls this directly

Calls 8

normalize_encodingFunction · 0.85
__import__Function · 0.85
CodecRegistryErrorClass · 0.85
getregentryFunction · 0.70
getMethod · 0.45
replaceMethod · 0.45
clearMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…