from . import aliases
_cache = {}
+_MAXCACHE = 500
_unknown = '--unknown--'
_import_tail = ['*']
_aliases = aliases.aliases
if mod is None:
# Cache misses
+ if len(_cache) >= _MAXCACHE:
+ _cache.clear()
_cache[encoding] = None
return None
entry = codecs.CodecInfo(*entry)
# Cache the codec registry entry
+ if len(_cache) >= _MAXCACHE:
+ _cache.clear()
_cache[encoding] = entry
# Register its aliases (without overwriting previously registered
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
+class CodecCacheTest(unittest.TestCase):
+ def test_cache_bounded(self):
+ for i in range(encodings._MAXCACHE + 1000):
+ try:
+ b'x'.decode(f'nonexist_{i}')
+ except LookupError:
+ pass
+
+ self.assertLessEqual(len(encodings._cache), encodings._MAXCACHE)
+
+
if __name__ == "__main__":
unittest.main()