diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 04594eab8224b39..46c08477980e6e4 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -38,6 +38,7 @@ _unknown = '--unknown--' _import_tail = ['*'] _aliases = aliases.aliases +_iconv_filesystem_encoding = None class CodecRegistryError(LookupError, SystemError): pass @@ -72,6 +73,10 @@ def search_function(encoding): if entry is not _unknown: return entry + # An import would need this same codec to encode the module path. + if encoding == _iconv_filesystem_encoding: + return None + # Import the module: # # First try to find an alias for the normalized encoding @@ -188,6 +193,7 @@ def win32_code_page_search_function(encoding): # any encoding iconv knows that Python has no built-in codec for. Registered # last, so it never shadows a built-in; an "iconv:" prefix forces it. def iconv_search_function(encoding): + global _iconv_filesystem_encoding if encoding.startswith('iconv:'): name = encoding[len('iconv:'):] else: @@ -200,6 +206,11 @@ def iconv_search_function(encoding): except (LookupError, OSError): return None - return create_iconv_codec(encoding, name) + codec = create_iconv_codec(encoding, name) + if _cache.get(encoding, _unknown) is None: + fs_encoding = sys.getfilesystemencoding().lower().replace(' ', '-') + if encoding == fs_encoding: + _iconv_filesystem_encoding = encoding + return codec codecs.register(iconv_search_function) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 4492e4d874189fa..7839723e9b87b97 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -6,7 +6,9 @@ import io import pickle import os +import subprocess import sys +import textwrap import unittest import encodings from unittest import mock @@ -3772,6 +3774,41 @@ def iconv_encoding_available(name): 'the iconv codec is not available') class IconvTest(unittest.TestCase): + @support.requires_subprocess() + @unittest.skipIf(sys.platform == 'darwin', 'macOS uses UTF-8 for paths') + @support.run_with_locale('LC_CTYPE', 'hy_AM.armscii8', 'hy_AM.ARMSCII-8') + def test_filesystem_encoding_after_cache_clear(self): + import locale + + self.require('ARMSCII-8') + code = textwrap.dedent(r""" + import codecs + import encodings + import os + import sys + + assert sys.getfilesystemencoding() == 'armscii-8' + path = '\u0531' + encoded = os.fsencode(path) + + def search(name): + return None + + encodings._cache.clear() + codecs.register(search) + codecs.unregister(search) + + assert os.fsencode(path) == encoded + assert os.fsdecode(encoded) == path + """) + result = subprocess.run( + [sys.executable, '-I', '-X', 'utf8=0', '-c', code], + env={**os.environ, 'LC_ALL': locale.setlocale(locale.LC_CTYPE)}, + capture_output=True, timeout=support.SHORT_TIMEOUT, + ) + self.assertEqual(result.returncode, 0, + result.stderr[-1500:].decode(errors='replace')) + def require(self, *names): for name in names: if iconv_encoding_available(name): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-22-40-00.gh-issue-158889.Qs2fNK.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-22-40-00.gh-issue-158889.Qs2fNK.rst new file mode 100644 index 000000000000000..d559b0437a616f0 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-29-22-40-00.gh-issue-158889.Qs2fNK.rst @@ -0,0 +1,2 @@ +Fix a recursive lookup after clearing the codec cache when the filesystem +encoding is provided by the ``iconv()`` fallback.