Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
_unknown = '--unknown--'
_import_tail = ['*']
_aliases = aliases.aliases
_iconv_filesystem_encoding = None

class CodecRegistryError(LookupError, SystemError):
pass
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
37 changes: 37 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import io
import pickle
import os
import subprocess
import sys
import textwrap
import unittest
import encodings
from unittest import mock
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a recursive lookup after clearing the codec cache when the filesystem
encoding is provided by the ``iconv()`` fallback.
Loading