Skip to content

bpo-32642: add support for path-like objects in sys.path #32118

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
Closed
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
7 changes: 3 additions & 4 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
@@ -1157,10 +1157,9 @@ always available.
To not prepend this potentially unsafe path, use the :option:`-P` command
line option or the :envvar:`PYTHONSAFEPATH` environment variable?

A program is free to modify this list for its own purposes. Only strings
should be added to :data:`sys.path`; all other data types are
ignored during import.

A program is free to modify this list for its own purposes. Only strings,
bytes, and :term:`path-like objects <path-like object>` should be added to
:data:`sys.path`; all other data types are ignored during import.

.. seealso::
* Module :mod:`site` This describes how to use .pth files to
5 changes: 4 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
@@ -1475,7 +1475,10 @@ def _get_spec(cls, fullname, path, target=None):
# the list of paths that will become its __path__
namespace_path = []
for entry in path:
if not isinstance(entry, str):
try:
# bytes, str and path-like objects will pass fspath
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to add a test for bytes on sys.path, and test it with the other import systems. Note these tests fail with a BytesWarning

Copy link
Contributor

Choose a reason for hiding this comment

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

entry = _os.fspath(entry)
except TypeError:
continue
finder = cls._path_importer_cache(entry)
if finder is not None:
12 changes: 6 additions & 6 deletions Lib/test/test_importlib/test_pkg_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import pathlib
import sys
import shutil
import string
@@ -24,13 +25,12 @@ def remove_modules(self):
del sys.modules[module_name]

def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.test_dir = pathlib.Path(tempfile.mkdtemp())
Copy link
Contributor

Choose a reason for hiding this comment

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

this removes the test for str on sys.path

sys.path.append(self.test_dir)
self.package_dir = os.path.join(self.test_dir,
self.package_name)
os.mkdir(self.package_dir)
create_empty_file(os.path.join(self.package_dir, '__init__.py'))
self.module_path = os.path.join(self.package_dir, 'foo.py')
self.package_dir = self.test_dir / self.package_name
self.package_dir.mkdir()
create_empty_file(self.package_dir / '__init__.py')
self.module_path = self.package_dir / 'foo.py'

def tearDown(self):
shutil.rmtree(self.test_dir)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:data:`sys.path` can now accept a :term:`path-like objects <path-like object>`. Patch by ncohen.