Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-91181: restore support for bytes on sys.path in FileFinder #31897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
14 changes: 14 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def _write_atomic(path, data, mode=0o666):
raise


def _fscodec():
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()

def fsdecode(filename):
return filename.decode(encoding, errors)

return fsdecode


_fsdecode = _fscodec()
_code_type = type(_write_atomic.__code__)


Expand Down Expand Up @@ -1567,6 +1578,9 @@ def __init__(self, path, *loader_details):
"""Initialize with the path to search on and a variable number of
2-tuples containing the loader and the file suffixes the loader
recognizes."""
if isinstance(path, bytes):
path = _fsdecode(path)

loaders = []
for loader, suffixes in loader_details:
loaders.extend((suffix, loader) for suffix in suffixes)
Expand Down
27 changes: 26 additions & 1 deletion Lib/test/test_importlib/import_/test_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from test.test_importlib import util
from test.test_importlib import util, fixtures

importlib = util.import_importlib('importlib')
machinery = util.import_importlib('importlib.machinery')
Expand Down Expand Up @@ -116,6 +116,31 @@ def test_None_on_sys_path(self):
if email is not missing:
sys.modules['email'] = email

def test_bytes_on_sys_path(self):
# Putting bytes in sys.path[0] caused an import regression from Python
# 3.2: https://bugs.python.org/issue47025
with fixtures.tempdir() as tmp_path:
(tmp_path / "email.py").write_bytes(b"EXAMPLE = 'bytes_on_sys_path'\n")
new_path = sys.path[:]
new_path.insert(0, os.fsencode(tmp_path))
new_path_importer_cache = sys.path_importer_cache.copy()
new_path_hooks = [zipimport.zipimporter,
self.machinery.FileFinder.path_hook(
*self.importlib._bootstrap_external._get_supported_file_loaders())]
missing = object()
email = sys.modules.pop('email', missing)
try:
with util.import_state(meta_path=sys.meta_path[:],
path=new_path,
path_importer_cache=new_path_importer_cache,
path_hooks=new_path_hooks):
module = self.importlib.import_module('email')
Copy link
Contributor Author

@graingert graingert Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this results in a BytesWarning: Comparison between bytes and string when bytes are added to the sys.path_importer_cache this is an existing problem for zipimport paths https://bugs.python.org/issue47026

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like that's causing CI to fail as BytesWarning is considered an error. Looks like I need to go to python-dev and ask what people want to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://discuss.python.org/t/drop-supporting-bytes-on-sys-path/17225

I'm now suggesting dropping bytes support in sys.path. We will see how that conversation goes.

self.assertIsInstance(module, ModuleType)
self.assertEqual(module.EXAMPLE, "bytes_on_sys_path")
finally:
if email is not missing:
sys.modules['email'] = email

def test_finder_with_find_module(self):
class TestFinder:
def find_module(self, fullname):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support bytes on sys.path in FileFinder. Patch by Thomas Grainger.