From 9ee27526bc7c20e9487995253be4b0834894fe07 Mon Sep 17 00:00:00 2001 From: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 12 May 2026 14:54:11 +0300 Subject: [PATCH] Update `encodings` to 3.14.5 (#7859) --- Lib/encodings/__init__.py | 5 +++++ Lib/test/test_codecs.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 298177eb8..8548bbe04 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -33,6 +33,7 @@ import sys from . import aliases _cache = {} +_MAXCACHE = 500 _unknown = '--unknown--' _import_tail = ['*'] _aliases = aliases.aliases @@ -115,6 +116,8 @@ def search_function(encoding): if mod is None: # Cache misses + if len(_cache) >= _MAXCACHE: + _cache.clear() _cache[encoding] = None return None @@ -136,6 +139,8 @@ def search_function(encoding): 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 diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index fefd062ea..58a7d6f3a 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3921,5 +3921,16 @@ class CodecNameNormalizationTest(unittest.TestCase): self.assertEqual(normalize('utf...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()