Skip to content

Commit a6c942c

Browse files
Limit the growth of encodings.search_function cache
1 parent 6f8867a commit a6c942c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/encodings/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from . import aliases
3535

3636
_cache = {}
37+
_MAXCACHE = 500
3738
_unknown = '--unknown--'
3839
_import_tail = ['*']
3940
_aliases = aliases.aliases
@@ -110,8 +111,8 @@ def search_function(encoding):
110111
mod = None
111112

112113
if mod is None:
113-
# Cache misses
114-
_cache[encoding] = None
114+
if len(_cache) < _MAXCACHE:
115+
_cache[encoding] = None
115116
return None
116117

117118
# Now ask the module for the registry entry

Lib/test/test_codecs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,5 +3908,16 @@ def test_encodings_normalize_encoding(self):
39083908
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
39093909

39103910

3911+
class CodecCacheTest(unittest.TestCase):
3912+
def test_cache_bounded(self):
3913+
for i in range(encodings._MAXCACHE + 1000):
3914+
try:
3915+
b'x'.decode(f'nonexist_{i}')
3916+
except LookupError:
3917+
pass
3918+
3919+
self.assertLessEqual(len(encodings._cache), encodings._MAXCACHE)
3920+
3921+
39113922
if __name__ == "__main__":
39123923
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Limit the size of :func:`encodings.search_function` cache.

0 commit comments

Comments
 (0)