Skip to content

Commit cf8337e

Browse files
gh-158889: Fix recursive lookup of the iconv filesystem codec
1 parent 0423fc6 commit cf8337e

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎Lib/encodings/__init__.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
_unknown = '--unknown--'
3939
_import_tail = ['*']
4040
_aliases = aliases.aliases
41+
_iconv_filesystem_encoding = None
4142

4243
class CodecRegistryError(LookupError, SystemError):
4344
pass
@@ -72,6 +73,10 @@ def search_function(encoding):
7273
if entry is not _unknown:
7374
return entry
7475

76+
# An import would need this same codec to encode the module path.
77+
if encoding == _iconv_filesystem_encoding:
78+
return None
79+
7580
# Import the module:
7681
#
7782
# First try to find an alias for the normalized encoding
@@ -188,6 +193,7 @@ def win32_code_page_search_function(encoding):
188193
# any encoding iconv knows that Python has no built-in codec for. Registered
189194
# last, so it never shadows a built-in; an "iconv:" prefix forces it.
190195
def iconv_search_function(encoding):
196+
global _iconv_filesystem_encoding
191197
if encoding.startswith('iconv:'):
192198
name = encoding[len('iconv:'):]
193199
else:
@@ -200,6 +206,11 @@ def iconv_search_function(encoding):
200206
except (LookupError, OSError):
201207
return None
202208

203-
return create_iconv_codec(encoding, name)
209+
codec = create_iconv_codec(encoding, name)
210+
if _cache.get(encoding, _unknown) is None:
211+
fs_encoding = sys.getfilesystemencoding().lower().replace(' ', '-')
212+
if encoding == fs_encoding:
213+
_iconv_filesystem_encoding = encoding
214+
return codec
204215

205216
codecs.register(iconv_search_function)

‎Lib/test/test_codecs.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import io
77
import pickle
88
import os
9+
import subprocess
910
import sys
11+
import textwrap
1012
import unittest
1113
import encodings
1214
from unittest import mock
@@ -3772,6 +3774,41 @@ def iconv_encoding_available(name):
37723774
'the iconv codec is not available')
37733775
class IconvTest(unittest.TestCase):
37743776

3777+
@support.requires_subprocess()
3778+
@unittest.skipIf(sys.platform == 'darwin', 'macOS uses UTF-8 for paths')
3779+
@support.run_with_locale('LC_CTYPE', 'hy_AM.armscii8', 'hy_AM.ARMSCII-8')
3780+
def test_filesystem_encoding_after_cache_clear(self):
3781+
import locale
3782+
3783+
self.require('ARMSCII-8')
3784+
code = textwrap.dedent(r"""
3785+
import codecs
3786+
import encodings
3787+
import os
3788+
import sys
3789+
3790+
assert sys.getfilesystemencoding() == 'armscii-8'
3791+
path = '\u0531'
3792+
encoded = os.fsencode(path)
3793+
3794+
def search(name):
3795+
return None
3796+
3797+
encodings._cache.clear()
3798+
codecs.register(search)
3799+
codecs.unregister(search)
3800+
3801+
assert os.fsencode(path) == encoded
3802+
assert os.fsdecode(encoded) == path
3803+
""")
3804+
result = subprocess.run(
3805+
[sys.executable, '-I', '-X', 'utf8=0', '-c', code],
3806+
env={**os.environ, 'LC_ALL': locale.setlocale(locale.LC_CTYPE)},
3807+
capture_output=True, timeout=support.SHORT_TIMEOUT,
3808+
)
3809+
self.assertEqual(result.returncode, 0,
3810+
result.stderr[-1500:].decode(errors='replace'))
3811+
37753812
def require(self, *names):
37763813
for name in names:
37773814
if iconv_encoding_available(name):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a recursive lookup after clearing the codec cache when the filesystem
2+
encoding is provided by the ``iconv()`` fallback.

0 commit comments

Comments
 (0)